Add: 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|Add|+}}, '''Plus''', '''Addition''', or '''Sum''', is a [[dyadic]] [[scalar function]] that [[wikipedia:Addition|adds]] numbers together. As a basic arithmetic operation, Add is subject to the language's [[number]] specification. Add shares the glyph <source lang=apl inline>+</source> with the [[monadic]] function [[Conjugate]], and is closely related to [[Subtract]] (<source lang=apl inline>-</source>).
{{Built-in|Add|+}}, '''Plus''', '''Addition''', or '''Sum''', is a [[dyadic]] [[scalar function]] that [[wikipedia:Addition|adds]] numbers together. As a basic arithmetic operation, Add is subject to the language's [[number]] specification. Add shares the glyph <syntaxhighlight lang=apl inline>+</syntaxhighlight> with the [[monadic]] function [[Conjugate]], and is closely related to [[Subtract]] (<syntaxhighlight lang=apl inline>-</syntaxhighlight>).


== Examples ==
== Examples ==


<source lang=apl>
<syntaxhighlight lang=apl>
       1 2 3 + 2 3 4
       1 2 3 + 2 3 4
3 5 7
3 5 7
       0.1 + 3 4 5
       0.1 + 3 4 5
3.1 4.1 5.1
3.1 4.1 5.1
</source>
</syntaxhighlight>


== Properties ==
== Properties ==
Line 14: Line 14:
Addition is [[wikipedia:Commutative property|commutative]] in almost all number systems. It is [[wikipedia:Associative property|associative]] when performed with no rounding, but is unlikely to be associative when rounded to an inexact precision (see [[#Floating point error|Floating point error]]).
Addition is [[wikipedia:Commutative property|commutative]] in almost all number systems. It is [[wikipedia:Associative property|associative]] when performed with no rounding, but is unlikely to be associative when rounded to an inexact precision (see [[#Floating point error|Floating point error]]).


The [[identity element]] for Add is <source lang=apl inline>0</source>. The [[inverse]] of the function <source lang=apl inline>n∘+</source> or the equivalent function <source lang=apl inline>+∘n</source> is <source lang=apl inline>-∘n</source>. Using [[Commute]] (<source lang=apl inline>⍨</source>), we can write <source lang=apl inline>+⍣¯1</source> {{←→}} <source lang=apl inline>-⍨</source>.
The [[identity element]] for Add is <syntaxhighlight lang=apl inline>0</syntaxhighlight>. The [[inverse]] of the function <syntaxhighlight lang=apl inline>n∘+</syntaxhighlight> or the equivalent function <syntaxhighlight lang=apl inline>+∘n</syntaxhighlight> is <syntaxhighlight lang=apl inline>-∘n</syntaxhighlight>. Using [[Commute]] (<syntaxhighlight lang=apl inline>⍨</syntaxhighlight>), we can write <syntaxhighlight lang=apl inline>+⍣¯1</syntaxhighlight> {{←→}} <syntaxhighlight lang=apl inline>-⍨</syntaxhighlight>.


Since adding a number to itself is equivalent to doubling that number, we can express the ''double'' function as <source lang=apl inline>+⍨</source>.
Since adding a number to itself is equivalent to doubling that number, we can express the ''double'' function as <syntaxhighlight lang=apl inline>+⍨</syntaxhighlight>.


=== Reduction ===
=== Reduction ===
Line 33: Line 33:


When using a limited-precision format such as 8-byte floats, the results of addition are rounded in order to fit into the result type. This rounding can cause the results of adding several numbers to be different depending on the order in which they are added, that is, addition fails to be associative:
When using a limited-precision format such as 8-byte floats, the results of addition are rounded in order to fit into the result type. This rounding can cause the results of adding several numbers to be different depending on the order in which they are added, that is, addition fails to be associative:
<source lang=apl>
<syntaxhighlight lang=apl>
       1e20 + ¯1e20 + 1
       1e20 + ¯1e20 + 1
0
0
Line 40: Line 40:
       (1e20 + ¯1e20) + 1      ⍝ Not equivalent
       (1e20 + ¯1e20) + 1      ⍝ Not equivalent
1
1
</source>
</syntaxhighlight>


Addition of floating-point numbers may also be subject to [[overflow]], resulting in a [[DOMAIN ERROR]] or an [[infinite]] result.
Addition of floating-point numbers may also be subject to [[overflow]], resulting in a [[DOMAIN ERROR]] or an [[infinite]] result.
Line 56: Line 56:
* [http://microapl.com/apl_help/ch_020_020_020.htm APLX]
* [http://microapl.com/apl_help/ch_020_020_020.htm APLX]
* [https://www.jsoftware.com/help/dictionary/d100.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/plus#dyadic J NuVoc]
* [https://www.jsoftware.com/help/dictionary/d100.htm J Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/plus#dyadic J NuVoc]
* [https://mlochbaum.github.io/BQN/doc/arithmetic.html#basic-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 22:11, 10 September 2022

+

Add (+), Plus, Addition, or Sum, is a dyadic scalar function that adds numbers together. As a basic arithmetic operation, Add is subject to the language's number specification. Add shares the glyph + with the monadic function Conjugate, and is closely related to Subtract (-).

Examples

      1 2 3 + 2 3 4
3 5 7
      0.1 + 3 4 5
3.1 4.1 5.1

Properties

Addition is commutative in almost all number systems. It is associative when performed with no rounding, but is unlikely to be associative when rounded to an inexact precision (see Floating point error).

The identity element for Add is 0. The inverse of the function n∘+ or the equivalent function +∘n is -∘n. Using Commute (), we can write +⍣¯1 -⍨.

Since adding a number to itself is equivalent to doubling that number, we can express the double function as +⍨.

Reduction

Reduction with Add gives the sum of the whole list.

Scalar mapping

In mathematics, addition of two identical structures almost always follows the same rules as in APL: it maps over the structures element-wise. This is a fundamental property of a (finite-dimensional) vector space, in which addition of two vectors is equivalent to adding the coefficients of basis vectors one by one. This property likely inspired APL's definition of a scalar function.

Addition of complex and hypercomplex numbers can also be considered an element-wise operation, since each of these types of numbers forms a vector space over the reals. Addition of scalars is always performed within a single domain: mixed-type addition such as adding a real to a complex number treats the real number as complex with imaginary part zero.

Addition in mathematics does not exhibit scalar extension: there is usually no way to add values of different types. The Multiply function on vector spaces does extend scalars, in that a scalar element of a field can be multiplied by a vector over that field.

Floating point error

When using a limited-precision format such as 8-byte floats, the results of addition are rounded in order to fit into the result type. This rounding can cause the results of adding several numbers to be different depending on the order in which they are added, that is, addition fails to be associative:

      1e20 + ¯1e20 + 1
0
      1e20 + (¯1e20 + 1)      ⍝ Equivalent
0
      (1e20 + ¯1e20) + 1      ⍝ Not equivalent
1

Addition of floating-point numbers may also be subject to overflow, resulting in a DOMAIN ERROR or an infinite result.

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