integrate()

D

Dr.Ruud

I found this interesting example on comp.lang.misc
<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/
;)
 
A

anno4000

Dr.Ruud said:
I found this interesting example on comp.lang.misc
<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
 
D

Dr.Ruud

(e-mail address removed)-berlin.de schreef:
Dr.Ruud:
I found this interesting example on comp.lang.misc
<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>

See also
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 "symbolic integration"
google: perl risch integral OR integration
 
I

Ilya Zakharevich

[A complimentary Cc of this posting was NOT [per weedlist] sent to
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::pari=: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.
 
J

jl_post

Ilya said:
perl -MMath::pari=: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
 
D

Dr.Ruud

Ilya Zakharevich schreef:
Dr.Ruud:

perl -MMath::pari=: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". :(
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,263
Messages
2,571,064
Members
48,769
Latest member
Clifft

Latest Threads

Top