Specialising on Vectors and Matrices
Asked Answered
C

1

6

I am using common-lisp for my real-time graphics experiments and so far it has being great. My requirements for speed and easy compatibility with cffi mean I am using 'typed' arrays. The one area of the code which really feels ugly is the generic versions of my matrix and vector math functions. As CLOS cant specialize on length of an array I am doing something like this:

(defun v+ (vec-a vec-b)
   (%v+ vec-a vec-b (length a) (length b)))

(defmethod %v+ (va vb (la (eql 3)) (lb (eql 3)))
   ***CODE HERE***)

This works but doesn't feel right. I have seen extensions to various CL implementations and heard about the promise of MOP.

I have steered away from this as I feared it would break functionality with some CL implementations but I have more recently seen the Closer-to-Mop project.

Core Question: Does MOP provide a more efficient method for specializing on length? Are there any area/techniques I should focus on?

Chophouse answered 2/10, 2013 at 15:2 Comment(5)
why would you use CLOS when the GF does no dispatching...Moonlighting
sorry v+ should have been defun rather than defmethod. Other than that I dont think I understand your question. %v+ is generic to handle the various lengths of vector as I commented to Menschenkindlein the recommendation came from a SO question #11996860. My only reason for asking this is to find out whether MOP allows for specialising on an array of a given length. maybe I should remove the background section of the question as it is not the point, just an example of a use case.Chophouse
It makes little sense to use CLOS and the MOP to write functions for just three vector types. Since you mention 'speed' I would use functions which can be inlined. If I'd need some short way of writing this stuff, I would write a macro for that...Moonlighting
Your aboslutely right, and I do that. All the core vector library is just inlineable functions. However for speed of experimentation I also have the generic versions. which call the length specific versions. I really appreciate the help in getting in the correct direction. I guess I should just clean my code up a bit and then read AMOP as a unrelated project.Chophouse
This question seems a bit daft now. I really just need to go read. Thanks to everyone for the suggestions though.Chophouse
B
0

Your code feels right for me, and what you are using is type tagging.

(defmethod v+ (vec-a vec-b)
   (labels ((find-tag (vec)
               (if (> (length vec) 3)
                   :more-than-3
                   :less-than-4)))
      (%v+ vec-a vec-b (find-tag a) (find-tag b)))

(defmethod %v+ (va vb (va-tag (eql :less-than-4)) (vb-tag (eql :less-than-4)))
   ***CODE HERE***)
Bijouterie answered 2/10, 2013 at 18:31 Comment(1)
Cheers, yeah it works just fine and in fact it was the method from one of my old questions here #11996860. However the question is more about how MOP increases possibilities with regards to specialising methodsChophouse

© 2022 - 2024 — McMap. All rights reserved.