Magnitude: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com")
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Built-in|Magnitude|<nowiki>|</nowiki>}}, or '''Absolute Value''', is a [[monadic]] [[scalar function]] which gives the [[wikipedia:Absolute value|absolute value]] of a real or [[complex]] number. Magnitude shares the [[glyph]] <source lang=apl inline>|</source> with the dyadic arithmetic function [[Residue]].
{{Built-in|Magnitude|<nowiki>|</nowiki>}}, or '''Absolute Value''', is a [[monadic]] [[scalar function]] which gives the [[wikipedia:Absolute value|absolute value]] of a real or [[complex]] number. Magnitude shares the [[glyph]] <syntaxhighlight lang=apl inline>|</syntaxhighlight> with the dyadic arithmetic function [[Residue]].


== Examples ==
== Examples ==
<source lang=apl>
<syntaxhighlight lang=apl>
       |0 1 2 ¯1 ¯2
       |0 1 2 ¯1 ¯2
0 1 2 1 2
0 1 2 1 2
Line 8: Line 8:
       |0J2 ¯3J¯4
       |0J2 ¯3J¯4
2 5
2 5
</source>
</syntaxhighlight>


== Properties ==
== Properties ==
Line 16: Line 16:
For real numbers, the magnitude equals the original number [[times]] (or [[Divide|divided]] by, for non-zero numbers) its [[Signum|sign]].
For real numbers, the magnitude equals the original number [[times]] (or [[Divide|divided]] by, for non-zero numbers) its [[Signum|sign]].


<source lang=apl>
<syntaxhighlight lang=apl>
       v←0 1E¯100 20 1E300 ¯1E¯100 ¯20 ¯1E300
       v←0 1E¯100 20 1E300 ¯1E¯100 ¯20 ¯1E300
       (|v)≡v××v
       (|v)≡v××v
Line 22: Line 22:
       (|v)=v÷×v
       (|v)=v÷×v
0 1 1 1 1 1 1
0 1 1 1 1 1 1
</source>
</syntaxhighlight>


For complex numbers, the magnitude is defined as the Euclidean distance from the number 0 on the [[wikipedia:Complex plane|complex plane]].
For complex numbers, the magnitude is defined as the Euclidean distance from the number 0 on the [[wikipedia:Complex plane|complex plane]].


<source lang=apl>
<syntaxhighlight lang=apl>
       Dist←{0.5*⍨+.×⍨9 11○⍵} ⍝ Square root of square sum of real and imaginary parts
       Dist←{0.5*⍨+.×⍨9 11○⍵} ⍝ Square root of square sum of real and imaginary parts
       Dist¨ 0 1J2 ¯3J4
       Dist¨ 0 1J2 ¯3J4
Line 32: Line 32:
       |0 1J2 ¯3J4
       |0 1J2 ¯3J4
0 2.236067977 5
0 2.236067977 5
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}


Any real or complex number is equal to the [[Times|product]] of its [[signum]] and magnitude.
Any real or complex number is equal to the [[Times|product]] of its [[signum]] and magnitude.


<source lang=apl>
<syntaxhighlight lang=apl>
       (⊢ ≡ ××|) 0 1 1E¯300 ¯2.5 0J3.5 ¯3J¯4
       (⊢ ≡ ××|) 0 1 1E¯300 ¯2.5 0J3.5 ¯3J¯4
1
1
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}


== See also ==
== See also ==
Line 52: Line 52:
* [https://help.dyalog.com/17.1/#Language/Primitive%20Functions/Magnitude.htm Dyalog]
* [https://help.dyalog.com/17.1/#Language/Primitive%20Functions/Magnitude.htm Dyalog]
* [https://www.jsoftware.com/help/dictionary/d230.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/bar NuVoc]
* [https://www.jsoftware.com/help/dictionary/d230.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/bar NuVoc]
* [https://mlochbaum.github.io/BQN/doc/arithmetic.html#additional-arithmetic BQN]
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar monadic functions]]
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar monadic functions]]

Latest revision as of 22:10, 10 September 2022

|

Magnitude (|), or Absolute Value, is a monadic scalar function which gives the absolute value of a real or complex number. Magnitude shares the glyph | with the dyadic arithmetic function Residue.

Examples

      |0 1 2 ¯1 ¯2
0 1 2 1 2
 
      |0J2 ¯3J¯4
2 5

Properties

The magnitude of any number is a non-negative real number.

For real numbers, the magnitude equals the original number times (or divided by, for non-zero numbers) its sign.

      v←0 1E¯100 20 1E300 ¯1E¯100 ¯20 ¯1E300
      (|v)≡v××v
1
      (|v)=v÷×v
0 1 1 1 1 1 1

For complex numbers, the magnitude is defined as the Euclidean distance from the number 0 on the complex plane.

      Dist←{0.5*⍨+.×⍨9 11○⍵} ⍝ Square root of square sum of real and imaginary parts
      Dist¨ 0 1J2 ¯3J4
0 2.236067977 5
      |0 1J2 ¯3J4
0 2.236067977 5
Works in: Dyalog APL

Any real or complex number is equal to the product of its signum and magnitude.

      (⊢ ≡ ××|) 0 1 1E¯300 ¯2.5 0J3.5 ¯3J¯4
1
Works in: Dyalog APL

See also

External links

Documentation

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector