Lots of new random fraction generator

This commit is contained in:
lafrite 2014-01-19 21:37:46 +01:00
parent 6bfd9cc686
commit 7ed1780bf6
5 changed files with 149 additions and 382 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
creations/ creations/
__pycache__/

View File

@ -1,219 +0,0 @@
#!/usr/bin/env python
# encoding: utf-8
from random import randint, random
"""Classe which generate randomly fractions sums
Types of sums
1 -> b / a + c / a
2 -> b / a + c / ka
3 -> b / a + e / d
4 -> f + b / a or b / a + f
where:
a integer > 2
b integer different from 0 (could be coprime with a)
c integer different from 0 (could be coprime with a or ka)
e integer different from 0 (could be coprime with d)
d integer > 2 ( a not divisible by d and d not divisible by a)
k integer > 2
f integer different from 0
Signs can be mod
"""
def a(min_ = 2, max_ = 10):
"""Generate randomly a
:param min_: minimum value for a
:param max_: maximum value for a
:returns: a value
"""
return randint(min_, max_)
def k(min_ = 2, max_ = 5):
"""Generate randomly k
:param min_: minimum value for k
:param max_: maximum value for k
:returns: k value
"""
return randint(min_, max_)
def b(a_ = 0, min_ = -20, max_ = 20):
"""Generate randomly b
:param a: the value of a if b has to be coprime with a (default 0 which means not necessarily coprime)
:param min_: minimum value for b (default -20)
:param max_: maximum value for b (default 20)
:returns: b value
"""
b_ = 0
while b_ == 0 or not coprime:
b_ = randint(min_, max_)
if a_ == 0:
coprime = 1
elif b_ != 0:
gcd_ = gcd(abs(a_),abs(b_))
coprime = (gcd_ == 1)
return b_
def c(a_ = 0, k_ = 1, min_ = -20, max_ = 20):
"""Generate randomly c
:param a: the value of a if c has to be coprime with a (default 0 which means not necessarily coprime)
:param k: the value of a if c has to be coprime with ak (default 0 which means not necessarily coprime)
:param min_: minimum value for c (default -20)
:param max_: maximum value for c (default 20)
:returns: c value
"""
return b(a_ = a_*k_, min_ = min_, max_ = max_)
def e(d_ = 0, min_ = -20, max_ = 20):
"""Generate randomly e
:param d: the value of a if e has to be coprime with a (default 0 which means not necessarily coprime)
:param min_: minimum value for e (default -20)
:param max_: maximum value for e (default 20)
:returns: e value
"""
return b(a_ = d_, min_ = min_, max_ = max_)
def d(a_, min_ = 2, max_ = 10):
"""Generate randomly d
:param a: the value of a
:param min_: minimum value for d
:param max_: maximum value for d
:returns: d value
"""
d_ = randint(min_, max_)
div = (not a_ % d_) or (not d_ % a_)
while div:
d_ = randint(min_, max_)
div = (not a_ % d_) or (not d_ % a_)
return d_
def f(min_ = -10, max_ = 10):
"""Generate randomly f
:param min_: minimum value for f
:param max_: maximum value for f
:returns: f value
"""
f_ = randint(min_, max_)
while f_ == 0:
f_ = randint(min_, max_)
return f_
def plusOrMinus(p = 0.5):
"""Return plus with prob p and minus otherwise
"""
pm = random()
return "+"*(pm >= p) + "-"*(pm < p)
def nothingOrMinus(p = 0.5):
"""Return nothing with prob p and minus otherwise
"""
pm = random()
return ""*(pm >= p) + "-"*(pm < p)
def type1():
"""@todo: Docstring for type1
:returns: @todo
"""
a_ = a()
b_ = b(a_=a_)
c_ = c(a_=a_)
return nothingOrMinus() + "\\frac{" + str(b_) + "}{" + str(a_) + "}" + plusOrMinus() + "\\frac{" + str(c_) + "}{" + str(a_) + "}"
def type2():
"""@todo: Docstring for type2
:returns: @todo
"""
a_ = a()
b_ = b(a_=a_)
k_ = k()
c_ = c(a_=a_, k_ = k_)
return nothingOrMinus() + "\\frac{" + str(b_) + "}{" + str(a_) + "}" + plusOrMinus() + "\\frac{" + str(c_) + "}{" + str(k_*a_) + "}"
def type3():
"""@todo: Docstring for type3
:returns: @todo
"""
a_ = a()
b_ = b(a_=a_)
c_ = c(a_=a_)
d_ = d(a_)
return nothingOrMinus() + "\\frac{" + str(b_) + "}{" + str(a_) + "}" + plusOrMinus() + "\\frac{" + str(c_) + "}{" + str(d_) + "}"
def type4():
"""@todo: Docstring for type4
:returns: @todo
"""
a_ = a()
b_ = b(a_=a_)
f_ = f()
return str(f_) + plusOrMinus() + "\\frac{" + str(b_) + "}{" + str(a_) + "}"
def gcd(a_, b_):
"""Compute gcd(a,b)
:param a: first number
:param b: second number
:returns: the gcd
"""
if a_ > b_:
c_ = a_ % b_
else:
c_ = b_ % a_
if c_ == 0:
return min(a_,b_)
elif a_ == 1:
return b_
elif b_ == 1:
return a_
else:
return gcd(min(a_,b_), c_)
if __name__ == '__main__':
# print(a())
# print(b())
# print(c())
# print(d(3))
# print(e())
# print(f())
print(type1())
print(type2())
print(type3())
print(type4())
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@ -1,160 +0,0 @@
#!/usr/bin/env python
# encoding: utf-8
from random import randint, random
"""Classe which generate randomly fractions multiplications
Types of multiplications
1 -> a x b / c
2 -> a x b / c + d / c
3 -> a x b / c + d / e >>> TODO
4 -> e / f x g / h >>> TODO
5 -> i / j x k / l >>> TODO
where:
a integer differente from -1, 0, 1
b integer different from 0
c integer different from 0 and 1 (could be coprime with b)
d integer different from 0
e, g integer different from 0
f, g integer different from 0 and 1 such that e*g is coprime with f*h
i, k integer different from 0
j, l integer different from 0 and 1 such that i*k and j*l have divisor in common
Signs can be mod
"""
def a(min_ = -10, max_ = 10, notIn = [-1,0,1]):
"""Generate randomly a
:param min_: minimum value for a
:param max_: maximum value for a
:param notIn: value that can't take a
:returns: a value
"""
a_ = randint(min_, max_)
while a_ in notIn:
a_ = randint(min_, max_)
return a_
def b(min_ = -10, max_ = 10, notIn = [0]):
"""Generate randomly b
:param min_: minimum value for b
:param max_: maximum value for b
:param notIn: value that can't take b
:returns: a value
"""
return a(min_, max_, notIn)
def c(b_ = 0, min_ = 2, max_ = 20):
"""Generate randomly c
:param a_: the value of b if c has to be coprime with b (default 0 which means not necessarily coprime)
:param min_: minimum value for b (default -20)
:param max_: maximum value for b (default 20)
:returns: c value
"""
c_ = 0
while c_ == 0 or not coprime:
c_ = randint(min_, max_)
if b_ == 0:
coprime = 1
elif c_ not in [-1,0,1]:
gcd_ = gcd(abs(c_),abs(b_))
coprime = (gcd_ == 1)
return c_
def d(min_ = -10, max_ = 10, notIn = [0]):
"""Generate randomly d
:param min_: minimum value for d
:param max_: maximum value for d
:param notIn: value that can't take d
:returns: a value
"""
return a(min_, max_, notIn)
def plusOrMinus(p = 0.5):
"""Return plus with prob p and minus otherwise
"""
pm = random()
return "+"*(pm >= p) + "-"*(pm < p)
def nothingOrMinus(p = 0.5):
"""Return nothing with prob p and minus otherwise
"""
pm = random()
return ""*(pm >= p) + "-"*(pm < p)
def type1():
"""@todo: Docstring for type1
:returns: @todo
"""
a_ = a()
b_ = b()
c_ = c(b_=b_)
return str(a_) + " \\times \\frac{" + str(b_) + "}{" + str(c_) + "}"
def type2():
"""@todo: Docstring for type2
:returns: @todo
"""
a_ = a()
b_ = b()
c_ = c(b_=b_)
d_ = d()
return str(a_) + " \\times \\frac{" + str(b_) + "}{" + str(c_) + "} + \\frac{" + str(d_) + "}{" + str(c_) + "}"
def gcd(a_, b_):
"""Compute gcd(a,b)
:param a: first number
:param b: second number
:returns: the gcd
"""
if a_ > b_:
c_ = a_ % b_
else:
c_ = b_ % a_
if c_ == 0:
return min(a_,b_)
elif a_ == 1:
return b_
elif b_ == 1:
return a_
else:
return gcd(min(a_,b_), c_)
if __name__ == '__main__':
# print(a())
# print(b())
# print(c())
# print(d(3))
# print(e())
# print(f())
print(type1())
print(type2())
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del

View File

@ -4,6 +4,7 @@
import jinja2, random, os import jinja2, random, os
import sys import sys
import optparse import optparse
from rd_frac import frac
def randfloat(approx = 1, low = 0, up = 10): def randfloat(approx = 1, low = 0, up = 10):
""" return a random number between low and up with approx floating points """ """ return a random number between low and up with approx floating points """
@ -28,6 +29,8 @@ def gaussRandomlist_strInt(mu = 0, sigma = 1, size = 10):
random.gaussRandomlist_strInt = gaussRandomlist_strInt random.gaussRandomlist_strInt = gaussRandomlist_strInt
random.frac = frac
report_renderer = jinja2.Environment( report_renderer = jinja2.Environment(
block_start_string = '%{', block_start_string = '%{',
@ -40,16 +43,23 @@ loader = jinja2.FileSystemLoader(os.path.abspath('.'))
def main(options): def main(options):
template = report_renderer.get_template(options.template) template = report_renderer.get_template(options.template)
cwd = os.getcwd()
if options.output: if options.output:
output_basename = options.output output_name = options.output
else: else:
tpl_base = os.path.splitext(options.template)[0] tpl_base = os.path.splitext(options.template)[0]
output_basename = tpl_base + "_" output_name = tpl_base + "_"
output_dir = os.path.dirname(output_name)
output_basename = os.path.basename(output_name)
output_tplname = output_basename.split("/")[-1]
os.chdir(output_dir)
for subj in range(options.num_subj): for subj in range(options.num_subj):
subj = subj+1 subj = subj+1
dest = output_basename + str(subj) + '.tex' dest = output_tplname + str(subj) + '.tex'
with open( dest, 'w') as f: with open( dest, 'w') as f:
f.write(template.render(random = random, infos = {"subj" : subj})) f.write(template.render(random = random, infos = {"subj" : subj}))
os.system("pdflatex " + dest) os.system("pdflatex " + dest)
@ -57,6 +67,8 @@ def main(options):
if not options.dirty: if not options.dirty:
os.system("rm *.aux *.log") os.system("rm *.aux *.log")
os.chdir(cwd)
if __name__ == '__main__': if __name__ == '__main__':

133
rd_frac.py Normal file
View File

@ -0,0 +1,133 @@
#!/usr/bin/env python
# encoding: utf-8
from random import randint, random
from random_expression import RdExpression
from arithmetic import gcd
from renders import tex_render
"""Classe which generate randomly fractions calculus
Types of sums
add1 -> b / a + c / a
add2 -> b / a + c / ka
add3 -> b / a + e / d
add4 -> f + b / a
add5 -> b / a + f
where:
a integer > 2
b integer different from 0 (could be coprime with a)
c integer different from 0 (could be coprime with a or ka)
e integer different from 0 (could be coprime with d)
d integer > 2 ( a not divisible by d and d not divisible by a)
k integer > 2
f integer different from 0
Types of multiplications
mult1 -> a x b / c
mult2 -> a x b / c + d / c
mult3 -> a x b / c + d / e
mult4 -> e / f x g / h >>> TODO
mult5 -> i / j x k / l
where:
a integer different from -1, 0, 1
b integer different from 0
c integer different from 0 and 1 (could be coprime with b)
d integer different from 0
e, g integer different from 0
f, g integer different from 0 and 1 such that e*g is coprime with f*h
i, k integer different from 0
j, l integer different from 0 and 1 such that i*k and j*l have divisor in common
Types of divisions
div1 -> a / b : c / d
where:
a integer different from 0
b integer different from 0, 1
c integer different from 0
d integer different from 0
#Signs can be mod
"""
add1 = RdExpression("{b} / {a} + {c} / {a}", \
conditions = ["{a} > 2", "{b} != 0","{c} != 0"])
add2 = RdExpression("{b} / {a} + {c} / {k*a}", \
conditions = ["{a} > 2","{k} > 2", "{b} != 0","{c} != 0"])
add3 = RdExpression("{b} / {a} + {e} / {d}", \
conditions = ["{a} not in [0,1]", "{e} not in [0,1]", "{b} != 0","{d} not in [0,1]"])
add4 = RdExpression("{b} / {a} + {f}", \
conditions = ["{a} > 2", "{b} != 0", "{f} != 0"])
add5 = RdExpression("{f} + {b} / {a}", \
conditions = ["{a} > 2", "{b} != 0", "{f} != 0"])
mult1 = RdExpression("{a} * {b} / {c}",\
conditions = ["{a} not in [-1, 0, 1]", "{b} != 0", "{c} not in [0,1]"])
mult2 = RdExpression("{a} * {b} / {c} + {d} / {c}",\
conditions = ["{a} not in [-1, 0, 1]", "{b} != 0", "{c} not in [0,1]", \
"{d} != 0"])
mult3 = RdExpression("{a} * {b} / {c} + {d} / {e}",\
conditions = ["{a} not in [-1, 0, 1]", "{b} != 0", "{c} not in [0,1]", \
"{d} != 0", "{e} not in [0,1]", "{c} != {e}"])
#mult4 = RdExpression("{e} / {f} * {g} / {h}", \
# conditions = ["{e} != 0", "{g} != 0", "{f} != 0", "{g} != 0", \
# "gcd({e*g}, {f*h}) == 1"])
mult5 = RdExpression("{e} / {f} * {g} / {h}", \
conditions = ["{e} != 0", "{g} != 0", "{f} not in [0, 1]", "{h} not in [0, 1]"])
#"gcd({e*g}, {f*h}) != 1"])
div1 = RdExpression("{a} / {b} : {c} / {d}", \
conditions = ["{a} not in [0]", "{b} not in [0,1]", "{c} != 0","{d} not in [0]"])
frac = {"add1": add1,\
"add2": add2,\
"add3": add3,\
"add4": add4,\
"add5": add5, \
"mult1": mult1,\
"mult2": mult2,\
"mult3": mult2,\
#"mult4": mult2,\
"mult5": mult5, \
"div1": div1 }
if __name__ == '__main__':
print(add1())
print(add2())
print(add3())
print(add4())
print(add5())
print(mult1())
print(mult2())
print(mult3())
#print(mult4())
print(mult5())
print(div1())
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del