Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > generate all possible math expr of one term

Thread Tools

generate all possible math expr of one term
Posts: n/a
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@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*"]) &]

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)

For example, these are first few items from the above code:

{1/(x^2*y^2), 1/(x*y^2), x/y^2, 1/(x*y), x/y, x^2/y, x*y, x^2*y,
x^2*y^2, Cos[x]/y^2, Cos[x]/y, Cos[x]/(x*y), (x*Cos[x])/y, y*Cos[x],
(y*Cos[x])/x, x*y*Cos[x], y^2*Cos[x], Cos[x]*Cos[y], Cot[x]/y^2,

For a gallery of selection plots of these equations, see

The above i wrote in 2002. If there are requests, i'll translate the
above code into emacs lisp. The result lisp expression should match
Mathematica's, token for token. (the code make a lot use of nested
lambda and or apply and or map) If you are interested, you could
translate the above into lisp too, it's not difficult (though the
number of lines will increase maybe 10 fold. And if Common Lisp
doesn't have combinatorics library providing KSubsets, and also since
CL doesn't have Outer, so the above in CL might be few hundred lines).
(see here for a example of how to:

PS as a after-thought, i decided to post this to perl, python, and
java too. This will take about the same number of lines in perl as in
Common Lisp. Probably llightly more in Python due to syntax. In Java,
it will be one million lines.

Gratuitous poem of the day:

in the climb to geekdom,
you have few rungs to catch,
before you see my ass.

—Xah Lee, 2005

Xah Removed)

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2) VK Javascript 15 05-02-2010 03:43 PM
generate all possible math expr of one term Java 1 05-15-2008 02:20 PM
generate all possible math expr of one term Python 0 05-14-2008 11:49 PM
Syntax bug, in 1.8.5? return not (some expr) <-- syntax error vsreturn (not (some expr)) <-- fine Good Night Moon Ruby 9 07-25-2007 04:51 PM
Why can I not use: Math a=new Math(); chirs Java 18 03-02-2004 06:00 PM