Hot answers tagged algebraic-manipulation
38
In general, a typical root of a negative number is complex, so you need to get rid of most roots. A nice approach would be Root, e.g.
Root[ x^3 + 8, #] & /@ Range[3]
{-2, 1 - I Sqrt[3], 1 + I Sqrt[3]}
To get only real roots you can do :
Select[Root[ x^3 + 8, #] & /@ Range[3], Re[#] == # &]
{-2}
This is a handy approach when you ...
31
You can use custom transformation rules, for example:
-11 - 2 x + x^2 - 4 y + y^2 - 6 z + z^2 //.
(a : _ : 1)*s_Symbol^2 + (b : _ : 1)*s_ + rest__ :>
a (s + b/(2 a))^2 - b^2/(4 a) + rest
returns
(* -25 + (-1 + x)^2 + (-2 + y)^2 + (-3 + z)^2 *)
The above rule does not account for cases where b is zero, but those are easy to add too, if ...
17
You can't use replacements that way, because Mathematica does not do replacements on expressions the way they appear to you. To see what I mean, take a look at the FullForm of your expression:
x/(y*z) // FullForm
Out[1]= Times[x,Power[y,-1],Power[z,-1]]
Whereas, the replacement that you're using is Times[y, z].
In general, it is not a good idea to use ...
17
Short answer is
Expand[(x + y)^2]
x^2 + 2 x y+ y^2
But I recommend you to look at the following tutorials.
Transforming Algebraic Expressions
Putting Expressions into Different Forms
And of course a super tutorial:
Algebraic Manipulation
Also this palette maybe really useful: Top Menu >> Palettes >> Other >> Algebraic Manipulation
16
You may use a function, which gives you the "real Power":
rprule=(b_?Negative)^Rational[m_,n_?OddQ]:>(-(-b)^(1/n))^m;
Attributes[realPower]={Listable, NumericFunction,OneIdentity} (* same as Power *)
realPower[b_?Negative, Rational[m_, n_?OddQ]] := (-(-b)^(1/n))^m;
realPower[x_,y_]:=Power[x,y];
realPower[x_]:=x//.rprule;
Then you'll get:
...
15
Here's a way to do it:
Begin["NonStandardAlgebra`"];
ClearAll /@ {plus, times};
SetAttributes[#, Orderless] & /@ {plus, times};
plus[x : 0 | 1, y : 0 | 1] := Unitize[x + y]
plus[Infinity, x : 0 | 1 | Infinity] := Infinity
times[0, Infinity] := 1
times[x_, y_] := System`Times[x, y]
End[];
A couple of examples:
NonStandardAlgebra`times[Infinity, 0]
(* ...
15
The function you want for this kind of case is TrigReduce:
TrigReduce[expr]
rewrites products and powers of trigonometric
functions in expr in terms of trigonometric functions with combined
arguments.
And it works:
14
Case #1
Observe:
"anything" /. Plus[___] -> "match"
"match"
This is because Plus[___] evaluates to ___, and ___ matches anything. You can use HoldPattern:
Sqrt[Plus[x, y]] /. HoldPattern[Plus[___]] -> u
Sqrt[u]
Case #2
You must understand that pattern matching is done on something close to the FullForm of an expression, rather than the ...
13
Since nobody pointed this out I think there is still room for another reply. Note that this works fine
Unevaluated[(x + Log[y*z])/(y*z)] /. (y*z) :> w
(x + Log[w])/w
In more complex cases you may also need to use HoldPattern
Unevaluated[(x + Log[(y*z)/2])/((y*z)/2)] /. HoldPattern[((y*z)/2)] :> w
(x + Log[w])/w
This is not a panacea. ...
13
Try using FullSimplify:
FullSimplify[Sin[x] == Tan[x] Cos[x]]
This returns True if Sin[x] == Tan[x] Cos[x] (which it does).
Please note that == (Equal) should be used instead of a single equal sign (Set). More complicated trig identities can be difficult to reason about. Mathematica may not be able to properly determine whether they are true or not. You ...
13
This explicitly converts any numeric quantities in the expression to the desired form:
polarForm = Expand[# /. z_?NumericQ :> Abs[z] Exp[I Arg[z]]] &;
e.g.
(1/4 + I/4) ((1 - 2 I) x + Sqrt[3] y) // polarForm
$\frac{1}{2} \sqrt{\frac{5}{2}} e^{\frac{i \pi }{4}-i \text{ArcTan}[2]} x+\frac{1}{2} \sqrt{\frac{3}{2}} e^{\frac{i \pi }{4}} y$
...
12
In general, to get a list of all the cube roots of -8 (or the $m$ roots of any number $n$), you can use either the the Roots or Solve or Reduce functions.
Roots[x^3 == -8, x]
(* Out[1]= x == 2 || x == 2 (-1)^(2/3) || x == -2 (-1)^(1/3) *)
Reduce and Solve are perhaps more flexible because you can specify the domain that you want or leave it out for all ...
12
Are you looking for Subtract?
eq=x>=y
Subtract@@eq>=0
gives:
x-y>=0
Edit
If one wants a function, which keeps the order sign and adds the 0, one may use:
oneSide=(Head[#][Subtract@@#,0]&)
and call e.g. eq//oneSide
12
Collect
Since it hasn't been mentioned (and one can interpret the question in another way) I'd recommend to use also Collect (it can be applied not only to polynomials) :
Collect[(x + y)^2, x]
x^2 + 2 x y + y^2
In more general cases it would be handy to use the second argument in the form of List, e.g. Collect[(x + y)^2, {x, y}].
Comparing it to ...
11
This approach works by using the fact that an inequality or equality can be traversed by Map in the same way that a regular list can. It can take an arbitrary inequality or equation eqn, and you don't have to know in advance whether it's >, < or anything else. First I define the equation eqn, and then I use the fact that the second part of eqn is the ...
11
There is no need to play around with Simplify, since to achive what you need one can use Collect, e.g.
expr = Exp[i k t] + 2 x Exp[i k t] + (2 x + 1) Exp[i k t]^2;
Collect[expr, Exp[i k t]]
E^(i k t) (1 + 2 x) + E^(2 i k t) (1 + 2 x)
If there are more variables you can use a list of them as the second argument,
look also at Simplify as the third ...
11
I'm assuming here that x is a list of points between which you want to calculate the distance. If so, then I think your code can be condensed to something like
PeriodicDistance[x_, size_: 1] := Outer[Norm@Mod[#2 - #1, size, -size/2] &, x, x, 1]
Edit
A faster version of the code above is something like
PeriodicDistance3[x_, size_: 1] :=
Map[Norm, ...
11
One way to do this is:
Sin[x]^8 + 2 Cos[x]^8 - 1/2 Cos[2 x]^2 + 4 Sin[x]^2 == 0 /.
Solve[t == Cos[2 x], x] //FullSimplify // Expand // Union // Column // TraditionalForm
It gives exactly your answer if you get rid of your denominator 16 (multiply both sides of your equation by 16).
This will also work with more complex substitutions (for example t ...
10
The reason why the replacement doesn't work is that replacement rules are not mathematical replacements, but pure structural replacements. Therefore the replacement z^2->x just looks for occurrences of the pattern z^2 and replaces that with x. Now z^4 doesn't match that pattern.
Also note that rules operate on the internal form, which doesn't always ...
10
A standard approach for this kind of task uses Eliminate. It works nicely with polynomial equations, and even though neither this transformation : $\;\sqrt{5x - 1}-\sqrt{5-2x} \rightarrow t \quad$ nor the original equation : $ (26-x)\sqrt{5x-1} -(13x+14)\sqrt{5-2x} + 12\sqrt{(5x-1)(5-2x) }= 18x+32\quad$ are of polynomial types, nevertheless we can make some ...
10
This is caused by a bug in RootReduce for Root objects representing last coordinates of solutions of triangular systems. The bug affects cases where the last coordinate of the solution is real, but some of the other coordinates are not real. Thanks for pointing it out.
The problem can be fixed with the following patch (you can put it in your init.m file).
...
9
The first thing that comes to mind is to use Through, as in
Through[(f + g)[x]]
f[x] + g[x]
However, this is a little tricky to apply when you also have powers as in f^2 - so in your case it seems to be more efficient to make use of the fact that all symbols are evaluated at the same x anyway (i.e., there isn't any f[y] and f[z] anywhere). Then you ...
9
A bit different approach :
Simplify @ TrigReduce[ Sin[x]^8 + 2 Cos[x]^8 - 1/2 Cos[2 x]^2 + 4 Sin[x]^2 == 0
/. Solve[ t == Cos[2 x], x, InverseFunctions -> True][[1]]]
35 + 10 t^2 + 4 t^3 + 3 t^4 == 28 t
or using Eliminate :
Eliminate[ TrigToExp[{ Sin[x]^8 + 2 Cos[x]^8 - 1/2 Cos[2 x]^2 + 4 Sin[x]^2 == 0,
t == Cos[2 x]}], x, ...
9
Are you sure you need to transform x^2 into x*x to achieve your goal? A simpler thing will work with the same result:
a b^2 c /. b^2 c -> b e
a b e
In cases like this you can always find correct algebraic form to replace, so the substitution will just work without any tricks. Of course you always can do something like
x^2 /. x^2 -> Defer[x x]
...
9
Do you mean this?
Power[#, 2] & /@ (x + 1 == y^2 + 2)
Or
#^2/@ (x + 1 == y^2 + 2)
, according to Vitaliy Kaurov's advice.
(1 + x)^2 == (2 + y^2)^2
9
As bound variables, $r$ and $i$ must play no role in the expansion and so they shouldn't even appear in our solution. It should be equally evident that $n$ is just along for the ride as a placeholder for the upper limit; we could call it anything, and therefore we may call it nothing and ignore it except in expressions where it will necessarily appear in ...
9
If you look at the Fullform of the expression, you can see what's going on. When you set ClearAttributes[Times, Orderless] to
FullForm[Expand[x (y - z)]]
you get the form
Plus[Times[x,y],Times[x,-1,z]]
Since you told Times that it could no longer place the x and the -1 in normal order, it listed them in the order in which they appear in the expression. ...
8
Well, I am more inclined to try something that leverages Mathematica "knowledge" of polynomials.
In fact, in Mathematica 7 and 8, you can collect by $x-y$. However, it only works if $(x-y)$ is explicitly apparent in the form of the argument seen by the Collect function.
So, this works:
Collect[a (x - y)^3 + b (x - y)^2 + c (x - y) + d, x - y]
But, this ...
8
Try FullSimplify:
m = {{1, (1 - (-1)^(1/5) + (-1)^(2/5) - (-1)^(3/5) + (-1)^(4/5)) b}, {0, 1}};
m // FullSimplify
{{1, 0}, {0, 1}}
Alternatively, for this very specific situation you can use the ExpToTrig function which will convert expressions of the form $(-1)^\alpha$ to $\cos (\pi \alpha )+i \sin (\pi \alpha )$. When written in this form, the ...
8
First, for the sake of simplicity let's define eqs - the system of our interest :
eqs = { x == p + R Cos[k],
y == Cos[p] + R Sin[k],
k == ArcTan[ 1/Sin[p]] };
Equations y = y(x)
For this system we can find an explicit equation $\;y = y(x)\;$ only assuming R == 0, otherwise we could find only implicit solutions.
Solve[ eqs /. R -> ...
Only top voted, non community-wiki answers of a minimum length are eligible



