This article is the 17th day article of Python Advent Calendar 2015. Again, [Python](http: //www.adventar.org/) from Mathematics Advent Calendar 2015 and ADVENTAR //www.adventar.org/calendars/846) and Math also serve as the 17th day m (_ _) m
This is an inspiring article of Solving all 2015 Center Test for University Admissions Mathematics IA with a program (Python). I really enjoyed doing it here, so I tried Mathematics IIB as well as studying Sympy.
National Center Test for University Admissions | Answer Bulletin 2015 | Preparatory School East Advance Mathematics II / Mathematics B http://www.toshin.com/center/sugaku-2b_mondai_0.html
https://github.com/massa142/mathematics-2b
First of all, I will summarize how to use Sympy, which will appear a lot after this.
Python algebra library Official documentation: http://www.sympy.org/en/index.html Japanese translation: http://turbare.net/transl/scipy-lecture-notes/index.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]
The problem of coordinates is OK if you think about each point in a vector using Sympy's Matrix. For the time being, let's set θ to 45 ° for convenience.
import math
import sympy as sy
import numpy as np
γ = sy.pi/4
O = sy.Matrix([0, 0])
P = sy.Matrix([2*sy.cos(γ), 2*sy.sin(γ)])
Q = sy.Matrix([2*sy.cos(γ) + sy.cos(7*γ), 2*sy.sin(γ) + sy.sin(7*γ)])
The distance between two points can be expressed by the concept of the Norm of a Matrix. http://docs.sympy.org/0.7.2/modules/matrices/matrices.html#sympy.matrices.matrices.MatrixBase.norm
(O - P).norm()
(P - Q).norm()
output
2 #A
1 #I
The next $ OQ ^ 2 $ seems to have to be calculated honestly. Since this calculation depends on the value of θ, the 45 ° that was set earlier cannot be used ... Let's define a new θ as a symbol.
#Symbolize θ as a value greater than 0
θ = sy.symbols('θ', positive=True)
sy.simplify((2*sy.cos(θ) + sy.cos(7*θ))**2 + (2*sy.sin(θ) + sy.sin(7*θ))**2)
4*cos(6*θ) + 5 #C,D,Oh
After simplifying it, I was able to come up with a nice answer. Next, consider the maximum value of OQ. This seems to be a little troublesome, so I will use the solution method of calculating by brute force using the fact that the answer column [ka] is an integer 1 digit.
for n in range(4, 9):
γ = math.pi / n
print("θ is π/"+ str(n) + "At that time, OQ**The value of 2 is" + str(5 + 4*np.cos(6*γ)))
θ is π/When 4, OQ**The value of 2 is 5.0
θ is π/When 5, OQ**The value of 2 is 1.7639320225
θ is π/At 6, OQ**The value of 2 is 1.0
θ is π/When 7, OQ**The value of 2 is 1.39612452839
θ is π/When it is 8, OQ**The value of 2 is 2.17157287525
It turns out that the maximum value is taken when θ = π / 5
.
[F]: 5
The value taken at that time was 1.7639320225. You can intuitively understand what square root this value is, but let's calculate it just in case.
from fractions import Fraction
#Finding the optimal fraction with a denominator of 1 or less
Fraction(1.7639320225**2).limit_denominator(1)
Fraction(3, 1)
Since it turned out that $ 1.7639320225 ^ 2 ≒ 3 $, the value of [ki] was also obtained. [Ki]: 3
The equation representing the straight line OP can be understood by mental arithmetic, so it is omitted. [K]
(\sin\theta)x - (\cos\theta)y = 0
Since the three points O, P, and Q are on a straight line, it is OK if you substitute the coordinates of the point Q into the above formula.
sy.simplify(sy.sin(θ)*(2*sy.cos(θ)+sy.cos(7*θ))-sy.cos(θ)*(2*sy.sin(θ)+sy.sin(7*θ)))
output
-sin(6*θ)
# _Receives the previous output value
sy.solve(_)
output
[pi/6] #Ke
Yes next! This is really easy. The angle OQP becomes a right angle from OP = 2, PQ = 1. [Co]: $ OQ = \ sqrt3 $
Using this value and the value of OQ obtained by [c] [d] [e], find θ when the angle OQP becomes a right angle.
# 5+4*sy.cos(6*θ) =From 3
sy.solve(5+4*sy.cos(6*θ) - 3)
output
[pi/9, 2*pi/9]
$ \ pi / 8 \ leqq θ \ leqq \ pi / 4 $ Why $ θ = 2/9 \ pi $ [sa] [shi]
import math
import sympy as sy
import numpy as np
x, y, a, b = sy.symbols('x y a b', real=True, positive=True) #Symbolized as a positive real number
c1 = x**2*y**3 - a**2
c2 = x*y**3 - b**3
sy.solve({c1, c2}, {x, y})
output
[{y: b**2/a**(2/3), x: a**2/b**3}] #Su,Se,So,Ta
I was able to solve it in an instant using solve!
After that, pay attention to the value of y and find the value of p. [Chi] [Tsu] [Te]
Substitute b = 2 * a ** (4/3)
for x and y calculated above, respectively.
First from x.
sy.simplify(a**2/(2*a**(4/3))**3)
output
a**(-2.0)/8 #To,Na
And y also.
sy.simplify((2*a**(4/3))**2/a**(2/3))
output
4*a**2.0 #D
When considering the minimum value of x + y here, if you use the power of Scipy, you don't have to remember the formula "the relationship between the arithmetic mean and the geometric mean"!
from scipy.optimize import differential_evolution
#Convert a, which is a symbol, so that it can be treated as a numerical value
numeric_a = sy.lambdify(a, a**(-2.0)/8 + 4*a**2.0)
#The range of a is a>0 Why, the upper limit is the int maximum value in python2 series
#A is 0 in brute force~Find the minimum value between 2147483647
differential_evolution(numeric_a, [(0, 2**31-1)])
output
nfev: 518
success: True
nit: 33
jac: array([ 7.06101844e-06])
message: 'Optimization terminated successfully.'
fun: array([ 1.41421356])
x: array([ 0.42044842])
From this result It can be seen that "when a = 0.42044842, x + y takes the minimum value 1.41421356".
After that, it is OK if you calculate "0.42044842 is the power of 2" and "1.41421356 is the square root".
from fractions import Fraction
math.log2(0.4204482)
Fraction(_).limit_denominator(9)
output
Fraction(-5, 4) #Ne,No,C
Fraction(1.41421356**2).limit_denominator(1)
output
Fraction(2, 1) #Nu
Finally the first question is over (´-ω-`)
Omit the second, third, and fourth questions
People People People People People > Sudden 5th question <  ̄Y^Y^Y^Y^Y^Y^Y^ ̄
Calculation of combinations is easy with a module called Scipy.misc.comb. http://docs.scipy.org/doc/scipy/reference/generated/scipy.misc.comb.html#scipy.misc.comb
import itertools
import scipy.misc as scm
scm.comb(7, 3, exact=True)
output
35 #I,C
scm.comb(4, 0, exact=True)*scm.comb(3, 3, exact=True)
scm.comb(4, 1, exact=True)*scm.comb(3, 2, exact=True)
scm.comb(4, 2, exact=True)*scm.comb(3, 1, exact=True)
scm.comb(4, 3, exact=True)*scm.comb(3, 0, exact=True)
output
1 #A
12 #D,Oh
18 #Mosquito,Ki
4 #Ku
#Stored in dictionary to calculate expected value and variance
dict = {0: 1/35, 1: 12/35, 2: 18/35, 3: 4/35}
mean = sum(x*y for x, y in dict.items())
from fractions import Fraction
Fraction(mean).limit_denominator(9)
output
Fraction(12, 7) #Ke,Ko,Service
variance = sum(x*x*y for x, y in dict.items()) - mean**2
Fraction(variance).limit_denominator(99)
output
Fraction(24, 49) #Shi,Su,Se,So
A standard normal distribution has come out, but this is also OK if you use Scipy's statistical library. http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.norm.html#scipy.stats.norm
Now you don't have to glare at this normal distribution table!
from scipy.stats import norm
norm.interval(alpha=0.99)
output
(-2.5758293035489004, 2.5758293035489004) #Ta
import sympy as sy
from scipy.stats import norm
n, σ = sy.symbols('n σ')
norm.interval(alpha=0.95)
norm.interval(alpha=0.99)
output
(-1.959963984540054, 1.959963984540054)
(-2.5758293035489004, 2.5758293035489004)
L1 = 2*1.96*σ/sy.sqrt(n)
L2 = 2*2.58*σ/sy.sqrt(n)
L2/L1
output
1.31632653061224 #Ji,Tsu
L3 = 2*1.96*σ/sy.sqrt(4*n)
L3/L1
output
0.500000000000000 #Te,To
Once I learned the basic usage of Sympy, I could easily reach the level of the center test. The impression of trying this time is like this.
Recommended Posts