3.1. Numbers


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



3.1.0.1. (1+ n)
[DX]


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



3.1.0.3. (1- n)
[DX]


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



3.1.0.5. (2* n)
[DX]


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



3.1.0.7. (2/ n)
[DX]


Divides n by 2.

Example:

? (2/ 5)
= 2.5



3.1.0.8. (** n m)
[DX]


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.



3.1.2.0. (sgn n)
[DZ]


Returns +1 if n is equal or greater than 0 , -1 otherwise.
? (sgn -2)
= -1

? (sgn 1.2)
= 1



3.1.2.1. (abs n)
[DZ]


Returns the absolute value of n .
? (abs 123.3)
= 123.3

? (abs -23.3)
= 23.3



3.1.2.2. (int n)
[DZ]


Returns the integral part of n .
? (int -4.5)
= -5

? (int 4.5)
= 4



3.1.2.3. (sqrt n)
[DZ]


Returns the square root of n
? (sqrt 15)
= 3.873



3.1.2.4. (0-x-1 n)
[DZ]


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



3.1.2.5. (0-1-0 n)
[DZ]


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



3.1.2.6. (sin n)
[DZ]


Returns the sine of n radians.
? (sin (/ 3.1415 3))
= 0.866



3.1.2.7. (cos n)
[DZ]


Returns the cosine of n radians.
? (cos (/ 3.1415 3))
= 0.5



3.1.2.8. (tan n)
[DZ]


Returns the tangent of n radians.
? (tan (/ 3.1415 3))
= 1.7319



3.1.2.9. (atan n)
[DZ]


Returns the arc tangent of n , in radians.
? (* 4 (atan 1))
= 3.1416



3.1.2.10. (exp n)
[DZ]


Returns the exponential of n .
? (exp 1)
= 2.7183



3.1.2.11. (exp-1 n)
[DZ]


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



3.1.2.12. (log n)
[DZ]


Returns the natural logarithm of n .
? (log 2)
= 0.6931



3.1.2.13. (log1+ n)
[DZ]


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



3.1.2.14. (sinh n)
[DZ]


Returns the hyperbolic sine of n .
? (sinh 1)
= 1.1752



3.1.2.15. (cosh n)
[DZ]


Returns the hyperbolic cosine of n .
? (cosh 1)
= 1.5431



3.1.2.16. (tanh n)
[DZ]


See: (qtanh n )
Returns the hyperbolic tangent of n .
? (tanh 1)
= 0.7616



3.1.2.17. (qtanh n)
[DZ]


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



3.1.3. Random Numbers


Statistical functions are provided for computing random numbers, computing various statistics on list of numbers or performing simple linear regressions.



3.1.3.0. (seed n)
[DX]


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:

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. Hashing




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 = .