[Date Index]
[Thread Index]
[Author Index]
Re: Extract Integers
*To*: mathgroup at smc.vnet.net
*Subject*: [mg131430] Re: Extract Integers
*From*: David Bailey <dave at removedbailey.co.uk>
*Date*: Sun, 21 Jul 2013 21:42:37 -0400 (EDT)
*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com
*Delivered-to*: l-mathgroup@wolfram.com
*Delivered-to*: mathgroup-outx@smc.vnet.net
*Delivered-to*: mathgroup-newsendx@smc.vnet.net
*References*: <ksg5j9$c1t$1@smc.vnet.net>
On 21/07/2013 09:19, Dana DeLouis 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
>
>
This seems a strange sort of thing to wish to do, and I am a bit
concerned that it is a class test question. However, you have obviously
explored the problem a bit, so (assuming there are no real numbers in
your larger problem) here are two suggestions:
Flatten[
Cases[equ, _?NumberQ, \[Infinity]] /.
x_Rational :> {Numerator[x], Denominator[x]}]
{1, 1, 2, 2, 1, 6, 3, 1, 24, 4, 1, 120, 5}
This is OK, but it contains some spurious ones because the numerators of
1 are not usually shown within a multiplication. Of course, if you would
like those implicit ones, it is ideal.
Alternatively, by converting the expression to a string, it is possible
to extract all the integers using a string pattern, and then convert
them back from strings to integers:
Map[ToExpression,
StringCases[ToString[eq, InputForm], DigitCharacter ..]]
{1, 2, 2, 3, 6, 4, 24, 5, 120}
It may be worth discussing your larger problem here, because this seems
a rather odd step to need to take!
David Bailey
http://www.dbaileyconsultancy.co.uk
Prev by Date:
**Re: Extract Integers**
Next by Date:
**Re: Extract Integers**
Previous by thread:
**Re: Extract Integers**
Next by thread:
**Re: Extract Integers**
| |