Solving 2015 Center Test for University Admissions Mathematics IIB in Python

Introduction

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

What to do here

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

Execution environment

Repository

https://github.com/massa142/mathematics-2b

Basic usage of Sympy

First of all, I will summarize how to use Sympy, which will appear a lot after this.

What is Sympy?

Python algebra library Official documentation: http://www.sympy.org/en/index.html Japanese translation: http://turbare.net/transl/scipy-lecture-notes/index.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

factor-factorization

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

diff-differentiation

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

Matrix-Matrix

In [13]: Matrix([[1, 2, 3], [-2, 0, 4]])
Out[13]:
Matrix([
[ 1, 2, 3],
[-2, 0, 4]])

solve --solve an expression

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

Actually solve

Question 1-Trigonometric functions, exponential / logarithmic functions

sugaku-2b_101.gif

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

sugaku-2b_102.gif

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]

sugaku-2b_103.gif

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] p = -2/3

sugaku-2b_104.gif

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^ ̄

Question 5-Probability distribution and statistical guessing

sugaku-2b_501.gif

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

sugaku-2b_502.gif

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! sugaku-2b_504.gif

from scipy.stats import norm
norm.interval(alpha=0.99)

output


(-2.5758293035489004, 2.5758293035489004) #Ta

sugaku-2b_503.gif

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

at the end

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

Solving 2015 Center Test for University Admissions Mathematics IIB in Python
Studying Mathematics in Python: Solving Simple Probability Problems
Algorithm in Python (primality test)
Search for strings in Python
Techniques for sorting in Python
Python template for Codeforces-manual test-
About "for _ in range ():" in python
Set python test in jenkins
Check for memory leaks in Python
Check for external commands in python
Write selenium test code in python
Created AtCoder test tool for Python
Statistical test (multiple test) in Python: scikit_posthocs
Run unittests in Python (for beginners)
Notes on nfc.ContactlessFrontend () for nfcpy in python
Write the test in a python docstring
Tips for dealing with binaries in Python
Summary of various for statements in Python
Type annotations for Python2 in stub files!
Post Test 3 (Working with PosgreSQL in Python)
Template for writing batch scripts in python
Process multiple lists with for in Python
MongoDB for the first time in Python
Get a token for conoha in python
Sample for handling eml files in Python
AtCoder cheat sheet in python (for myself)
I searched for prime numbers in python
Notes for using python (pydev) in eclipse
Tips for making small tools in python
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7