This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Question on Arrays Code

2 posts · 2752 views

Hi. I was looking at implementing some matrix functions and have some questions on style and efficiency. My use of arrays is limited, so it took me a few hours to come up with this :
(defun mult-arrays (&rest arrays)
  (let ((temp-array (make-array (array-dimension (car arrays) 0) :initial-element 1))) ;; use temporary array as accumulator 
    (dolist (aX arrays)
      (dotimes (i (array-dimension aX 0))
	     (setf (aref temp-array i) (* (aref temp-array i) (aref aX i)))))
    temp-array))

(mult-arrays #(23 45 17 9 27))                ;==> #(23 45 17 9 27)

(mult-arrays #(12 3 0 8) #(2 3 8 12))         ;==> #(24 9 0 96)
And yes, I know I need to implement bounds checking... My concern is are there better solutions for this? Thanks.

Re: Question on Arrays Code

One of my first attempt would look like this, it depends on its intention (the performance aspect, etc.).
(defun scalar-product (a b)
  (dotimes (index (array-dimension a 0) a)
    (setf (aref a index) (* (aref a index)
                            (aref b index)))))

(defun mult-arrays (&rest arrays)
  (when arrays (reduce #'scalar-product arrays)))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.