Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/mathlib.py
diff options
context:
space:
mode:
authorReinier Heeres <reinier@heeres.eu>2008-12-23 21:50:32 (GMT)
committer Reinier Heeres <reinier@heeres.eu>2008-12-23 21:50:32 (GMT)
commit6807696129436dbf071b606300ce6dda2adb1c50 (patch)
tree984328276f12f6d18f89ea5ea4e940a379c3993f /mathlib.py
parentf0b95df2389c5686979b489fa5f2084fd3738edf (diff)
Move to more flexible and faster parser based on python AST
Diffstat (limited to 'mathlib.py')
-rw-r--r--mathlib.py238
1 files changed, 0 insertions, 238 deletions
diff --git a/mathlib.py b/mathlib.py
index a0dad3e..4b81a4d 100644
--- a/mathlib.py
+++ b/mathlib.py
@@ -35,37 +35,12 @@ class MathLib:
ANGLE_RAD = 1
ANGLE_GRAD = 1
- MATH_FUNCTIONS = (
- 'is_int', 'is_bool', 'compare', 'negate', 'abs', 'add', 'sub',
- 'mul', 'div', 'pow', 'sqrt', 'mod', 'exp', 'ln', 'log10', 'factorial',
- 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'sinh', 'cosh', 'tanh',
- 'asinh', 'acosh', 'atanh', 'round', 'floor', 'ceil', 'rand_float',
- 'rand_int', 'shift_left', 'shift_right', 'factorize',
- )
-
def __init__(self):
self.constants = {}
self.set_angle_type(self.ANGLE_DEG)
self._setup_i18n()
-#Constants should maybe become variables in eqnparser.py
- self.set_constant('true', True)
- self.set_constant('True', True)
- self.set_constant('false', False)
- self.set_constant('False', False)
- self.set_constant('pi', self.parse_number('3.1415926535'))
- self.set_constant('kb', self.parse_number('1.380650524e-23'))
- self.set_constant('Na', self.parse_number('6.02214e23'))
- self.set_constant('e', self.exp(1))
- self.set_constant('c', self.parse_number('2.99792458e8'))
- self.set_constant('c_e', self.parse_number('-1.60217648740e-19')) #electron properties
- self.set_constant('m_e', self.parse_number('9.109382616e-31'))
- self.set_constant('c_p', self.parse_number('1.6021765314e-19')) #proton properties
- self.set_constant('m_p', self.parse_number('1.6726217129e-27'))
- self.set_constant('c_n', self.parse_number('0')) #neutron properties
- self.set_constant('m_n', self.parse_number('1.6749272928e-27'))
-
def _setup_i18n(self):
loc = locale.localeconv()
@@ -93,27 +68,6 @@ class MathLib:
self.angle_scaling = self.d(type)
_logger.debug('Angle type set to:%s', self.angle_scaling)
- def set_constant(self, name, val):
- self.constants[name] = val
-
- def get_constant(self, name):
- if name in self.constants:
- return self.constants[name]
- else:
- return None
-
- def get_constants(self):
- return self.constants
-
- def get_math_functions(self):
- ret = {}
- items = inspect.getmembers(self)
- for key, val in items:
- if key in self.MATH_FUNCTIONS:
- ret[key] = val
-
- return ret
-
def d(self, val):
if isinstance(val, Decimal):
return val
@@ -227,195 +181,3 @@ class MathLib:
(sign, d, e) = n.normalize().as_tuple()
return e >= 0
-
- def is_float(self, n):
- if isinstance(n, Decimal):
- return not self.is_int(n)
- else:
- return False
-
- def is_bool(self, n):
- return type(n) is types.BoolType
-
- def compare(self, x, y):
- return x == y
-
- def negate(self, x):
- return -x
-
- def abs(self, x):
- return self.d(math.fabs(x))
-
- def add(self, x, y):
- if isinstance(x, Decimal) or isinstance(y, Decimal):
- x = self.d(x)
- y = self.d(y)
- return x + y
-
- def sub(self, x, y):
- if isinstance(x, Decimal) or isinstance(y, Decimal):
- x = self.d(x)
- y = self.d(y)
- return x - y
-
- def mul(self, x, y):
- if isinstance(x, Decimal) or isinstance(y, Decimal):
- x = self.d(x)
- y = self.d(y)
- return x * y
-
- def div(self, x, y):
- if y == 0 or y == 0.0:
- return _('Undefined')
-
- if self.is_int(x) and float(self.abs(y)) < 1e12 and \
- self.is_int(x) and float(self.abs(y)) < 1e12:
- return Rational(x, y)
-
- if isinstance(x, Decimal) or isinstance(y, Decimal):
- x = self.d(x)
- y = self.d(y)
-
- return x / y
-
- def pow(self, x, y):
- if self.is_int(y):
- if self.is_int(x):
- return long(x) ** int(y)
- elif hasattr(x, '__pow__'):
- return x ** y
- else:
- return float(x) ** int(y)
- else:
- if isinstance(x, Decimal) or isinstance(y, Decimal):
- x = self.d(x)
- y = self.d(y)
- return self.d(math.pow(float(x), float(y)))
-
- def sqrt(self, x):
- return self.d(math.sqrt(float(x)))
-
- def mod(self, x, y):
- if self.is_int(y):
- return x % y
- else:
- return self.d(0)
-
- def exp(self, x):
- return self.d(math.exp(float(x)))
-
- def ln(self, x):
- if float(x) > 0:
- return self.d(math.log(float(x)))
- else:
- return 0
-
- def log10(self, x):
- if float(x) > 0:
- return self.d(math.log10(float(x)))
- else:
- return 0
-
- def factorial(self, n):
- if not self.is_int(n):
- return self.d(0)
-
- if n == 0:
- return 1
-
- n = long(n)
- res = long(n)
- while n > 2:
- res *= n - 1
- n -= 1
-
- return res
-
- def sin(self, x):
- return self.d(math.sin(float(x * self.angle_scaling)))
-
- def cos(self, x):
- return self.d(math.cos(float(x * self.angle_scaling)))
-
- def tan(self, x):
- return self.d(math.tan(float(x * self.angle_scaling)))
-
- def asin(self, x):
- return self.d(math.asin(float(x))) / self.angle_scaling
-
- def acos(self, x):
- return self.d(math.acos(float(x))) / self.angle_scaling
-
- def atan(self, x):
- return self.d(math.atan(float(x))) / self.angle_scaling
-
- def sinh(self, x):
- return self.d(math.sinh(float(x)))
-
- def cosh(self, x):
- return self.d(math.cosh(float(x)))
-
- def tanh(self, x):
- return self.d(math.tanh(float(x)))
-
- def asinh(self, x):
- return self.d(math.asinh(float(x)))
-
- def acosh(self, x):
- return self.d(math.acosh(float(x)))
-
- def atanh(self, x):
- return self.d(math.atanh(float(x)))
-
- def round(self, x):
- return self.d(round(float(x)))
-
- def floor(self, x):
- return self.d(math.floor(float(x)))
-
- def ceil(self, x):
- return self.d(math.ceil(float(x)))
-
- def rand_float(self):
- return self.d(random.random())
-
- def rand_int(self):
- return self.d(random.randint(0, 65535))
-
- def shift_left(self, x, y):
- if self.is_int(x) and self.is_int(y):
- return self.d(int(x) << int(y))
- else:
- return 0
-
- def shift_right(self, x, y):
- if self.is_int(x) and self.is_int(y):
- return self.d(int(x) >> int(y))
- else:
- return 0
-
- def factorize(self, x):
- if not self.is_int(x):
- return 0
-
- factors = []
- num = x
- i = 2
- while i <= math.sqrt(num):
- if num % i == 0:
- factors.append(i)
- num /= i
- i = 2
- elif i == 2:
- i += 1
- else:
- i += 2
- factors.append(num)
-
- if len(factors) == 1:
- return "1 * %d" % x
- else:
- str = "%d" % factors[0]
- for fac in factors[1:]:
- str += " * %d" % fac
- return str