On Mon, Feb 9, 2015 at 12:40 PM, Ondřej Čertík <[email protected]> wrote: > Hi Peter, > > On Mon, Feb 9, 2015 at 9:09 AM, Peter Chervenski <[email protected]> wrote: >> I made this function to test for the equivalence of two expressions. It >> doesn't really prove anything, but if the tests are many, the probability of >> it being wrong becomes negligible. Do such utility functions have a place in >> SymPy? >> >> def equiv(a, b, ntests=15): >> """ Test if expression a is equivalent to b >> by comparing the results of many random numeric tests """ >> >> # get the symbols >> sb_a = filter(lambda x: x.is_Symbol, a.atoms()) >> sb_b = filter(lambda x: x.is_Symbol, b.atoms()) >> >> sb = list(set(sb_a + sb_b)) >> >> eq = True >> for i in xrange(ntests): >> k = dict(zip(sb, np.random.randn(len(sb)))) >> >> r_a = a.subs( k ) >> r_b = b.subs( k ) >> >> # prove there is a difference >> if (r_a - r_b)**2 > 1e-30: # not the same? the expressions are >> different >> eq = False >> break >> >> return eq > > > Absolutely. I've also implemented a similar function in one PR: > > https://github.com/sympy/sympy/pull/8036/files?diff=unified#diff-2c9ef1ef2c82f5d5781d0d12e1fe4910R33 > > It was pointed out to me that we have similar machinery here already: > > https://github.com/sympy/sympy/blob/master/sympy/utilities/randtest.py#L43 > > This should be unified and put into sympy. We can call it > "zero_numerical" or something like that ("test_numerically", ...). > Mathematica calls this PossibleZeroQ (though I think it does both > symbolic an numerical tests). > > Look at the implementation in my PR --- you should allow the user to > specify the range (I call it [a, b]) as well as the precision. I think > we can perhaps just test for 0, and then your "equiv" can just call > this zero testing function for a-b.
Actually, in my implementation I choose random integers from [-a, a] and then test an interval [-a/b, a/b]. That way you will get rational numbers, as opposed to only integers (that could hide differences). Ondrej -- You received this message because you are subscribed to the Google Groups "sympy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sympy. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CADDwiVDnybsk6Ke-ocGoPd%2BuB_S7v%3DDCeSgvGTR1JFTXiw5F_g%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
