-
Notifications
You must be signed in to change notification settings - Fork 1
/
multivar_interp_generic.py
77 lines (60 loc) · 2.8 KB
/
multivar_interp_generic.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
import math
"""
array of numbers (repr method ouotputs formatted string)
- generate coeff via vandermond, put these in array
"""
"""
def funct(...):
generate our function
return function obj
"""
def populate(num_points):
"""
Takes a squaer number of points (i.e. 1,4,9,etc.) and populates two lists, one of y terms and the other of x terms
with each term having exponents beginning at 0 and increasing to sqrt(num_points).
Ex/ num_points = 4
-> xterms = [x^0, x^1, x^2],
-> yterms = [y^0, y^1, y^2]
param: num_points: the square-number of points for which to find the interpolating polynomial for.
return: List[xterms, yterms] -- a list of 2 lists, where xterms and yterms are as depicted in the above example.
"""
superscript = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
highest_deg = int(math.sqrt(num_points))
x_terms = []
y_terms = []
for i in range(highest_deg):
x_terms.append(f'(x{i})'.translate(superscript))
y_terms.append(f'(y{i})'.translate(superscript))
return [x_terms, y_terms]
def make_order_right(string):
first = string[:4]
second = string[4:]
return f'{second}{first}'
def generate_generic_form(num_points):
subscript = str.maketrans("0123456789", "₀₁₂₃₄₅₆₇₈₉")
x_terms = populate(num_points)[0]
y_terms = populate(num_points)[1]
combined_terms =[]
for termx in x_terms:
for termy in y_terms:
combined_terms.append(f'{termx}{termy}')
const = 0
for term_index in range(len(combined_terms)):
if combined_terms[term_index][2] == "⁰": # if x term has 0 exponent
if combined_terms[term_index][6] == "⁰": # if y term ALSO has 0 exponent
combined_terms[term_index] = "c0" # replace with appropriate constant
else: # if just the x term is ^0
combined_terms[term_index] = combined_terms[term_index].replace("(x⁰)", f'c{const}')
elif combined_terms[term_index][6] == "⁰": # if just the y term is ^0
combined_terms[term_index] = combined_terms[term_index].replace(f"(y⁰)", f"c{const}")
combined_terms[term_index] = make_order_right(combined_terms[term_index])
else:
combined_terms[term_index] = f'c{const}{combined_terms[term_index]}'
const += 1
return " + ".join(combined_terms).translate(subscript)
def main():
print("General function using 4 nodes: ", generate_generic_form(4))
print("General functio using 9 nodes: ", generate_generic_form(9))
print("General functio using 16 nodes: ", generate_generic_form(16))
if __name__ == "__main__":
main()