Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now
52

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


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


38

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


30

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


30

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


26

In addition to the previous answer... I designed and implemented Total 17 years ago. Its first version was named ListSum. The primary reason for ListSum's implementation was to encapsulate the functionalities dealing with error accumulation while summing a list of numbers. (A well known phenomena in, say, numerical solvers for ODE's.) Of course, I also ...


25

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


24

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


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: Sqrt[Plus[...


23

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


22

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


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

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


20

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


19

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


19

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


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


18

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]


17

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


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


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

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


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

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


15

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


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


15

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


14

RatDenom[x_]:= Module[{y,nn,dd,f,g,c,k,blah}, (y=Together[x]; nn=Numerator[y]; dd=Denominator[y]; f=MinimalPolynomial[dd,t]; c=f /. t -> 0; g=Factor[(c-f)/t]; {k, blah}=FactorTermsList[Expand[nn*(g /. t -> dd)]]; Sign[c] ((k/GCD[k,c])*blah)/HoldForm[Evaluate@Abs[c/GCD[k,c]]])] Write $x=\nu/\delta$. The point is ...


14

If you use the matrix representation, addition and multiplication works by default, but you need to use matrix multiplication and not just space (which is element-wise multiplication), for instance f[a_,b_]:=a.b+b.b would for instance work exactly like you would expect if a and b where dual numbers. If you just want to have the rule enforce that you ...


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