Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Extract Integers
Replies: 5   Last Post: Jul 21, 2013 9:35 PM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Andrzej Kozlowski

Posts: 226
Registered: 1/29/05
Re: Extract Integers
Posted: Jul 21, 2013 9:34 PM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply


On 21 Jul 2013, at 10:23, Dana DeLouis <dana01@icloud.com> wrote:

> Hi. I've run out of ideas. Does anyone know the proper way to extract all the integers from the following simplified version of a larger problem I'm working on?
>
> equ=Series[Exp[x],{x,0,5}] //Normal
>
> 1+x+x^2/2+x^3/6+x^4/24+x^5/120
>
> When trying to extract all the integers, I understand numbers like 120 are part of Rational[1,120]
>
> equ //FullForm
>
> << Not shown here >>
>
> I thought looking at all LevelSpec was the solution, but apparently not.
>
> Cases[equ, _Integer, Infinity]
> {1,2,3,4,5}
>
> (* Or *)
>
> Position[equ, _Integer, Infinity]
> {{1},{3,2,2},{4,2,2},{5,2,2},{6,2,2}}
>
> Extract[equ, %]
> {1,2,3,4,5}
>
> Both ways extract from the numerator, but I just can't seem to extract the {2,6,24,120} inside the denominator.
> Does anyone know the proper way?
> Thanks in advance. :>~
>
> Dana
> Mac & Math 9
>
>


In this case the following will do:

expr = 1 + x + x^2/2 + x^3/6 + x^4/24 + x^5/120

Cases[
expr /. Rational[x_, y_] :> rational[x, y], _Integer, {0, Infinity}]

{1, 2, 1, 2, 3, 1, 6, 4, 1, 24, 5, 1, 120}

If your expression involved complex numbers you would have to do the same with Complex as well as with Rational.

Andrzej Kozlowski




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.