-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.py
210 lines (181 loc) · 4.49 KB
/
library.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
def add(a, c):
b = c
while len(a) != len(b):
b = list(b[::-1])
b.append(0)
b = list(b[::-1])
i = 0
addition = []
while i < len(a):
addition.append(a[i] + b[i])
i += 1
return addition
def subtract(a, c):
b = c
while len(a) != len(b):
b = list(b[::-1])
b.append(0)
b = list(b[::-1])
i = 0
subtraction = []
while i < len(a):
subtraction.append(a[i] - b[i])
i += 1
return subtraction
def multiply(a, b):
a, b = a[::-1], b[::-1]
multiply = [0] * (len(a) + len(b))
i = 0
while i < len(a):
j = 0
while j < len(b):
multiply[i+j] += a[i] * b[j]
j += 1
i += 1
return trim(list(multiply[::-1]))
def float_multiply(a, f):
i = 0
while i < len(a):
a[i] *= f
i += 1
return a
def divide(a, b):
i = 0
m = len(a) - 1
n = len(b) - 1
scale = 1. / b[0]
q = [0] * max(m - n + 1, 1)
r = a
while i < m - n + 1:
d = scale * r[i]
q[i] = d
r[i:i+n+1] = subtract(r[i:i+n+1],float_multiply(b, d))
i += 1
return [q, r]
def trim(a):
while a[0] == 0:
a.pop(0)
return a
def differentiate(a, n):
if n > len(a):
return 'invalid'
elif n == len(a):
return [0]
a = a[::-1]
j = 0
differentiated = []
while j < n:
i = 0
while i < len(a):
differentiated.append(i * a[i])
i += 1
differentiated.pop(0)
a = differentiated
differentiated = []
j += 1
differentiated = a[::-1]
return differentiated
def integrate(a, n):
print(a)
a = a[::-1]
j = 0
integrated = []
while j < n:
i = 0
while i < len(a):
if a[i] == 'c':
i += 1
integrated.append('c')
continue
integrated.append((1/(i+1)) * a[i])
i += 1
integrated = integrated[::-1]
integrated.append('c')
a = integrated[::-1]
integrated = []
j += 1
integrated = a[::-1]
return integrated
def evaluate(a, n):
val = 0
i = 0
while i < len(a):
val += n**(len(a) - i - 1) * a[i]
i += 1
return int(val)
def ready_input(a):
i = 0
split_poly = a.split('+')[::-1]
coefficients = []
prev_power = 0
if 'x^' in split_poly[0]:
lowest_power = split_poly[0].split('x')[1].replace(' ', '').replace('^', '')
i = 0
while i < int(lowest_power):
coefficients.append(0)
i += 1
elif 'x' in split_poly[0] and 'x^' not in split_poly[0]:
coefficients.append(0)
else:
coefficients.append(int(split_poly[0].replace(' ', '')))
split_poly.pop(0)
while i < len(split_poly):
coe = split_poly[i].split('x')[0].replace(' ', '')
power = split_poly[i].split('x')[1].replace(' ', '').replace('^', '')
if power == '':
coefficients.append(int(coe))
prev_power += 1
elif int(power) - prev_power != 1:
coefficients.append(0)
prev_power += 1
continue
else:
coefficients.append(int(coe))
prev_power += 1
i += 1
return coefficients[::-1]
def find_sign(a):
if a > 0:
return '+'
elif a == 0:
return ''
else:
return '-'
def ready_output(a):
i = 0
tex_str = ''
while i < len(a):
if a[i] == 0 and i != len(a) - 1:
tex_str += f' {find_sign(a[i + 1])} '
i += 1
continue
elif i == len(a) - 1:
tex_str += f'{abs(a[i])}'
i += 1
continue
elif i == len(a) - 2:
tex_str += f'{abs(a[i])}x {find_sign(a[i + 1])} '
i += 1
elif i == 0:
tex_str += f'{a[i]}x^{len(a) - i - 1} {find_sign(a[i + 1])} '
i += 1
else:
tex_str += f'{abs(a[i])}x^{len(a) - i - 1} {find_sign(a[i + 1])} '
i += 1
return tex_str
def find_root(a):
i = 0
acc = 0.001
error = 1
x1 = 3.5
diff_function = differentiate(a, 1)
while error > acc:
if evaluate(diff_function, x1) == 0:
x1 += 1
x2 = x1 - evaluate(a, x1)/evaluate(diff_function, x1)
error = abs(x2 - x1)
x1 = x2
if abs(x2 - round(x2)) < acc:
return int(round(x2))
else:
return x2