-
Notifications
You must be signed in to change notification settings - Fork 0
/
irrationals.py
139 lines (118 loc) · 4.43 KB
/
irrationals.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
# We're not getting fancy here. The irrational is considered a sqrt and positive
# If a negative value is given to the irrational part, it will be taken
# to mean the sqrt(ir) value is negative, not imaginary
# WILL NOT accept an IR as an input for anything
from math import sqrt
class IR:
def ir(self):
return(self.irrational)
def wh(self):
return(self.whole)
def fa(self):
return(self.factor)
def reduce(self):
max_x = int(sqrt(self.irrational))
for x in range(2,max_x+1):
y = self.irrational/(x**2)
if y == 1:
self.irrational = 0
self.whole += self.checkInt(self.factor*x)
break
elif y%1 == 0:
self.irrational = int(y)
self.factor *= x
break
def checkInt(self,x):
if type(x) is float:
if x%1 == 0:
return(int(x))
elif type(x) is int:
return(x)
return(float(x))
def __init__(self,ir,wh,irFa=1):
if type(wh) is IR or type(ir) is IR or type(irFa) is IR:
raise TypeError("IR given as input")
if ir < 0:
self.factor = -irFa
else:
self.factor = irFa
self.irrational = abs(ir)
self.whole = wh
def __str__(self):
signWh = self.whole/abs(self.whole)
if self.irrational == 1:
str_ir = str(self.factor)
elif self.irrational == 0:
str_ir = ""
else:
if abs(self.factor) == 1:
str_ir = "√(" + str(self.irrational) + ")"
else:
str_ir = str(self.factor) + "√" + str(self.irrational)
if self.factor < 0 and not self.irrational == 0:
signFactor = "-"
else:
signFactor = ""
if self.irrational != 0 and self.factor != 0:
if signWh == 1:
signWhole = "+"
else:
signWhole = "-"
else:
signWhole = ""
return(f"{signFactor}{str_ir}{signWhole}{abs(self.whole)}")
def __float__(self):
return(self.factor*sqrt(self.irrational)+self.whole)
def __int__(self):
return(int(float(self)))
def __neg__(self):
return(IR(self.irrational,-self.whole,-self.factor))
def __add__(self,x):
if type(x) is IR:
if self.irrational == x.irrational:
return(IR(self.irrational,self.whole+x.whole,self.factor+x.factor))
else:
raise TypeError("IR with non-matching irrationals given")
else:
return(IR(self.irrational,self.whole+x,self.factor))
def __radd__(self,x):
return(self + x)
def __sub__(self,x):
return(self - x)
def __rsub__(self,x):
return(-self + x)
def __mul__(self,x):
if type(x) is IR:
if self.irrational == x.irrational:
return(IR(self.irrational,self.checkInt(self.factor*x.factor*self.irrational+self.whole*x.whole),self.checkInt(self.factor*x.whole+x.factor*self.whole)))
else:
raise TypeError("IR with non-matching irrationals given")
def __rmul__(self,x):
return(self*x)
def __truediv__(self,x):
if type(x) is IR:
if self == x:
return(1)
else:
raise TypeError("IR given as input")
else:
return(IR(self.irrational,self.checkInt(self.whole/x),self.checkInt(self.factor/x)))
def __rtruediv__(self,x):
if type(x) is IR:
if self == x:
return(1)
else:
raise TypeError("IR given as input")
else:
mag = self.factor**2*self.irrational - self.whole**2
return(IR(self.irrational,self.checkInt(-x*self.whole/mag),self.checkInt(x*self.factor/mag)))
def __eq__(self,x):
return(self.irrational == x.irrational and self.whole == x.whole and self.factor == x.factor)
def __lt__(self,x):
return((self.factor*sqrt(self.irrational)+self.whole) < (x.factor*sqrt(x.irrational)+x.whole))
def __gt__(self,x):
return((self.factor*sqrt(self.irrational)+self.whole) > (x.factor*sqrt(x.irrational)+x.whole))
def __le__(self,x):
return(self<x or self==x)
def __ge__(self,x):
return(self>x or self==x)