Main Page       Index


math.lsp


 math.lsp
 Version 1.021 11 June 2005, Minor documentation changes
 Version 1.02  02 June 2005, Commented out several never-used functions
 Version 1.01  21 May 2005, Minor bug fix, pick now handles empty list.
 Version 1.00  31 October 2004

 Author Steven Jones

 Contact jones57@swbell.net include the word "nyquist" in subject line

 The contents of this file are released under the terms of the GNU General
 Public License. See the file LICENSE.txt

 Basic math functions, see also primes.lsp and nbase.lsp


function

clamp

 (clamp v mn mx)
 Restrict value to given interval
 
 v      - flonum. The value
 mn     - flonum. The minimum result
 mx     - flonum. The maximum result
 return - flonum. v | mn <= v <= mx


function

range

 (range n [initial [increment [acc]]])
 Generate arithmetic sequence
 
 n         - integer. Number of elements
 initial   - flonum. Initial value, default 0
 increment - flonum. Common difference, default 1
 acc       - list. Used as part of recursive call but may be used by the 
             user to add static elements to the resulting list. Any elements 
             in acc will appear at the end of the resulting list.
 return    - list


function

sqr

 (sqr x)
 Calculate square

 x      - flonum.
 return - flonum. x^2


function

log2

 (log2 n)
 Determine the base 2 log of n
 n      - flonum
 return - flonum


function

log10

 (log10 n)
 Determine the base 10 log of n
 n      - flonum
 return - flonum


function

rnd

 (rnd)
 Generate random number

 return - flonum. A random number to 5 decimal places in the 
          interval 0 <= (rnd) < 1


function

rndr

 (rndr low high [fn])
 Generate a random number in given interval
 
 low  - flonum. Minimum result
 high - flonum. limit of maximum result
 fn   - closure. A random number function which returns a value in half-open
        interval [0,1) Default #'rnd
 return - flonum. A random number  low <= (rndr low high) < high


function

random-list

 (random-list n low high [fn])
 Generate list of n random numbers.

 n      - integer. Number of elements.
 low    - flonum. Minimum result
 high   - flonum. Limit of maximum result
 return - list. A list of n random values in half-open interval [low, high)

(defun random-list (n low high &optional (fn #'rnd))
  (do ((index 0 (+ index 1))(acc '() (append acc (list (rndr low high fn)))))
      ((>= index n) acc)))


function

pick

 (pick lst)
 Pick an element of list at random

 lst    - list
 return - any. 


function

permute

 (permute lst)
 Create permutation of list

 lst    - list
 return - list.


function

mean

 (mean [a [b [c ...]]])
 Calculate mean of numeric values
 return - flonum


function

fractional

 (fractional n)
 
 n      - flonum. 
 return - flonum. The fractional part of n (always positive)


function

split-number

 (split-number n)
 Split number into whole and fractional parts.

 n      - flonum.
 return - cons. The result is a cons (w . f) where w is the whole number
          part and f is the fractional part.


function

polyn

 (polyn  coeflst [x])
 Evaluate polynomial.

 coeflst - list. A list of coefficients   (a0 a1 a2 ....an)
           Where each ai is a real number.

 x       - flonum. 

 return  - flonum. The result is the sum
           a0*x^0 + a1*x^1 + a2*x^2 + ... + an*x^n


function

about

 (about n [max])
 Return a random number in the neighborhood of n

 n      - flonum. The center value
 max    - flonum. The maximum deviation from n as ratio of n.
          Default 0.01 ~ 1%
 return - flonum. The result is a random number in the interval
          (n-n*max) <= result <= (n+n*max)


View the Sourcecode :



;; math.lsp
;; Version 1.021 11 June 2005, Minor documentation changes
;; Version 1.02  02 June 2005, Commented out several never-used functions
;; Version 1.01  21 May 2005, Minor bug fix, pick now handles empty list.
;; Version 1.00  31 October 2004
;;
;; Author Steven Jones
;;
;; Contact jones57@swbell.net include the word "nyquist" in subject line
;;
;; The contents of this file are released under the terms of the GNU General
;; Public License. See the file LICENSE.txt
;;
;; Basic math functions, see also primes.lsp and nbase.lsp
;;

(current-file "math")
(provide 'math)


;; @doc function clamp
;; (clamp v mn mx)
;; Restrict value to given interval
;; 
;; v      - flonum. The value
;; mn     - flonum. The minimum result
;; mx     - flonum. The maximum result
;; return - flonum. v | mn <= v <= mx
;;

(defun clamp (v mn mx)
  (min (max v mn) mx))


;;                                      --- Never used it ---
;;                                      --- and not necessary ---
;;                                            
;; -doc function mod
;; (mod a [b])
;; Calculate a modulo b
;; Unlike the standard XLISP rem function, mod only accepts two values either
;; of which may be flonum. In the case of a flonum argument the value is
;; "demoted" to an integer with the truncate function.
;;
;; a      - flonum. 
;; b      - flonum. Default 1
;; return - integer
;;
;;(defun mod (a &optional (b 1))
;;  (rem (truncate a) (truncate b)))


;; @doc function range
;; (range n [initial [increment [acc]]])
;; Generate arithmetic sequence
;; 
;; n         - integer. Number of elements
;; initial   - flonum. Initial value, default 0
;; increment - flonum. Common difference, default 1
;; acc       - list. Used as part of recursive call but may be used by the 
;;             user to add static elements to the resulting list. Any elements 
;;             in acc will appear at the end of the resulting list.
;; return    - list
;;

(defun range (n &optional (initial 0)(increment 1.0)(acc ()))
  (if (plusp n)
      (cons initial (range (- n 1)(+ initial increment) increment acc))
    acc))


;;                                      --- Never used it ----
;; -doc function xrange
;; (xrange n [initial [ratio [acc]]])
;; Generate geometric sequence
;;
;; n       - integer. Number of elements
;; initial - flonum. Initial value, default 1
;; ratio   - flonum. Common ratio, default 2
;; acc     - list. Used as part of recursive call but may be used by the 
;;           user to add static elements to the resulting list. Any elements 
;;           in acc will appear at the end of the resulting list.
;; return  - list
;;
;;(defun xrange (n &optional (initial 1.0)(ratio 2)(acc ()))
;;  (if (plusp n)
;;      (cons initial (xrange (- n 1)(* initial ratio) ratio acc))
;;    acc))


;; @doc function sqr
;; (sqr x)
;; Calculate square
;;
;; x      - flonum.
;; return - flonum. x^2
;;

(defun sqr (x)
  (* x x))


;; @doc function  log2
;; (log2 n)
;; Determine the base 2 log of n
;; n      - flonum
;; return - flonum
;;

(defun  log2 (n)
  (/ (log (float n)) +LOG2+))


;; @doc function log10
;; (log10 n)
;; Determine the base 10 log of n
;; n      - flonum
;; return - flonum
;;

(defun log10 (n)
  (/ (log (float n)) +LOG10+))


;;                                      --- Never used it ---
;; -doc function logn                         
;; (logn n [b])
;; Determine the base b log of n
;; Note for bases 2 and 10 use log2 and log10 functions instead, they should
;; be a bit faster.
;;
;; n      - flonum
;; b      - flonum, default 10
;; return - flonum
;;
;;(defun logn (n &optional (base 10))
;;  (/ (log (float n))(log (float base))))


;; @doc function rnd
;; (rnd)
;; Generate random number
;;
;; return - flonum. A random number to 5 decimal places in the 
;;          interval 0 <= (rnd) < 1
;;

(defun rnd ()
  (/ (random 100000) 100000.0))


;; @doc function rndr
;; (rndr low high [fn])
;; Generate a random number in given interval
;; 
;; low  - flonum. Minimum result
;; high - flonum. limit of maximum result
;; fn   - closure. A random number function which returns a value in half-open
;;        interval [0,1) Default #'rnd
;; return - flonum. A random number  low <= (rndr low high) < high
;;

(defun rndr (low high &optional (fn #'rnd))
  (+ low (* (- high low) (funcall fn))))


;;                                      --- Never used it ---
;;                                      --- Code could be cleaner anyway ---
;; @doc function random-list
;; (random-list n low high [fn])
;; Generate list of n random numbers.
;;
;; n      - integer. Number of elements.
;; low    - flonum. Minimum result
;; high   - flonum. Limit of maximum result
;; return - list. A list of n random values in half-open interval [low, high)
;;
;;(defun random-list (n low high &optional (fn #'rnd))
;;  (do ((index 0 (+ index 1))(acc '() (append acc (list (rndr low high fn)))))
;;      ((>= index n) acc)))


;; @doc function pick
;; (pick lst)
;; Pick an element of list at random
;;
;; lst    - list
;; return - any. 
;;

(defun pick (lst)
  (if lst
      (nth (random (length lst)) lst)
    nil))


;; @doc function permute
;; (permute lst)
;; Create permutation of list
;;
;; lst    - list
;; return - list.
;;

(defun permute (lst)
  (if lst (let ((obj (pick lst)))
	    (cons obj (permute (remove obj lst))))
    nil))
      

;; @doc function mean
;; (mean [a [b [c ...]]])
;; Calculate mean of numeric values
;; return - flonum
;;

(defun mean (&rest args)
  (/ (apply #'+ args)(float (length args))))

;;                                      --- Never used it ---
;; -doc function decimal->binary                    
;; Convert decimal value to binary. The binary value is represented as a
;; string. For negative values a '-' character is used as a prefix.
;; See also the file nbase.lsp
;;
;; n      - integer
;; return - string.
;;
;;(defun decimal->binary (n)
;;  (cond ((minusp n)
;;	 (strcat "-" (decimal->binary (d2b (abs n)))))
;;	((zerop n) "0")
;;	(t (let ((acc ""))
;;	     (while (plusp n)
;;	       (setf acc (strcat (if (evenp n) "0" "1") acc))
;;	       (setf n (/ n 2)))
;;	     acc))))
;;

;; @doc function fractional
;; (fractional n)
;; 
;; n      - flonum. 
;; return - flonum. The fractional part of n (always positive)
;;

(defun fractional (n)
  (abs (- n (truncate n))))


;; @doc function split-number
;; (split-number n)
;; Split number into whole and fractional parts.
;;
;; n      - flonum.
;; return - cons. The result is a cons (w . f) where w is the whole number
;;          part and f is the fractional part.
;; 

(defun split-number (n)
  (cons (truncate n)(fractional n)))


;; @doc function polyn
;; (polyn  coeflst [x])
;; Evaluate polynomial.
;;
;; coeflst - list. A list of coefficients   (a0 a1 a2 ....an)
;;           Where each ai is a real number.
;;
;; x       - flonum. 
;;
;; return  - flonum. The result is the sum
;;           a0*x^0 + a1*x^1 + a2*x^2 + ... + an*x^n
;;

(defun polyn (coeflst &optional (x 10))
  (let (acc)
    (setf acc 0)
    (dotimes (i (length coeflst))
      (setf acc (+ acc (* (nth i coeflst)(expt (float x) i)))))
    acc))


;; @doc function about
;; (about n [max])
;; Return a random number in the neighborhood of n
;;
;; n      - flonum. The center value
;; max    - flonum. The maximum deviation from n as ratio of n.
;;          Default 0.01 ~ 1%
;; return - flonum. The result is a random number in the interval
;;          (n-n*max) <= result <= (n+n*max)
;;

(defun about (n &optional (maxratio 0.01))
  (let ((range (* maxratio (float n))))
    (+ n (- range (* range 2 (rnd))))))


Main Page       Index