2016 The University of Tokyo Mathematics Solved with Python


This article is the 11th day article of Python Advent Calendar 2016.

What to do here

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).


Basic usage of Sympy

What is Sympy?

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

Symbols-Variable definitions

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

expand --expand

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)

simplify --Simplify

In [7]: simplify((x**3 + x**2 - x - 1)/(x**2 + 2*x + 1))
Out[7]: x - 1

limit --extreme

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

integrate --integrate

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]])
[ 1, 2, 3],
[-2, 0, 4]])

solve --solve an expression

In [14]: solve(x**2 - 1, x)
Out[14]: [-1, 1]

Question 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])
In [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 **

Reference book

Introduction to Mathematics Starting with Python

The Sympy and matplotlib introduced this time are also introduced. Recommended for getting started with math programming.

at the end

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

2016 The University of Tokyo Mathematics Solved with Python
Try scraping the data of COVID-19 in Tokyo with Python
2016 University of Tokyo Entrance Examination Problem Solving the first question of mathematics (humanities) using Python
Check the existence of the file with python
Color extraction with Python + OpenCV solved the mystery of the green background
The story of making a university 100 yen breakfast LINE bot with Python
Prepare the execution environment of Python3 with Docker
[Note] Export the html of the site with python.
Calculate the total number of combinations with python
the zen of Python
Check the date of the flag duty with Python
Convert the character code of the file with Python3
I tried to solve the first question of the University of Tokyo 2019 math entrance exam with python sympy
[Python] Determine the type of iris with SVM
Extract the table of image files with OneDrive & Python
Learn Nim with Python (from the beginning of the year).
Destroy the intermediate expression of the sweep method with Python
Visualize the range of interpolation and extrapolation with python
Calculate the regression coefficient of simple regression analysis with python
Summary of the basic flow of machine learning with Python
Get the operation status of JR West with Python
Extract the band information of raster data with python
Towards the retirement of Python2
About the ease of Python
Call the API with python3.
About the features of Python
The Power of Pandas: Python
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
Visualize the results of decision trees performed with Python scikit-learn
Calculate the square root of 2 in millions of digits with python
I wrote the basic grammar of Python with Jupyter Lab
Tank game made with python About the behavior of tanks
Run the intellisense of your own python library with VScode.
I evaluated the strategy of stock system trading with Python.
Introduction to Statistics The University of Tokyo Press Chapter 2 Exercises
Check the scope of local variables with the Python locals function.
Let's touch the API of Netatmo Weather Station with Python. #Python #Netatmo
The story of rubyist struggling with python :: Dict data with pycall
[Homology] Count the number of holes in data with Python
Try to automate the operation of network devices with Python
Rewrite the record addition node of SPSS Modeler with Python.
Estimate the attitude of AR markers with Python + OpenCV + drone
Play with the password mechanism of GitHub Webhook and Python
Get the source of the page to load infinitely with python.
Extract the xz file with python
The story of Python and the story of NaN
[Python] The stumbling block of import
First Python 3 ~ The beginning of repetition ~
Existence from the viewpoint of Python
Get the weather with Python requests
Get the weather with Python requests 2
pyenv-change the python version of virtualenv
Getting Started with Python Basics of Python
Find the Levenshtein Distance with python
Change the Python version of Homebrew
Hit the Etherpad-lite API with Python
Install the Python plugin with Netbeans 8.0.2
Life game with Python! (Conway's Game of Life)
[Python] Understanding the potential_field_planning of Python Robotics
10 functions of "language with battery" python