Hey guys,

Thanks for the feedback.

Indeed, it has specific structure. Even though I have not been able to
find a formula for calculating the nth term of the original problem I
posed, I did construct an algorithm that can give it to me. This
particular problem is from Paul Zeit´s The Art and Craft of Problem
Solving and it asks for the 1997th term of that expanded polynomial.
The number of non-zero terms in the expansion is 2**n.

I wanted to somehow validate my answer (and my algorithm) so I thought
I could use some CAS. I like Python so much (don´t use it much,
however) that I tried Sympy to see if I could replicate my answer.

Sympy is excellent software!

Again, thanks to all. I will keep in mind everything you have posted
for future experiments.


On Apr 17, 4:45 pm, "Chris Smith" <[email protected]> wrote:
> >> One last question. I´m trying to expand that expression up to n=2000,
> >> but I get a memory error. Besides buying more physical RAM, can you
> >> recommend any other way of doing it?
>
> ...
>
> >> This polynomial has specific structure, so it should be possible to
> >> derive a formula for the terms of `f_n`. SymPy should help you with
> >> experimentation with `f_n` for small `n` (as you did already).  
>
> This is where python shines, I think. If you make some little helper 
> functions it can make the task easier.
>
> Not wanting to spoil the fun of finding the terms of your original 
> expression, let's say you wanted to investigate the terms of (x+y)**n as n 
> increased. One way to order terms would be by the exponent that is on the x, 
> so we write a function to tell us that:
>
>     h[19] >>> def xpow(t):
>          ...   xpart = t.as_independent(x)[1]
>          ...   if xpart.has(x):
>          ...    return xpart.as_base_exp()[1]
>          ...   return 0
>          ...
>
> We test it
>     h[19] >>> xpow(S(1)) # S(1) gives a sympy 1 so we don't get an error
>     0
>     h[19] >>> xpow(x)
>     1
>     h[19] >>> xpow(x**2)
>     2
>     h[20] >>> xpow(y*x**3)
>     3
>     h[20] >>> p=list(((x+y)**5).expand().args)
>     h[20] >>> p
>     [x**5, 10*x**2*y**3, 5*y*x**4, 5*x*y**4, y**5, 10*x**3*y**2]
>     h[21] >>> p.sort(key=lambda a: xpow(a))
>     h[21] >>> p
>     [y**5, 5*x*y**4, 10*x**2*y**3, 10*x**3*y**2, 5*y*x**4, x**5]
>
> Now let's see if we can see a pattern as we change n:
>
>     h[21] >>> for n in range(1, 5):
>          ...   p = list(((x+y)**n).expand().args)
>          ...   p.sort(key=lambda a: xpow(a))
>          ...   print n,p
>          ...
>     1 [y, x]
>     2 [y**2, 2*x*y, x**2]
>     3 [y**3, 3*x*y**2, 3*y*x**2, x**3]
>     4 [y**4, 4*x*y**3, 6*x**2*y**2, 4*y*x**3, x**4]
>
> So if each term (x and y) is independent of n we get the binomial 
> coefficients.
>
> What if y has a coefficient of n?
>
>     h[35] >>> for nn in range(1,7):
>          ...   nn,sorted(list(product(x+n*y, 
> (n,1,nn)).expand().args),key=lambda l:
>     xpow(l))
>          ...
>     (1, [y, x])
>     (2, [2*y**2, 3*x*y, x**2])
>     (3, [6*y**3, 11*x*y**2, 6*y*x**2, x**3])
>     (4, [24*y**4, 50*x*y**3, 35*x**2*y**2, 10*y*x**3, x**4])
>     (5, [120*y**5, 274*x*y**4, 225*x**2*y**3, 85*x**3*y**2, 15*y*x**4, x**5])
>     (6, [720*y**6, 1764*x*y**5, 1624*x**2*y**4, 735*x**3*y**3, 175*x**4*y**2, 
> 21*y*x**5, x**6])
>
> If you arrange these coefficients into a "Pascal's triangle", can you see any 
> pattern?
>
> /c

-- 
You received this message because you are subscribed to the Google Groups 
"sympy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sympy?hl=en.

Reply via email to