Is there a way to simplify this conjugate expression:
expr=d12*Conjugate[C1]*C2 + d12*Conjugate[C2]*C1 + d13*Conjugate[C1]*C3 + d13*Conjugate[C3]*C1
into something like:
2 d12 (Re[C1*Conjugate[C2]]) + 2 d13 (Re[C1*Conjugate[C3]])
And in general, how Mathematica determines one expression is simpler than another? Is there a way to add some personalized rules to its simplification process? For example, can we tell Mathematica that we prefer 2*Re[C1*Conjugate[C2]] than C1*Conjugate[C2]+C2*Conjugate[C1] ? Thanks.
Update:
Thanks for the suggestions. ComplexExpand can expand it to real and imaginary part, but seems still can't simplify to the preferred form:
In: Simplify[ComplexExpand[expr, {C1, C2, C3}]]
Out: 2 (Im[C1] (d12 Im[C2] + d13 Im[C3]) + Re[C1] (d12 Re[C2] + d13 Re[C3]))
I tried the TransformationFunctions function like this but it doesn't work:
In: t = # /. (Im[C1] Im[C2] + Re[C1] Re[C2] -> 1/2 Re[C1\[Conjugate] C2]) &;
In: Simplify[ComplexExpand[expr, {C1, C2, C3}], TransformationFunctions -> {Automatic, t}]
Out: 2 (Im[C1] (d12 Im[C2] + d13 Im[C3]) + Re[C1] (d12 Re[C2] + d13 Re[C3]))
Am I doing the wrong way? Thanks.
Actually it simplifies to smaller expression
expr=d12*Conjugate[C1]*C2+d12*Conjugate[C2]*C1+d13*Conjugate[C1]*C3 +
d13*Conjugate[C3]*C1;
Simplify[ComplexExpand[expr]]
gives
2 C1 (C2 d12 + C3 d13)
how Mathematica determines one expression is simpler than another? Is there a way to add some personalized rules to its simplification process
You can use the ComplexityFunction option to Simplify. The default is Automatic and I think this uses Leaf count to decide. You can also use the TransformationFunctions option to Simplify to provide your own functions to apply. See help.
http://reference.wolfram.com/mathematica/ref/ComplexityFunction.html
http://reference.wolfram.com/mathematica/ref/TransformationFunctions.html
http://reference.wolfram.com/mathematica/ref/LeafCount.html
Related
I am trying to write the following expression:
L[y[x]] = y'[x] - 1/h (a0 y[x - h] + a1 y[x] + a2 y[x + h])
I already saw an answer about something similar to this problem: f[y_]:=D[y,x]*2 and I understood the command of delayed definition. The problem is that in my case the argument x is important because I have to evaluate the function y in different points and this is giving me some issue.
How I can write the formula in a proper way?
Thanks in advance
I'm not exactly sure what you are trying to accomplish.
Is there any chance that this helps?
y[x_]:=Sin[x];
L[y_,x_] := (y'[z] - 1/h (a0 y[z - h] + a1 y[z] + a2 y[z + h]))/.z->x;
L[y,x]
L[y,2]
which returns
Cos[x] - (-(a0*Sin[h - x]) + a1*Sin[x] + a2*Sin[h + x])/h
and
Cos[2] - (a1*Sin[2] + a0*Sin[2 - h] + a2*Sin[2 + h])/h
That depends on z and perhaps x not previously having been assigned any values.
There are almost certainly other ways of doing that, like everything else in Mathematica.
Please test this VERY carefully before you even think of depending on this.
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.
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))}}
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
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.
Mathematica 7.0 seems to dislike having blanks in the denominator. Can anyone explain why this is?
Input:
ClearAll["Global`*"];
(*Without blanks:*)
a^2 / b^2 /. a^2 / b^2 -> d
(*with:*)
a^2 / b^2 /. a^c_ / b^c_ -> d
(*Without blanks:*)
a^2 / b^2 /. (a / b)^2 -> d
(*With:*)
a^2 / b^2 /. (a / b)^c_ -> d
(*Without blanks:*)
a^2 / b^2 /. a^2 * b^(-2) -> d
(*With:*)
a^2 / b^2 /. a^c_ * b^(-c_) -> d
Output:
d
a^2/b^2
d
a^2/b^2
d
a^2/b^2
I'm trying to work this for a more complicated problem. The substitution that I want to make is in an expression of the form:
(a ^ c_. * Coefficient1_. / b ^ c_. / Coefficient2_.) + (a ^ d_. * Coefficient3_. / b ^ d_. / Coefficient4_.)
Where the coefficients may involve sums, products, and quotients of variables that may or may not includea and b.
Possibly relevant:
The FullForm shows that the power in the denominator is stored as a product of -1 and c:
Input:
FullForm[a^2/b^2]
FullForm[a^c_/b^c_]
FullForm[ (a / b)^2 ]
FullForm[(a / b)^c_ ]
FullForm[a^2 * b^(-2) ]
FullForm[a^c_ * b^(-c_)]
Output:
Times[Power[a,2],Power[b,-2]]
Times[Power[a,Pattern[c,Blank[]]],Power[b,Times[-1,Pattern[c,Blank[]]]]]
Times[Power[a,2],Power[b,-2]]
Power[Times[a,Power[b,-1]],Pattern[c,Blank[]]]
Times[Power[a,2],Power[b,-2]]
Times[Power[a,Pattern[c,Blank[]]],Power[b,Times[-1,Pattern[c,Blank[]]]]]
Edit: Bolded change to my actual case.
Generally speaking you should try to avoid doing mathematical manipulation using ReplaceAll which is a structural tool.
As opposed to FullForm, I will use TreeForm to illustrate these expressions:
a^2/b^2 // TreeForm
a^c_/b^c_ // TreeForm
You can see that while these expressions are mathematically similar, they are structurally quite different. You may be able to hammer out a functioning replacement rule for a specific case, but you will usually be better off using the Formula Manipulation (or Polynomial Algebra) tools that Mathematica provides.
If you carefully describe the mathematical manipulation you wish to achieve, I will attempt to provide a better solution.
As belisarius humorously points out in a comment, trying to force Mathematica to "see" or display expressions the way you do is often largely futile. This is one of the reasons that the opening statement above is true.
I agree with everything Mr.Wizard wrote. Having said that, a replacement rule that would work in this specific case would be:
a^2/b^2 /. (Times[Power[a,c_],Power[b,e_]]/; e == -c )-> d
or
a^2/b^2 /. (a^c_ b^e_/; e == -c )-> d
Note that I added the constraint /; e == -c so that I effectively have a -c_ without actually creating the corresponding Times[-1,c_] expression
The primary reason a^2 / b^2 /. a^c_ / b^c_ -> d doesn't work is your using Rule (->) not RuleDelayed (:>). The second reason, as you found with FullForm, is that a/b is interpreted as Times[a, Power[b,-1]], so it is best to not use division. Making these changes,
a^2 / b^2 /. a^n_ b^m_ :> {n,m}
returns {2, -2}. Usually, you'll want to have a default value, so that
a / b^2 /. a^n_. b^m_. :> {n,m}
returns {1,-2}.
Edit: to ensure that the two exponents are equal, requires the addition of the Condition (/;)
a^2 / b^2 /. a^n_. b^m_. /; n == m :> n
Note: by using _. this will also catch a/b.
Let's say I have a relation r^2 = x^2 + y^2. Now suppose after a calculation i get a complicated output of x and y, but which could in theory be simplified a lot by using the above relation. How do I tell Mathematica to do that?
I'm referring to situations where replacement rules x^2+y^2 -> r^2 and using Simplify/FullSimplify with Assumptions won't work, e.g. if the output is x/y + y/x = (x^2+y^2)/(xy) = r^2/(xy).
Simplification works really well with built in functions but not with user defined functions! So essentially I would like my functions to be treated like the built in functions!
I believe you are looking for TransformationFunctions.
f = # /. x^2 + y^2 -> r^2 &;
Simplify[x/y + y/x, TransformationFunctions -> {Automatic, f}]
(* Out= r^2/(x y) *)
In the example you give
(x/y + y/x // Together) /. {x^2 + y^2 -> r^2}
==> r^2/(x y)
works. But I've learned that in many occasions replacements like this don't work. A tip I once got was to replace this replacement with one which has a more simpler LHS like: x^2 -> r^2-y^2 (or even x->Sqrt[r^2-y^2] if you know that the values of x and y allow this).
I generate very long and complex analytic expressions of the general form:
(...something not so complex...)(...ditto...)(...ditto...)...lots...
When I try to use Simplify, Mathematica grinds to a halt, I am assuming due to the fact that it tries to expand the brackets and or simplify across different brackets. The brackets, while containing long expressions, are easily simplified by Mathematica on their own. Is there some way I can limit the scope of Simplify to a single bracket at a time?
Edit: Some additional info and progress.
So using the advice from you guys I have now started using something in the vein of
In[1]:= trouble = Log[(x + I y) (x - I y) + Sqrt[(a + I b) (a - I b)]];
In[2]:= Replace[trouble, form_ /; (Head[form] == Times) :> Simplify[form],{3}]
Out[2]= Log[Sqrt[a^2 + b^2] + (x - I y) (x + I y)]
Changing Times to an appropriate head like Plus or Power makes it possible to target the simplification quite accurately. The problem / question that remains, though, is the following: Simplify will still descend deeper than the level specified to Replace, e.g.
In[3]:= Replace[trouble, form_ /; (Head[form] == Plus) :> Simplify[form], {1}]
Out[3]= Log[Sqrt[a^2 + b^2] + x^2 + y^2]
simplifies the square root as well.
My plan was to iteratively use Replace from the bottom up one level at a time, but this clearly will result in vast amount of repeated work by Simplify and ultimately result in the exact same bogging down of Mathematica I experienced in the outset. Is there a way to restrict Simplify to a certain level(s)?
I realize that this sort of restriction may not produce optimal results, but the idea here is getting something that is "good enough".
There are a number of ways you can do this, but it can be a little tricky and depends on the structure of your actual expression. However, usually a product of a number of terms in brackets will have the head Times, and you can use FullForm to verify this:
In[1]:= FullForm[(a+b)(c+d)]
Out[1]= Times[Plus[a, b], Plus[c, d]]
You can use the higher-order function Map with expressions with head Times the same way you use it with expressions with head List, and that may allow you to Simplify the expression one term at a time, like so:
Map[Simplify, yourGinormousExpression]
You can use Expand on the result if you need to subsequently expand out the brackets.
EDIT to add: If you want to specify the forms that you do want to simplify, you can use Replace or ReplaceAll instead of one of the relatives of Map. Replace is particularly useful because it takes a level specification, allowing you to only affect the factors in the topmost product. As a simple example, consider the following:
In[1]:= expr = Sqrt[(a + 1)/a] Sqrt[(b + 1)/b];
In[2]:= Simplify[expr]
Out[2]= Sqrt[1 + 1/a] Sqrt[1 + 1/b]
If you don't want to simplify factors that depend on a. you can do this instead:
In[3]:= Replace[expr, form_ /; FreeQ[form, a] :> Simplify[form], {1}]
Out[3]= Sqrt[(1 + a)/a] Sqrt[1 + 1/b]
Only the second term, which depends on b, has been changed. One thing to bear in mind though is that some transformations are done automatically by Times or Plus; for instance a + a will be turned into 2 a even without use of Simplify.
I beg to differ with my colleagues, in that using Map to apply Simplify to each subexpression may not save any time as it will still be applied to each one. Instead try, MapAt, as follows:
In[1]:= MapAt[f, SomeHead[a,b,c,d], {4}]
Out[1]:= SomeHead[a, b, c, f[d]]
The tricky part is determining the position specification. Although, if the expression you want to simplify is at the first level, it shouldn't be any more difficult then what I've written above.
Now if you would still like to simplify everything, but you wish to preserve some structure, try using the option ExcludedForms. In the past, I've used to prevent this simplification:
In[2]:= Simplify[d Exp[I (a + b)] Cos[c/2]]
Out[2]:= Exp[I(a + b + c)](d + d Exp[c])
which Mathematica seems to like, so I do
In[3]:= Simplify[d Exp[I (a + b)] Cos[c/2], ExcludedForms -> {_Cos,_Sin}]
Out[3]:= d Exp[I (a + b)] Cos[c/2]
Also, don't forget that the second parameter for Simplify is for assumptions, and can greatly ease your struggles in getting your expressions into a useful form.
You should try Map.
In general, Map[foo, G[a, b, c, ...]] gives G[foo[a], foo[b], foo[c], ...] for any head G and any expression foo, so for
Map[Simplify, a b c d e]
it gives
Simplify[a] Simplify[b] Simplify[c] Simplify[d] Simplify[e]
Note you can denote Map[foo, expr] als foo /# expr if you find that more convenient.