Get mathematica to simplify expression with another equation

社会主义新天地 提交于 2019-12-18 10:27:14

问题


I have a very complicated mathematica expression that I'd like to simplify by using a new, possibly dimensionless parameter.

An example of my expression is:

K=a*b*t/((t+f)c*d);

(the actual expression is monstrously large, thousands of characters). I'd like to replace all occurrences of the expression t/(t+f) with p

p=t/(t+f);

The goal here is to find a replacement so that all t's and f's are replaced by p. In this case, the replacement p is a nondimensionalized parameter, so it seems like a good candidate replacement.

I've not been able to figure out how to do this in mathematica (or if its possible). I tried:

eq1= K==a*b*t/((t+f)c*d);
eq2= p==t/(t+f);
Solve[{eq1,eq2},K]

Not surprisingly, this doesn't work. If there were a way to force it to solve for K in terms of p,a,b,c,d, this might work, but I can't figure out how to do that either. Thoughts?


Edit #1 (11/10/11 - 1:30) [deleted to simplify]

OK, new tact. I've taken p=ton/(ton+toff) and multiplied p by several expressions. I know that p can be completely eliminated. The new expression (in terms of p) is

testEQ = A B p + A^2 B p^2 + (A+B)p^3;

Then I made the substitution for p, and called (normal) FullSimplify, giving me this expression.

testEQ2= (ton (B ton^2 + A^2 B ton (toff + ton) + 
   A (ton^2 + B (toff + ton)^2)))/(toff + ton)^3;

Finally, I tried all of the suggestions below, except the last (not sure how it works yet!)

Only the eliminate option worked. So I guess I'll try this method from now on. Thank you.

EQ1 = a1 == (ton (B ton^2 + A^2 B ton (toff + ton) + 
        A (ton^2 + B (toff + ton)^2)))/(toff + ton)^3;
EQ2 = P1 == ton/(ton + toff);
Eliminate[{EQ1, EQ2}, {ton, toff}]

A B P1 + A^2 B P1^2 + (A + B) P1^3 == a1

I should add, if the goal is to make all substitutions that are possible, leaving the rest, I still don't know how to do that. But it appears that if a substitution can completely eliminate a few variables, Eliminate[] works best.


回答1:


Have you tried this?

K = a*b*t/((t + f) c*d);
Solve[p == t/(t + f), t]
 -> {{t -> -((f p)/(-1 + p))}}

Simplify[K /. %[[1]] ]
 -> (a b p)/(c d)

EDIT: Oh, and are you aware of Eliminiate?

Eliminate[{eq1, eq2}, {t,f}]
 -> a b p == c d K && c != 0 && d != 0

Solve[%, K]
 -> {{K -> (a b p)/(c d)}}

EDIT 2: Also, in this simple case, solving for K and t simultaneously seems to do the trick, too:

Solve[{eq1, eq2}, {K, t}]
 -> {{K -> (a b p)/(c d), t -> -((f p)/(-1 + p))}}



回答2:


Something along these lines is discussed in the MathGroup post at

http://forums.wolfram.com/mathgroup/archive/2009/Oct/msg00023.html

(I see it has an apocryphal note that is quite relevant, at least to the author of that post.)

Here is how it might be applied in the example above. For purposes of keeping this self contained I'll repeat the replacement code.

replacementFunction[expr_, rep_, vars_] := 
 Module[{num = Numerator[expr], den = Denominator[expr], 
   hed = Head[expr], base, expon}, 
  If[PolynomialQ[num, vars] && 
    PolynomialQ[den, vars] && ! NumberQ[den], 
   replacementFunction[num, rep, vars]/
    replacementFunction[den, rep, vars], 
   If[hed === Power && Length[expr] == 2, 
    base = replacementFunction[expr[[1]], rep, vars];
    expon = replacementFunction[expr[[2]], rep, vars];
    PolynomialReduce[base^expon, rep, vars][[2]], 
    If[Head[hed] === Symbol && 
      MemberQ[Attributes[hed], NumericFunction], 
     Map[replacementFunction[#, rep, vars] &, expr], 
     PolynomialReduce[expr, rep, vars][[2]]]]]]

Your example is now as follows. We take the input, and also the replacement. For the latter we make an equivalent polynomial by clearing denominators.

kK = a*b*t/((t + f) c*d);
rep = Numerator[Together[p - t/(t + f)]];

Now we can invoke the replacement. We list the variables we are interested in replacing, treating 'p' as a parameter. This way it will get ordered lower than the others, meaning the replacements will try to remove them in favor of 'p'.

In[127]:= replacementFunction[kK, rep, {t, f}]
Out[127]= (a b p)/(c d)

This approach has a bit of magic in figuring out what should be the listed "variables". Possibly some further tweakage could be done to improve on that. But I believe that, generally, simply not listing the things we want to use as new replacements is the right way to go.

Over the years there have been variants of this idea on MathGroup. It is possible that some others may be better suited to the specific expression(s) you wish to handle.

--- edit ---

The idea behind this is to use PolynomialReduce to do algebraic replacement. That is to say, we do not try for pattern matching but instead use polynomial "canonicalization" a method. But in general we're not working with polynomial inputs. So we apply this idea recursively on PolynomialQ arguments inside NumericQ functions.

Earlier versions of this idea, along with some more explanation, can be found at the note referenced below, as well as in notes it references (how's that for explanatory recursion?).

http://forums.wolfram.com/mathgroup/archive/2006/Aug/msg00283.html

--- end edit ---

--- edit 2 ---

As observed in the wild, this approach is not always a simplifier. It does algebraic replacement, which involves, under the hood, a notion of "term ordering" (roughly, "which things get replaced by which others?") and thus simple variables may expand to longer expressions.

Another form of term rewriting is syntactic replacement via pattern matching, and other responses discuss using that approach. It has a different drawback, insofar as the generality of patterns to consider might become overwhelming. For example, what does one do with k^2/(w + p^4)^3 when the rule is to replace k/(w + p^4) with q? (Specifically, how do we recognize this as being equivalent to (k/(w + p^4))^2*1/(w + p^4)?)

The upshot is one needs to have an idea of what is desired and what methods might be feasible. This of course is generally problem specific.

One thing that occurs is perhaps you want to find and replace all commonly occurring "complicated" expressions with simpler ones. This is referred to as common subexpression elimination (CSE). In Mathematica this can be done using a function called Experimental`OptimizeExpression[]. Here are several links to MathGroup posts that discuss this.

http://forums.wolfram.com/mathgroup/archive/2009/Jul/msg00138.html

http://forums.wolfram.com/mathgroup/archive/2007/Nov/msg00270.html

http://forums.wolfram.com/mathgroup/archive/2006/Sep/msg00300.html

http://forums.wolfram.com/mathgroup/archive/2005/Jan/msg00387.html

http://forums.wolfram.com/mathgroup/archive/2002/Jan/msg00369.html

Here is an example from one of those notes.

InputForm[Experimental`OptimizeExpression[(3 + 3*a^2 + Sqrt[5 + 6*a + 5*a^2] +
      a*(4 + Sqrt[5 + 6*a + 5*a^2]))/6]]

Out[206]//InputForm=
Experimental`OptimizedExpression[Block[{Compile`$1, Compile`$3, Compile`$4, 
   Compile`$5, Compile`$6}, Compile`$1 = a^2; Compile`$3 = 6*a; 
   Compile`$4 = 5*Compile`$1; Compile`$5 = 5 + Compile`$3 + Compile`$4; 
   Compile`$6 = Sqrt[Compile`$5]; (3 + 3*Compile`$1 + Compile`$6 + 
     a*(4 + Compile`$6))/6]]

--- end edit 2 ---

Daniel Lichtblau




回答3:


K = a*b*t/((t+f)c*d);

FullSimplify[ K, 
 TransformationFunctions -> {(# /. t/(t + f) -> p &), Automatic}]
(a b p) / (c d)

Corrected update to show another method:

EQ1 = a1 == (ton (B ton^2 + A^2 B ton (toff + ton) + 
        A (ton^2 + B (toff + ton)^2)))/(toff + ton)^3;

f = # /. ton + toff -> ton/p &;

FullSimplify[f @ EQ1]
a1 == p (A B + A^2 B p + (A + B) p^2)

I don't know if this is of any value at this point, but hopefully at least it works.



来源:https://stackoverflow.com/questions/8073530/get-mathematica-to-simplify-expression-with-another-equation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!