-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsupermath.py
280 lines (213 loc) · 8.45 KB
/
supermath.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# supermath.py
'''This is a module designed either to fit in all the things that the regular Python math module 'missed', or simply
to make them easier to use.'''
# Authors: Noah Broyles and ankith26
# Copyright: "None"
import math
import random
import fractions
pi = 3.1415926535897932384662643383279502884197
realpi = 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
e = 2.718281828459045235360287471352662497757
# Here are the regular functions of math
def average(listOfNums):
total = math.fsum(listOfNums)
av = (total / len(listOfNums))
return av
# The mean is sorta the same as the average
def mean(listOfNums):
return average(listOfNums)
# I did not write this one
def gcd(a, b):
while a != 0:
a, b = b % a, a
return b
# we know lcm * gcd of two numbers is equal to their product
def lcm(a, b):
return (a * b) / gcd(a, b)
def is_real_number(number):
if not math.isnan(number):
return True
else:
return False
def square_root(number):
return math.sqrt(number)
def fast_exp(num, power):
# Thanks to anteprandium @ https://gist.github.com/anteprandium/3c5d855f96e3d39fe604 for the algorithm for super fast exponentiation in python!
def bits_of(m):
n = int(m)
while n:
yield n & 1
n >>= 1
result = 1
partial = num
for bit in bits_of(power):
if bit:
result *= partial
partial **= 2
return result
# This is a beautiful quadratic equation solver! I used it for math. It worketh!
def quadratic(a, b, c):
try:
x = (-b + math.sqrt(fastExp(b, 2) - (4 * a * c))) / (2 * a)
y = (-b - math.sqrt(fastExp(b, 2) - (4 * a * c))) / (2 * a)
return x, y
except ValueError:
return 'The equation has no real roots!'
except ZeroDivisionError:
return 'The equation entered is not a quadratic (how could you enter a = 0)!!'
# Returns the volume of a sphere based on the radius
def vol_sphere(r):
global pi
return 4 / 3 * (pi * r ** 3)
# Permutation notation
def permutation_notation(n, r):
return math.factorial(n) / math.factorial((n - r))
# Combination notation
def combination_notation(n, r):
return permNote(n, r) / math.factorial(r)
# Returns a division quotient with a remainder
def divide_remain(dividend, divisor):
remainder = dividend % divisor
if remainder == 0:
return int(dividend / divisor), 0
else:
ans = str(dividend / divisor)
main = (ans.split('.'))[0]
return int(main), remainder
# Trig functions in DEGREES like all the truly scientific minds prefer
# small modification by ankith26,
# you can specify in the trigonometric function
# wether you want degree or radian with the degree being the default
# if you want radians, use
# sin(pi/2, False)
def sin(x, degree=True):
if degree:
x = math.radians(x)
return math.sin(x)
def cos(x, degree=True):
if degree:
x = math.radians(x)
return math.cos(x)
def tan(x, degree=True):
if degree:
x = math.radians(x)
return math.tan(x)
def csc(x, degree=True):
return 1 / sin(x, degree)
def sec(x, degree=True):
return 1 / cos(x, degree)
def cot(x, degree=True):
return 1 / tan(x, degree)
# Returns the equation of a parabola when given the coordinates of the vertex and focus. Vertex vars: (xv, yv) Focus vars: (xf, yf)
def parabola_equation(xv, yv, xf, yf):
# I am so proud of this function! I can't sing it's praise enough!
try:
h = xv
k = yv
p = -(k - yf)
if yv != 0:
if k > 0:
if h > 0:
return "y = " + str(fractions.Fraction(1 / (4 * p))) + "(" + "x - " + str(h) + ")^2 + " + str(k)
elif h < 0:
return "y = " + str(fractions.Fraction(1 / (4 * p))) + "(" + "x + " + str(-h) + ")^2 + " + str(k)
elif h == 0:
return "y = " + str(fractions.Fraction(1 / (4 * p))) + "x^2 + " + str(k)
elif k < 0:
if h > 0:
return "y = " + str(fractions.Fraction(1 / (4 * p))) + "(" + "x - " + str(h) + ")^2 - " + str(-k)
elif h < 0:
return "y = " + str(fractions.Fraction(1 / (4 * p))) + "(" + "x + " + str(-h) + ")^2 - " + str(k)
elif h == 0:
return "y = " + str(fractions.Fraction(1 / (4 * p))) + "x^2 - " + str(k)
else:
return "y = " + str(fractions.Fraction(1 / (4 * p))) + "x^2"
except ZeroDivisionError:
return "Those are not valid data points"
def area_of_polygon_in_circle(sides, radius):
angle = (360 / sides) / 2
x = radius * (sin(angle))
A = radius * (cos(angle))
area = ((x * A) / 2) * (sides * 2)
return area
# This method solves algebraic equations using Cramer's rule of matrices with a problem looking like this:
def cramers_rule(q, w, e, r, t, y):
divisor = (q * t) - (r * w)
if divisor != 0:
x = ((e * t) - (y * w)) / divisor
y = ((q * y) - (r * e)) / divisor
return x, y
elif (q * y) == (r * e):
return "This problem has infinite solutions (when equations are graphed, we get coincident lines)"
else:
return "This problem has no solution (when equations are graphed, they are parallel and do not intersect)"
# Cramer's rule is just so hot! I sure wish I had been taught about it a long while ago!
# This method gives the statistics on a list of numbers. I am quite proud of it!
def stats(lis: list) -> dict:
# Get vars
length = len(lis)
lis = sorted(lis)
# Get the mean
mean = average(lis)
# Get median
if length == 1:
median = lis[0]
elif length == 2:
median = mean
else:
if (length % 2) != 0:
in1 = math.floor(length / 2)
median = (lis[in1])
else:
in1 = int(length / 2) - 1
in2 = int(length / 2)
median = (lis[in1] + lis[in2]) / 2
# Get range
r = lis[-1] - lis[0] # Whew! That was pretty easy! We can do that because the list was sorted.
# Get modes / mode
if not (length <= 1):
modes = []
detailed_modes = []
for element in set(lis):
cnt = lis.count(element)
if cnt > 1:
modes.append(element)
detailed_modes.append({"val": element, "occurrences": cnt})
mode = list(set(modes))
if len(modes) == 0:
mode, detailed_modes = None, None
else:
mode, detailed_modes = None, None
# Get standard deviation
new = []
for number in lis:
newNum = (number - mean) ** 2
new.append(newNum)
standard = math.sqrt(average(new))
# Get variance
variance = fastExp(standard, 2)
# Return all the values
return {"mean": mean, "median": median, "range": r, "mode": mode, "detailed_mode": sorted(detailed_modes, key=lambda d: d['occurrences'], reverse=True), "standardDeviation": standard, "variance": variance}
def is_prime(n):
# Miller-Rabin primality test implemented in Python
if n == 2:
return True
if n % 2 == 0:
return False
r, s = 0, n - 1
while s % 2 == 0:
r += 1
s //= 2
for _ in range(40): # 40 is a good amount of times to test for primality
a = random.randrange(2, n - 1)
x = pow(a, s, n)
if x == 1 or x == n - 1:
continue
for _ in range(r - 1):
x = pow(x, 2, n)
if x == n - 1:
break
else:
return False
return True