See: Special Numerical Values (IEEE754).
Numbers are the simplest kind of atoms. They are stored as double
precision floating point numbers.
Numerical computations in Lush are usually performed in double
precision. Single precision however is used by some number crunching
functions, like the matrix calculus operations.
You can type in decimal number using the traditional exponential
notation or using hexadimal numbers preceded by characters
"0x" . Here are a few examples of legal numbers:
23
3.14
12e6 ;; equivalent to 12000000
1.2E6 ;; equivalent to 1200000
0x1A ;; equivalent to 26
3.1.0. Elementary Numerical Functions
|
|
The following elementary functions operate on numbers:
3.1.0.0. (+ n1 ... nm)
|
[DX] |
Computes the sum of numbers n1 to
nm .
Example:
? (+ 1 2 3 4)
= 10
Adds 1 to n .
Example:
? (1+ 4)
= 5
3.1.0.2. (- [n1] n2)
|
[DX] |
Subtracts n2 from
n1 . If one argument only is provided, returns the opposite
of n2 .
Example:
? (- 2 5)
= -3
Subtracts 1 from n .
Example:
? (1- (+ 4 5))
= 8
3.1.0.4. (* n1 ... nm)
|
[DX] |
Computes the product of n1 to
nm .
Here is a memory intensive way of computing a factorial:
? (apply '* (range 1 5))
= 120
Multiplies n by 2.
Example:
? (2* (+ 4 5))
= 18
3.1.0.6. (/ [n1] n2)
|
[DX] |
Divides n1 by
n2 . If one argument only is provided, Function
/ returns the inverse of n2
.
Example:
? (/ 3)
= 0.3333
Divides n by 2.
Example:
? (2/ 5)
= 2.5
Returns n raised to the
m th power.
Example:
? (** 2 5)
= 32
3.1.0.9. (max l1 ... ln)
|
[DX] |
Return the maximum element within l1
... ln . Arguments
li may be numbers or strings.
Example:
? (max 2 3 (* 3 3) (+ 4 3))
= 9
3.1.0.10. (min l1 ... ln)
|
[DX] |
Return the minimum element within l1
... ln . Arguments
li may be numbers or strings.
Example:
? (min 2 3 (* 3 3) (+ 4 3))
= 2
3.1.1. Integer Arithmetic Functions
|
|
Besides the ordinary number operation functions, a few functions deal
with integer numbers only. For that purpose we consider only the first
32 bits of the integer part of the number.
3.1.1.0. (div n1 n2)
|
[DX] |
Returns the quotient of the Euclidian division of
n1 by n2 .
Example:
? (div 5 2)
= 2
3.1.1.1. (mod n1 n2)
|
[DX] |
Returns the remainder of the Euclidian division of
n1 by n2 .
Example:
? (mod 5 2)
= 1
3.1.1.2. (bitand n1 n2)
|
[DX] |
Returns the bitwise AND of integer numbers n1
and n2 .
3.1.1.3. (bitor n1 n2)
|
[DX] |
Returns the bitwise OR of integer numbers n1
and n2 .
3.1.1.4. (bitxor n1 n2)
|
[DX] |
Returns the bitwise exclusive or (XOR) of integer numbers
n1 and n2 .
3.1.1.5. (bitshl n1 n2)
|
[DX] |
Shifts the bits of integer n1 by
n2 positions to the left and returns the result. This is
similar to the C operator >> .
3.1.1.6. (bitshr n1 n2)
|
[DX] |
Shifts the bits of integer n1 by
n2 positions to the right and returns the result. This is
similar to the C operator >> .
3.1.2. Mathematical Numerical Functions
|
|
Most mathematical functions are implemented as DZ functions. DZs are a
remnant of a now obsolete compiler for numerical expressions. For more
details about DZ functions the corresponding section.
Returns +1 if n is equal or greater
than 0 , -1 otherwise.
? (sgn -2)
= -1
? (sgn 1.2)
= 1
Returns the absolute value of n .
? (abs 123.3)
= 123.3
? (abs -23.3)
= 23.3
Returns the integral part of n .
? (int -4.5)
= -5
? (int 4.5)
= 4
Returns the square root of n
? (sqrt 15)
= 3.873
This function implements a piecewise saturated linear function. It
returns 0 if n is smaller than -1. It
returns 1 if n is larger than +1. It
returns n if n
is in the -1 to +1 range.
? (0-x-1 -2)
= -1
? (0-x-1 0.7)
= 0.7
? (0-x-1 1.3)
= 1
This function implements the indicator function of the -1 to +1 range.
It returns 1 if n is in the -1 to +1
range. It returns 0 otherwise.
? (0-1-0 -2)
= 0
? (0-1-0 0.7)
= 1
Returns the sine of n radians.
? (sin (/ 3.1415 3))
= 0.866
Returns the cosine of n radians.
? (cos (/ 3.1415 3))
= 0.5
Returns the tangent of n radians.
? (tan (/ 3.1415 3))
= 1.7319
Returns the arc tangent of n , in
radians.
? (* 4 (atan 1))
= 3.1416
Returns the exponential of n .
? (exp 1)
= 2.7183
Returns the exponential of n minus 1.
This function gives an accurate value of exp(n)-1
even for small values of n .
? (exp-1 0.5)
= 0.6487
Returns the natural logarithm of n .
? (log 2)
= 0.6931
Returns the natural logarithm of n
plus 1. This function gives an accurate value of
log(1+n) even for small values of n
.
? (log1+ 1)
= 0.6931
Returns the hyperbolic sine of n .
? (sinh 1)
= 1.1752
Returns the hyperbolic cosine of n .
? (cosh 1)
= 1.5431
See: (qtanh n )
Returns the hyperbolic tangent of n .
? (tanh 1)
= 0.7616
See: (tanh n )
Returns a rational approximation of the hyperbolic tangent of
n . This function is orders of magnitude faster than
tanh . Its acuracy however is quite low (about 0.00001.)
? (qtanh 1)
= 0.7616
3.1.2.18. (qdtanh n)
|
[DZ] |
Returns a rational approximation of the derivative of the hyperbolic
tangent of n . The accuracy of this
function is quite low (about 0.00001), but its computation time is very
small.
? (qdtanh 1)
= 0.42
3.1.2.19. (qstdsigmoid n)
|
[DZ] |
See: (qtanh n )
Returns the value of standard sigmoid at point n
, computed with a rational approximation to the hyperbolic tangent. The
standard sigmoid is defined as
3.1.2.20. (qdstdsigmoid n)
|
[DZ] |
See: (qdtanh n )
Returns the value of the derivative of standard sigmoid at point
n , computed with a rational approximation to the hyperbolic
tangent. The standard sigmoid is defined as
3.1.2.21. (qexpmx n)
|
[DZ] |
Returns a rational approximation of the exponential of minus the
absolute value of n . The accuracy of
this function is quite low (about 0.00001), but its computation time may
be smaller on certain computers.
? (qexpmx 1)
= 0.3679
3.1.2.22. (qdexpmx n)
|
[DZ] |
Returns a rational approximation of the derivative of the exponential of
minus the absolute value of n . The
accuracy of this function is quite low (about 0.00001), but its
computation time may be smaller on certain computers.
? (qdexpmx 1)
= 0.3679
3.1.2.23. (qexpmx2 n)
|
[DZ] |
Returns a rational approximation of the exponential of minus the square
of n . The accuracy of this function
is quite low (about 0.00001), but its computation time may be smaller on
certain computers.
? (qexpmx 1)
= 0.3679
3.1.2.24. (qdexpmx2 n)
|
[DZ] |
Returns a rational approximation of the exponential of minus the square
of n . The accuracy of this function
is quite low (about 0.00001), but its computation time may be smaller on
certain computers.
? (qdexpmx 1)
= 0.3679
3.1.2.25. (solve min max f)
|
[DX] |
Simple dichotomical root finder. f is
a function with one argument only. solve
will return a numerical approximation of the solution of
f(x)=0 between min and
max . To ensure the existence of this solution,
f(min) and f(max) must have
different signs.
Example:
? (solve 1 2 (lambda (x)
(- 2 (* x x)) ))
= 1.4142
Statistical functions are provided for computing random numbers,
computing various statistics on list of numbers or performing simple
linear regressions.
Sets the random number generator seed. The random generator is
replicable. The same seed will produce the same sequence of random
numbers. Argument n must be a legal
integer number.
3.1.3.1. (rand [[a] b])
|
[DX] |
Returns a uniform random number:
- If no arguments are provided,
function rand returns a random number
between 0 and 1.
- If argument b only is given,
function rand returns a random number
between -b and
b .
- If both arguments a and
b , function rand returns a
random number between a and
b .
Example:
? (rand)
= 0.8437
3.1.3.2. (gauss [[m] s])
|
[DX] |
Returns a gaussian number with mean m
and standard deviation s . The default
value for argument m is
0 . The default value for argument s
is 1 .
Example
? (gauss 2)
= 1.7561
3.1.4.0. (hashcode expr)
|
[DX] |
Returns a string containing a hash code for the lisp expression
expr . Hash codes are guaranteed to be equal when two
expressions are logically equal, that is to say, equal as defined by
function = .