linfn.lsp 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 Provides linear functions.
function
Determine linfn:slope of line through 2 points. x0 - flonum. y0 - flonum. x1 - flonum. x2 - flonum. x0 != x1 return - flonum.
function
(x [slope [y0]]) Evaluate linear function y = x*slope+y0 x - flonum. The function argument slope - flonum. The lines slope, default 1 y0 - flonum. The y-intercept, default 0 return - flonum.
function
(linfn2 x x0 y0 x1 y1) Evaluate linear function through points (x0,y0) and (x1,y1) where x0 != x1 for x. x - flonum. Function argument. x0 - flonum. Abscissa of point 0 y0 - flonum. Ordinate of point 0 x1 - flonum. Abscissa of point 1, x1 != x0 y1 - flonum. Ordinate of point 1 return - flonum.
function
Evaluate piece wise linear function for x. For x < x1 the line through points (x0 y0)(x1 y1) is used. For x >= x1 the line through points (x1 y1)(x2 y2) is used. y2 * **** **** **** y1 * * * y0 * x0 x1 x2
function
(linfn:3pt-bounded x x0 y0 x1 y1 x2 y2) Like linfn:3pt except result is bounded. y2 ********** **** **** **** y1 * * * y0 ***** x0 x1 x2
function
(linfn:4pt x x0 y0 x1 y1 x2 y2 x3 y3) Evaluate linear function for x. If x >= x2 the line through points (x2 y2)(x3 y3) is used. If x < x1 the line through points (x0 y0)(x1 y1) is used. For x1 <= x < x2, the line through (x1 y1)(x2 y2) is used.
function
(linfn:4pt-bounded x x0 y0 x1 y1 x2 y2 x3 y3) Like linfn:4pt except result is bounded.
macro
(deflin name slope [y0]) Define new linear function with given slope and y intercept (name x) ---> x*slope+y0 name - symbol. The new function's name slope - flonum. The slope of the new function y0 - flonum. The y-intercept, default 0
macro
(deflin2 name [x0 [y0 [x1 [y2]]]]) Define a linear function named name through the two given points. (name x) ---> m(x-x1)+y1 where m is the slope (y0-y1)/(x0-x1) name - Symbol. The name of the new function x0 - Flonum. The abscissa of point 0, default 0 y0 - Flonum. The ordinate of point 0, default 0 x1 - Flonum. The abscissa of point 1, default 1 y1 - Flonum. The ordinate of point 1, default 1
macro
(deflin:3pt name x0 y0 x1 y1 x2 y2) ISSUE: deflin:3pt needs documentation
macro
(deflin:3pt-bounded name x0 y0 x1 y1 x2 y2) ISSUE: deflin:3pt-bounded needs documentation
macro
(deflin:4pt name x0 y0 x1 y1 x2 y2 x3 y3) ISSUE: deflin:4pt needs documentation
macro
(deflin:4pt name x0 y0 x1 y1 x2 y2 x3 y3) ISSUE: deflin:4pt-bounded needs documentation
;; linfn.lsp ;; 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 ;; ;; Provides linear functions. ;; (provide 'linfn) (current-file "linfn") ;; @doc function linfn:slope ;; Determine linfn:slope of line through 2 points. ;; ;; x0 - flonum. ;; y0 - flonum. ;; x1 - flonum. ;; x2 - flonum. x0 != x1 ;; return - flonum. ;; (defun linfn:slope (x0 y0 x1 y1) (/ (- y0 y1)(- (float x0) x1))) ;; @doc function linfn ;; (x [slope [y0]]) ;; Evaluate linear function y = x*slope+y0 ;; ;; x - flonum. The function argument ;; slope - flonum. The lines slope, default 1 ;; y0 - flonum. The y-intercept, default 0 ;; return - flonum. ;; (defun linfn (x &optional (slope 1)(y0 0)) (+ (* x (float slope)) y0)) ;; @doc function linfn2 ;; (linfn2 x x0 y0 x1 y1) ;; Evaluate linear function through points (x0,y0) and (x1,y1) where x0 != x1 ;; for x. ;; ;; x - flonum. Function argument. ;; x0 - flonum. Abscissa of point 0 ;; y0 - flonum. Ordinate of point 0 ;; x1 - flonum. Abscissa of point 1, x1 != x0 ;; y1 - flonum. Ordinate of point 1 ;; return - flonum. ;; (defun linfn2 (x x0 y0 x1 y1) (let ((m (linfn:slope x0 y0 x1 y1))) (linfn x m (- y1 (* m x1))))) ;; @doc function linfn:3pt ;; Evaluate piece wise linear function for x. ;; For x < x1 the line through points (x0 y0)(x1 y1) is used. ;; For x >= x1 the line through points (x1 y1)(x2 y2) is used. ;; ;; ;; y2 * ;; **** ;; **** ;; **** ;; y1 * ;; * ;; * ;; y0 * ;; x0 x1 x2 ;; (defun linfn:3pt (x x0 y0 x1 y1 x2 y2) (assert (> x2 x1 x0) "linfn:3pt expects monotonically increasing domain x0= x x1) (setf m (linfn:slope x1 y1 x2 y2) b (- y1 (* m x1)))) (t (setf m (linfn:slope x0 y0 x1 y1) b (- y1 (* m x1))))) (+ (* m x) b))) ;; @doc function linfn:3pt-bounded ;; (linfn:3pt-bounded x x0 y0 x1 y1 x2 y2) ;; Like linfn:3pt except result is bounded. ;; ;; y2 ********** ;; **** ;; **** ;; **** ;; y1 * ;; * ;; * ;; y0 ***** ;; x0 x1 x2 (defun linfn:3pt-bounded (x x0 y0 x1 y1 x2 y2) (assert (> x2 x1 x0) "linfn:3pt expects monotonically increasing domain x0 = x x1) (setf m (linfn:slope x1 y1 x2 y2) b (- y1 (* m x1)))) (t (setf m (linfn:slope x0 y0 x1 y1) b (- y1 (* m x1))))) (min (max y0 y1 y2)(max (min y0 y1 y2)(+ (* m x) b))))) ;; @doc function linfn:4pt ;; (linfn:4pt x x0 y0 x1 y1 x2 y2 x3 y3) ;; Evaluate linear function for x. ;; If x >= x2 the line through points (x2 y2)(x3 y3) is used. ;; If x < x1 the line through points (x0 y0)(x1 y1) is used. ;; For x1 <= x < x2, the line through (x1 y1)(x2 y2) is used. ;; (defun linfn:4pt (x x0 y0 x1 y1 x2 y2 x3 y3) (progn (assert (> x3 x2 x1 x0) "deflin:4pt expects monotonically increasing domain x0 = x x2) (setf m (linfn:slope x2 y2 x3 y3) b (- y2 (* m x2)))) ((>= x x1) (setf m (linfn:slope x1 y1 x2 y2) b (- y1 (* m x1)))) (t (setf m (linfn:slope x0 y0 x1 y1) b (- y0 (* m x0))))) (+ (* m x) b)))) ;; @doc function linfn:4pt-bounded ;; (linfn:4pt-bounded x x0 y0 x1 y1 x2 y2 x3 y3) ;; Like linfn:4pt except result is bounded. ;; (defun linfn:4pt-bounded (x x0 y0 x1 y1 x2 y2 x3 y3) (progn (assert (> x3 x2 x1 x0) "deflin:4pt expects monotonically increasing domain x0 = x x2) (setf m (linfn:slope x2 y2 x3 y3) b (- y2 (* m x2)))) ((>= x x1) (setf m (linfn:slope x1 y1 x2 y2) b (- y1 (* m x1)))) (t (setf m (linfn:slope x0 y0 x1 y1) b (- y0 (* m x0))))) (min (max y0 y1 y2 y3)(max (min y0 y1 y2 y3)(+ (* m x) b)))))) ;; @doc macro deflin ;; (deflin name slope [y0]) ;; Define new linear function with given slope and y intercept ;; (name x) ---> x*slope+y0 ;; ;; name - symbol. The new function's name ;; slope - flonum. The slope of the new function ;; y0 - flonum. The y-intercept, default 0 ;; (defmacro deflin (name slope &optional (y0 0)) `(defun ,name (x)(+ ,y0 (* ,slope x)))) ;; @doc macro deflin2 ;; (deflin2 name [x0 [y0 [x1 [y2]]]]) ;; Define a linear function named name through the two given points. ;; (name x) ---> m(x-x1)+y1 ;; where m is the slope (y0-y1)/(x0-x1) ;; ;; name - Symbol. The name of the new function ;; x0 - Flonum. The abscissa of point 0, default 0 ;; y0 - Flonum. The ordinate of point 0, default 0 ;; x1 - Flonum. The abscissa of point 1, default 1 ;; y1 - Flonum. The ordinate of point 1, default 1 ;; (defmacro deflin2 (name &optional (x0 0)(y0 0)(x1 1)(y1 1)) `(deflin ,name (linfn:slope ,x0 ,y0 ,x1 ,y1) (- ,y1 (* (linfn:slope ,x0 ,y0 ,x1 ,y1) ,x1)))) ;; @doc macro deflin:3pt ;; (deflin:3pt name x0 y0 x1 y1 x2 y2) ;; ISSUE: deflin:3pt needs documentation ;; (defmacro deflin:3pt (name x0 y0 x1 y1 x2 y2) (progn (assert (> x2 x1 x0) "deflin:3pt expects monotonically increasing domain x0 = x ,x1) (setf m (linfn:slope ,x1 ,y1 ,x2 ,y2) b (- ,y1 (* m ,x1)))) (t (setf m (linfn:slope ,x0 ,y0 ,x1 ,y1) b (- ,y1 (* m ,x1))))) (+ (* m x) b))))) ;; @doc macro deflin:3pt-bounded ;; (deflin:3pt-bounded name x0 y0 x1 y1 x2 y2) ;; ISSUE: deflin:3pt-bounded needs documentation ;; (defmacro deflin:3pt-bounded (name x0 y0 x1 y1 x2 y2) (progn (assert (> x2 x1 x0) "deflin:3pt-bounded expects monotonically increasing domain x0 = x ,x1) (setf m (linfn:slope ,x1 ,y1 ,x2 ,y2) b (- ,y1 (* m ,x1)))) (t (setf m (linfn:slope ,x0 ,y0 ,x1 ,y1) b (- ,y1 (* m ,x1))))) (max miny (min maxy (+ (* m x) b))))))) ;; @doc macro deflin:4pt ;; (deflin:4pt name x0 y0 x1 y1 x2 y2 x3 y3) ;; ISSUE: deflin:4pt needs documentation ;; (defmacro deflin:4pt (name x0 y0 x1 y1 x2 y2 x3 y3) (progn (assert (> x3 x2 x1 x0) "deflin:4pt expects monotonically increasing domain x0 = x ,x2) (setf m (linfn:slope ,x2 ,y2 ,x3 ,y3) b (- ,y2 (* m ,x2)))) ((>= x ,x1) (setf m (linfn:slope ,x1 ,y1 ,x2 ,y2) b (- ,y1 (* m ,x1)))) (t (setf m (linfn:slope ,x0 ,y0 ,x1 ,y1) b (- ,y0 (* m ,x0))))) (+ (* m x) b))))) ;; @doc macro deflin:4pt-bounded ;; (deflin:4pt name x0 y0 x1 y1 x2 y2 x3 y3) ;; ISSUE: deflin:4pt-bounded needs documentation ;; (defmacro deflin:4pt-bounded (name x0 y0 x1 y1 x2 y2 x3 y3) (progn (assert (> x3 x2 x1 x0) "deflin:4pt-bounded expects monotonically increasing domain x0 = x ,x2) (setf m (linfn:slope ,x2 ,y2 ,x3 ,y3) b (- ,y2 (* m ,x2)))) ((>= x ,x1) (setf m (linfn:slope ,x1 ,y1 ,x2 ,y2) b (- ,y1 (* m ,x1)))) (t (setf m (linfn:slope ,x0 ,y0 ,x1 ,y1) b (- ,y0 (* m ,x0))))) (max miny (min maxy (+ (* m x) b)))))))