Tag Info

Hot answers tagged

46

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


34

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


21

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


20

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]


19

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


18

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


17

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


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

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


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


16

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


16

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


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

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


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


14

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] *)


13

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


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

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


13

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


12

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


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


12

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


12

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


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


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

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

Mathematica does not consider 9 Log[2] to be "simpler" than Log[512]. The full default ComplexityFunction is not disclosed (the one in the documentation is not entirely equivalent IIRC), but a good first-order approximation is often LeafCount LeafCount /@ {Log[512], 9 Log[2]} {2, 4} If you provide a ComplexityFunction that uses a different metric by ...


10

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



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