Mathematica Asked by pdmclean on June 26, 2021
The expression 1+Sqrt[2]I-Complex[1,Sqrt[2]]
should be zero. In fact, N[1+Sqrt[2]I-Complex[1,Sqrt[2]]]
yields 0.+0. I
.
But FullSimplify
and ComplexExpand
cannot reduce this to 0
.
What is going on here?
1 + Sqrt[2] I - Complex[1, Sqrt[2]] /. Complex[x_, y_] :> x + I*y
Answered by cvgmt on June 26, 2021
Many what's-going-on questions really just turn out to be people wanting a fix for their code. They just don't understand why it doesn't work. Given the simple code, maybe an explanation of how Complex
works is actually being sought.
There's pretty much the same problem with 1/Sqrt[2] - Rational[1, Sqrt[2]]
and 1/2 - Rational[1., 2]
. Complex[x, y]
is not a valid expression unless x
and y
are numbers, which is reflected in how Complex[1, Sqrt[2]]
is typeset in the Front End. (Sqrt[2]
is not a number either. It's a numeric expression. Compare NumberQ[Sqrt[2]]
and NumericQ[Sqrt[2]]
.) Generally, you should construct a complex number with I
, not Complex
, somewhat like your first two terms. However, 1 + Sqrt[2] I
cannot be represented by a Complex
number in Mathematica. It can only be represented by a composite, complex-numeric expression.
For Complex[x, y]
to be valid, x
and y
can be any numbers, Integer
, Real
, Rational
or even Complex
. However, Complex
parts will be automatically simplified:
Complex[Complex[1, 2], 1]
(* 1 + 3 I *)
Another undocumented quirk is that if one part is MachinePrecision
, then both parts will be made MachinePrecision
(the docs show only numbers with both parts entered with machine precision):
Complex[1., 2]
Complex[1., 2`500]
Complex[1.`6, 2] (* may have mixed-precision non-MachinePrecision parts *)
(*
1. + 2. I
1. + 2. I
1.00000 + 2 I
*)
Under the everything-is-an-expression philosophy, Complex[1, Sqrt[2]]
is treated as an expression, but not one with internally defined semantics apparently. So it is not combined with other numeric expressions as though it represented a complex number.
A valid number passes the NumberQ
test (as well as AtomQ
, too):
Complex[1, Sqrt[2]] // NumberQ
Complex[1, 2] // NumberQ
(*
False
True
*)
P.S.
For Rational[x, y]
to be valid, x
and y
each have to be an Integer
with y
nonzero, though Rational[1, 0]
will evaluate to ComplexInfinity
.
Answered by Michael E2 on June 26, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP