-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.py
More file actions
83 lines (62 loc) · 1.59 KB
/
Copy pathfunction.py
File metadata and controls
83 lines (62 loc) · 1.59 KB
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
import math
class function:
"""
A class to hold functions of the form: f(x) = m x + a
"""
def __init__(self,m,a):
self.mult = m
self.add = a
def __repr__(self):
if self.mult ==1 :
txt = ""
else:
txt = self.mult
answer = "f(x) = {}x".format(txt)
if self.add > 0:
answer += " + {}".format(self.add)
if self.add < 0:
answer += " - {}".format(-self.add)
return answer
def setMult(self,mult):
self.mult = mult
def setAdd(self,add):
self.add = add
def getCounterAmount(self):
return abs(self.add)
def apply(self, x):
return self.mult *x + self.add
def isCounter(self):
return self.mult == 1
def isPositiveCounter(self):
return self.isCounter() and self.add >= 0
def isNegativeCounter(self):
return self.isCounter() and self.add < 0
def isPureInverter(self):
return self.mult == -1
def isInverter(self):
return self.mult < 0
def isGrower(self):
return abs(self.mult) > 1
def isRestarter(self):
return self.mult == 0
def growingFrom(self, extra=0):
#only makes sense if not counter
return int(math.ceil((abs(self.add) + abs(extra))/(abs(self.mult)-1)))
def isEqual(self, fun):
if type(self) != type(fun):
return False
return self.mult == fun.mult and self.add == fun.add
def __eq__(self,fun):
return self.isEqual(fun)
def compose(self, fun):
a = self.mult
b = self.add
c = fun.mult
d = fun.add
return function(a*c, a*d + b)
def isIdentity(self):
return self.isEqual(function(1,0))
def getReverseFunction(self):
return function(self.mult, -self.add)
def simplestr(self):
return str(self.mult) + " " + str(self.add)