Última actividad 1 month ago

Integration from string playground

racerxdl's Avatar Lucas Teske revisó este gist 9 years ago. Ir a la revisión

1 file changed, 3 insertions, 5 deletions

integrate.py

@@ -48,13 +48,11 @@ def parseAndRunInteg(message):
48 48 step = 0.1
49 49
50 50 if float(step) < minimumStepSize:
51 - print "Sorry, I dont do integrations with stepsize < %s" % minimumStepSize
52 - return None
51 + return "Sorry, I dont do integrations with stepsize < %s" % minimumStepSize
53 52
54 - #print "Calculating %s from %s to %s with step %s" %(formula, a, b, step)
55 - print "The result of %s is: %s" %(message, runStringIntegral(formula, float(a), float(b), float(step)))
53 + return "The result of %s is: %s" %(message, runStringIntegral(formula, float(a), float(b), float(step)))
56 54 else:
57 - print "Sorry I did not understand"
55 + return "Sorry I did not understand"
58 56
59 57 '''
60 58 Tests

racerxdl's Avatar Lucas Teske revisó este gist 9 years ago. Ir a la revisión

1 file changed, 1 deletion

integrate.py

@@ -41,7 +41,6 @@ def runStringIntegral(f, a, b, step=0.1):
41 41 return s
42 42
43 43 def parseAndRunInteg(message):
44 - print message
45 44 data = parser.match(message)
46 45 if not data == None:
47 46 formula, a, b, _, _, step = data.groups()

racerxdl's Avatar Lucas Teske revisó este gist 9 years ago. Ir a la revisión

1 file changed, 66 insertions

integrate.py(archivo creado)

@@ -0,0 +1,66 @@
1 + #!/usr/bin/env python
2 + import math, re
3 +
4 + parser = re.compile('integrate (.*) from ([0-9]+\.?[0-9]*) to ([0-9]+\.?[0-9]*)( )?(step)? ([0-9]+\.?[0-9]*)?')
5 +
6 + safeFuncs = {
7 + 'acos' : math.acos,
8 + 'asin' : math.asin,
9 + 'atan' : math.atan,
10 + 'atan2' : math.atan2,
11 + 'ceil' : math.ceil,
12 + 'cos' : math.cos,
13 + 'e' : math.e,
14 + 'exp' : math.exp,
15 + 'floor' : math.floor,
16 + 'log' : math.log,
17 + 'log10' : math.log10,
18 + 'pi' : math.pi,
19 + 'pow' : math.pow,
20 + 'sin' : math.sin,
21 + 'sqrt' : math.sqrt,
22 + 'tan' : math.tan
23 + }
24 +
25 + minimumStepSize = 0.0001
26 +
27 + def runStringIntegral(f, a, b, step=0.1):
28 + s = 0
29 + i = a
30 + sandbox = { t:safeFuncs[t] for t in safeFuncs.keys()}
31 +
32 + if step == 0:
33 + sandbox["x"] = a
34 + return eval(f, {"__builtins__":None}, sandbox)
35 +
36 + while i < b:
37 + sandbox["x"] = i
38 + s = s + eval(f, {"__builtins__":None}, sandbox)
39 + i += step
40 + s /= (1.0 / step)
41 + return s
42 +
43 + def parseAndRunInteg(message):
44 + print message
45 + data = parser.match(message)
46 + if not data == None:
47 + formula, a, b, _, _, step = data.groups()
48 + if step == None:
49 + step = 0.1
50 +
51 + if float(step) < minimumStepSize:
52 + print "Sorry, I dont do integrations with stepsize < %s" % minimumStepSize
53 + return None
54 +
55 + #print "Calculating %s from %s to %s with step %s" %(formula, a, b, step)
56 + print "The result of %s is: %s" %(message, runStringIntegral(formula, float(a), float(b), float(step)))
57 + else:
58 + print "Sorry I did not understand"
59 +
60 + '''
61 + Tests
62 + '''
63 + #parseAndRunInteg("integrate sin(x) from 0 to 3.14 step 0.1") # Should be near 2
64 + #parseAndRunInteg("integrate sin(x) * pi from 0 to 3.14 step 0.1") # Should be near 6.28
65 + #parseAndRunInteg("integrate x ** 2 from 0 to 10 step 0.001") # Should be near 333
66 + #parseAndRunInteg("integrate x ** 2 from 1 to 4 step 0.000001") # Should print error
Siguiente Anterior