D
Dr.Ruud
I found this interesting example on comp.lang.misc
<that I think would be nice to translate to Perl.
That seems to come from
http://documents.wolfram.com/mathematica/book/section-2.3.14
which also mentions:
integrate[ 1/(a_. x_ + b_.), x_] := Log[a x + b]/a /; FreeQ[{a,b}, x]
integrate[Exp[a_. x_ + b_.], x_] := Exp[a x + b]/a /; FreeQ[{a,b}, x]
I assumed that somebody would have done something in this area already
(without calling Mathematica), so I checked CPAN, but I found nothing
for polynomial integration, so I probably just didn't look well enough,
did I?
Note that you can write the (trivial) example as
['c', '2 b', '3 a'] --> ['', 'c', 'b', 'a']
A variant:
['c', 'b', 'a'] --> ['', 'c', 'b / 2', 'a / 3']
See also google: mathematica FreeQ.
http://documents.wolfram.com/mathematica/book/section-1.8.5
http://www.physic.ut.ee/~kkannike/english/prog/mathematica/patterns/index.html
Legenda:
"^n." : the trailing "." means optional. If absent, than 1 is used.
Perlish: /(\d+)/ ? $1 : 1
"/;" : constraint follows.
Perlish: if ...
Solution-1: s/.*/c x + b x^2 + a x^3/

<that I think would be nice to translate to Perl.
The following Mathematica code implements polynomial integration
using only Mathematica's pattern matcher and the FreeQ[e, f] function
(which checks that the pattern "f" does not match any subexpression
of "e"):
integrate[y_ + z_ , x_] := integrate[y, x] + integrate[z, x]
integrate[c_ y_ , x_] := c integrate[y, x] /; FreeQ[c, x]
integrate[c_ , x_] := c x /; FreeQ[c, x]
integrate[x_^n_. , x_] := x^(n + 1)/(n + 1) /; FreeQ[n, x] && n != -1
For example:
integrate[3 a x^2 + 2 b x + c, x]
=> c x + b x^2 + a x^3
That seems to come from
http://documents.wolfram.com/mathematica/book/section-2.3.14
which also mentions:
integrate[ 1/(a_. x_ + b_.), x_] := Log[a x + b]/a /; FreeQ[{a,b}, x]
integrate[Exp[a_. x_ + b_.], x_] := Exp[a x + b]/a /; FreeQ[{a,b}, x]
I assumed that somebody would have done something in this area already
(without calling Mathematica), so I checked CPAN, but I found nothing
for polynomial integration, so I probably just didn't look well enough,
did I?
Note that you can write the (trivial) example as
['c', '2 b', '3 a'] --> ['', 'c', 'b', 'a']
A variant:
['c', 'b', 'a'] --> ['', 'c', 'b / 2', 'a / 3']
See also google: mathematica FreeQ.
http://documents.wolfram.com/mathematica/book/section-1.8.5
http://www.physic.ut.ee/~kkannike/english/prog/mathematica/patterns/index.html
Legenda:
"^n." : the trailing "." means optional. If absent, than 1 is used.
Perlish: /(\d+)/ ? $1 : 1
"/;" : constraint follows.
Perlish: if ...
Solution-1: s/.*/c x + b x^2 + a x^3/