TransWikia.com

Two "identical" functions returning different values

Mathematica Asked by Jared Gdanski on June 13, 2021

When taking the norm of a complex number in Mathematica you get the same result when you use the Norm function or the Abs function. So why do the following two functions (kp and kpp) return different results?

k[k1_, k2_] := {Sqrt[3]/2 k1, k1/2 + k2, 0};
kp[k1_, k2_] := (#/Norm[#]) &[k[k1, k2][[1]] + I k[k1, k2][[2]]]
kpp[k1_, k2_] := (k[k1, k2][[1]] + I k[k1, k2][[2]])/Abs[k[k1, k2][[1]] + I k[k1, k2][[2]]]

array = Range[-1.4, 1.4, 0.1];

kp[2 π (array + 0.5), 2 π (-array + 0.5)]
kpp[2 π (array + 0.5), 2 π (-array + 0.5)]

To me these functions should return the same list of values but instead, they return two different lists of values.

3 Answers

Since you did not show the result of your execution, I think people misunderstood your situation. The problem is not in the inequality of the two function forms. The problem is that the functions kp and kpp, when operated on an array of complex numbers (actually, the real and complex parts, separated), behave differently. I could not find more detail reasons, but they operate on the list in different order.

The results are exactly the same if you change the definition of kp to

kp[k1_, k2_] := (#/Norm[#])& /@ (k[k1, k2][[1]] + I k[k1, k2][[2]])

Map is, surprisingly, much safer to use when your function may work on only one data point or a list of data points. Don't rely too much on square brackets to tell whether you are now working on scalar or vector. Things may go wrong at unexpected situations.

Correct answer by 梁國淦 on June 13, 2021

Clear["Global`*"]

k[k1_, k2_] := {Sqrt[3]/2 k1, k1/2 + k2, 0};
kp[k1_, k2_] := (#/Norm[#]) &[k[k1, k2][[1]] + I k[k1, k2][[2]]]
kpp[k1_, k2_] := (k[k1, k2][[1]] + I k[k1, k2][[2]])/
  Abs[k[k1, k2][[1]] + I k[k1, k2][[2]]]

Test whether the functions are equivalent

array = {a, b};

FullSimplify[kp[c (array + d), c (-array + d)]] === 
 FullSimplify[kpp[c (array + d), c (-array + d)]]

(* False *)

They are not even equivalent for real values

kp[c (array + d), c (-array + d)] // ComplexExpand // Simplify

(* {(c ((-I + Sqrt[3]) a + (3 I + Sqrt[3]) d))/(
 2 Sqrt[c^2 (a^2 + b^2 + 6 d^2)]), (
 c ((-I + Sqrt[3]) b + (3 I + Sqrt[3]) d))/(2 Sqrt[c^2 (a^2 + b^2 + 6 d^2)])} *)

kpp[c (array + d), c (-array + d)] // ComplexExpand // Simplify

(* {(c ((-I + Sqrt[3]) a + (3 I + Sqrt[3]) d))/(2 Sqrt[c^2 (a^2 + 3 d^2)]), (
 c ((-I + Sqrt[3]) b + (3 I + Sqrt[3]) d))/(2 Sqrt[c^2 (b^2 + 3 d^2)])} *)

% === %%

(* False *)

Answered by Bob Hanlon on June 13, 2021

Consider:

kp[k1, k2] == kpp[k1, k2]

enter image description here

As you see, the numerator are identical, therefore the difference must come from the denominator. Further note that array and also the arguments are vectors. Now investigate how Norm and Abs behave with vector arguments. E.g.:

Norm[{1,1}]
Abs[{1,1}]

enter image description here

Norm gives a scalar, Abs a vector!

Now if we simplify array to:

array = Range[1.3, 1.4, 0.1];

The numerator is in both cases the same:

enter image description here

(* {9.79452 + 0.628319 I, 10.3387 + 0.314159 I} *)

But the denominator:

enter image description here

(* 14.2588 *)
(* {9.81465, 10.3434} *)

In the first case with Norm we have:

 {9.79452 + 0.628319 I, 10.3387 + 0.314159 I} / 14.2588 ==> {0.686909 + 0.0440653 I, 0.725071 + 0.0220326 I}

And in the second case with Abs:

{9.81465, 10.3434} / {9.81465, 10.3434} ==> {0.997949 + 0.0640184 I, 0.999539 + 0.0303728 I}

Answered by Daniel Huber on June 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP