Residue: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(Created page with "{{Built-in|Residue|<nowiki>|</nowiki>}}, '''Remainder''', or '''Modulus''' is a dyadic scalar function which gives the remainder of division be...")
 
m (Text replacement - "<source" to "<syntaxhighlight")
Tags: Mobile edit Mobile web edit
 
(5 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Built-in|Residue|<nowiki>|</nowiki>}}, '''Remainder''', or '''Modulus''' is a [[dyadic]] [[scalar function]] which gives the [[wikipedia:Remainder|remainder]] of division between two real numbers. It takes the divisor as the left [[argument]], and the dividend as the right argument. Residue shares the [[glyph]] <source lang=apl inline>|</source> with the monadic arithmetic function [[Magnitude]].
{{Built-in|Residue|<nowiki>|</nowiki>}}, '''Remainder''', or '''Modulus''' is a [[dyadic]] [[scalar function]] which gives the [[wikipedia:Remainder|remainder]] of [[divide|division]] between two real numbers. It takes the divisor as the left [[argument]], and the dividend as the right argument. Residue shares the [[glyph]] <syntaxhighlight lang=apl inline>|</syntaxhighlight> with the monadic arithmetic function [[Magnitude]].


== Examples ==
== Examples ==
<source lang=apl>
<syntaxhighlight lang=apl>
       2|¯2 ¯1 0 1 2 3 4 5
       2|¯2 ¯1 0 1 2 3 4 5
0 1 0 1 0 1 0 1
0 1 0 1 0 1 0 1
Line 8: Line 8:
       3.5|5 10 14
       3.5|5 10 14
1.5 3 0
1.5 3 0
</source>
</syntaxhighlight>


== Properties ==
== Properties ==
Line 14: Line 14:
For positive x and y, the following identity holds:
For positive x and y, the following identity holds:


<source lang=apl>
<syntaxhighlight lang=apl>
       x←?⍨10 ⋄ y←?⍨10
       x←?⍨10 ⋄ y←?⍨10
       x≡(y|x)+y×⌊x÷y
       x≡(y|x)+y×⌊x÷y
1
1
</source>
</syntaxhighlight>


== Caveats ==
== Caveats ==
Line 26: Line 26:
For negative arguments, one may decide to return non-negative numbers in all cases or follow the sign of the dividend or the divisor. For complex arguments, the [[floor]] of a complex number is not mathematically defined, so allowing complex arguments does not add much of mathematical value.
For negative arguments, one may decide to return non-negative numbers in all cases or follow the sign of the dividend or the divisor. For complex arguments, the [[floor]] of a complex number is not mathematically defined, so allowing complex arguments does not add much of mathematical value.


Dyalog APL uses the expression <source lang=apl inline>Y-X×⌊Y÷X+0=X</source> as the definition of <source lang=apl inline>X|Y</source>, so that the above identity holds for all possible arguments. With this definition, zero X returns Y unchanged, and negative X returns a value between X and 0 (excluding the value X). The result for complex arguments is also defined (since Dyalog APL allows them as the argument to [[Floor]]).
Dyalog APL uses the expression <syntaxhighlight lang=apl inline>Y-X×⌊Y÷X+0=X</syntaxhighlight> as the definition of <syntaxhighlight lang=apl inline>X|Y</syntaxhighlight>, so that the above identity holds for all possible arguments. With this definition, zero X returns Y unchanged, and negative X returns a value between X and 0 (excluding the value X). The result for complex arguments is also defined (since Dyalog APL allows them as the argument to [[Floor]]).


<source lang=apl>
<syntaxhighlight lang=apl>
       5 5 ¯5 ¯5 0 0|2 ¯2 2 ¯2 2 ¯2
       5 5 ¯5 ¯5 0 0|2 ¯2 2 ¯2 2 ¯2
2 3 ¯3 ¯2 2 ¯2
2 3 ¯3 ¯2 2 ¯2
Line 35: Line 35:
       3J4{⍵-⍺×⌊⍵÷⍺+0=⍺}5J12
       3J4{⍵-⍺×⌊⍵÷⍺+0=⍺}5J12
3J1
3J1
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}


== See also ==
* [[Encode]]
== External links ==
== External links ==


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


* [http://help.dyalog.com/17.1/#Language/Primitive%20Functions/Residue.htm Dyalog]
* [https://help.dyalog.com/17.1/#Language/Primitive%20Functions/Residue.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#dyadic NuVoc]
* [https://mlochbaum.github.io/BQN/doc/arithmetic.html#additional-arithmetic BQN]
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar dyadic functions]]
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar dyadic functions]]

Latest revision as of 21:10, 10 September 2022

|

Residue (|), Remainder, or Modulus is a dyadic scalar function which gives the remainder of division between two real numbers. It takes the divisor as the left argument, and the dividend as the right argument. Residue shares the glyph | with the monadic arithmetic function Magnitude.

Examples

      2|¯2 ¯1 0 1 2 3 4 5
0 1 0 1 0 1 0 1
 
      3.5|5 10 14
1.5 3 0

Properties

For positive x and y, the following identity holds:

      x←?⍨10 ⋄ y←?⍨10
      x≡(y|x)+y×⌊x÷y
1

Caveats

The usual definition of "remainder" only holds when both arguments are positive. An implementation is free to decide what to do when the left argument is zero, or at least one of the arguments is negative or complex.

For negative arguments, one may decide to return non-negative numbers in all cases or follow the sign of the dividend or the divisor. For complex arguments, the floor of a complex number is not mathematically defined, so allowing complex arguments does not add much of mathematical value.

Dyalog APL uses the expression Y-X×⌊Y÷X+0=X as the definition of X|Y, so that the above identity holds for all possible arguments. With this definition, zero X returns Y unchanged, and negative X returns a value between X and 0 (excluding the value X). The result for complex arguments is also defined (since Dyalog APL allows them as the argument to Floor).

      5 5 ¯5 ¯5 0 0|2 ¯2 2 ¯2 2 ¯2
2 3 ¯3 ¯2 2 ¯2
      3J4|5J12
3J1
      3J4{⍵-⍺×⌊⍵÷⍺+0=⍺}5J12
3J1
Works in: Dyalog APL

See also

External links

Documentation

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
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 axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector