# integrate()

Discussion in 'Perl Misc' started by Dr.Ruud, Aug 28, 2006.

1. ### Dr.RuudGuest

I found this interesting example on comp.lang.misc
<news:44f24874\$0\$3585\$>
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']

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/

--
Affijn, Ruud

"Gewoon is een tijger."

Dr.Ruud, Aug 28, 2006

2. ### -berlin.deGuest

Dr.Ruud <> wrote in comp.lang.perl.misc:
> I found this interesting example on comp.lang.misc
> <news:44f24874\$0\$3585\$>
> that I think would be nice to translate to Perl.
>
>
> > The following Mathematica code implements polynomial integration

[...]

> 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?

There's a huge family of modules Math::Symbolic::*. I'm sure there
is something...

Anno

-berlin.de, Aug 29, 2006

3. ### Dr.RuudGuest

-berlin.de schreef:
> Dr.Ruud:

>> I found this interesting example on comp.lang.misc
>> <news:44f24874\$0\$3585\$>
>> that I think would be nice to translate to Perl.
>> [...]
>> 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?

>
> There's a huge family of modules Math::Symbolic::*. I'm sure there
> is something...

I searched again (like on "integration" and on "integral")
and found only
Math::Integral::Romberg - scalar numerical integration
but that is a different area. <g>

http://www.tangentspace.net/cz/archives/2005/05/mathsymbolic-modules

Math::Integral is i:
http://search.cpan.org/~aqumsieh/

These have 9000 hits:
google: perl risch integral OR integration

--
Affijn, Ruud

"Gewoon is een tijger."

Dr.Ruud, Aug 29, 2006
4. ### Dr.RuudGuest

Dr.Ruud, Aug 29, 2006
5. ### Ilya ZakharevichGuest

[A complimentary Cc of this posting was NOT [per weedlist] sent to
Dr.Ruud
<>], who wrote in article <>:
> > There's a huge family of modules Math::Symbolic::*. I'm sure there
> > is something...

>
> I searched again (like on "integration" and on "integral")
> and found only
> Math::Integral::Romberg - scalar numerical integration
> but that is a different area. <g>

perl -MMath:ari=:all -wle "\$x = PARIvar q(x); print \$p = (\$x**7 - 1)/(\$x - 1); print intformal \$p"
x^6+x^5+x^4+x^3+x^2+x+1
1/7*x^7+1/6*x^6+1/5*x^5+1/4*x^4+1/3*x^3+1/2*x^2+x

Hope this helps,
Ilya

P.S. This is with PARI 2.3.0; older version might have this function
named differently; check the docs.

Ilya Zakharevich, Aug 29, 2006
6. ### Guest

Ilya Zakharevich wrote:
>
> perl -MMath:ari=:all -wle "\$x = PARIvar q(x); print \$p = (\$x**7 - 1)/(\$x - 1); print intformal \$p"
> x^6+x^5+x^4+x^3+x^2+x+1
> 1/7*x^7+1/6*x^6+1/5*x^5+1/4*x^4+1/3*x^3+1/2*x^2+x
>
> Hope this helps,
> Ilya

Thanks, Ilya! I've been looking for something like this for a long
time.

And I'm happy to see that this module is even available on Win32
ActiveState Perl! (All that was needed to install it was the command
"ppm install Math-Pari".)

Thanks again.

-- Jean-Luc

--
perl -le "print(pack'B*','0'.unpack'B*',pack'w*',
5592691776,37562575106519616,25926642752,354130435682904)"

, Aug 29, 2006
7. ### Dr.RuudGuest

Ilya Zakharevich schreef:
> Dr.Ruud:

>> I searched again (like on "integration" and on "integral")
>> and found only
>> Math::Integral::Romberg - scalar numerical integration
>> but that is a different area. <g>

>
> perl -MMath:ari=:all -wle "
> \$x = PARIvar q(x);
> print \$p = (\$x**7 - 1)/(\$x - 1);
> print intformal \$p
> "
> x^6+x^5+x^4+x^3+x^2+x+1
> 1/7*x^7+1/6*x^6+1/5*x^5+1/4*x^4+1/3*x^3+1/2*x^2+x
>
> P.S. This is with PARI 2.3.0; older version might have this function
> named differently; check the docs.

Yes, very nice example indeed.

I should have searched for "integrals".

--
Affijn, Ruud

"Gewoon is een tijger."

Dr.Ruud, Aug 30, 2006