-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
212 lines (165 loc) · 8.01 KB
/
util.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import math
import sympy
def analyze_term_contribution_additive(function, epsilon, a=1, b=1, c=1, d=1):
"""
Determines if each term in a function contributes at least epsilon percent to the total function value.
Args:
function (Function): An object representing the function to analyze.
epsilon (float): The minimum percentage each term must contribute to the total function value.
a (float, optional): A coefficient applied to the first term in the function. Defaults to 1.
b (float, optional): A coefficient applied to the second term in the function. Defaults to 1.
c (float, optional): A coefficient applied to the third term in the function. Defaults to 1.
d (float, optional): A coefficient applied to the fourth term in the function. Defaults to 1.
Returns:
bool: True if each term in the function contributes at least epsilon percent to the total function value, False otherwise.
Examples:
>>> f = Function('x^2 + 3x + 5')
>>> analyze_term_contribution_additive(f, 10)
True
>>> g = Function('x^3 + 4x^2 + 2x + 1')
>>> analyze_term_contribution_additive(g, 5)
False
"""
total_sum = eval(function.function)
term_contributions = []
for i in range(len(function.terms)):
result = eval(function.terms[i])
percent = (result / total_sum) * 100
term_contributions.append(percent)
return all(i >= epsilon for i in term_contributions)
def convert_to_sympy_function(python_function):
"""
Converts a Python function to a SymPy function.
Args:
python_function (str): A string representing a Python function.
Returns:
str: A string representing a SymPy function.
Examples:
>>> convert_to_sympy_function("x**2 + 2*x + 1")
'x**2 + 2*x + 1'
>>> convert_to_sympy_function("math.log(x)")
'sympy.log(x, 2)'
"""
sympy_function = python_function
while sympy_function.find("math.log") != -1:
pos = sympy_function.find("math.log")
head = sympy_function[:pos]
head += "sympy.log("
log_var = sympy_function[pos + 10:][0]
log_var += ", 2"
sympy_function = head + log_var + sympy_function[pos + 11:]
return sympy_function
def analyze_term_contributions_multiplicative(python_function, epsilon, nr_parameters, x1=1, x2=1, x3=1, x4=1):
"""
Analyze the term contributions of a Python function by calculating the partial derivatives of each parameter and
their respective contributions in percentage to the total function value, for a given set of input values.
Args:
python_function (str): The Python function to be analyzed in string format.
epsilon (float): The minimum threshold for the contribution of each parameter to be considered significant.
nr_parameters (int): The number of parameters of the Python function (must be 2, 3, or 4).
x1 (float, optional): The value of the first parameter. Defaults to 1.
x2 (float, optional): The value of the second parameter. Defaults to 1.
x3 (float, optional): The value of the third parameter (only used if nr_parameters=3 or 4). Defaults to 1.
x4 (float, optional): The value of the fourth parameter (only used if nr_parameters=4). Defaults to 1.
Returns:
bool: True if all the contributions are greater than or equal to the epsilon threshold, False otherwise.
"""
term_contributions = []
if nr_parameters == 2:
# calculate the sum of the python_function
a = x1
b = x2
function_value = eval(python_function)
# create a "symbol" called a, b for calculating the derivative with sympy
a = sympy.Symbol('a')
b = sympy.Symbol('b')
# convert python function to sympy function
sympy_function = convert_to_sympy_function(python_function)
# convert the string into a returnable python_function using eval method
sympy_function = eval(sympy_function)
# calculating derivative
df_da = sympy_function.diff(a)
df_db = sympy_function.diff(b)
# calculate derivative value using df_da, and df_db and the parameter values
parameter_values = {a: x1, b: x2}
df_da = df_da.evalf(subs=parameter_values)
df_db = df_db.evalf(subs=parameter_values)
# calculate the contribution in percent for each function term
term_contributions.append((df_da / function_value) * 100)
term_contributions.append((df_db / function_value) * 100)
return all(i >= epsilon for i in term_contributions)
elif nr_parameters == 3:
# calculate the sum of the python_function
a = x1
b = x2
c = x3
function_value = eval(python_function)
# create a "symbol" called a, b for calculating the derivative with sympy
a = sympy.Symbol('a')
b = sympy.Symbol('b')
c = sympy.Symbol('c')
# convert python function to sympy function
sympy_function = convert_to_sympy_function(python_function)
# convert the string into a returnable python_function using eval method
sympy_function = eval(sympy_function)
# calculating derivative
df_da = sympy_function.diff(a)
df_db = sympy_function.diff(b)
df_dc = sympy_function.diff(c)
# calculate derivative value using df_da, and df_db and the parameter values
parameter_values = {a: x1, b: x2, c: x3}
df_da = df_da.evalf(subs=parameter_values)
df_db = df_db.evalf(subs=parameter_values)
df_dc = df_dc.evalf(subs=parameter_values)
# calculate the contribution in percent for each function term
term_contributions.append((df_da / function_value) * 100)
term_contributions.append((df_db / function_value) * 100)
term_contributions.append((df_dc / function_value) * 100)
counter = 0
for i in range(len(term_contributions)):
if term_contributions[i] >= epsilon:
counter += 1
if counter >= (nr_parameters-1):
return True
else:
return False
elif nr_parameters == 4:
# calculate the sum of the python_function
a = x1
b = x2
c = x3
d = x4
function_value = eval(python_function)
# create a "symbol" called a, b for calculating the derivative with sympy
a = sympy.Symbol('a')
b = sympy.Symbol('b')
c = sympy.Symbol('c')
d = sympy.Symbol('d')
# convert python function to sympy function
sympy_function = convert_to_sympy_function(python_function)
# convert the string into a returnable python_function using eval method
sympy_function = eval(sympy_function)
# calculating derivative
df_da = sympy_function.diff(a)
df_db = sympy_function.diff(b)
df_dc = sympy_function.diff(c)
df_dd = sympy_function.diff(d)
# calculate derivative value using df_da, and df_db and the parameter values
parameter_values = {a: x1, b: x2, c: x3, d: x4}
df_da = df_da.evalf(subs=parameter_values)
df_db = df_db.evalf(subs=parameter_values)
df_dc = df_dc.evalf(subs=parameter_values)
df_dd = df_dd.evalf(subs=parameter_values)
# calculate the contribution in percent for each function term
term_contributions.append((df_da / function_value) * 100)
term_contributions.append((df_db / function_value) * 100)
term_contributions.append((df_dc / function_value) * 100)
term_contributions.append((df_dd / function_value) * 100)
counter = 0
for i in range(len(term_contributions)):
if term_contributions[i] >= epsilon:
counter += 1
if counter >= (nr_parameters-1):
return True
else:
return False