Complex number: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
No edit summary
m (→‎Support: Uiua)
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
A '''complex number''' type is a [[number|numeric type]] which represents, usually with some limited precision, the [[wikipedia:Complex number|complex numbers]]. Complex number support is defined as an optional facility in the XAPL standard ([[ISO/IEC 13751:2001]]), and complex numbers are available in many APLs. Usually these numbers are written with a syntax such as <source lang=apl inline>5j2</source> or <source lang=apl inline>5J2</source> for the complex number with real part 5 and imaginary part 2.
A '''complex number''' type is a [[number|numeric type]] which represents, usually with some limited precision, the [[wikipedia:Complex number|complex numbers]]. Complex number support is defined as an optional facility in the XAPL standard ([[ISO/IEC 13751:2001]]), and complex numbers are available in many APLs. Usually these numbers are written with a syntax such as <syntaxhighlight lang=apl inline>5j2</syntaxhighlight> or <syntaxhighlight lang=apl inline>5J2</syntaxhighlight> for the complex number with real part 5 and imaginary part 2.


== Examples ==
== Examples ==


Complex numbers are usually written with a "J" joining the real and imaginary parts. Complex numbers are added component-wise.
Complex numbers are usually written with a "J" joining the real and imaginary parts. Complex numbers are added component-wise.
<source lang=apl>
<syntaxhighlight lang=apl>
       1J¯2 + 3J4
       1J¯2 + 3J4
4J2
4J2
</source>
</syntaxhighlight>
The [[Circle function]] can be used to split a complex number into its components: a left argument of 9 gets the real part and a left argument of 11 gets the imaginary part. With the [[Rank operator]], Circle can be used to convert a [[shape]] <source lang=apl inline>s</source> complex array to a shape <source lang=apl inline>s,2</source> real array where each row contains the components of one complex number.
The [[Circle function]] can be used to split a complex number into its components: a left argument of 9 gets the real part and a left argument of 11 gets the imaginary part. With the [[Rank operator]], Circle can be used to convert a [[shape]] <syntaxhighlight lang=apl inline>s</syntaxhighlight> complex array to a shape <syntaxhighlight lang=apl inline>s,2</syntaxhighlight> real array where each row contains the components of one complex number.
<source lang=apl>
<syntaxhighlight lang=apl>
       9 11 ○ 12J3
       9 11 ○ 12J3
12 3
12 3
Line 16: Line 16:
4 3
4 3
2 1
2 1
</source>
</syntaxhighlight>
Operations on real numbers can sometimes yield complex numbers. Famously, the square root of minus one is the imaginary unit:
Operations on real numbers can sometimes yield complex numbers. Famously, the square root of minus one is the imaginary unit:
<source lang=apl>
<syntaxhighlight lang=apl>
       ¯1*÷2
       ¯1*÷2
0J1
0J1
</source>
</syntaxhighlight>


== Implementation ==
== Implementation ==
Line 29: Line 29:
== Support ==
== Support ==


The following languages support complex numbers as a built in [[numeric type]].
The following languages support complex numbers as a built-in [[numeric type]].


* [[APL2]]
* [[APL2]]
Line 38: Line 38:
* [[GNU APL]]
* [[GNU APL]]
* [[ngn/apl]]
* [[ngn/apl]]
* [[Uiua]]


{{APL features}}[[Category:Numbers]]
{{APL features}}[[Category:Numbers]]

Latest revision as of 21:10, 28 August 2024

A complex number type is a numeric type which represents, usually with some limited precision, the complex numbers. Complex number support is defined as an optional facility in the XAPL standard (ISO/IEC 13751:2001), and complex numbers are available in many APLs. Usually these numbers are written with a syntax such as 5j2 or 5J2 for the complex number with real part 5 and imaginary part 2.

Examples

Complex numbers are usually written with a "J" joining the real and imaginary parts. Complex numbers are added component-wise.

      1J¯2 + 3J4
4J2

The Circle function can be used to split a complex number into its components: a left argument of 9 gets the real part and a left argument of 11 gets the imaginary part. With the Rank operator, Circle can be used to convert a shape s complex array to a shape s,2 real array where each row contains the components of one complex number.

      9 11 ○ 12J3
12 3
      9 11∘○⍤0 ⊢6J5 4J3 2J1
6 5
4 3
2 1

Operations on real numbers can sometimes yield complex numbers. Famously, the square root of minus one is the imaginary unit:

      ¯1*÷2
0J1

Implementation

Complex numbers are almost always defined to be a pair of 64-bit floating point numbers (IEEE 754). If 64-bit floats are used for real numbers as well then this definition makes complex numbers a superset of the real numbers as represented. This means that the language semantics can be defined purely in terms of complex numbers. However, for performance and precision reasons, operations are implemented both on real numbers and complex numbers. If all the numbers in an array have imaginary part zero, then it is represented as a real array (or a smaller type such as integer), and primitives applied to it will use code written specifically for real numbers.

Support

The following languages support complex numbers as a built-in numeric type.


APL features [edit]
Built-ins Primitives (functions, operators) ∙ Quad name
Array model ShapeRankDepthBoundIndex (Indexing) ∙ AxisRavelRavel orderElementScalarVectorMatrixSimple scalarSimple arrayNested arrayCellMajor cellSubarrayEmpty arrayPrototype
Data types Number (Boolean, Complex number) ∙ Character (String) ∙ BoxNamespaceFunction array
Concepts and paradigms Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity elementComplex floorArray ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ GlyphLeading axis theoryMajor cell searchFirst-class function
Errors LIMIT ERRORRANK ERRORSYNTAX ERRORDOMAIN ERRORLENGTH ERRORINDEX ERRORVALUE ERROREVOLUTION ERROR