Class 11: Using External Programs in Lisp


Review from previous class

Simulating a smoother
Accumulation of results can consume a great deal of memory, as the system saves all of the coordinates. Perhaps its better only to save a few. However, adding lines to a plot saves all of the points that appear.


Tools for the Day


Status of Projects


Importing Foreign Functions into Lisp

Smoothing
Here's one version of a moving average smoother, written in a style to make the algorithm simple to grasp. This version is not very efficient but seems to run quickly enough. You can try for a better version for assignment 2.
			(defun MOVING-AVERAGE (x width)
			  "Smooths assuming input x is equally spaced."
			  (unless (oddp width)
			          (format t "Width needs to be odd integer.~%")
			          (setf width (1+ (round width)))  )
			  (let ((half (/ (1- width) 2))      ; invariants done once
			        (n-1  (1- (length x)))
			        (smth ())  )
			    (flet ((avg (i) 
			                (mean (select x
			                              (iseq (max 0   (- i half))
			                                    (min n-1 (+ i half)))))  ))
			      (dotimes (i (length x))
			               (push (avg i) smth))
			      (nreverse smth)                ; native reverse is "safe" here
			      )))
        
This version does OK with n=1000 points, but starts to bog down when one think about "twicing", smoothing the smoother.
Slider animation
LispStat provides interactive plot controls known as sliders. As you move the control, the value of a paramter is passed to a function that you give to the slider control. In this case, we'll use the function to control the smoothness of the moving average. (See page 226 and section 10.1.3, page 304)

If you think the previous version was slow, this one absolutely drags with 1000 points in the display (only a subset are shown in the illustration). What to do? There are three choices, and the first one is always worth a try:

  1. Compile the function.
  2. Use a better algorithm.
  3. Write the function in C.

Writing the program in C only makes sense after you have spent time thinking about the second and will need the function repeatedly. For one or two times, why spend the hours that it takes to convert your program to C?

Writing a simple foreign function in C
Writing your program in C requires a bit more "discipline" than doing the same in Lisp. Debugging can be quite a bit harder, with the failures more severe (things like "core dumps"). In some cases, however, the attention to detail is helpful and encourages you to focus on the algorithm more carefully.

Our first program simply computes an average, and illustrates the passing of data from Lisp to C and back to Lisp.

Now for the real thing, the smoother. Since we're being so careful, might there be a better way to compute the smoother, such as by using St to compute St+1?

Lisp script for today's class
class11.lsp


Next time

Arrays and matrix calculations in Lisp; supported matrix algebra features.