-
Notifications
You must be signed in to change notification settings - Fork 0
/
variable.py
80 lines (66 loc) · 1.88 KB
/
variable.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
import copy
from fraction import *
class Variable:
def __init__(self,vars,scalars=[1]):
if isinstance(vars,Variable):
self.vars = copy.deepcopy(vars.vars)
else:
if isinstance(vars,str):
vars = [vars]
if isinstance(scalars,int):
scalars = [scalars]
self.vars = {}
for s,v in zip(scalars,vars):
self.vars[v] = Fraction(s)
def __copy__(self):
print "COPYING",self
ret = Variable(copy.copy(self.vars.keys()), copy.copy(self.vars.values()))
print "RESULT IS",ret
return ret
def __deepcopy(self):
print "DEEPCOPYING",self
ret = Variable(copy.deepcopy(self.vars.keys()), copy.deepcopy(self.vars.values()))
print "RESULT IS",ret
return ret
def __add__(self,other):
ret = Variable(self)
for var in other.vars.keys():
if var in ret.vars.keys():
ret.vars[var] += other.vars[var]
else:
ret.vars[var] = other.vars[var]
return ret
def __sub__(self,other):
ret = Variable(self)
for var in other.vars.keys():
if var in ret.vars.keys():
ret.vars[var] -= other.vars[var]
else:
ret.vars[var] = other.vars[var]
return ret
def __mul__(self,other):
ret = Variable(self)
for v in ret.vars.keys():
ret.vars[v] *= other
return ret
def __div__(self,other):
ret = Variable(self)
for var in ret.vars.values():
var /= other
return ret
def __str__(self):
return self.__repr__()
def __repr__(self):
for v in self.vars.keys():
if self.vars[v] == 0:
del self.vars[v]
if self.vars[self.vars.keys()[0]] == 1:
ret = self.vars.keys()[0]
else:
ret = str(self.vars[self.vars.keys()[0]]) + self.vars.keys()[0]
for v in self.vars.keys()[1:]:
if (self.vars[v] == 1):
ret += "+" + v
else:
ret += "+" + str(self.vars[v]) + v
return ret