Question on Arrays Code

Discussion of Common Lisp

Question on Arrays Code

Postby macrolyte » Sat Jun 28, 2014 9:05 am

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 :

Code: Select all

(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)))))

(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.
Posts: 40
Joined: Sat Jan 25, 2014 8:56 pm
Location: The wilderness of North America

Re: Question on Arrays Code

Postby Goheeca » Mon Jun 30, 2014 10:21 am

One of my first attempt would look like this, it depends on its intention (the performance aspect, etc.).
Code: Select all
(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.
User avatar
Posts: 271
Joined: Thu May 10, 2012 12:54 pm

Return to Common Lisp

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests