Skip to content

Instantly share code, notes, and snippets.

@carlohamalainen
Created September 15, 2011 01:33
Show Gist options
  • Save carlohamalainen/1218303 to your computer and use it in GitHub Desktop.
Save carlohamalainen/1218303 to your computer and use it in GitHub Desktop.
(defun blerp (x y)
"A closure that provides a linear interpolation of a function y = f(x). Attempts to evaluate f
at values of x below the minimum value of x result in zero, and attempts above the maximum
value of x result in the last value of y."
(assert (vectorp x))
(assert (vectorp y))
(let ((min-x (aref x 0))
(max-x (aref x (1- (length x))))
(interp-f (cl-numlib:make-interpolating-function x y)))
(lambda (x)
(cond
((eq x :min) min-x)
((eq x :max) max-x)
(t (cond
((< x min-x) 0.0)
((> x max-x) (funcall interp-f max-x))
(t (funcall interp-f x))))))))
(defparameter f (blerp (vector 1 2 3 4 5) (vector 1 0 3 4 9)))
(funcall f :min) ; => 1
(funcall f 0) ; => 0
(funcall f 2.3) ; => 0.899
(funcall f 100) ; => 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment