You are viewing lhealy

Liam Healy

May. 5th, 2013

10:14 pm - cl-plplot for simple x-y plots from Lisp

I had need recently to create quick x-y plots on the screen. I have been generating TikZ and PGFplots from Lisp for journal and conference papers and presentations. They look very nice but are not quick, either in writing or displaying. There are a number of CL packages listed for plotting, and I gave cl-plplot a try. It is in quicklisp. Though there is no active work on it, I was able to generate plots that I wanted quickly.

Here is an example, plotting the square function. First load needed libraries

sudo aptitude install libplplot-dev plplot11-driver-cairo plplot11-driver-xwin

(in Ubuntu 12.04LTS), then in Lisp
(ql:quickload :cl-plplot)
(defun x-y-plot ()
       (let* ((x (iter (for x from 0.0 to 10.0 by 0.25) (collect x result-type vector)))
              (y (map 'vector (alexandria:rcurry 'expt 2) x))
              (p (cl-plplot:new-x-y-plot x y))
              (w (cl-plplot:basic-window :x-label "x" :y-label "x squared" :title "The square function")))
         (cl-plplot:add-plot-to-window w p)
         (cl-plplot:render w "xwin")))
(x-y-plot)

Click on window and Enter to close; clicking on the close box for the window causes Lisp to crash, at least from Slime.

Tags:

Mar. 4th, 2013

08:43 pm - cffi-libffi in latest CFFI release 0.11.0

A week ago CFFI version 0.11.0 was released. This includes cffi-libffi, which permits calling and returning with structures by value. It replaces an old system, FSBV, which is now obsolete. As explained in my previous blogs, it is better integrated into CFFI. Quicklisp should pick up this release in the next update.

Tags:

Apr. 28th, 2012

06:31 pm - mem-aref and mem-aptr in CFFI

The master branch of CFFI has the cffi-libffi system, which permits passing and returning structures by value. As part of that addition, some of the syntax for structures has changed, even without loading that system. Before, "bare structure" references were permitted, and they were interpreted to mean pointers. Now, the syntax to specify a structure type is (:struct name) for the structure by value, and :pointer or (:pointer (:struct name)) for the structure by reference. The bare structure form is still accepted, with a style warning.

The function mem-aref returns an element of the array, an object; since we now can translate foreign structures, that includes structures. If the type is designated as (:struct name), that means a CL representation of the structure will be returned. By default (with no translate-from-foreign method defined), that is a plist. If you want a pointer instead, use the new function mem-aptr. As a special case, using the bare structure type specification, mem-aref still returns a pointer, but if you use the new form, you need to call mem-aptr to get the pointer instead of the object translated into CL.

(Rationale for the new function is here.)

If this isn't working as described, please post to cffi-devel.

Tags:

Apr. 14th, 2012

03:02 pm - cffi-libffi

CFFI now has (on the master branch, but not yet in quicklisp) the system cffi-libffi, which allows calling and returning structures by value. For example, in the GSL library, complex numbers and functions are defined passing and returning the complex structures by value, so we can define

    (cffi:defcstruct (complex-double-c :class complex-double-type)
      (dat :double :count 2))

    (defmethod cffi:translate-into-foreign-memory ((value complex) (type complex-double-type) p)
      (cffi:with-foreign-slots ((dat) p (:struct complex-double-c))
        (setf (cffi:mem-aref dat :double 0) (realpart value)
    	  (cffi:mem-aref dat :double 1) (imagpart value))))

    (defmethod cffi:translate-from-foreign (p (type complex-double-type))
      (cffi:with-foreign-slots ((dat) p (:struct complex-double-c))
        (complex (cffi:mem-aref dat :double 0)
    	     (cffi:mem-aref dat :double 1))))

    (cffi:define-foreign-library libgslcblas
      (:unix (:or "libgslcblas.so.0" "libgslcblas.so"))
      (t (:default "libgslcblas")))

    (cffi:define-foreign-library libgsl
      (:unix (:or "libgsl.so.0" "libgsl.so"))
      (t (:default "libgsl")))

    (cffi:use-foreign-library libgslcblas)
    (cffi:use-foreign-library libgsl)

    (defun complex-log (c)
       (cffi:foreign-funcall "gsl_complex_log"
        (:struct complex-double-c) c (:struct complex-double-c)))

and then find
   (complex-log #C(1.0d0 2.0d0))
   #C(0.8047189562170501d0 1.1071487177940904d0)

Note that there are changes that affect CFFI users even if they don't load cffi-libffi; using bare structure names foo insead of (:struct foo) or :pointer or (:pointer (:struct foo)) will trigger a style warning, and there is a new function mem-aptr that does for structure arrays what mem-aref does when the bare structure names are used. Please try it and report issues on cffi-devel. Thank you.

Note: the old system FSBV, which did what cffi-libffi does but in a clumsier way, is now obsolete and will not work with this version of CFFI.

Tags:

Nov. 29th, 2011

10:00 pm - How to specify foreign libraries

Does this look right to you?

(define-foreign-library frobnicator
  (cffi-features:unix "/usr/lib/libfrobnicate.so"))

If it does, you should read Anton Kovalenko's blog, whether or not you use CFFI. (Reblogging this because the original was evidently not picked up by Planet Lisp.)

Tags:

Sep. 25th, 2011

11:33 pm - CFFI-FSBV first light

Luis and I have been working on merging FSBV into CFFI as a new system CFFI-FSBV. This will allow calling foreign functions with structures passed and/or returned by value. It uses libffi.

The new system will have some advantages over the old. From the users perspective, there won't be a need to download a separate repository, and there won't be a need to directly use anything from CFFI-FSBV; all calls by value look just like regular CFFI with the appropriate type declarations. Based on a discussion on the cffi-devel mailing list, we decided and Luis implemented the syntax (:struct foo) for the structure (by value) and (:pointer (:struct foo)) for the pointer. Using 'foo directly is accepted for now with a warning, but is deprecated.

We now have, as astronomers say about a new telescope, "first light". Using my favorite complex number example from GSL

(defcstruct (complex-double :class complex-type)
  (dat :double :count 2))

and then with a few more definitions, we can call the foreign functions with the structure passed by value,
(foreign-funcall "gsl_complex_conjugate"
 (:struct complex-double) #C(3.0d0 4.0d0) (:struct complex-double))
#C(3.0d0 -4.0d0)
(foreign-funcall "gsl_complex_abs" (:struct complex-double) #C(3.0d0 4.0d0) :double)
5.0d0

Soon I hope we can reduce the "few more definitions", which do the translation between Lisp and C and are given in fsbv/examples.lisp, to something like what's in the old system's definition of defcstruct, with :constructor and :deconstructor options.

There's a lot more to do before this is ready for release, but we've got a start. The work is in the fsbv branch.

Tags:

Apr. 5th, 2011

10:40 pm - Kawa Google summer of code

Kawa is an implementation of lisp-like and dynamic languages (notably Scheme) that compiles to JVM. They are part of Google Summer of Code this year. One of their wishlist tasks is to enhance Common Lisp support. It would be really interesting to see work on this; I understand the deadline is this week for GSoC if anyone is interested.

Tags:

Feb. 5th, 2011

05:52 pm - Tablet usage in Ubuntu

Having just received a Thinkpad X201 Tablet, and installing Ubuntu 10.10, I decided that I should try the tablet function. There are some packages available for Ubuntu that work well: Xournal, which allows you to draw and write on the stylus, CellWriter, which recognizes your printed letters (one at a time, each in a box), and Magick Rotation, which rotates the screen from landscape to portrait. when you flip the cover to tablet mode. The first two are regular Ubuntu packages; Magick Rotation is downloaded from Launchpad and has a somewhat odd installation; it appears that you download a script that then wgets the real application.

They all work quite well. I particularly like xournal's feature that can read a PDF and save the written-on result to a PDF; even apart from the tablet functionality, just being able to fill in a PDF form and save it is nice (you can use the keys to write, so you don't need a stylus and touchscreen). Of course, you're not really filling in a form in the PDF sense, you are creating an image on top of the old one.

The buttons adjacent to the screen don't work, I'm not sure how to make them work.

Oct. 15th, 2010

10:50 pm - Quicklisp is now the official installation method for GSLL

Quicklisp is now the official installation method for GSLL. As far as I know it is the first and maybe so far the only project to recommend quicklisp as the installation method. Part of my motivation for writing GSLL was to lure^H^H^H^H induce people doing numerical computation into using Lisp, even if only as a desk calculator. A major impediment to that was loading everything up, and quicklisp goes a long way to solving that problem. There is a subtle difficulty in being able to push patches from a frequently-changing library like GSLL but I think we can work around that.

Tags:

Jun. 21st, 2010

10:36 pm - A CL workbook

I am looking for a way to conveniently keep a session of Lisp interaction. For example, when I write down a problem and solution for class, I define a succession of variables with defparameter (or a slightly more convenient form I've defined). In a later year, if I give the problem again with different numbers, I just change the defining parameters, and recalculate. This is clumsy, but it mostly works. What I'd really like is to define a few input parameters, and stepwise calculate the intermediate quantities. A single defun isn't appropriate because I need the intermediate values to show to the students. The defparameter approach is tedious and prone to error.

What I envision is a structure or class instance with "given values" provided at the beginning, and steps consisting of the form and the value. A new instance of the same problem would have the same forms to evaluate, but different values. It might also be nice to branch, i.e., at some step in the calculation, continue with a different set of calculations. but still be able to keep the tree structure (kind of like git for Lisp forms? hmmm).

Is there anything like this (in any language) out there? I tried some googling but if there's something along these lines out there, I'm not using the right keywords. For lack of a better word, I'm calling it a Lisp workbook or notebook. Bonus if it will integrate with LaTeX and/or org-mode.

Tags:

Navigate: (Previous 10 Entries)