RE: Rearranging expressions in a user-defined form
- To: mathgroup at smc.vnet.net
- Subject: [mg94121] RE: [mg94083] Rearranging expressions in a user-defined form
- From: "David Park" <djmpark at comcast.net>
- Date: Fri, 5 Dec 2008 05:32:12 -0500 (EST)
- References: <24833256.1228395441799.JavaMail.root@m02>
Alexei, I agree that it is generally better to do things by Mathematica operations than by cutting and pasting or retyping. I have always been interested in the problem of manipulating expressions so they follow textbook expressions or are in a convenient form. I have put a number of routines in the Presentations that make this easier. One gap in Mathematica is the ability to operate on a subset of level parts in an expression, and reinsert the result back into the expression. One of the routines in Presentations is MapLevelParts (there is also a MapLevelPatterns) that does this. We can just use that on your second example: Needs["Presentations`Master`"] a b + a c + c d + c a - g d - g a % // MapLevelParts[Factor, {{1, 2, 4}}] % // MapLevelParts[Factor, {{1, 3}}] a b + 2 a c + c d - a g - d g c d + a (b + 2 c - g) - d g d (c - g) + a (b + 2 c - g) You have thrown a spanner into the first example in that Mathematica will immediately combine the two a c terms. There are several ways to handle this. The first way uses a routine CreateSubexpression from Presentations. A Subexpression will not combine with other terms. It has a Tooltip on it. It is very convenient when one has a subexpression, often a product of factors that one wants to treat as a unit in some expression and prevent routines such as Simplify from breaking up. Here we just use it to split the 2ac term into two terms, one in a Subexpression. Then we can use MapLevelParts to do the two factorings. The term in parentheses is the Subexpression. (You can have it with or without parentheses.) a b + a c + c d + c a - g d - g a MapAt[(#/2 + CreateSubexpression[#/2]) &, %, 2] % // MapLevelParts[Factor, {{1, 2}}] // ReleaseSubexpressions[] % // MapLevelParts[Factor, {{1, 3, 4, 5}}] a b + 2 a c + c d - a g - d g a b + a c + c d - a g - d g + (a c) a c + a (b + c) + c d - a g - d g a (b + c) + (a + d) (c - g) For a long time I have had a routine EvaluateAtPattern that allows functions to be applied to patterns within Held expressions. I also had an EvaluateAt routine but did not put it in Presentations. As a result of your question I now have, but not yet in the current release at the download site. With EvaluateAt we can write your initial expression in a HoldForm and then do your first case as: expr = HoldForm[a b + a c + c d + c a - g d - g a] % // EvaluateAt[{1, 4}, CreateSubexpression] // ReleaseHold % // MapLevelParts[Factor, {{1, 2}}] // ReleaseSubexpressions[] % // MapLevelParts[Factor, {{1, 3, 4, 5}}] a b + a c + c d + c a - g d - g a a b + a c + c d - a g - d g + (a c) a c + a (b + c) + c d - a g - d g a (b + c) + (a + d) (c - g) So that is one way to manipulate expressions into desired forms. David Park djmpark at comcast.net http://home.comcast.net/~djmpark From: Alexei Boulbitch [mailto:Alexei.Boulbitch at iee.lu] Dear MathGroup, in symbolic transformations of mathematical expressions I often need to group or transform differently different parts of these expression, just to make them more visible , or more easy to analyze or so. Just to give a simplest example, assume that we have an expression: expr = ab + ac + cd + ca- gd - ga and I need to have it once in the form like say, expr=a (b + c) + (c - g) (d + a) and another time in the form say, expr=a (b + 2 c - g) + d (c - g) or I will need to group it in some third, fourth ... form. You indeed understand that I gave the example easily treatable without computer, while I have in mind much more complex transformations of real interest. This evidently may be done by copy-pasting parts of the expression in question and transforming then separately and then again copy-pasting and thus, collecting the results together. That's exactly what I do in such a case. However, this may easily become a source of errors: such as copying without a sign, or copying a part of the expression, rather than the whole one and so on. Finally it considerably slows you down. Could you think of tricks to instruct Mathematica to group only certain terms and not others, and to take certain factors out of parentheses, while leave others, according to your choice? Thank you, Alexei -- Alexei Boulbitch, Dr., Habil. Senior Scientist IEE S.A. ZAE Weiergewan 11, rue Edmond Reuter L-5326 Contern Luxembourg Phone: +352 2454 2566 Fax: +352 2454 3566 Website: www.iee.lu This e-mail may contain trade secrets or privileged, undisclosed or otherwise confidential information. If you are not the intended recipient and have received this e-mail in error, you are hereby notified that any review, copying or distribution of it is strictly prohibited. Please inform us immediately and destroy the original transmittal from your system. Thank you for your co-operation.