Hi Zoho,
thanks for your interest. I think you can do that in SymPy.
But I have problems understanding the Maxima code, could you please
write in python terms what it's doing? :)
For example:
> for i thru n do (
> C[i]:sum(a[i,j]*c^(2*j+1),j,1,i),
> Cpp[i]:sum(a[i,j]*(-(2*j+1)^2*c^(2*j+1)+(2*j+1)*(2*j)*c^(2*j-1)),j,
> 1,i) ),
This assignes some expressions into arrays C and Cpp, right?
> psi:sum(b^(2*i+1)*C[i], i,0,n),
> lam:sum(b^(2*i)*k[i], i,0,n),
This uses C to construct psi. Where is Cpp used? Isn't k[i]
uninitialized, except k[0]? What is in a[i,j],
if you didn't assign to it? But if you are just using arrays, symbolic
expressions, sums, substitution,
all of that work nice in sympy.
Ask here if you have any problems rewriting your code and please
report all bugs that you may find,
we'll try to fix them promptly.
Ondrej
Is this what you need?
========================================
#!/usr/bin/env python
from sympy import *
pprint_try_use_unicode()
c = Symbol('c')
b = Symbol('b')
def irange(a,b):
"""inclusive range -- range(a,b+1)"""
return range(a,b+1)
def setup_terms(n):
a = {}
C = [None]*(n+1)
C[0] = c
for i in irange(1,n):
for j in irange(1,i):
a[i,j] = Symbol('a_%i%i' % (i,j))
C[i] = sum(a[i,j] * c**(2*j+1) for j in irange(1,i))
psi = sum(b**(2*i+1) * C[i] for i in irange(0,n))
return psi
if __name__ == '__main__':
psi = setup_terms(3)
print 'PSI:'
pprint(psi)
========================================
On my host the output is:
kirr@evo:~/src/sympy/sympy$ ./psi.py
PSI:
5 ⎛ 3 5⎞ 7 ⎛ 3 5 7⎞ 3 3
b*c + b *⎝a₂₁*c + a₂₂*c ⎠ + b *⎝a₃₁*c + a₃₂*c + a₃₃*c ⎠ + a₁₁*b *c
> post scriptum: Thank you for writing such great software!
Thanks for your interest to SymPy too!
--
Всего хорошего, Кирилл.
http://landau.phys.spbu.ru/~kirr/aiv/
Hi Zoho,
first I am sorry for my later reply, I forgot about this.
Unfortunately, I don't have time right now to learn the syntax of
maxima. Could you please post here some examples of usage of the above
code? I.e. which expression you plug in and what you get? It will be
then easy for me to help you do the same in SymPy.
Thanks a lot,
Ondrej
Actually I have altered the code slightly.I believe the only replacement
routine I need now is for ratcoef (in Maxima) or coeff and collect (in
Maple). I guess it is easiest to explain what the maple commands do and
then see if that is possible in sympy.
syntax coeff(p,x,n)
The coeff command takes a polynomial p and picks out the coefficient of
the term containing the variable x to the given power n.
An example for coeff:
f := (a+1)*x + (a+2)*x^2 + a;
-> f := (a+1)x + (a+2)x^2 + a
coeff(f,x,2);
-> a+2
coeff(f,x,0)
-> a
And an example for collect:
syntax collect(p,x)
The collect command is used to collect like terms of the variable x in
the polynomial p.
f := (a+1)*x + a^2*x + a;
-> f := (a+1)x + a^2*x + a
collect(f,x);
-> (a+1 + a^2)x + a
collect(f,a);
-> a^2*x + (x+1)a + x
I hope that helps. I really appreciate your help. I have read through
the documentation on the website but didn't find answers there. Is there
another source of material available to read through? Perhaps these are
really simple questions and I just didn't find the right things to read!
Thanks again,
Zoho
You can use this:
In [1]: a = Symbol("a")
In [2]: f = (a+1)*x + (a+2)*x**2 + a
In [3]: f = Polynomial(f, var=[x])
In [4]: f.nth_coeff(2)
Out[4]: 2 + a
In [5]: f.nth_coeff(0)
Out[5]: a
But using the method you suggested is a more common interface, so I
implemented that:
http://code.google.com/p/sympy/issues/detail?id=691
But it still needs some polishing.
>
> And an example for collect:
> syntax collect(p,x)
>
> The collect command is used to collect like terms of the variable x in
> the polynomial p.
>
> f := (a+1)*x + a^2*x + a;
> -> f := (a+1)x + a^2*x + a
> collect(f,x);
> -> (a+1 + a^2)x + a
> collect(f,a);
> -> a^2*x + (x+1)a + x
This is implemented using the same syntax:
In [1]: a = Symbol("a")
In [2]: f = (a+1)*x + a**2*x+a
In [3]: collect(f, x)
Out[3]:
⎛ 2⎞
a + x*⎝1 + a + a ⎠
In [4]: collect(f, a)
Out[4]:
2
a + x*a + x*(1 + a)
>
> I hope that helps. I really appreciate your help. I have read through
> the documentation on the website but didn't find answers there. Is there
> another source of material available to read through? Perhaps these are
> really simple questions and I just didn't find the right things to read!
The best thing is to read through sources (all methods should have
understandable docstrings) and especially the tests, because all
features have tests in sympy, so just by reading those you get the
idea what you can do. Another good approach is to start isympy and hit
"coll" + TAB, this completes to "collect", then you type "collect?"
and it will give you a docstring.
Ondrej
.coeff() was just pushed into our main hg repo, so update your sympy and do:
In [1]: a = Symbol("a")
In [2]: f = (a+1)*x + (a+2)*x**2 + a
In [3]: f = Polynomial(f)
In [4]: f.coeff(x, 2)
Out[4]: 2 + a
In [5]: f.coeff(x, 0)
Out[5]: a
As always, documentation can be found using for example:
In [6]: f.coeff?
Type: instancemethod
Base Class: <type 'instancemethod'>
String Form:
<bound method Polynomial.coeff of Polynomial( 2 2
a + x + 2*x + a*x + a*x , ((1, 1, 2), (1, 1, 1), (2, 0, 2), (1, 1,
0), (1, 0, 1)), [a, x], 'grevlex')>
Namespace: Interactive
File: /home/ondra/sympy/sympy/polynomials/base.py
Definition: f.coeff(self, x, n)
Docstring:
Returns the coefficient at x**n
Example:
>>> a, x = symbols("ax")
>>> f = (a+1)*x + (a+2)*x**2 + a
>>> Polynomial(f).coeff(x, 2)
2 + a
>>> Polynomial(f).coeff(a, 1)
1 + x + x**2
The messy output of repr(f) is due to:
http://code.google.com/p/sympy/issues/detail?id=659
Ondrej
I do feel embarrassed that I asked about collect but it was already
there! Sorry for the unnecessary noise.
I will update sympy now to get .coeff and thank you so much for all your
help!
Zoho
No problem, that's what the mailinglist is for. Also I didn't know it
either off top of my head. What I do is that I write the expression
into a variable in isympy and then
t.<TAB>
this prints all the available method names. If I didn't find the one I
want, I do:
dir()
this prints all functions, that are imported, so I do
In [2]: c<TAB>
cache chebyshevu_root combsimp cos
callable chr compile cosh
cancel class complex cot
casoratian classmethod concrete coth
cat clear conjugate count_real_roots
ceiling cmp continue cp
chebyshevt coerce convex_hull credits
chebyshevt_root collect copyright
chebyshevu combinatorial core
and that's it. (Then collect? and it will print a nice help with
examples of usage).
> I will update sympy now to get .coeff and thank you so much for all your
> help!
Thanks for your feedback! Please write when you have more problems/questions.
Ondrej