Arithmetic Fuctions

Arithmetic Fuctions#

The arithmetic functions are all scalar functions; they penetrate into nested arrays all the way down to the scalars. The standard functions +, -, ×, ÷ all act as you might expect:

8+9
17
4 3 2-3 6 4
1 ¯3 ¯2
length20.5
height13.1
arealength×height
area
268.55
(4 2 3)÷(1 1 5)(1 5 1)(5 1 1)
┌───────┬───────┬───────┐ │4 4 0.8│2 0.4 2│0.6 3 3│ └───────┴───────┴───────┘

All of those functions can also be applied monadically. For example -x is negative x (or 0-x). ÷x is the reciprocal of x (or 1÷x)

-3 ¯1 4 ¯1 5
¯3 1 ¯4 1 ¯5
÷3
0.333333

Monadic + and × are designed for complex numbers. + gives the complex conjugate. × gives a complex number with magnitude 1 (unless it is applied to 0) in the direction of its argument. For real numbers, + has no effect, and × gives the sign of its argument.

+1j2 0j¯1 4 3j3
1J¯2 0J1 4 3J¯3
×10 3j3 4j4 8j¯22 ¯3 0
1 0.707107J0.707107 0.707107J0.707107 0.341743J¯0.939793 ¯1 0

* is the power function. X*Y is X to the power of Y

1.3×1.3×1.3
2.197
1.3*3
2.197
9*0.5  ⍝ square root
3
20*¯1  ⍝ reciprocal
0.05

Monadically, *x is e to the power of x.

e2.71828182845
(e*3.2) (*3.2)
24.5325 24.5325

A close relative of * is which is logarithm. a⍟b is the base-a logarithm of b.

264
6

As a monad, ⍟x is the natural logarithm of x (or e⍟x).

1
0
(*23.14)
23.14

a⌈b, gives the maximum of a and b. While ⌈x is the ceiling of x, that is, the smallest integer larger than x.

4 4 2 ¯4 0 ¯10  0
4 4 2 0 0 0
1.5 3 ¯0.3 5.0001 ¯2.7
2 3 0 6 ¯2

After seeing what does, it is not hard to guess what does. It is of course minimum and floor

3 2 10¯2 5 5
¯2 2 5
1.4 ¯6.02 5
1 ¯7 5

Numbers can be rounded to the nearest integer with ⌊0.5+. You can see it highlighted below because it is an idiom, recognised and optimised by the interpreter.

0.5+3.3 3.7 6.5 1
3 4 7 1

Monadically, |x gives the magnitude of x. For real numbers, this is the absolute value.

| 2 ¯3 4 0 ¯10.5
2 3 4 0 10.5
| 3j4
5

Dyadically, | is residue. x|y is the remainder when y is divided by x.

3|0 1 2 3 4 5 6 7
0 1 2 0 1 2 0 1
1|2 3.4 0.31 5.2   ⍝ The fractional parts of each number
0 0.4 0.31 0.2