﻿ + – Algosim documentation
Algosim documentation: +

# + (plus)

The plus sign is used for several different operations on various kinds of operands and has two distinct syntactic forms:

• As a binary operator, `+` is typically used for mathematical addition or string concatenation.

• As a unary operator, `+` typically has no effect.

## Syntax

• `a + b`

• applicable to several different kinds of operands `a` and `b`

• `+a`

• `a` is any object

## Description and examples

If `a` and `b` are numbers (integers, rational numbers, real number, or complex numbers), `a + b` is the mathematical sum of `a` and `b`. The type of the sum is the most specific type possible.

Some examples:

• If `a` and `b` are both integers, so is `a + b` if no integer overflow occurs; in that case, the result is a real number.

• If `a` is an integer and `b` a rational number, `a + b` is a rational number.

• If `a` is an integer and `b` a real number, `a + b` is a real number.

• If `a` is an integer and `b` a complex number, `a + b` is a complex number.

• If `a` is a real number and `b` a complex number, `a + b` is a complex number.

• If `a` is a rational number and `b` a complex number, `a + b` is a complex number.

`π + e`
`5.85987448205`
`3/7 + 2/5`
`29/35	(=0.828571428571)`

If `u` and `v` are vectors, `u + v` is the vector sum of `u` and `v`. `u + v` is a complex vector if at least one of `u` and `v` is complex; otherwise, `u + v` is a real vector.

`u` and `v` must be of the same dimension.

`u ≔ ❨2, 1, 3❩; v ≔ ❨0, 2, 1❩; u + v`
``` ⎛2⎞
e⎜3⎟
⎝4⎠
```

If `A` and `B` are matrices, `A + B` is the matrix sum of `A` and `B`. `A + B` is a complex matrix if at least one of `A` and `B` is complex; otherwise, `A + B` is a real matrix.

`A` and `B` must be of the same size.

`A ≔ ❨❨1, 3❩, ❨−i, 1❩❩; B ≔ ❨❨2, 1❩, ❨0, −1❩❩; A + B`
```⎛ 3   4⎞
⎝−i   0⎠
```

#### Adding a scalar to a vector

If `v` is a vector and `x` a number, then `v + x` is the vector obtained from `v` by adding `x` to each component. If either `v` or `x` is complex, `v + x` is a complex vector. Otherwise, `v + x` is a real vector.

`❨1, 5, 2❩ + i`
``` ⎛1 + i⎞
e⎜5 + i⎟
⎝2 + i⎠
```

#### Adding a scalar to a matrix

If `A` is a matrix and `x` a number, then `A + x` is the matrix obtained from `A` by adding `x` to each entry. If either `A` or `x` is complex, `A + x` is a complex matrix. Otherwise, `A + x` is a real matrix.

`ZeroMatrix(4) + 1`
```⎛1  1  1  1⎞
⎜1  1  1  1⎟
⎜1  1  1  1⎟
⎝1  1  1  1⎠
```

#### Concatenating two strings

If `a` and `b` are strings, `a + b` is the concatenation of `a + b`.

`MessageBox("Welcome, " + InputBox("Please enter your name:") + "!")`
`OK`

#### Adding an object to a string

If `s` is a string and `X` any object, then `s + X` obtains a textual representation of `X`, concatenates `s` and this textual representation, and returns the result.

`"car" + 2`
`car2`

#### Superposing two sounds by samplewise addition

If `s` and `t` are two sounds, `s + t` is the superposed sound obtained by samplewise addition.

`SineTone(100, 0.1, 1) + SineTone(400, 0.1, 1)`
`A 1-second 32-bit 48000 Hz 1-channel sound.`

### Unary operator

Typically, the unary plus operator `+` has no effect. It is typically used to highlight to the reader of the expression that a number is non-negative, that a quantity is included in an expression with its original sign, or to achieve visual symmetry when used together with unary minus signs.

The unary operator `+` maps to the identity function defined as `x ↦ x`, thus returning its argument unchanged. However, the inclusion of a unary plus might affect the way in which an expression is evaluated under special circumstances.

In Algosim, the symbols for built-in kernel functions can be used as if they were objects representing the kernel functions (in the same way user-defined functions really are objects that can be stored in variables).

For example,

`sin`
`sin`
`type(ans)`
`kernel function`

or

`s ≔ sin`
`sin`
`s(π/2)`
`1`

But in some cases this automatic interpretation of a name of a built-in kernel function as a kernel function object doesn’t work. Here is one example:

`type(sin)`
```failure
Unknown identifier "sin".
Call stack: type
```

This is because the `type` function is special: it tries to optimise the evaluation of the expression by not actually fetching its argument using the default, automated, mechanism, but by using it unevaluated as a reference into the internal variable database. But since `sin` isn’t a true variable, this fails.

However, by writing `+sin`, this optimisation fails, and standard evaluation gives us the desired result:

`type(+sin)`
`kernel function`

## Notes

• The binary operator `+` is mapped to the `add` function.

• The unary operator `+` is mapped to the `identity` function.