On Jun 23, 2009, at 5:39 PM, smichr wrote: > >> On Jun 19, 6:42 pm, "Aaron S. Meurer" <[email protected]> wrote: >>> I have been working on getting separable equations in dsolve, and I >>> noticed this: >>>>>> c = Wild('c', exclude=[y]) >>>>>> d = Wild('d', exclude=[x]) >>>>>> (x).match(c*d) >>> {c: x, d: 1} >>>>>> (log(x)).match(c*d) >>> {c: log(x), d: 1} >>>>>> (x*log(x)).match(c*d) >>> <Returns None> >>> >>> I would like for the last one to return {c:x*log(x), d:1}. > > Although you could get what you want by using the .as_independent() > method, I agree that the expected behavior is missing. It doesn't look like .as_independent() differentiates between additive independence and multiplicative independence. I need only multiplicative independence for separable equations. I had to write a function separatevars() to separate expressions anyway (similar to collect, except it collects all parts with symbols together), so I made it also an option to return a dict like collect, which gives me the separate parts and removes the need for match. > >> I have also noticed this with the same Wild variables: >> >>>>> print (exp(x)*exp(y)).match(c*d) >> {d_: exp(y), c_: exp(x)} >>>>> print (exp(x)*exp(y)*y).match(c*d) >> {d_: y*exp(y), c_: exp(x)} >>>>> print (x*exp(x)*exp(y)*y).match(c*d) >> None >> > Ahh...but the common base (E) gathers in the x and y so that x*exp(x) > *exp(y)*y really exists as something else: > I should have mentioned that this is after my patch that removed automatic combining of exponents. That should make it in soon. See http://github.com/asmeurer/sympy/tree/expand-exp-review . Feel free to pull it and review it. > ### >>>> x*exp(x)*exp(y)*y > x*y*exp(x + y) > ### > > So after finding the "x" the rest of the expression does not match a > non-x pattern so the matching fails. It seems to me, however, that > match should split up a function as long as no assumptions would be > violated. In the case of exp(x+y) are there any assumptions that would > be violated by breaking this up as exp(x)*exp(y)? (In the case of sqrt > (x*y) we could only break it up into sqrt(x)*sqrt(y) if x and y are > not both negative.) > > But if you do that, then you should probably handle all of the > following: > > powers as products: > a**(x+y) = a**x*a**y > > function as product: > exp(x+y) = exp(x)*exp(y) > sqrt(x*y) = sqrt(x)*sqrt(y) if not(x<0 and y<0) else -sqrt(x)*sqrt(y) expand(power_exp) will be able to do this once my patch is in. sqrt(x) is represented as x**Rational(1,2) in sympy, so it isn't really a function. See also powsimp(combine='exp') in my branch. > log(x,y) = log(x)*log(y)**-1 I didn't even think of this one. But it looks like SymPy splits it automatically. >>> log(x,y) log(x) ────── log(y) > > sums as product: > [sin (x+y) + sin (x-y)]/2 = sinx cosy > [sin (x+y) - sin (x-y)]/2 = cosx siny > [cos (x+y) + cos (x-y)]/2 = cosx cosy > -[cos (x+y) - cos (x-y)]/2 = sinx siny Expand(trig) does this. See also expand_trig in my branch. > > sums as constants: > (sinx)**2 + (cosx)**2 = 1 > (secx)**2 - (tanx)**2 = 1 > (cscx)**2 - (cotx)**2 = 1 See trigsimp. Some of this and the ones below may have to wait until Luke is done working on trigsimp. It should be able to handle them all by then. > > function as sum > log(x*y) = log(x) + log(y) expand can do this assuming x and y have the right assumptions (otherwise it is not True). See also expand_log in my branch. > > > products as sums > 2 sin((x+y)/2) cos((x-y)/2) = sinx + siny > 2 sin((x-y)/2) cos((x+y)/2) = sinx - siny > 2 cos((x+y)/2) cos((x-y)/2) = cosx + cosy > -2 sin((x+y)/2) sin((x-y)/2) = cosx - cosy > > Am I missing anything? Is this the way to go with pattern matching? I don't know if you can do anything with special functions like gamma. Also, what about things like log(x**y) == y*log(x) (expand with correct assumptions on x and y, see also logcombine in http://github.com/asmeurer/sympy/commits/odes-stable2) . (x*y)**a == x**a*y**a (expand and powsimp, powsimp(combine='base') and expand(power_base) in my branch) Factoring stuff like x*y + x + y + 1 == (x+1)*(y+1), see (factor and expand, expand_mul in my branch).
If you or anyone were planning on looking into this, I would say that Issue 1429 (http://code.google.com/p/sympy/issues/detail?id=1429&q=asmeurer ) is a more important bug in match, at least in my work on dsolve. I have really no need for the above with my separatevars() function although this is a bug and should be fixed. > > /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 -~----------~----~----~----~------~----~------~--~---
