-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolynomials.py
More file actions
49 lines (31 loc) · 949 Bytes
/
Copy pathpolynomials.py
File metadata and controls
49 lines (31 loc) · 949 Bytes
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
# -*- coding: utf-8 -*-
"""
Created on Wed Feb 15 16:32:11 2017
@author: Oliver
"""
def parseMono(text):
for
class Poly:
""" has a dict of (basis vector: str, power: int) => coefficient: float"""
POWER_SYMB = '^'
def __init__(self, coefs):
self.coefs = {}
if type(coefs) is str:
for term in coefs.split("+"):
term = term.strip()
elif type(coefs) is dict:
for v,n in coefs.items():
if type(v) is str and POWER_SYMB in v:
idx = v.find(POWER_SYMB)
self.coefs[(v[:idx], int(v[idx+1:]))] = coefs[v]
elif type(v) is tuple and len(v) is 2:
self.coefs[v] = coefs[v]
else:
self.coefs[(v,1)] = coefs[v]
# Or it's a list like?
# assume we've been passed coefficients indexed by strings
self.coefs = {(v,1):d for v,d in coefs.items()}
self.coefs = coefs
def __radd__(self, other):
return Poly(self.coefs + other.coefs)
def __str__(self):