Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions math.r2py
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 18 additions & 0 deletions tests/ut_seattlelib_math_acoshfunct.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions tests/ut_seattlelib_math_arccosfunct.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions tests/ut_seattlelib_math_arcsinfunct.py
Original file line number Diff line number Diff line change
@@ -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
31 changes: 31 additions & 0 deletions tests/ut_seattlelib_math_arctanfunct.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions tests/ut_seattlelib_math_asinhfunct.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 20 additions & 0 deletions tests/ut_seattlelib_math_atanhfunct.py
Original file line number Diff line number Diff line change
@@ -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

19 changes: 19 additions & 0 deletions tests/ut_seattlelib_math_cosfunct.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions tests/ut_seattlelib_math_coshfunct.py
Original file line number Diff line number Diff line change
@@ -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

17 changes: 17 additions & 0 deletions tests/ut_seattlelib_math_fabs.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions tests/ut_seattlelib_math_sinfunct.py
Original file line number Diff line number Diff line change
@@ -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
21 changes: 21 additions & 0 deletions tests/ut_seattlelib_math_sinhfunct.py
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions tests/ut_seattlelib_math_sqrt.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions tests/ut_seattlelib_math_tanfunct.py
Original file line number Diff line number Diff line change
@@ -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

Loading