Why does the Mathematica give different numerical results?

3

Why do the two inputs give different values?

in:=(-y)^a /. a -> 2.99 /. y -> -2
out:=7.94474

and

in:=(-1)^a (y)^a /. a -> 2.99 /. y -> -2
out:=7.92906 - 0.498854 I

Antimony51

Posted 2020-05-13T06:43:28.047

Reputation: 91

Answers

9

They are supposed to be different.

(-y)^a /. a -> 2.99 /. y -> -2
(*7.94474*)

is equivalent to

2^2.99

or

Exp[2.99 Log[2]]

all giving the same answer whereas

(-1)^a (y)^a /. a -> 2.99 /. y -> -2
(*7.92906 -0.498854 I*)

is

(-1)^2.99
(*-0.999507 + 0.0314108 I*)

and

(-2)^2.99
(*-7.94082+0.24955 I*)

%%*%
(*7.92906 -0.498854 I*)

or another way

Exp[2.99 Log[-2]] Exp[2.99 Log[-1]]
(*7.92906 -0.498854 I*)

Powers and roots of complex numbers really do matter how and in what order they are calculated.

Bill Watts

Posted 2020-05-13T06:43:28.047

Reputation: 6 261

5

Complex power is really a multivalued function. Consider this function, that returns all possible values of a complex number $x^y$:

complexPowers[x_, y_?NumericQ] := With[{r = Rationalize[y]},
  SortBy[ResourceFunction["ComplexRoots"][x, Denominator[r]]^Numerator[r], Arg]]

Also an auxiliary function to multiply them in all possible ways:

multiplyPowers[xs_ /; VectorQ[xs, NumericQ], ys_ /; VectorQ[ys, NumericQ]] := 
 SortBy[DeleteDuplicates@Flatten@Outer[Times, xs, ys], Arg]

Then your example reduces to comparing all values of complexPowers[-y, a] and multiplyPowers[complexPowers[-1, a], complexPowers[y, a]]

complexPowers[-y, a] == multiplyPowers[complexPowers[-1, a], complexPowers[y, a]] /. {y -> -2, a -> 2.99}
(* True *)

swish

Posted 2020-05-13T06:43:28.047

Reputation: 6 720

3

If you want to write things in terms of symbols without minus signs flying around, you can use PowerExpand. If used naively, it treats all symbols as positive real numbers which is not always the case, and certainly is incorrect in OP's situation.

The trick is to set all assumptions to true, hence disabling all simplifications that are correct only for positive real numbers. Indeed, we have

PowerExpand[(-y)^a]

$$(-1)^a y^a$$

whereas

PowerExpand[(-y)^a, Assumptions -> True]

$$y^a e^{2 i \pi a \left\lfloor -\frac{\arg (y)}{2 \pi }\right\rfloor +i \pi a}$$

If you now set $\{a,y\}$ to any particular numbers you like, you get the correct result:

N[(E^(I a π+2 I a π Floor[-(Arg[y]/(2 π))]) y^a)/.y->-2/.a->299/100]

7.94474 +0. I

Similar to all numeric computations, one needs to be careful with potential finite precision errors. Indeed, we see that

(E^(I a π + 2 I a π Floor[-(Arg[y]/(2 π))]) y^a) /. y -> -2 /. a -> 2.99

7.94474 - 2.77556*10^-17 I

which can be avoided as we did in the first case (using 299/100 instead of 2.99 and hitting with N at the end) or we can try increased precision:

(E^(I a π + 2 I a π Floor[-(Arg[y]/(2 π))]) y^a) /.  y -> -2 /. a -> 2.99`10

7.94473996 + 0.*10^-8 I

Soner

Posted 2020-05-13T06:43:28.047

Reputation: 1 903