Subtract: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
Miraheze>Marshall
(Created page with ":''This page describes the dyadic arithmetic function. For negation of a single argument, see Negate. For subtraction of sets, see Without.'' {{Built-in|Subtract|-}},...")
 
m (Text replacement - "</source>" to "</syntaxhighlight>")
 
(12 intermediate revisions by 4 users not shown)
Line 1: Line 1:
:''This page describes the dyadic arithmetic function. For negation of a single argument, see [[Negate]]. For subtraction of sets, see [[Without]].''
:''This page describes the dyadic arithmetic function. For negation of a single argument, see [[Negate]]. For subtraction of sets, see [[Without]].''


{{Built-in|Subtract|-}}, '''Minus''', '''Subtraction''', or '''Difference''' is a [[dyadic]] [[scalar function]] which gives the arithmetic [[wikipedia:Subtraction|difference]] of its [[argument]]s. Subtract shares the [[glyph]] <source lang=apl inline>-</source> with the arithmetic negation function [[Minus]], and its result is the left argument [[plus]] the negation of the right.
{{Built-in|Subtract|-}}, '''Minus''', '''Subtraction''', or '''Difference''' is a [[dyadic]] [[scalar function]] which gives the arithmetic [[wikipedia:Subtraction|difference]] of its [[argument]]s. Subtract shares the [[glyph]] <syntaxhighlight lang=apl inline>-</syntaxhighlight> with the arithmetic function [[Negate]], and its result is the left argument [[plus]] the negation of the right.


== Examples ==
== Examples ==


<source lang=apl>
<syntaxhighlight lang=apl>
       ¯2 9 5 - 3 ¯4 6
       ¯2 9 5 - 3 ¯4 6
¯5 13 ¯1
¯5 13 ¯1
       × 3 - 2 3 4.5  ⍝ Sign of difference
       × 3 - 2 3 4.5  ⍝ Sign of difference
1 0 ¯1
1 0 ¯1
</source>
</syntaxhighlight>
The second example computes a [[wikipedia:Three-way comparison|three-way comparison]] of each pair of arguments, with a result of <source lang=apl inline>1</source> to indicate the left argument was greater, <source lang=apl inline>0</source> to indicate the arguments are [[Comparison tolerance|intolerantly]] equal, and <source lang=apl inline>¯1</source> to indicate the right argument was greater.
The second example computes a [[wikipedia:Three-way comparison|three-way comparison]] of each pair of arguments, with a result of <syntaxhighlight lang=apl inline>1</syntaxhighlight> to indicate the left argument was greater, <syntaxhighlight lang=apl inline>0</syntaxhighlight> to indicate the arguments are [[Comparison tolerance|intolerantly]] equal, and <syntaxhighlight lang=apl inline>¯1</syntaxhighlight> to indicate the right argument was greater.


== Properties ==
== Properties ==


See also [[Plus#Properties]].
See also [[Add#Properties]].


Subtraction is anti-commutative: swapping the arguments negates the result, or using [[Commute]], <source lang=apl inline>-⍨</source> {{←→}} <source lang=apl inline>--</source>.
Subtraction is anti-commutative: swapping the arguments negates the result, or using [[Commute]], <syntaxhighlight lang=apl inline>-⍨</syntaxhighlight> {{←→}} <syntaxhighlight lang=apl inline>--</syntaxhighlight>.


=== Reduction and scan ===
=== Reduction and scan ===


[[Reduction]] with Subtract gives an ''alternating sum'' of the argument array, that is, elements are alternately added and subtracted to the result. The first element, third element, and so on are added to the final result while the second, fourth, and so on are subtracted.
[[Reduction]] with Subtract gives an ''alternating sum'' of the argument array, that is, elements are alternately added and subtracted to the result. The first element, third element, and so on are added to the final result while the second, fourth, and so on are subtracted.
<source lang=apl>
<syntaxhighlight lang=apl>
       6 - 1 - 2
       6 - 1 - 2
7
7
       -/ 6 1 2
       -/ 6 1 2
7
7
</source>
</syntaxhighlight>
In the absense of rounding error we have <source lang=apl inline>-/v {{←→}} <source lang=apl inline>+/v×(⍴v)⍴1 ¯1</source> for a [[vector]] <source lang=apl inline>v</source>.
In the absense of rounding error we have <syntaxhighlight lang=apl inline>-/v</syntaxhighlight> {{←→}} <syntaxhighlight lang=apl inline>+/v×(⍴v)⍴1 ¯1</syntaxhighlight> for a [[vector]] <syntaxhighlight lang=apl inline>v</syntaxhighlight>.


An interesting property of the alternating difference is that it can be used as a [[wikipedia:Divisibility rule|divisibility test]] for division by 11, a counterpart to the better-known test for divisibility by 9. A number is divisible by 11 if and only if the sum of its digits is divisible by 11, so repeatedly taking the alternating sum of the digits determines divisibility by 11.
An interesting property of the alternating difference is that it can be used as a [[wikipedia:Divisibility rule|divisibility test]] for division by 11, a counterpart to the better-known test for divisibility by 9. A number is divisible by 11 if and only if the sum of its digits is divisible by 11, so repeatedly taking the alternating sum of the digits determines divisibility by 11.
<source lang=apl>
<syntaxhighlight lang=apl>
       11 | 946 943
       11 | 946 943
0 8
0 8
Line 38: Line 38:
       (-⌿ 10 ⊥⍣¯1 ⊢)⍣≡ 946 943
       (-⌿ 10 ⊥⍣¯1 ⊢)⍣≡ 946 943
0 2
0 2
</source>
</syntaxhighlight>


[[Scan]] with subtraction produces a prefix of an [[wikipedia:Alternating series|alternating series]]. This property is one of the reasons why scan was designed to reduce [[prefix]]es rather than [[suffix]]es of the argument array. As an example, we can see that an alternating series using the [[power]]s of two begins to converge to a third:
[[Scan]] with subtraction produces a prefix of an [[wikipedia:Alternating series|alternating series]]. This property is one of the reasons why scan was designed to reduce [[prefix]]es rather than [[suffix]]es of the argument array. As an example, we can see that an alternating series using the [[power]]s of two begins to converge to a third:
<source lang=apl inline>
<syntaxhighlight lang=apl>
       -\ ÷2*⍳6
       -\ ÷2*⍳6
0.5 0.25 0.375 0.3125 0.34375 0.328125
0.5 0.25 0.375 0.3125 0.34375 0.328125
</source>
</syntaxhighlight>
== See also ==
* [[Divide]]
* [[Without]]


== External links ==
== External links ==
Line 50: Line 53:
=== Documentation ===
=== Documentation ===


* [http://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Subtract.htm Dyalog]
* [https://help.dyalog.com/latest/index.htm#Language/Primitive%20Functions/Subtract.htm Dyalog]
* [http://microapl.com/apl_help/ch_020_020_040.htm APLX]
* [http://microapl.com/apl_help/ch_020_020_040.htm APLX]
* J [https://www.jsoftware.com/help/dictionary/d120.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/minus#dyadic NuVoc]
* J [https://www.jsoftware.com/help/dictionary/d120.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/minus#dyadic NuVoc]
* [https://mlochbaum.github.io/BQN/doc/arithmetic.html#basic-arithmetic BQN]
{{APL built-ins}}[[Category:Primitive functions]][[Category:Scalar dyadic functions]]

Latest revision as of 10:58, 11 September 2022

This page describes the dyadic arithmetic function. For negation of a single argument, see Negate. For subtraction of sets, see Without.
-

Subtract (-), Minus, Subtraction, or Difference is a dyadic scalar function which gives the arithmetic difference of its arguments. Subtract shares the glyph - with the arithmetic function Negate, and its result is the left argument plus the negation of the right.

Examples

      ¯2 9 5 - 3 ¯4 6
¯5 13 ¯1
      × 3 - 2 3 4.5  ⍝ Sign of difference
1 0 ¯1

The second example computes a three-way comparison of each pair of arguments, with a result of 1 to indicate the left argument was greater, 0 to indicate the arguments are intolerantly equal, and ¯1 to indicate the right argument was greater.

Properties

See also Add#Properties.

Subtraction is anti-commutative: swapping the arguments negates the result, or using Commute, -⍨ --.

Reduction and scan

Reduction with Subtract gives an alternating sum of the argument array, that is, elements are alternately added and subtracted to the result. The first element, third element, and so on are added to the final result while the second, fourth, and so on are subtracted.

      6 - 1 - 2
7
      -/ 6 1 2
7

In the absense of rounding error we have -/v +/v×(⍴v)⍴1 ¯1 for a vector v.

An interesting property of the alternating difference is that it can be used as a divisibility test for division by 11, a counterpart to the better-known test for divisibility by 9. A number is divisible by 11 if and only if the sum of its digits is divisible by 11, so repeatedly taking the alternating sum of the digits determines divisibility by 11.

      11 | 946 943
0 8
      -⌿ 10 (⊥⍣¯1) 946 943
11 8
      (-⌿ 10 ⊥⍣¯1 ⊢)⍣≡ 946 943
0 2

Scan with subtraction produces a prefix of an alternating series. This property is one of the reasons why scan was designed to reduce prefixes rather than suffixes of the argument array. As an example, we can see that an alternating series using the powers of two begins to converge to a third:

      -\ ÷2*⍳6
0.5 0.25 0.375 0.3125 0.34375 0.328125

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