diff --git a/math.r2py b/math.r2py index df38369..793ce3c 100644 --- a/math.r2py +++ b/math.r2py @@ -61,3 +61,121 @@ def math_log(X, base=math_e, epsilon=1e-16): X *= X # We perform the squaring again return (integer + decimal) + +#Trigonometric functions via Euler's formula +def math_tan(x): + if math_cos(x) == 0: + raise ValueError, "tan function domain error" + return math_sin(x)/math_cos(x) + +def math_sin(x): + value = ((math_e**(complex(0,x)) - math_e**(complex(0,-x)))/complex(0,2)) + return value.real + +def math_cos(x): + value = ((math_e**(complex(0,x)) + math_e**(complex(0,-x)))/2) + return value.real + +#Hyperbolic functions via standard analytic expressions +def math_tanh(x): + if x == 0: + raise ValueError, "tan function domain error, Hyperbolic cotangent: x != 0 " + return math_sinh(x)/math_cosh(x) + +def math_sinh(x): + value = ((math_e ** x) - (math_e ** -x))/2 + return value + +def math_cosh(x): + value = ((math_e ** x) + (math_e ** -x))/2 + return value + +#Area functions via logarithms + +def math_atanh(x): + if(math_fabs(x) < 1): + return math_log((1 + x)/(1 - x))/2 + else: + raise ValueError, "math domain error, |'x'| cannot be >= 1 for math_artanh" + +def math_asinh(x): + return math_log((x + math_sqrt((x*x) + 1))) + +def math_acosh(x): + if(x >= 1): + return math_log((x + math_sqrt((x*x) - 1))) + else: + raise ValueError, "math domain error, 'x' cannot be < 1 for math_arcosh" + +#Arcus functions via Taylor/Maclaurin Series +def math_atan(x): + # arc tan 1 <= x + if(x >= 1): + value = math_pi/2 + count = 1 + for y in range(1,300,2): + if(count % 2 == 1): + value -= (float(1)/float(((x ** y)*y))) + else: + value += (float(1)/float(((x ** y)*y))) + count += 1 + return value + # arc tan -1 >= x + elif(x <= -1): + value = -math_pi/2 + count = 1 + for y in range(1,300,2): + if(count % 2 == 1): + value -= (float(1)/float(((x ** y)*y))) + else: + value += (float(1)/float(((x ** y)*y))) + count += 1 + return value + # arc tan -1 < x < 1 + else: + value = x + count = 1 + for y in range(3,300,2): + if(count % 2 == 1): + value -= (x ** y)/y + else: + value += (x ** y)/y + count += 1 + return value + +def math_asin(x): + if(x < 1 and x > -1): + value = x + numerator = 1 + denominator = 2 + for y in range(3,300,2): + value += (float(numerator)/float(denominator)) * ((x ** y) /float(y)) + numerator *= y + denominator *= (y + 1) + return value + else: + raise ValueError, "math domain error, 'x' cannot be >= 1 and 'x' cannot be <= -1 for math_asin" + +def math_acos(x): + if(x < 1 and x > -1): + return math_pi/2 - math_asin(x) + else: + raise ValueError, "math domain error, 'x' cannot be >= 1 and 'x' cannot be <= -1 for math_acos" + +# square root function using the Newton-Raphson method +def math_sqrt(x): + if(x == 0): + return x + if(x < 0): + raise ValueError, "math domain error, 'x' cannot be < 1 for math_sqrt" + k = 1.0 + while((k*k - x) > 0.00000000001 or (x - k * k) > 0.00000000001): + k = (k + x / k) / 2 + return k + +#basic absolute value function +def math_fabs(x): + if(x < 0): + return -x + else: + return x diff --git a/tests/ut_seattlelib_math_acoshfunct.py b/tests/ut_seattlelib_math_acoshfunct.py new file mode 100644 index 0000000..d4aa79f --- /dev/null +++ b/tests/ut_seattlelib_math_acoshfunct.py @@ -0,0 +1,18 @@ +#this unit test checks the functionality of the acosh function + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + + +import math + + +# Checks values form 1 to 9 +count = 1 +while(count != 9): + if(math_acosh(count) != math.acosh(count)): + if(str(math_acosh(count)) != str(math.acosh(count))): + print ("[FAIL]: graphing the positive domain of cos was a failure") + count += 1 diff --git a/tests/ut_seattlelib_math_arccosfunct.py b/tests/ut_seattlelib_math_arccosfunct.py new file mode 100644 index 0000000..45d9ff9 --- /dev/null +++ b/tests/ut_seattlelib_math_arccosfunct.py @@ -0,0 +1,19 @@ +#this unit test checks the functionality of the arc cos function + +from repyportability import * +add_dy_support(locals()) + +dy_import_module_symbols("math.r2py") + + +import math + + +# Checks values from 0 to +- 2 pi in intervals of pi/4 +count = 0.0 +while(str(count) != str(1.0)): + if(str(math_acos(count)) != str(math.acos(count))): + print ("[FAIL]: graphing the positive domain of cos was a failure") + if(str(math_acos(-count)) != str(math.acos(-count))): + print ("[FAIL]: graphing the negative domain of cos was a failure") + count += 0.1 diff --git a/tests/ut_seattlelib_math_arcsinfunct.py b/tests/ut_seattlelib_math_arcsinfunct.py new file mode 100644 index 0000000..8a3936d --- /dev/null +++ b/tests/ut_seattlelib_math_arcsinfunct.py @@ -0,0 +1,19 @@ +#this unit test checks the functionality of the arc sin fuinction + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + + +import math + + +# Checks values from 0 to +- 1 in intervals of 0.1 +count = 0.0 +while(str(count) != str(1.0)): + if(str(math_asin(count)) != str(math.asin(count))): + print ("[FAIL]: graphing the positive domain of sin was a failure") + if(str(math_asin(-count)) != str(math.asin(-count))): + print ("[FAIL]: graphing the negative domain of sin was a failure") + count += 0.1 diff --git a/tests/ut_seattlelib_math_arctanfunct.py b/tests/ut_seattlelib_math_arctanfunct.py new file mode 100644 index 0000000..26ddc6e --- /dev/null +++ b/tests/ut_seattlelib_math_arctanfunct.py @@ -0,0 +1,31 @@ +#this unit test checks the functionality of the arc tan fuinction + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + +import math + +# Checks values from 0 to +- 1 in intervals of 0.1 +count = 0.0 +while(str(count) != str(1.0)): + if(math_atan(count) != math.atan(count)): + if(str(math_atan(count)) != str(math.atan(count))): + print ("[FAIL]: graphing the positive domain of tan was a failure") + if(math_atan(-count) != math.atan(-count)): + if(str(math_atan(-count)) != str(math.atan(-count))): + print ("[FAIL]: graphing the negative domain of tan was a failure") + count += 0.1 + + +# Checks values form 0 to +- 10 in intervals of 1 +count = 0 +while(count != 1): + if(math_atan(count) != math.atan(count)): + if(str(math_atan(count)) != str(math.atan(count))): + print ("[FAIL]: graphing the positive domain of tan was a failure") + if(math_atan(-count) != math.atan(-count)): + if(str(math_atan(-count)) != str(math.atan(-count))): + print ("[FAIL]: graphing the negative domain of tan was a failure") + count += 1 \ No newline at end of file diff --git a/tests/ut_seattlelib_math_asinhfunct.py b/tests/ut_seattlelib_math_asinhfunct.py new file mode 100644 index 0000000..fea1282 --- /dev/null +++ b/tests/ut_seattlelib_math_asinhfunct.py @@ -0,0 +1,21 @@ +#this unit test checks the functionality of the asinh function + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + + +import math + + +# Checks values form 0 to +- 2 pi in intervals of pi/4 +count = 0 +while(count != 9): + if(math_asinh((math_pi*count) / 4) != math.asinh((math_pi*count) / 4)): + if(str(math_asinh(count)) != str(math.asinh(count))): + print ("[FAIL]: graphing the positive domain of sin was a failure") + if(math_asinh((-math_pi*count) / 4) != math.asinh((-math_pi*count) / 4)): + if(str(math_asinh(count)) != str(math.asinh(count))): + print ("[FAIL]: graphing the negative domain of sin was a failure") + count += 1 diff --git a/tests/ut_seattlelib_math_atanhfunct.py b/tests/ut_seattlelib_math_atanhfunct.py new file mode 100644 index 0000000..493490e --- /dev/null +++ b/tests/ut_seattlelib_math_atanhfunct.py @@ -0,0 +1,20 @@ +#this unit test checks the functionality of the atanh function + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + +import math + +#Checks value from -1 to 1 in intervals of 0.1 +count = 0.0 +while(str(count) != str(1.0)): + if(math_atanh(count) != math.atanh(count)): + if(str(math_atanh(count)) != str(math.atanh(count))): + print ("[FAIL]: graphing the positive domain of tan was a failure") + if(math_atanh(-count) != str(math.atanh(-count))): + if(str(math_atanh(count)) != str(math.atanh(count))): + print ("[FAIL]: graphing the negative domain of tan was a failure") + count += 0.1 + diff --git a/tests/ut_seattlelib_math_cosfunct.py b/tests/ut_seattlelib_math_cosfunct.py new file mode 100644 index 0000000..0dc3935 --- /dev/null +++ b/tests/ut_seattlelib_math_cosfunct.py @@ -0,0 +1,19 @@ +#this unit test checks the functionality of the cos function + +from repyportability import * +add_dy_support(locals()) + +dy_import_module_symbols("math.r2py") + + +import math + + +# Checks values from 0 to +- 2 pi in intervals of pi/4 +count = 0 +while(count != 9): + if(math_cos((math_pi*count) / 4) != math.cos((math_pi*count) / 4)): + print ("[FAIL]: graphing the positive domain of cos was a failure") + if(math_cos((-math_pi*count) / 4) != math.cos((-math_pi*count) / 4)): + print ("[FAIL]: graphing the negative domain of cos was a failure") + count += 1 diff --git a/tests/ut_seattlelib_math_coshfunct.py b/tests/ut_seattlelib_math_coshfunct.py new file mode 100644 index 0000000..5e3bca4 --- /dev/null +++ b/tests/ut_seattlelib_math_coshfunct.py @@ -0,0 +1,21 @@ +#this unit test checks the functionality of the cosh function + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + +import math + + +# Checks values from 0 to +- 2 pi in intervals of pi/4 +count = 0 +while(count != 9): + if(math_cosh((math_pi*count) / 4) != math.cosh((math_pi*count) / 4)): + if(str(math_cosh((math_pi*count) / 4)) != str(math.cosh((math_pi*count) / 4))): + print ("[FAIL]: graphing the positive domain of tan was a failure") + if(math_cosh((-math_pi*count) / 4) != math.cosh((-math_pi*count) / 4)): + if(str(math_cosh((-math_pi*count) / 4)) != str(math.cosh((-math_pi*count) / 4))): + print ("[FAIL]: graphing the negative domain of tan was a failure") + count += 1 + diff --git a/tests/ut_seattlelib_math_fabs.py b/tests/ut_seattlelib_math_fabs.py new file mode 100644 index 0000000..d44b5ab --- /dev/null +++ b/tests/ut_seattlelib_math_fabs.py @@ -0,0 +1,17 @@ +#this unit test checks the functionality of the absolute value function + +from repyportability import * +add_dy_support(locals()) + +dy_import_module_symbols("math.r2py") + +import math + +# Checks values from -10 to 10 in intervals of 1 +count = 0 +while(count != 10): + if(math.fabs(count) != math_fabs(count)): + print ("[FAIL]: Compairing absolute value was a failure") + if(math.fabs(-count) != math_fabs(-count)): + print ("[FAIL]: Compairing absolute value was a failure") + count += 1 \ No newline at end of file diff --git a/tests/ut_seattlelib_math_sinfunct.py b/tests/ut_seattlelib_math_sinfunct.py new file mode 100644 index 0000000..565ecc2 --- /dev/null +++ b/tests/ut_seattlelib_math_sinfunct.py @@ -0,0 +1,19 @@ +#this unit test checks the functionality of the sin function + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + + +import math + + +# Checks values from 0 to +- 2 pi in intervals of pi/4 +count = 0 +while(count != 9): + if(math_sin((math_pi*count) / 4) != math.sin((math_pi*count) / 4)): + print ("[FAIL]: graphing the positive domain of sin was a failure") + if(math_sin((-math_pi*count) / 4) != math.sin((-math_pi*count) / 4)): + print ("[FAIL]: graphing the negative domain of sin was a failure") + count += 1 diff --git a/tests/ut_seattlelib_math_sinhfunct.py b/tests/ut_seattlelib_math_sinhfunct.py new file mode 100644 index 0000000..bf1895e --- /dev/null +++ b/tests/ut_seattlelib_math_sinhfunct.py @@ -0,0 +1,21 @@ +#this unit test checks the functionality of the sinh function + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + + +import math + + +# Checks values from 0 to +- 2 pi in intervals of pi/4 +count = 0 +while(count != 9): + if(math_sinh((math_pi*count) / 4) != math.sinh((math_pi*count) / 4)): + if(str(math_sinh((math_pi*count) / 4)) != str(math.sinh((math_pi*count) / 4))): + print ("[FAIL]: graphing the positive domain of sin was a failure") + if(math_sinh((-math_pi*count) / 4) != math.sinh((-math_pi*count) / 4)): + if(str(math_sinh((-math_pi*count) / 4)) != str(math.sinh((-math_pi*count) / 4))): + print ("[FAIL]: graphing the negative domain of sin was a failure") + count += 1 diff --git a/tests/ut_seattlelib_math_sqrt.py b/tests/ut_seattlelib_math_sqrt.py new file mode 100644 index 0000000..a9fda96 --- /dev/null +++ b/tests/ut_seattlelib_math_sqrt.py @@ -0,0 +1,16 @@ +#this unit test checks the functionality of the sqrt function + +from repyportability import * +add_dy_support(locals()) + +dy_import_module_symbols("math.r2py") + +import math + +# Checks values from 0 to 2 +count = 0.0 +while(str(count) != str(2.0)): + if(math.sqrt(count) != math_sqrt(count)): + if(str(math.sqrt(count)) != str(math_sqrt(count))): + print ("[FAIL]: Compairing sqrt was a failure") + count += 0.1 \ No newline at end of file diff --git a/tests/ut_seattlelib_math_tanfunct.py b/tests/ut_seattlelib_math_tanfunct.py new file mode 100644 index 0000000..ce95f1f --- /dev/null +++ b/tests/ut_seattlelib_math_tanfunct.py @@ -0,0 +1,19 @@ +#this unit test checks the functionality of the tan function + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + +import math + + +# Checks values from 0 to +- 2 pi in intervals of pi/4 +count = 0 +while(count != 9): + if(math_tan((math_pi*count) / 4) != math.tan((math_pi*count) / 4)): + print ("[FAIL]: graphing the positive domain of tan was a failure") + if(math_tan((-math_pi*count) / 4) != math.tan((-math_pi*count) / 4)): + print ("[FAIL]: graphing the negative domain of tan was a failure") + count += 1 + diff --git a/tests/ut_seattlelib_math_tanhfunct.py b/tests/ut_seattlelib_math_tanhfunct.py new file mode 100644 index 0000000..849cc98 --- /dev/null +++ b/tests/ut_seattlelib_math_tanhfunct.py @@ -0,0 +1,21 @@ +#this unit test checks the functionality of the tanh function + + +from repyportability import * +add_dy_support(locals()) +dy_import_module_symbols("math.r2py") + +import math + + +# Checks values from +-pi/4 to +- 2 pi in intervals of pi/4 +count = 1 +while(count != 9): + if(math_tanh((math_pi*count) / 4) != math.tanh((math_pi*count) / 4)): + if(str(math_tanh((math_pi*count) / 4)) != str(math.tanh((math_pi*count) / 4))): + print ("[FAIL]: graphing the positive domain of tan was a failure") + if(math_tanh((-math_pi*count) / 4) != math.tanh((-math_pi*count) / 4)): + if(str(math_tanh((-math_pi*count) / 4)) != str(math.tanh((-math_pi*count) / 4))): + print ("[FAIL]: graphing the negative domain of tan was a failure") + count += 1 +