[sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-18 Thread smichr
But that only groups together one variety of M**(n+1). If you want them all together you have to give them a common coefficient that can be used for collection: >>> u = Dummy('u') These are the quantities that may have the n+1 power >>> expr2.atoms(IndexedElement) set([M(1, 2), M(1, 1), M(2, 1)])

[sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread smichr
> a = Symbol("x", dummy=True) > print collect(expr.subs([(a**(n+1), u*a**(n+1)) for a in _]), u) > # end code > If you try C.Dummy('u') that will work for you; Symbol('u', dummy=True) is equivalent. > However, this does not seem to work, since the following error is produced: > > Traceback (mos

[sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-20 Thread smichr
On Sep 19, 10:20 pm, "Aaron S. Meurer" wrote: > So given Chris's tip in issue 2058 (which flew over my head the first time > for some reason), I think I might have this figured out, except for one > thing.  Given M(i, j, n), how to I get the third argument, n?  I tried M(i, > j, n)[2] and M(i

[sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread smichr
> > That would be cool to have implemented, though.  Or maybe some kind of lambda > thing, so you could just say > > collect(expr, lambda i: i.is_Indexed, lambda=True) > Part of the problem I have run into in the past is a non-kludge way to tell if you have a lambda argument. I think I ended up s

[sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread smichr
Just a follow up about using subs(wild=True). If Idx is derived from Expr rather than Basic, then the wild subs shown below (in t2) works: >>> eq=A*M(i, j)**n + B*M(1 + i, j)**(1 + n)- D*M(i, j)**(1 + n) >>> Eq(*reversed(collect(eq.subs(w**(1+n), w**(1+n)*u, wild=True), >>> u).as_independent( u)

[sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread smichr
Another way of handling the lambda functionality would be to piggy back off of match so that eq.match(lambda) would return True if the lambda was true for eq. That way routines would gain matching and lambda capabilities at the same time. Or, like for .has(), routines already having wild matching w

[sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread smichr
On Sep 22, 8:18 am, "Aaron S. Meurer" wrote: > I think it would be better design wise if the lambda were entered in the Wild > itself, so instead use something like: I won't be working on this until I finish the t2 work and the solver mods so someone else might want to look at this. Since I've

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-18 Thread Nicholas Kinar
On 10-09-18 09:30 PM, smichr wrote: But that only groups together one variety of M**(n+1). If you want them all together you have to give them a common coefficient that can be used for collection: u = Dummy('u') These are the quantities that may have the n+1 power expr2.atom

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-18 Thread Nicholas Kinar
u = Dummy('u') I can't seem to use the u = Dummy('u') variable, since I now receive the following error: Traceback (most recent call last): File "test.py", line 12, in u = Dummy('u') NameError: name 'Dummy' is not defined Moreover, I think that something like this would be extremel

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-18 Thread Ondrej Certik
On Sat, Sep 18, 2010 at 8:54 PM, Nicholas Kinar wrote: >> u = Dummy('u') > > > I can't seem to use the u = Dummy('u') variable, since I now receive the > following error: > > Traceback (most recent call last): >  File "test.py", line 12, in >    u = Dummy('u') > NameError: name 'Dummy' is no

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Øyvind Jensen
> Moreover, I think that something like this would be extremely useful as > a function incorporated into the sympy distribution: > >>> collect(expr2.subs([(a**(n+1), u*a**(n+1)) for a in _]), u) Another approach would be to use Wild symbols when calling collect(). Like this: >>> from sympy.tens

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Nicholas Kinar
Thanks Ondrej and Řyvind; that's greatly appreciated! Ondrej: As you suggest, I've attempted to use a = Symbol("x", dummy=True) In the example code given below, I am still using "from sympy import *" since I am not certain exactly what I would have to import. However, I do agree that it is f

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Aaron S. Meurer
On Sep 19, 2010, at 10:30 AM, smichr wrote: > >> a = Symbol("x", dummy=True) >> print collect(expr.subs([(a**(n+1), u*a**(n+1)) for a in _]), u) >> # end code >> > > If you try C.Dummy('u') that will work for you; Symbol('u', > dummy=True) is equivalent. > > >> However, this does not seem to

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Nicholas Kinar
Thanks for your response, smichr; that's greatly appreciated. However, this does not seem to work, since the following error is produced: Traceback (most recent call last): File "test-1.py", line 17, in print collect(expr.subs([(a**(n+1), u*a**(n+1)) for a in _]), u) NameError: na

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Aaron S. Meurer
So given Chris's tip in issue 2058 (which flew over my head the first time for some reason), I think I might have this figured out, except for one thing. Given M(i, j, n), how to I get the third argument, n? I tried M(i, j, n)[2] and M(i, j, n).args, but neither works. Aaron Meurer -- You r

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Aaron S. Meurer
I think you have found a bug with subs. Can you report it in the issues, with the traceback? Aaron Meurer On Sep 19, 2010, at 11:19 AM, Nicholas Kinar wrote: > Thanks for your response, smichr; that's greatly appreciated. > >> >>> However, this does not seem to work, since the following er

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Nicholas Kinar
That would be cool to have implemented, though. Or maybe some kind of lambda thing, so you could just say collect(expr, lambda i: i.is_Indexed, lambda=True) and it would collect all Indexed terms. Also, as far as making the Indexed separation work, maybe some kind of lambda support in as_i

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Aaron S. Meurer
On Sep 19, 2010, at 11:24 AM, Nicholas Kinar wrote: > >> That would be cool to have implemented, though. Or maybe some kind of >> lambda thing, so you could just say >> >> collect(expr, lambda i: i.is_Indexed, lambda=True) >> >> and it would collect all Indexed terms. >> >> Also, as far as

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Nicholas Kinar
I think you have found a bug with subs. Can you report it in the issues, with the traceback? Aaron Meurer Done, the issue has been reported here (http://code.google.com/p/sympy/issues/detail?id=2060). I expect that it will be closed soon, but at least it gets everyone thinking ;-)

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Nicholas Kinar
Generally it's easiest to convert an Equality into a regular expression and then convert it back (like I showed in my first email to this thread). If you only want to manipulate one side of an Equality, you could do Eq(manipulation(expr.lhs), expr.rhs), i.e., only do it to whatever side and

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Aaron S. Meurer
Well, now I am having trouble reproducing the problem I had. I was getting: In [23]: M(i, j, n).args --- TypeError Traceback (most recent call last) /Users/aaronmeurer/Documents/Python/sympy/s

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Aaron S. Meurer
OK, ignore that. I was using i as a loop variable (using i as a Symbol is a bad idea because of this, imho). But now I get this strange error: In [48]: M(i, j, n).args[3] Out[48]: n In [49]: M(i, j, n).args[3] == n Out[49]: False Aaron Meurer On Sep 19, 2010, at 11:55 AM, Aaron S. Meurer w

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Øyvind Jensen
sø., 19.09.2010 kl. 12.01 -0600, skrev Aaron S. Meurer: > OK, ignore that. I was using i as a loop variable (using i as a Symbol is a > bad idea because of this, imho). > > But now I get this strange error: > > In [48]: M(i, j, n).args[3] > Out[48]: n > > In [49]: M(i, j, n).args[3] == n > O

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Øyvind Jensen
> > And Øyvind, I don't think it's a bug that collect doesn't respect > Wild. It's just not implemented. Yeah, that was kind of what I meant :-) > Someone suggested that subs might do > substitutions using Wilds and I put that work into t2. And this thread > provided a test case for it, but do

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Øyvind Jensen
> > > And Øyvind, I don't think it's a bug that collect doesn't respect > > Wild. It's just not implemented. Someone suggested that subs might do > > substitutions using Wilds and I put that work into t2. And this thread > > provided a test case for it, but doing expr.subs(W**(n+1), u*W**(n+1), >

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Aaron S. Meurer
Unfortunately, you usually have to sacrifice learning curve for flexibility, especially in SymPy where we are limited to the Python language semantics. Of course, if it's just the lambda that is confusing, you can make it more explicit (at the cost of using more lines): def is_index(i): re

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Øyvind Jensen
sø., 19.09.2010 kl. 14.43 -0600, skrev Aaron S. Meurer: > Unfortunately, you usually have to sacrifice learning curve for flexibility, > especially in SymPy where we are limited to the Python language semantics. > Of course, if it's just the lambda that is confusing, you can make it more > expl

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Nicholas Kinar
In the mean time I found an easy way to do the Wild() trick in collect, and have uploaded to github, the branch is fix_collect. I am trying to use the pull request feature, without any luck so far... Ųyvind That's very exciting, Ųyvind - it will be a neat experience to try your patch. I'v

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Øyvind Jensen
sø., 19.09.2010 kl. 17.19 -0600, skrev Nicholas Kinar: > > In the mean time I found an easy way to do the Wild() trick in collect, > > and have uploaded to github, the branch is fix_collect. I am trying to > > use the pull request feature, without any luck so far... > > > > Ųyvind > > That's very

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-19 Thread Nicholas Kinar
sø., 19.09.2010 kl. 17.19 -0600, skrev Nicholas Kinar: In the mean time I found an easy way to do the Wild() trick in collect, and have uploaded to github, the branch is fix_collect. I am trying to use the pull request feature, without any luck so far... Ųyvind That's very exciti

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-20 Thread Nicholas Kinar
Working again on this example program, I am now trying to use as_independent() on expr1 after collect() has been called on expr. # begin code from sympy import * from sympy.tensor import Indexed, Idx, IndexedElement M = Indexed('M') var('A B C D E') i,j,n = symbols('i j n', integer = True) i =

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-20 Thread Nicholas Kinar
Working again on this example program, I am now trying to use as_independent() on expr1 after collect() has been called on expr. # begin code from sympy import * from sympy.tensor import Indexed, Idx, IndexedElement M = Indexed('M') var('A B C D E') i,j,n = symbols('i j n', integer = True) i

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-20 Thread Aaron S. Meurer
Yeah, see one of my previous emails on this thread. I was doing something stupid, and so it didn't work when I tried it. The problem now is with >>> m.args[-1] == n False Aaron Meurer On Sep 20, 2010, at 10:04 PM, smichr wrote: > > > On Sep 19, 10:20 pm, "Aaron S. Meurer" wrote: >> So gi

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-20 Thread Øyvind Jensen
ma., 20.09.2010 kl. 22.06 -0600, skrev Aaron S. Meurer: > Yeah, see one of my previous emails on this thread. I was doing something > stupid, and so it didn't work when I tried it. The problem now is with > > >>> m.args[-1] == n > False > A workaround is to use : >>> m.args[-1] == Idx(n) Tr

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Aaron S. Meurer
OK, so here is how to do it (with all the work arounds). First off, again I HIGHLY RECOMMEND that you use M(i, j, n) instead of M(i, j)**n. As a simple example of how M(i, j)**n can lead to unintended consequences, consider what will happen if you set n = 0. You will get M(i, j)**0 == 1! So,

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
OK, so here is how to do it (with all the work arounds). First off, again I HIGHLY RECOMMEND that you use M(i, j, n) instead of M(i, j)**n. As a simple example of how M(i, j)**n can lead to unintended consequences, consider what will happen if you set n = 0. You will get M(i, j)**0 == 1!

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
Just a follow up about using subs(wild=True). If Idx is derived from Expr rather than Basic, then the wild subs shown below (in t2) works: eq=A*M(i, j)**n + B*M(1 + i, j)**(1 + n)- D*M(i, j)**(1 + n) Eq(*reversed(collect(eq.subs(w**(1+n), w**(1+n)*u, wild=True), u).as_independent( u))).subs(

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Aaron S. Meurer
That's why I included the lambda=True argument to collect() in that example. But I think the Wild trick is better than the lambda idea anyway, and I think whatever lambda stuff is implemented should be implemented in Wild and matches(). Aaron Meurer On Sep 21, 2010, at 7:55 AM, smichr wrote:

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
On 10-09-21 01:18 AM, Aaron S. Meurer wrote: OK, so here is how to do it (with all the work arounds). First off, again I HIGHLY RECOMMEND that you use M(i, j, n) instead of M(i, j)**n. As a simple example of how M(i, j)**n can lead to unintended consequences, consider what will happen if you

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Aaron S. Meurer
On Sep 21, 2010, at 7:51 PM, Nicholas Kinar wrote: > On 10-09-21 01:18 AM, Aaron S. Meurer wrote: >> OK, so here is how to do it (with all the work arounds). First off, again I >> HIGHLY RECOMMEND that you use M(i, j, n) instead of M(i, j)**n. As a simple >> example of how M(i, j)**n can lead

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
Now I believe that as_base_exp() and other related functions can be found in expr.py, and I've tried copying these functions into the Idx class (indexed.py) to serve as a template for what I would like to do. I've then attempted to modify these functions, but based on these very preliminary

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
I received this same error when manually copying the functions between the files, which isn't too extraordinary (given that we're subclassing one class from the other). Nicholas My mistake; I was running the wrong Python script! Now by subclassing Idx from Expr, the following works qu

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
I am interested in re-arranging equations in this fashion since I would like to create a function to generate systems for the solution of finite-difference time domain (FDTD) schemes, which are very useful in computational physics. I plan to create a function such as generate_fdtd(expr) that

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
Another way of handling the lambda functionality would be to piggy back off of match so that eq.match(lambda) would return True if the lambda was true for eq. That way routines would gain matching and lambda capabilities at the same time. Or, like for .has(), routines already having wild matchin

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Aaron S. Meurer
I think it would be better design wise if the lambda were entered in the Wild itself, so instead use something like: w = Wild('w', lambda=lambda i: i.is_Whatever and ) It also gives more flexibility this way, because you can have just a part of the match expression use the lambda. Aaron Meurer

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
So given that Idx can subclass from Expr as suggested by smichr, and Oyvind's fix gets pushed into the trunk, my proposed (extremely modest) function would become: def generate_fdtd(expr, nsolve): w = Wild('w') expr0 = expr.lhs - expr.rhs expr1 = collect(expr0, w**nsolve) r

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Aaron S. Meurer
Try using .args, as in expr.args Aaron Meurer On Sep 21, 2010, at 9:43 PM, Nicholas Kinar wrote: > >> >> So given that Idx can subclass from Expr as suggested by smichr, and >> Oyvind's fix gets pushed into the trunk, my proposed (extremely modest) >> function would become: >> >> def gene

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
Try using .args, as in expr.args Aaron Meurer Thanks, Aaron; that's the icing on the cake, and it is exactly what I needed. Isn't it neat how expr.args can be cascaded? expr1 = expr0.args[0].args[1].args[1] Nicholas -- You received this message because you are subscribed to the Goog

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Aaron S. Meurer
Yep. .args is basically the key to doing advanced stuff in SymPy. You can access any part of an expression you want by walking the .args tree, and you can use it to do any kind of manipulation you want. Basically, if you look at the source for many of these functions like collect() or as_inde

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-21 Thread Nicholas Kinar
Yep. .args is basically the key to doing advanced stuff in SymPy. You can access any part of an expression you want by walking the .args tree, and you can use it to do any kind of manipulation you want. Basically, if you look at the source for many of these functions like collect() or as_i

Re: [sympy] Re: Algebraic rearrangement and obtaining coefficients of variables

2010-09-22 Thread Nicholas Kinar
I've written another example program :-D What I would like to do is separate each term into coefficients and variables. Is it possible to obtain the coefficients of a term using .coeff without knowing the indexed variable? The following example program demonstrates what I would like to achi