-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgebra_builder.py
226 lines (202 loc) · 6.56 KB
/
algebra_builder.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
from IPython.display import display, Markdown
import random
from collections import Counter
from IPython.display import HTML
def toggle_button():
# Hide from print preview use $('div.input').html("")
HTML('''<script>
code_show=true;
function code_toggle() {
if (code_show){
$('div.input').hide();
} else {
$('div.input').show();
}
code_show = !code_show
}
$( document ).ready(code_toggle);
</script>
<form action="javascript:code_toggle()"><input type="submit" value="Click here to toggle on/off the raw code."></form>''')
class Simplification():
def __init__(self):
self.samples = []
@classmethod
def type1(cls):
a = random.randint(1,9)
b = random.randint(2,9)
return a*a*b
@classmethod
def type2(cls):
a = random.randint(1,9)
b = random.randint(2,5)
c = random.randint(2,5)
return a*a*b*c
@classmethod
def type12(cls):
c = random.randint(0,1)
if c:
return cls.type1()
else:
return cls.type2()
@classmethod
def simple_prime_factorization(cls, num):
def _iter_(x,y):
if x==1:
return y
for each in [2,3,5,7,11,13,17,19]:
if x % each == 0:
y.append(each)
return _iter_(int(x/each), y)
return _iter_(num, [])
@classmethod
def simplify_sqrt(cls, num):
a=1
b=1
for k,v in Counter(cls.simple_prime_factorization(num)).items():
a *= k**int(v/2)
if v%2:
b *= k
return a,b
class BasicSimplification(Simplification):
def generate(self, n):
samples = []
while len(samples)<n:
x = self.type12()
if not x in samples:
samples.append(self.type12())
self.samples=samples
def output_exercises(self):
display(Markdown('#### Exercises ####'))
count = 0
for each in self.samples:
count +=1
display(Markdown('{}) $\sqrt{{{}}}$'.format(count, each)))
def output_solutions(self):
display(Markdown('#### Solutions ####'))
count = 0
for each in self.samples:
count +=1
display(Markdown('{}) $\sqrt{{{}}}={}$'.format(count, each, self.display_simplified(each))))
def display_simplified(self, num):
a,b = self.simplify_sqrt(num)
da = ''
db=''
if a>1:
da = '{}'.format(a)
if b>1:
db='\sqrt{{{}}}'.format(b)
return da+db
from math import gcd
class QuotientSimplification(Simplification):
def generate(self, n):
samples = []
while len(samples)<n:
numerator = random.randint(1, 9)*random.randint(1,9)
x=(numerator, self.type12())
if not x in samples:
samples.append(x)
self.samples=samples
def output_exercises(self):
display(Markdown('#### Exercises ####'))
count = 0
for each in self.samples:
count +=1
display(Markdown('{}) $\displaystyle {{{}\over\sqrt{{{}}}}}$'.format(count, each[0],each[1])))
def output_solutions(self):
display(Markdown('#### Solutions ####'))
count = 0
for each in self.samples:
count +=1
display(Markdown('{}) $\displaystyle {{{}\over\sqrt{{{}}}}}={}$'.format(count, each[0],each[1],self.display_simplified(each))))
def display_simplified(self, quot):
num, denom = quot
a1,b = self.simplify_sqrt(denom)
a=num
c=a1*b
gc = gcd(c,num)
a=int(a/gc)
c=int(c/gc)
da = ''
db=''
if a>1:
da = '{}'.format(a)
if b>1:
db='\sqrt{{{}}}'.format(b)
if c>1:
return '{{{}\over{}}}'.format(da+db,c)
return da+db
"""
from sympy import sqrt as simsqrt
from sympy import latex, simplify
class MixedSimplification(Simplification):
def generate(self, n):
samples = []
while len(samples)<n:
a = random.randint(-9,9)
b= random.choice([-5,-4,-3, -2, -1, 1, 2,3,4,5])
c= random.randint(-9,9)
d= random.choice([-5,-4,-3, -2, -1, 1, 2,3,4,5])
e = random.choice([2,3,5,6,7,8,10])
x=(a,b,c,d,e)
if not x in samples:
samples.append(x)
self.samples=samples
def output_exercises(self):
display(Markdown('#### Exercises ####'))
count = 0
for a,b,c,d,e in self.samples:
count +=1
display(Markdown('{}) $\displaystyle {}$'.format(count, latex((a+b*simsqrt(e))*(c+d*simsqrt(e))))) )
def output_solutions(self):
display(Markdown('#### Solutions ####'))
count = 0
for a,b,c,d,e in self.samples:
count +=1
eq = (a+b*simsqrt(e))*(c+d*simsqrt(e))
display(Markdown('{}) $\displaystyle {}={}$'.format(count, latex(eq), latex(simplify(eq)))))
"""
def root(a,b):
if a==1:
return b
if a==2:
return r'{{\sqrt{{{}}}}}'.format(b)
else:
return r'{{\root {} \of {{{}}}}}'.format(a,b)
# return r'{{\root {} \of {}}}'.format(a,b)
class RadicalSimplification(Simplification):
def generate(self, n):
samples = []
while len(samples)<n:
a = random.randint(2,9)
b= random.randint(2,a)
c= random.randint(1,3)
d= random.randint(2,10)
x=[a,b,c,d]
random.shuffle(x[0:2])
if not x in samples:
samples.append(x)
self.samples=samples
def output_exercises(self):
display(Markdown('#### Exercises ####'))
count = 0
for x in self.samples:
count +=1
display(Markdown('{}) $\displaystyle {}$'.format(count, root(x[0],root(x[1], root(x[2],x[3]))))))
def output_solutions(self):
display(Markdown('#### Solutions ####'))
count = 0
for a,b,c,d in self.samples:
expr = root(a,root(b, root(c,d)))
rad = a*b*c
if d==4 and rad%2==0:
rad=int(rad/2)
d=2
if d==9 and rad%2==0:
rad=int(rad/2)
d=3
if d==8 and rad %3==0:
rad= int(rad/3)
d=2
count+=1
display(Markdown('{}) $\displaystyle {}={}$'.format(count, expr, root(rad,d))))
# document.querySelectorAll("div.input").forEach(function(a){a.remove()})