Complex (function): Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(History)
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Built-in|Complex|⌾}} is a [[dyadic]] [[scalar function]] which combines its arguments into a [[complex number]] having with the left argument as real part and the right argument as imaginary part. It was added to [[J]] as <source lang=j inline>j.</source> together with initial support for complex numbers, and was adopted in [[Extended Dyalog APL]] using the [[glyph]] <source lang=apl inline>⌾</source> because it was seen as a type of numerical [[Beside|composition]] (<source lang=apl inline>∘</source>) for the [[Circle|circular]] (<source lang=apl inline>○</source>) domain. Complex shares its glyph with [[Imaginary]] which is equivalent to applying Complex with a left argument of 0.
{{Built-in|Complex|⌾}} is a [[dyadic]] [[scalar function]] which combines its arguments into a [[complex number]] having with the left argument as real part and the right argument as imaginary part. It was added to [[J]] as <syntaxhighlight lang=j inline>j.</syntaxhighlight> together with initial support for complex numbers, and was adopted in [[Extended Dyalog APL]] using the [[glyph]] <syntaxhighlight lang=apl inline>⌾</syntaxhighlight> because it was seen as a type of numerical [[Beside|composition]] (<syntaxhighlight lang=apl inline>∘</syntaxhighlight>) for the [[Circle|circular]] (<syntaxhighlight lang=apl inline>○</syntaxhighlight>) domain. Complex shares its glyph with [[Imaginary]] which is equivalent to applying Complex with a left argument of 0.


== History ==
== History ==


The question of whether to include this function is one of [[Roger Hui]]'s 50 exercises in APL language design<ref>[[Roger Hui]]. Some Exercises in APL Language Design. Jsoftware. 2016.</ref>:
The question of whether to include this function is one of [[Roger Hui]]'s 50 exercises in APL language design:<ref>[[Roger Hui]]. [https://www.jsoftware.com/papers/APLDesignExercises.htm Some Exercises in APL Language Design]. §8 Imaginary/Complex and sample answer. Jsoftware. 2016.</ref>
<blockquote>Complex numbers can be constructed as ordered pairs of real numbers, similar to how integers can be constructed as ordered pairs of natural numbers and rational numbers as ordered pairs of integers. For complex numbers, <source lang=j inline>j.</source> plays the same role as <source lang=apl inline>-</source> for integers and <source lang=apl inline>÷</source> for rational numbers. </blockquote>
<blockquote>Complex numbers can be constructed as ordered pairs of real numbers, similar to how integers can be constructed as ordered pairs of natural numbers and rational numbers as ordered pairs of integers. For complex numbers, <syntaxhighlight lang=j inline>j.</syntaxhighlight> plays the same role as <syntaxhighlight lang=apl inline>-</syntaxhighlight> for integers and <syntaxhighlight lang=apl inline>÷</syntaxhighlight> for rational numbers. </blockquote>
Hui goes on to quote [[Adám Brudzewsky]]:<ref>[[Roger Hui|Hui, Roger]]. [https://forums.dyalog.com/viewtopic.php?f=30&t=1786&p=7020&hilit=complex+imaginary#p7020 ⊕ and ⊗]. Dyalog Forums. 13 Jul 2021.</ref>
In a later blog post, Hui quoted [[Adám Brudzewsky]]:<ref>[[Roger Hui]]. [https://forums.dyalog.com/viewtopic.php?f=30&t=1786&p=7020&hilit=complex+imaginary#p7020 ⊕ and ⊗]. Dyalog Forums. 2021-06-13.</ref>
<blockquote>(…) I’d probably go for <source lang=apl inline>⊕</source> because:
<blockquote>(…) I’d probably go for <syntaxhighlight lang=apl inline>⊕</syntaxhighlight> because:


* it is more clearly related to <source lang=apl inline>○</source> (for which the monadic form is also a simple multiplication)
* it is more clearly related to <syntaxhighlight lang=apl inline>○</syntaxhighlight> (for which the monadic form is also a simple multiplication)
* it consists of two overstruck basic APL symbols
* it consists of two overstruck basic APL symbols
* it looks more harmonic (in my eyes)
* it looks more harmonic (in my eyes)


Oh, and Euler’s formula turns out beautiful (to my taste) with it:
Oh, and Euler’s formula turns out beautiful (to my taste) with it:
<source lang=apl>0 = 1+*⊕○1
<syntaxhighlight lang=apl>0 = 1+*⊕○1
0 = 1+*○⊕1</source></blockquote>
0 = 1+*○⊕1</syntaxhighlight></blockquote>
Hui continues:
Hui continues:
<blockquote>
<blockquote>
On further reflection, I like the <source lang=apl inline>⊕</source> symbol for <source lang=apl inline>j←{⍺←0 ⋄ ⍺+0j1×⍵}</source>. If there are existing interpretation of it this one should predominate, if the function is as important as I think it is. (Peer to <source lang=apl inline>-</source> and <source lang=apl inline>÷</source>, etc.)
On further reflection, I like the <syntaxhighlight lang=apl inline>⊕</syntaxhighlight> symbol for <syntaxhighlight lang=apl inline>j←{⍺←0 ⋄ ⍺+0j1×⍵}</syntaxhighlight>. If there are existing interpretation of it this one should predominate, if the function is as important as I think it is. (Peer to <syntaxhighlight lang=apl inline>-</syntaxhighlight> and <syntaxhighlight lang=apl inline>÷</syntaxhighlight>, etc.)


Moreover, complex numbers are commonly represented by magnitude and phase. So if you have a magnitude and phase, how do you get the number so represented? Why <source lang=apl inline>k←{⍺←1 ⋄ ⍺×*0j1×⍵}</source> (or <source lang=apl inline>{⍺←1 ⋄ ⍺×*⊕⍵}</source>) of course. And what symbol would you use? <source lang=apl inline>⊗</source> (Monadic <source lang=apl inline>⊗</source>, with a default magnitude of 1, gets you a point on the unit circle.)
Moreover, complex numbers are commonly represented by magnitude and phase. So if you have a magnitude and phase, how do you get the number so represented? Why <syntaxhighlight lang=apl inline>k←{⍺←1 ⋄ ⍺×*0j1×⍵}</syntaxhighlight> (or <syntaxhighlight lang=apl inline>{⍺←1 ⋄ ⍺×*⊕⍵}</syntaxhighlight>) of course. And what symbol would you use? <syntaxhighlight lang=apl inline>⊗</syntaxhighlight> (Monadic <syntaxhighlight lang=apl inline>⊗</syntaxhighlight>, with a default magnitude of 1, gets you a point on the unit circle.)
</blockquote>
</blockquote>
== Examples ==
== Examples ==


[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn//1Hf1EdtEw6tN1KwVDBVeNSzT8FY4dB6EwWz//8B Try it online!]<source lang=apl>
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn//1Hf1EdtEw6tN1KwVDBVeNSzT8FY4dB6EwWz//8B Try it online!]<syntaxhighlight lang=apl>
       ¯2 9 5 ⌾ 3 ¯4 6
       ¯2 9 5 ⌾ 3 ¯4 6
¯2J3 9J¯4 5J6
¯2J3 9J¯4 5J6
</source>{{Works in|[[Extended Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Extended Dyalog APL]]}}


=== Documentation ===
=== Documentation ===

Latest revision as of 22:25, 10 September 2022

Complex () is a dyadic scalar function which combines its arguments into a complex number having with the left argument as real part and the right argument as imaginary part. It was added to J as j. together with initial support for complex numbers, and was adopted in Extended Dyalog APL using the glyph because it was seen as a type of numerical composition () for the circular () domain. Complex shares its glyph with Imaginary which is equivalent to applying Complex with a left argument of 0.

History

The question of whether to include this function is one of Roger Hui's 50 exercises in APL language design:[1]

Complex numbers can be constructed as ordered pairs of real numbers, similar to how integers can be constructed as ordered pairs of natural numbers and rational numbers as ordered pairs of integers. For complex numbers, j. plays the same role as - for integers and ÷ for rational numbers.

In a later blog post, Hui quoted Adám Brudzewsky:[2]

(…) I’d probably go for because:

  • it is more clearly related to (for which the monadic form is also a simple multiplication)
  • it consists of two overstruck basic APL symbols
  • it looks more harmonic (in my eyes)

Oh, and Euler’s formula turns out beautiful (to my taste) with it:

0 = 1+*1
0 = 1+*○1

Hui continues:

On further reflection, I like the symbol for j{0 +0j1×}. If there are existing interpretation of it this one should predominate, if the function is as important as I think it is. (Peer to - and ÷, etc.)

Moreover, complex numbers are commonly represented by magnitude and phase. So if you have a magnitude and phase, how do you get the number so represented? Why k{1 ×*0j1×} (or {1 ×*}) of course. And what symbol would you use? (Monadic , with a default magnitude of 1, gets you a point on the unit circle.)

Examples

Try it online!

      ¯2 9 5  3 ¯4 6
¯2J3 9J¯4 5J6

Documentation

References

  1. Roger Hui. Some Exercises in APL Language Design. §8 Imaginary/Complex and sample answer. Jsoftware. 2016.
  2. Roger Hui. ⊕ and ⊗. Dyalog Forums. 2021-06-13.
APL built-ins [edit]
Primitive functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace