-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.py
63 lines (46 loc) · 2.37 KB
/
expression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from numpy import Infinity
from uncertainValue import UncertainValue
import toolbox
#add erro handling and checks if wolfram messages arent completed and cant get uncertainvalue
# add support for uncertainvaluelists (requires wolframcloud edit and toolbox edit)
class Expression():
"""
This class is used to perform error analysis on complex equations.
"""
def __init__(self, expression, label=""):
"""
The constructor takes an expression in as a string. This string is sent to wolframcloud so the equation has to be in wolfram format.
"""
self.expression = expression
self.label = label
self.variables = {}
self.derivatives = []
self.errorExpression = ""
self.resultingUncertainValue = None
def addVariable(self, variableName):
self.setVariable(variableName)
def setVariable(self, variableName, uncertainValue, value = Infinity, error = 0, absolute = False, digits = (-1, -1)):
if uncertainValue is not None:
self.variables[variableName] = uncertainValue
else:
self.variables[variableName] = UncertainValue(value, absolute = absolute, error = error, digits = digits)
def getPartialDerivatives(self):
if len(self.derivatives) < 1:
self.derivatives = toolbox.determinePartialDerivatives(self.expression, list(self.variables.keys()))
return self.derivatives
def getErrorExpression(self):
if not self.errorExpression:
self.errorExpression = toolbox.determineErrorExpression(self.getPartialDerivatives(), list(self.variables.keys()))
return self.errorExpression
def getUncertainValue(self):
if not self.resultingUncertainValue:
values = {}
for variableName, uncertainValue in self.variables.items():
values[variableName] = uncertainValue.value
values["d" + variableName] = uncertainValue.absoluteError
newValue = toolbox.evaluateExpression(self.expression, values)
newError = toolbox.evaluateExpression(self.getErrorExpression(), values)
self.resultingUncertainValue = UncertainValue(newValue, absolute=True, error=newError, label=self.label)
return self.resultingUncertainValue
def __str__(self):
return self.getUncertainValue().__str__()