> > Good idea. The ability to generate random expressions is very useful. We > should make this more general and add it to sympy/utilities/iterables.py. >
It's mesmerizing...may make the thing into a screen saver ;). I had to make a few modifications to your script. First off, you were > testing if integral(function.diff(x)) == function, which has two problems: > first, you don't even pass it to simplify(). As I said, the integral will > usually not look like the original function. > Ahh, right. Does it make sense to simplify the original formula before returning it from build_func? Attached is a copy that simplifies that equation, as well as the diff. > Fourth, I also regular print the original expression, which makes it easier > to copy and paste it if it is wrong and I need to debug it. > > I attached an improved script. Thanks for taking the time to look into > this. > So the attached alteration (which is just a few edits to yours...we should invent a system to control versions of files or something ;)) also makes the seed explicit. You can copy the printed seed into the source to repeat the same tests. Cheers -- You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sy...@googlegroups.com. To unsubscribe from this group, send email to sympy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
#! /usr/bin/env python from sympy import (exp, log, risch_integrate, cancel, pretty_print, Symbol, diff, simplify) import random import time # Seed this test seed = int(time.time()) random.seed(seed) print "Random Seed Used %d" % seed # Uncomment this with a proper seed if you wish to reproduce results # random.seed(1234) x = Symbol('x') unops = [exp, log, lambda x: -x] binops = [lambda x,y: x+y, lambda x,y: x-y, lambda x,y: x*y, lambda x,y: x/y] TRIALS = 5 def build_func(starting_vars = 2, starting_nums = 4, prob_unary = 0.5): parts = [x] * starting_vars for i in range(starting_nums): parts.append(random.randrange(1,10)) #print "Working with the following building blocks: %s" % str(parts) while len(parts) > 1: if random.random() < prob_unary: index = random.randrange(0,len(parts)) parts[index] = random.choice(unops)(parts[index]) else: random.shuffle(parts) parts.append(random.choice(binops)(parts.pop(), parts.pop())) return simplify(parts[0]) for i in range(TRIALS): eq = build_func() print "\n\n------------------------------\nTesting the following equation {%s}:\n" % str(eq) pretty_print(eq) print "\n\nDiff:\n" d = simplify(diff(eq, x)) pretty_print(d) print "\n\nIntegral of Diff:\n" try: res = risch_integrate(d, x) except NotImplementedError, detail: print "\nNotImplemented (%s)" % detail else: pretty_print(res) print "\nDifference (should be constant): %s" % str(cancel(res - eq)) print "\nDiff difference (should be 0): %s" % str(cancel(diff(res, x) - d)) print "------------------------------"