This article is the 11th day article of Python Advent Calendar 2016.
Last year's Python Adevent Calendar dealt with Mathematics IIB of the Center Test, but this year we will deal with the University of Tokyo Mathematics (Humanities).
Python algebra library Official documentation: http://www.sympy.org/en/index.html Japanese materials: http://www.turbare.net/transl/scipy-lecture-notes/packages/sympy.html
In [1]: from sympy import *
In [2]: x + 1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-2-4cf92658b648> in <module>()
----> 1 x + 1
NameError: name 'x' is not defined
In [3]: x = symbols('x')
In [4]: x + 1
Out[4]: x + 1
In [5]: expand((x + 1)**2)
Out[5]: x**2 + 2*x + 1
In [6]: factor(x**4 - 3*x**2 + 1)
Out[6]: (1 + x - x**2)*(1 - x - x**2)
In [7]: simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
Out[7]: x - 1
In [8]: limit(x, x, oo)
Out[8]: oo
In [9]: diff(cos(x), x)
Out[9]: -sin(x)
In [10]: diff(x**3 + x**2 - x - 1, x)
Out[10]: 3*x**2 + 2*x - 1
In [11]: integrate(cos(x), x)
Out[11]: sin(x)
In [12]: integrate(x**3 + x**2 - x - 1, x)
Out[12]: x**4/4 + x**3/3 - x**2/2 - x
In [13]: Matrix([[1, 2, 3], [-2, 0, 4]])
Out[13]:
Matrix([
[ 1, 2, 3],
[-2, 0, 4]])
In [14]: solve(x**2 - 1, x)
Out[14]: [-1, 1]
In [1]: import sympy as sy
In [2]: x, y = sy.symbols('x y')
In [3]: P = sy.Matrix([x, y])
In [4]: Q = sy.Matrix([-x, -y])
In [5]: R = sy.Matrix([1, 0])
norm
is the length of the vectorIn [6]: sy.simplify((Q - P).dot(R - P) > 0)
Out[6]: 2*x*(x - 1) + 2*y**2 > 0
In [7]: sy.simplify((P - Q).dot(R - Q) > 0)
Out[7]: 2*x*(x + 1) + 2*y**2 > 0
In [8]: sy.simplify((P - R).dot(Q - R) > 0)
Out[8]: -x**2 - y**2 + 1 > 0
2x(x-1)+2y^2 > 0 <=> (x-\frac{1}{2})^2+y^2 > \frac{1}{4}
2x(x+1)+2y^2 > 0 <=> (x+\frac{1}{2})^2+y^2 > \frac{1}{4}
-x^2 - y^2 + 1 > 0 <=> x^2+y^2 < 1
In [9]: import matplotlib.pyplot as plt
In [10]: fig = plt.figure()
In [11]: ax = plt.gca()
In [12]: ax.add_patch(plt.Circle((0,0),1,fc="#770000"))
Out[12]: <matplotlib.patches.Circle at 0x109689518>
In [13]: ax.add_patch(plt.Circle((0.5,0),0.5, fc="#FFFFFF"))
Out[13]: <matplotlib.patches.Circle at 0x109689f28>
In [14]: ax.add_patch(plt.Circle((-0.5,0),0.5, fc="#FFFFFF"))
Out[14]: <matplotlib.patches.Circle at 0x109696710>
In [15]: ax.set_aspect('equal')
In [16]: plt.xlim([2, 2])
Out[16]: (-2, 2)
In [17]: plt.ylim([-2, 2])
Out[17]: (-2, 2)
In [18]: plt.show()
** Answer: The range of points P (x, y) is the red part of the graph **
Introduction to Mathematics Starting with Python
The Sympy and matplotlib introduced this time are also introduced. Recommended for getting started with math programming.
By using Sympy like this, you can easily solve it at the university entrance examination level. There is a concern that the University of Tokyo mathematics should be at such an easy level, but ...
Only the first question has been dealt with here, but if you are interested, please try solving other math problems with Python!
Recommended Posts