Python 언어로 역삼각함수, 역쌍곡선함수 값을 구하는 예제
역삼각함수란 삼각함수의 역함수를 의미하고,
역쌍곡선함수란 쌍곡선함수의 역함수를 의미한다.
수학에서 sin 함수의 역함수는 arcsin 으로 표기되는데,
Python 언어에서는 math.asin() 함수로 구현되어 있다.
이를 사용하기 위해서는 구문
import math
이 필요하다.
다음 소스는 Python, Jython, IronPython 중 어느 것으로 실행해도 같은 결과를 얻는다.
특히 IronPython 으로는 옵션 /target:exe 를 사용하여 컴파일하면 실행파일 testArcSine.exe 및 testArcSine.dll 파일을 얻는다, dll 파일이 있어야 exe 파일이 실행된다.
# -*- encoding: utf-8 -*-
# Filename: testArcSine.py
#
# Execute: python testArcSine.py
#
# Or
#
# Execute: jython testArcSine.py
#
# Or
#
# Execute: ipy testArcSine.py
#
# Or
#
# Compile: ipy %IRONPYTHON_HOME%\Tools\Scripts\pyc.py /target:exe /main:testArcSine.py
# Execute: testArcSine
#
# Date: 2013. 1. 1.
# Copyright (c) pkim _AT_ scripts.pe.kr
import math
def sin(x):
y = math.sin(x)
return y
def asin(x):
y = math.asin(x)
return y
def sinh(x):
y = math.sinh(x)
return y
def cosh(x):
y = math.cosh(x)
return y
def asinh(x):
y = math.log(x + math.sqrt(x*x + 1))
return y
def acosh(x):
y = math.log(x + math.sqrt(x*x - 1))
return y
if __name__ == "__main__":
x = -0.9
y = asin(x)
print "y = asin(%.1g) = %.9f" % (x, y)
print "sin(y) = sin(%.9f) = %.1g" % (y, sin(y))
print
x = 1.1
u = acosh(x)
print "u = acosh(%3.2g) = %.10f" % (x, u)
v = asinh(x)
print "v = asinh(%3.2g) = %.10f" % (x, v)
print "cosh(u) = cosh(%.10f) = %3.2g" % (u, cosh(u))
print "sinh(v) = sinh(%.10f) = %3.2g" % (v, sinh(v))
"""
Output:
y = asin(-0.9) = -1.119769515
sin(y) = sin(-1.119769515) = -0.9
u = acosh(1.1) = 0.4435682544
v = asinh(1.1) = 0.9503469298
cosh(u) = cosh(0.4435682544) = 1.1
sinh(v) = sinh(0.9503469298) = 1.1
"""