Hot answers tagged

50

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 ...


44

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 ...


26

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 ...


24

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] For this particular case you could also use the form _Plus, which matches any expression with head Plus but not Plus itself: ...


23

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: realPower[{8^...


22

Mathematica 9 introduces two new functions, CubeRoot and Surd, that give real-valued roots: In[1]:= CubeRoot[-8] Out[1]= -2 In[2]:= Surd[-32, 5] Out[2]= -2 You can use these to plot real roots: Plot[CubeRoot[x], {x, -3, 3}] Note that these functions are undefined for complex numbers: In[5]:= CubeRoot[1 + I] CubeRoot::preal: The parameter 1+I ...


21

You can use TrigExpand to expand all trigonometric functions to fundamental forms and then Eliminate solves the rest eq1 = Sin[x]^8 + 2 Cos[x]^8 - 1/2 Cos[2 x]^2 + 4 Sin[x]^2 == 0; eq2 = t == Cos[2 x] Eliminate[TrigExpand[{eq1, eq2}], x]


21

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$ (-1)^(...


20

I was waiting for OP to post his answer before posting mine. In any event, here's a general routine for performing polynomial depression (where completing the square corresponds to the quadratic case): depress[poly_] := depress[poly, First@Variables[poly]] depress[poly_, x_] /; PolynomialQ[poly, x] := Module[{n = Exponent[poly, x], x0}, x0 = -...


19

Disclaimer: This is not a full answer, but perhaps it's a start. From an algebraic stand point this seems like a very hard problem. I attacked it with a more brute force approach. I guess a basis and use LatticeReduce to try to find a Diophantine relation. Note this code only tries to identify roots as the product of integral powers of trig. If it returns ...


18

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] (* ...


18

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


17

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 ...


17

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). ...


17

You can use GroebnerBasis: eq = (a + b)^10 - a^10 - b^10; eqXY = GroebnerBasis[{eq, a + b - x, a b - y}, {x, y}, {a, b}]; (*out*){10 x^8 y - 35 x^6 y^2 + 50 x^4 y^3 - 25 x^2 y^4 + 2 y^5} Check: First@Expand[eqXY /. x -> (a + b) /. y -> a b] === Expand[eq] (*out*)True --EDIT-- Following @DanielLichtblau's suggestion, it's better to do this in ...


17

You are using SameQ which does a direct structural comparison rather than a mathematical one. Since the expressions are not exactly the same it returns False. Try Equal: FullSimplify[Abs[1/x + x^2] == Abs[1 + x^3]/Abs[x]] True FullSimplify is needed for nontrivial comparisons; without it Mathematica will return the equality as given if it is not ...


16

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 ...


16

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 ...


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:


15

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


15

You need to use a pattern in Collect which will match the terms you are trying to collect. In this case we want to collect terms like f[x] and f[y] so a suitable pattern is _f which matches any expression with head f: Collect[a*f[x] + f[y] + x*f[x], _f] (* (a + x) f[x] + f[y] *)


14

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 ...


14

As this has been answered, already, here is my solution using Distribute: Distribute[ (x+1 == y^2 + 2)^2, Equal ] (* (1 + x)^2 == (2 + y^2)^2 *)


14

Distribute @ Sum[-2 Subscript[x, i] (-a Subscript[x, i] - b + Subscript[y, i]) // Expand, {i, n}] == 0


13

In[409]:= PolynomialReduce[z^4 + z^2 + 4, z^2 - x, {z, x}][[2]] Out[409]= 4 + x + x^2 This is similar to the Solve approach in that both use algebraic means to effect the substitution. But one can be a bit more general using PolynomialReduce (by taking advantage of term orders, say). For further detail on this approach, might have a look at some ...


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

Here's a tiny piece of advice I follow: any time I want to implement a new, exotic number system in Mathematica, the first thing I do is to look within the implementation of the Quaternions` package, and try to adapt/emulate the constructions in the package to the number system I am trying to implement. Having said this, here's a bunch of rules for doing ...


13

Did you mean this? Power[#, 2] & /@ (x + 1 == y^2 + 2) Or #^2 & /@ (x + 1 == y^2 + 2) works as well, according to Vitaliy Kaurov's advice. (1 + x)^2 == (2 + y^2)^2


13

You can use Normal, ConditionalExpression is not explicitly mentioned there but documentation says it deals with special forms. p1 = y /. {First[Solve[x^2 + y^2 + x == 1, y, Reals]]} // First ConditionalExpression[-Sqrt[1 - x - x^2], 1/2 (-1 - Sqrt[5]) < x < 1/2 (-1 + Sqrt[5])] Normal @ p1 -Sqrt[1 - x - x^2]


13

In the course of answering this question, I ran into a little bit of weirdness that doesn't square with my experience with previous versions of Mathematica. I think writing this answer is as good a time as any to bring it up. Firstly, there is this tantalizing line from the internal implementation notes: FunctionExpand uses an extension of Gauss's ...



Only top voted, non community-wiki answers of a minimum length are eligible