Chapter 20: Scalar Numerical Functions

In this chapter we look at built-in scalar functions for computing numbers from numbers. This chapter is a straight catalog of functions, with links to the sections as follows:

Ceiling Conjugate cos cos-1 cosh cosh-1
Decrement divide Double Exponential Factorial Floor
GCD Halve Increment LCM Logarithm Log, Natural
Magnitude Minus multiply Negate OutOf PiTimes
Plus power Pythagorean Reciprocal Residue Root
Signum sin sin-1 sinh sinh-1 Square
SquareRoot tan tan-1 tanh tanh-1  

20.1 Numbers from Numbers

20.1.1 Plus and Conjugate

Dyadic + is arithmetic addition.

2 + 2 3j4 + 5j4 2r3 + 1r6
4
8j8
5r6

Monadic + is "Conjugate". For a real number y, the conjugate is y. For a complex number xjy (that is, x + 0jy), the conjugate is x - 0jy.

+ 2 + 3j4
2
3j_4

20.1.2 Minus and Negate

Dyadic - is arithmetic subtraction.

2 - 2 3 - 0j4 2r3 - 1r6
0
3j_4
1r2

Monadic - is "Negate".

- 2 - 3j4
_2
_3j_4

20.1.3 Increment and Decrement

Monadic >: is called "Increment". It adds 1 to its argument.

>: 2 >: 2.5 >: 2r3 >: 2j3
3
3.5
5r3
3j3

Monadic <: is called "Decrement". It subtracts 1 from its argument.

<: 3 <: 2.5 <: 2r3 <: 2j3
2
1.5
_1r3
1j3

20.1.4 Times and Signum

Dyadic * is multiplication.

2 * 3 3j1 * 2j2
6
4j8

Monadic * is called "Signum". For a real number y, the value of (* y) is _1 or 0 or 1 as y is negative, zero or positive.

* _2 * 0 * 2
_1
0
1

More generally, y may be real or complex, and the signum is equivalent to y % | y. Hence the signum of a complex number has magnitude 1 and the same angle as the argument.

y =: 3j4 | y y % | y * y | * y
3j4
5
0.6j0.8
0.6j0.8
1

20.1.5 Division and Reciprocal

Dyadic % is division.

2 % 3 3j4 % 2j1 12x % 5x
0.666667
2j1
12r5

1 % 0 is "infinity" but 0 % 0 is 0

1 % 0 0 % 0
_
0

Monadic % is the "reciprocal" function.

% 2 % 0j1
0.5
0j_1

20.1.6 Double and Halve

Monadic +: is the "double" verb.

+: 2.5 +: 3j4 +: 3x
5
6j8
6

Monadic -: is the "halve" verb:

-: 6 -: 6.5 -: 3j4 -: 3x
3
3.25
1.5j2
3r2

20.1.7 Floor and Ceiling

Monadic <. (left-angle-bracket dot) is called "Floor". For real y the floor of y is y rounded downwards to an integer, that is, the largest integer not exceeding y.

<. 2 <. 3.2 <. _3.2
2
3
_4

For complex y, the floor lies within a unit circle center y, that is, the magnitude of (y - <. y) is less than 1.

y =: 3.4j3.4 z =: <. y y - z | y-z
3.4j3.4
3j3
0.4j0.4
0.565685

This condition (magnitude less than 1) means that the floor of say 3.8j3.8 is not 3j3 but 4j3 because 3j3 does not satisfy the condition.

y =: 3.8j3.8 z =: <. y | y-z | y - 3j3
3.8j3.8
4j3
0.824621
1.13137

Monadic >. is called "Ceiling". For real y the ceiling of y is y rounded upwards to an integer, that is, the smallest integer greater than or equal to y. For example:

>. 3.0 >. 3.1 >. _2.5
3
4
_2

Ceiling applies to complex y

>. 3.4j3.4 >. 3.8j3.8
3j4
4j4

20.1.8 Power and Exponentiation

Dyadic ^ is the "power" verb: (x^y) is x raised-to-the-power y

10 ^ 2 10 ^ _2 100 ^ 1%2
100
0.01
10

Monadic ^ is exponentiation (or antilogarithm): ^y means (e^y) where e is Euler's constant, 2.71828...

^ 1 ^ 0j1
2.71828
0.540302j0.841471

Euler's equation, supposedly engraved on his tombstone is: e i pi +1 = 0

   (^ 0j1p1) + 1
0

20.1.9 Square

Monadic *: is "Square".

*: 4 *: 2j1
16
3j4

20.1.10 Square Root

Monadic %: is "Square Root".

%: 9 %: 3j4 2j1 * 2j1
3
2j1
3j4

20.1.11 Root

If x is integral, then x %: y is the "x'th root" of y:

3 %: 8 _3 %: 8
2
0.5

More generally, (x %: y) is an abbreviation for (y ^ % x)

x =: 3 3.1 x %: 8 8 ^ % x
3 3.1
2 1.95578
2 1.95578

20.1.12 Logarithm and Natural Logarithm

Dyadic ^. is the base-x logarithm function, that is, (x ^. y) is the logarithm of y to base x :

10 ^. 1000 2 ^. 8
3
3

Monadic ^. is the "natural logarithm" function.

e =: ^ 1 ^. e
2.71828
1

20.1.13 Factorial and OutOf

The factorial function is monadic !.

! 0 1 2 3 4 ! 5x 6x 7x 8x
1 1 2 6 24
120 720 5040 40320

The number of combinations of x objects selected out of y objects is given by the expression x ! y

1 ! 4 2 ! 4 3 ! 4
4
6
4

20.1.14 Magnitude and Residue

Monadic | is called "Magnitude". For a real number y the magnitude of y is the absolute value:

| 2 | _2
2
2

More generally, y may be real or complex, and the magnitude is equivalent to (%: y * + y).

y =: 3j4 y * + y %: y * + y | y
3j4
25
5
5

The dyadic verb | is called "residue". the remainder when y is divided by x is given by (x | y).

10 | 12 3 | _2 _1 0 1 2 3 4 5 1.5 | 3.7
2
1 2 0 1 2 0 1 2
0.7

If x | y is zero, then x is a divisor of y:

4 | 12 12 % 4
0
3

The residue function applies to complex numbers:

a =: 1j2 b=: 2j3 a | b a | (a*b) (b-1j1) % a
1j2
2j3
1j1
0
1

20.1.15 GCD and LCM

The greatest common divisor (GCD) of x and y is given by (x +. y). Reals and rationals in the domain of +..

6 +. 15 _6 +. _15 2.5 +. 3.5 6r7 +. 15r7
3
3
0.5
3r7

Complex numbers are also in the domain of +..

a=: 1j2 b=:2j3 c=:3j5 (a*b) +. (b*c)
1j2
2j3
3j5
2j3

If x and y are complex, then x +. y may differ from y +. x.

1 +. 0j1 0j1 +. 1
1
0j1

We can see that the same result is produced by Euclid's algorithm for (x GCD y), which is: if y=0 then x, otherwise (x|y) GCD x. Here is a verb E, to model the algorithm.

   E  =:  (| E [) ` [ @. (]=0:)
   

6 E 15 1 +. 0j1 1 E 0j1 0j1 +. 1 0j1 E 1
3
1
1
0j1
0j1

The Least Common Multiple of x and y is given by (x *. y).

(2 * 3) *. (3 * 5) 2*3*5
30
30

20.2 Circle Functions

20.2.1 Pi Times

There is a built-in verb o. (lower-case o dot). Monadic o. is called "Pi Times"; it multiplies its argument by 3.14159...

o. 1 o. 2 o. 1r6
3.14159
6.28319
0.523599

20.2.2 Trigonometric and Other Functions

If y is an angle in radians, then the sine of y is given by the expression 1 o. y. The sine of (pi over 6) is 0.5

y =: o. 1r6 1 o. y
0.523599
0.5

The general scheme for dyadic o. is that (k o. y) means: apply to y a function selected by k. Giving conventional names to the available functions, we have:

   sin   =:  1 & o.  NB.  sine
   cos   =:  2 & o.  NB.  cosine 
   tan   =:  3 & o.  NB.  tangent
   
   sinh  =:  5 & o.  NB.  hyperbolic sine 
   cosh  =:  6 & o.  NB.  hyperbolic cosine  
   tanh  =:  7 & o.  NB.  hyperbolic tangent 
   
   asin  =: _1 & o.  NB.  inverse sine 
   acos  =: _2 & o.  NB.  inverse cosine 
   atan  =: _3 & o.  NB.  inverse tangent 
   
   asinh =: _5 & o.  NB.  inverse hyperbolic sine
   acosh =: _6 & o.  NB.  inverse hyperbolic cosine 
   atanh =: _7 & o.  NB.  inverse hyperbolic tangent
   
   

y sin y asin sin y
0.523599
0.5
0.523599

20.2.3 Pythagorean Functions

There are also the "pythagorean"functions:

      0 o. y  means   %:   1 - y^2
      4 o. y  means   %:   1 + y^2
      8 o. y  means   %: - 1 + y^2
     _4 o. y  means   %:  _1 + y^2
     _8 o. y  means - %: - 1 + y^2 
   

y =: 0.6 0 o. y %: 1 - y^2
0.6
0.8
0.8

This is the end of chapter 20


NEXT
Table of Contents


Copyright © Roger Stokes 2002. This material may be freely reproduced, provided that this copyright notice is also reproduced.

last updated 16 Mar 2002