Prev: FDTD Code or Example? (2D wave eqn, rect grid, coords x,y,t)
Next: Writing to same file from several kernels?
From: fajar on 21 Jul 2010 07:13 Hi all, I'm new to symbolic computation. I have this coming from previous computation: a + b -2*c How can I convert that expression, with Mathematica, into: (a - c) + (b - c) ? Another example: Given a*b + a*c - 2*a*d + b*c - 2*b*d - 2*c*d + 3*d^2 How can I convert that expression, with Mathematica, into: (a-d)*(b-d) + (a-d)*(c-d) + (b-d)*(c-d) ? Thanks Fajar
From: Bob Hanlon on 22 Jul 2010 05:43 expr1 = a + b - 2*c; subs1 = {t1 == a - c, t2 == b - c}; expr2 = HoldForm[Evaluate[ Simplify[expr1, subs1]]] /. (Rule @@@ subs1) (a-c)+(b-c) expr1 == expr2 // ReleaseHold True expr3 = a*b + a*c - 2*a*d + b*c - 2*b*d - 2*c*d + 3*d^2; subs2 = Thread[{t1, t2, t3} == {a, b, c} - d] {t1 == a - d, t2 == b - d, t3 == c - d} expr4 = HoldForm[Evaluate[ Simplify[expr3, subs2]]] /. (Rule @@@ subs2) (b-d) (c-d)+(a-d) ((b-d)+(c-d)) expr3 == expr4 // ReleaseHold // Simplify True Bob Hanlon ---- fajar <fajar96te(a)yahoo.com> wrote: ============= Hi all, I'm new to symbolic computation. I have this coming from previous computation: a + b -2*c How can I convert that expression, with Mathematica, into: (a - c) + (b - c) ? Another example: Given a*b + a*c - 2*a*d + b*c - 2*b*d - 2*c*d + 3*d^2 How can I convert that expression, with Mathematica, into: (a-d)*(b-d) + (a-d)*(c-d) + (b-d)*(c-d) ? Thanks Fajar
From: David Park on 22 Jul 2010 05:45
If you are new to Mathematica, then I think both of these are somewhat tricky. You might receive answers that are more transparent. The problem with expr1 is that Mathematica will automatically recombine your desired answer to the initial form. The only way to only way to stop that is to put the two terms in a HoldForm. One method for doing that is to use substitution rules and some temporary variables. expr1 = a + b - 2 c; expr1 /. {a -> x + c, b -> y + c} % /. {x -> HoldForm[a - c], y -> HoldForm[b - c]} x + y (a - c) + (b - c) You would have to use ReleaseHold if you wanted to do further calculations with this expression. The second expression is a little more tricky. I think the simplest approach is to again introduce temporary variables representing the factors, and then use the GroebnerBasis routine to obtain a simple expression in terms of these variables, and then substitute back. expr2 = a b + a c - 2 a d + b c - 2 b d - 2 c d + 3 d^2; expr2 /. {a -> x + d, b -> y + d, c -> z + d}; GroebnerBasis[%, {x, y, z}] // First % /. {x -> a - d, y -> b - d, z -> c - d} x y + x z + y z (a - d) (b - d) + (a - d) (c - d) + (b - d) (c - d) David Park djmpark(a)comcast.net http://home.comcast.net/~djmpark/ From: fajar [mailto:fajar96te(a)yahoo.com] Hi all, I'm new to symbolic computation. I have this coming from previous computation: a + b -2*c How can I convert that expression, with Mathematica, into: (a - c) + (b - c) ? Another example: Given a*b + a*c - 2*a*d + b*c - 2*b*d - 2*c*d + 3*d^2 How can I convert that expression, with Mathematica, into: (a-d)*(b-d) + (a-d)*(c-d) + (b-d)*(c-d) ? Thanks Fajar |