Abs¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Abs[z]
gives the absolute value (modulus) of numeric z, Sqrt[Re[z]^2 + Im[z]^2] for complex z.
Examples (18)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (7)¶
In[1]:= Re[1/(2 + 3 I)]
Out[1]= 2/13
In[2]:= Im[Log[-1]]
Out[2]= Pi
In[3]:= ReIm[(2 + I)^3]
Out[3]= {2, 11}
In[4]:= Abs[-9223372036854775807 - 1]
Out[4]= 9223372036854775808
In[5]:= Sign[3 + 4 I]
Out[5]= 3/5 + 4/5*I
In[6]:= Conjugate[(2 + I)/(1 - 3 I)]
Out[6]= -1/10 - 7/10*I
In[7]:= Arg[(-1)^(1/6)]
Out[7]= 1/6 Pi
Applications (11)¶
The modulus of a Gaussian integer, exactly
Listable
|1 + I|^10 = Sqrt[2]^10
The modulus of an exact complex expression
|-2^63| does not fit an int64, so it becomes a bignum
A negative real Root stays an exact algebraic number
Interval arithmetic
An MPFR complex goes through mpfr_hypot at 40 digits
The int64 kernel keeps integers exact
Not yet evaluated: exact real constants other than rationals stay as they are
Performance¶
Against other systems, from the benchmark suite (same input, results cross-checked for agreement):
| case | Mathilda | Wolfram | Python |
|---|---|---|---|
| NI 50-digit Gaussian | 6.87 s | 2.11 s | 1.1 s |
| NI 2-D ridge | 2.54 s | 43.5 s | 0.685 s |
| NI oscillatory k=40 | 0.515 s | 4.05 s | 0.002 s |
| NI oscillatory k=200 | 0.477 s | 21.5 s | 0.002 s |
| NI oscillatory k=1000 | 0.389 s | 140 s | 0.002 s |
| NI oscillatory k=1001 nonzero | 0.382 s | 1.03 s | 0.626 s |
Implementation notes¶
Algorithm. builtin_abs tries the following cases in order. (1) An Interval goes to interval_apply_function (iv_abs), so Abs[Interval[{-2, 1}]] is Interval[{0, 2}]. (2) A real AlgebraicNumber or Root object is compared with 0 through FLINT qqbar (flint_qqbar_compare) and returned as itself or its negation, so it stays an exact algebraic object. (3) A Complex[re, im] literal, or an expression that complex_decompose splits into concretely numeric real and imaginary parts (a recursive Plus/Times walk that propagates Complex factors), becomes the modulus. When either part is MPFR this goes through mpfr_hypot at the combined precision, which is also stable when the two magnitudes are very different. Otherwise it builds (re^2 + im^2)^(1/2) exactly, so Abs[3 + 4 I] is 5 and Abs[Sqrt[2] + Sqrt[3] I] is Sqrt[5]. (4) Real leaves: mpz_abs for a bigint, a sign flip for an int64 (INT64_MIN is promoted to the bignum 2^63 instead of wrapping), fabs for a machine Real, mpfr_abs at the input precision, and |n|/d for a Rational, including one with bignum parts. Anything else returns NULL, so the call stays unevaluated.
Data structures. The routine works directly on Expr leaves. MPFR scratch values are sized by numeric_combined_bits. The packed and NDArray surfaces use NDKU_AbsInt (ndkernels.c): an exact int64 arm (it abandons the whole buffer on INT64_MIN, so the List path answers with a bignum), and a hypot arm for real and complex buffers that writes a real buffer. Abs is on pack.c's AWARE list. In Compile[], Abs of an Integer stays an Integer, of a Real a Real, and of a Complex a Real, at scalar and array shapes.
Complexity / limits. Each case costs O(1) numeric work, except the complex_decompose walk, which is linear in the size of the expression. Symbolic arguments stay unevaluated (Abs[x]), as in Mathematica. A gap: a real exact constant that is neither a rational nor a Complex literal, such as Abs[Sqrt[2]], Abs[-Pi] or Abs[Pi - 4], also stays unevaluated at present, whereas Mathematica returns Sqrt[2], Pi and 4 - Pi. Apply N first for a numeric value.
Attributes: Listable, NumericFunction, Protected.
References¶
See also: Re, Im, ReIm, Sign, Conjugate, Arg, Real, Rational
- Source:
src/complex.c - Specification:
docs/spec/builtins/arithmetic.md - Tests:
tests/test_accuracygoal.c - Tests:
tests/test_airybi.c - Tests:
tests/test_algebraicnumber.c - Tests:
tests/test_assoc_ops.c
Notes & additional examples¶
Notes¶
Abs[z]is the absolute value of a real number and the modulusSqrt[Re[z]^2 + Im[z]^2]of a complex one. Exact inputs give exact results.- Limitation: an exact real constant that is not rational, such as
Sqrt[2],-PiorPi - 4, stays unevaluated (Mathematica givesSqrt[2],Pi,4 - Pi).Abs[N[...]]is numeric. - Symbolic arguments stay unevaluated (
Abs[x]). There is noComplexExpand-style assumption that symbols are real. Listable,NumericFunction,Protected. There is a packed/NDArray kernel with an exact int64 arm (and ahypotarm for real and complex data), andCompile[]lowers it at scalar and array shapes (Absof a Complex is Real).- See also:
Sign,Arg,Norm,RealAbs.