File: matrix.lsp

package info (click to toggle)
xlispstat 3.52.14-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,560 kB
  • ctags: 12,676
  • sloc: ansic: 91,357; lisp: 21,759; sh: 1,525; makefile: 521; csh: 1
file content (35 lines) | stat: -rw-r--r-- 1,178 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
; Matrix functions by Tom Almy
; Multidimensional arrays are implemented here as arrays of arrays
; make-array is redefined to mimic common lisp
; Unfortunately AREF cannot be changed since its operation in setf is
; "wired in", so we will use a new (macro) function MREF


(when (eq (type-of (symbol-function 'make-array))
	  'subr)
      (setf (symbol-function 'orig-make-array)
	    (symbol-function 'make-array)))

(defun make-array (dims &key initial)
    (cond ((null dims) initial)
	  ((atom dims) (make-array (list dims) :initial initial))
	  (t (let ((result (orig-make-array (first dims))))
	       (when (or (rest dims) initial)
		     (dotimes (i (first dims))
			      (setf (aref result i)
				    (make-array (rest dims) :initial initial))))
	       result))))

(defun mref (matrix &rest indices)
    (dolist (index indices)
	    (setq matrix (aref matrix index)))
    matrix)

(setf (get 'mref '*setf*)
      #'(lambda (mat &rest arglist)
	  (do ((index (first arglist) (first remainder))
	       (remainder (rest arglist) (rest remainder)))
	      ((null (rest remainder))
	       (setf (aref mat index) (first remainder)))
	    (setf mat (aref mat index)))))