Python 언어로 scipy, numpy, mpmath 모듈을 이용하여 역삼각함수, 역쌍곡선함수 값을 구하는 예제
역삼각함수란 삼각함수의 역함수를 의미하고,
역쌍곡선함수란 쌍곡선함수의 역함수를 의미한다.
수학에서 sin 함수의 역함수는 arcsin 으로 표기되는데,
Python 언어에서는 math.asin() 함수로 이미 구현되어 있다.
이를 사용하기 위해서는 import 구문
import math
가 있으면 된다. 그러나 scipy 나 numpy 모듈을 사용할 때는 얘기가 달라진다.
sin 함수의 역함수가 scipy 모듈에서는 scipy.arcsin 으로, numpy 모듈에서는 numpy.arcsin 으로 구현되어 있다. (asin 이 아니라 arcsin 임에 주의하자.) 한편 mpmath 모듈에서는 mpmath.asin 으로 구현되어 았다. 이들 이용하기 위해서는 import 구문
import scipy
또는
import numpy
또는
import mpmath
가 각각 필요하다.
다음의 첫 두 개 소스는 scipy 와 numpy 의 차이 이외에는 똑 같다.
[ scipy 모듈을 이용하는 Python 소스 ]
# -*- encoding: utf-8 -*-
# Filename: testArcSineWithScipy.py
#
# Approximate square roots, cubic roots and n-th roots of a given number.
#
# Execute: python testArcSineWithScipy.py
#
# Date: 2013. 1. 9.
# Copyright (c) 2013 PH Kim (pkim __AT__ scripts.pe.kr)
import scipy as sp
x = -0.9
y = sp.arcsin(x)
print "x = %g" % x
print "y = sp.arcsin(x) = sp.arcsin(%g) = %.9f" % (x, y)
print "sp.sin(y) = sp.sin(%.9f) = %g" % (y, sp.sin(y))
print
x = 1.1
u = sp.arccosh(x)
v = sp.arcsinh(x)
print "x = %g" % x
print "u = sp.arccosh(x) = sp.arccosh(%g) = %.10f" % (x, u)
print "v = sp.arcsinh(x) = sp.arcsinh(%g) = %.10f" % (x, v)
print
print "sp.cosh(u) = sp.cosh(%.10f) = %g" % (u, sp.cosh(u))
print "sp.sinh(v) = sp.sinh(%.10f) = %g" % (v, sp.sinh(v))
"""
Output:
x = -0.9
y = sp.arcsin(x) = sp.arcsin(-0.9) = -1.119769515
sp.sin(y) = sp.sin(-1.119769515) = -0.9
x = 1.1
u = sp.arccosh(x) = sp.arccosh(1.1) = 0.4435682544
v = sp.arcsinh(x) = sp.arcsinh(1.1) = 0.9503469298
sp.cosh(u) = sp.cosh(0.4435682544) = 1.1
sp.sinh(v) = sp.sinh(0.9503469298) = 1.1
"""
[ numpy 모듈을 이용하는 Python 소스 ]
# -*- encoding: utf-8 -*-
# Filename: testArcSineWithNumpy.py
#
# Approximate square roots, cubic roots and n-th roots of a given number.
#
# Execute: python testArcSineWithNumpy.py
#
# Date: 2013. 1. 9.
# Copyright (c) 2013 PH Kim (pkim __AT__ scripts.pe.kr)
import numpy as np
x = -0.9
y = np.arcsin(x)
print "x = %g" % x
print "y = np.arcsin(x) = np.arcsin(%g) = %.9f" % (x, y)
print "np.sin(y) = np.sin(%.9f) = %g" % (y, np.sin(y))
print
x = 1.1
u = np.arccosh(x)
v = np.arcsinh(x)
print "x = %g" % x
print "u = np.arccosh(x) = np.arccosh(%g) = %.10f" % (x, u)
print "v = np.arcsinh(x) = np.arcsinh(%g) = %.10f" % (x, v)
print
print "np.cosh(u) = np.cosh(%.10f) = %g" % (u, np.cosh(u))
print "np.sinh(v) = np.sinh(%.10f) = %g" % (v, np.sinh(v))
"""
Output:
x = -0.9
y = np.arcsin(x) = np.arcsin(-0.9) = -1.119769515
np.sin(y) = np.sin(-1.119769515) = -0.9
x = 1.1
u = np.arccosh(x) = np.arccosh(1.1) = 0.4435682544
v = np.arcsinh(x) = np.arcsinh(1.1) = 0.9503469298
np.cosh(u) = np.cosh(0.4435682544) = 1.1
np.sinh(v) = np.sinh(0.9503469298) = 1.1
"""
[ mpmath 모듈을 이용하는 Python 소스 ]
# -*- encoding: utf-8 -*-
# Filename: testArcSineWithMpmath.py
#
# Approximate square roots, cubic roots and n-th roots of a given number.
#
# Execute: python testArcSineMpmath.py
#
# Date: 2013. 1. 9.
# Copyright (c) 2013 PH Kim (pkim __AT__ scripts.pe.kr)
import mpmath as mp
x = -0.9
y = mp.asin(x)
print "x = %g" % x
print "y = mp.asin(%g) = %.9f" % (x, y)
print "mp.sin(y) = mp.sin(%.9f) = %g" % (y, mp.sin(y))
print
x = 1.1
u = mp.acosh(x)
v = mp.asinh(x)
print "x = %g" % x
print "u = mp.acosh(%g) = %.9f" % (x, u)
print "v = mp.asinh(%g) = %.9f" % (x, v)
print
print "mp.cosh(u) = mp.cosh(%.9f) = %g" % (u, mp.cosh(u))
print "mp.sinh(v) = mp.sinh(%.9f) = %g" % (u, mp.sinh(v))