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?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityThe 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?
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
.
N[1+Sqrt[2]I-Complex[1,Sqrt[2]]]
succeeds because N[Complex[1,Sqrt[2]]]
becomes Complex[1., 1.41421]
, a valid Complex
number.
$\endgroup$
– Michael E2
yesterday
1 + Sqrt[2] I - Complex[1, Sqrt[2]] /. Complex[x_, y_] :> x + I*y
Complex
doesn't mention things like Complex[1, Sqrt[2]]
are not NumericQ
.
$\endgroup$
– Chip Hurst
2 days ago
Complex
represents a number, and NumberQ[Sqrt[2]]
is False
. But it's very strange to find the docs are incomplete.
$\endgroup$
– Michael E2
2 days ago