generate all possible math expr of one term

Jon Harrop jon at ffconsultancy.com
Thu May 15 13:00:35 EDT 2008


xahlee at gmail.com wrote:
> Here's a example of Expressiveness of a Language.
> 
> The following is Mathematica code that generates all possible
> equations of one term involving trig function. (tweak the funList and
> nesting level to define what ?all possible? means. if nesting level is
> 2, it takes about 20 minutes and returns a list of 2876 terms on a
> 2004 personal computer.
> 
> << DiscreteMath`Combinatorica`
> funList = {Sin, Tan, Power[#, -1] &};
> Nest[Module[{li = #},
>  (Union[#, SameTest -> (Module[{arg1 = #1, arg2 = #2},
>    If[(*both expression contains both x and y*)
>      And @@ (((((StringMatchQ[#, "*x*"] &&
>      StringMatchQ[#, "*y*"]) &)@
>      ToString@#) &) /@ {arg1, arg2})
>      , SameQ[arg1, arg2 /. {x -> y, y -> x}],
>      SameQ[arg1, arg2]]
>    ] &)] &)@
>    Union at Flatten@(Table[(Times @@ # &) /@ KSubsets[#, i], {i, 1,
>    2}] &)@Flatten@{li, Outer[#1@#2 &, funList, li]}
>  ] &, {x, y}, 1];
> Select[%, (StringMatchQ[ToString@#, "*x*"] &&
>  StringMatchQ[ToString@#, "*y*"]) &]

This does not even start running in Mathematica 6, let alone produce any
correct results. The first line was deprecated some time ago. I don't know
what is wrong with the rest of your program but it takes a while to produce
the empty list.

> The problem is this: generate a list of all possible math expressions
> using the following combination and rules:
> 
> ? the math expression involves both x and y. (must have both present)
> ? you can use any of the 6 trig functions (you must, since the goal is
> to create all possibilities)
> ? The binary operations you can use are multiply, divide. (no addition
> or substraction, since that can make the result 2 terms)
> ? a sub-expression (such as x or y) can be replaced by a more
> complicated one. (i.e. you can nest)

Then your program should produce an infinite number of terms, e.g. x,
Sin[x], Sin[Sin[x]], ...

> The above i wrote in 2002. If there are requests, i'll translate the
> above code into emacs lisp.

A correct definition of what the program is supposed to do with a correct
implementation in any language would be a good start.

If you are trying to do what I think you are trying to do then this is a
trivial recursive definition in just about any language and it does not
take advantage of anything specific to Mathematica anyway.

-- 
Dr Jon D Harrop, Flying Frog Consultancy
http://www.ffconsultancy.com/products/?u



More information about the Python-list mailing list