Specialization in vectors and matrices

I use common-lisp for my real-time graphical experiments, and so far this has been wonderful. My speed and usability requirements for cffi compatibility mean that I use typed arrays. One area of ​​code that really seems ugly is the generic versions of my matrix and vector math functions. Since CLOS can not specializes in array length, 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 does not seem to be correct . I saw extensions for various CL implementations and heard about the promise of SS.

I stepped back from this, as I was afraid that it would break functionality with some CL implementations, but I recently saw a Closer to Mop project.

Key question: Does MOP provide a more efficient length specialization method? Is there any area / methods that I should focus on?

+6
source share
1 answer

Your code suits me, and what you use are tags.

 (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***) 
0
source

Source: https://habr.com/ru/post/955112/


All Articles