;;; Lisplab, level1-array.lisp ;;; Lisp array methods. ;;; Copyright (C) 2009 Joern Inge Vestgaarden ;;; ;;; This program is free software; you can redistribute it and/or modify ;;; it under the terms of the GNU General Public License as published by ;;; the Free Software Foundation; either version 2 of the License, or ;;; (at your option) any later version. ;;; ;;; This program is distributed in the hope that it will be useful, ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;;; GNU General Public License for more details. ;;; ;;; You should have received a copy of the GNU General Public License along ;;; with this program; if not, write to the Free Software Foundation, Inc., ;;; 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. (in-package :lisplab) (defmethod matrix-p ((a array)) "True for an array of rank 2" (= (rank a) 2)) (defmethod vector? ((a array)) "True for any array through row-major-aref" t) (defmethod dim ((a array) &optional axis) (if axis (array-dimension a axis) (array-dimensions a))) (defmethod size ((a array)) (reduce #'* (dim a))) (defmethod rank ((a array)) (array-rank a)) (defmethod rows ((a array)) (array-dimension a 0)) (defmethod cols ((a array)) (array-dimension a 1)) (defmethod element-type ((a array)) "Gets the element type of the array" (array-element-type a)) (defmethod mref ((a array) row col) "Row major order" (aref a row col)) (defmethod (setf mref) (value (a array) row col) (setf (aref a row col) (convert value (element-type a)))) (defmethod vref ((a array) idx) "Row major order" (row-major-aref a idx)) (defmethod (setf vref) (value (a array) idx) (setf (row-major-aref a idx) value)) (defmethod make-matrix-instance ((x (eql 'array)) dim value) (make-array dim :initial-element value)) (defmethod copy ((a array)) ;; TODO move to level2 (if (vectorp a) (copy-seq a) (let ((y (make-array (dim a) :element-type (element-type a)))) (dotimes (i (size a)) (setf (row-major-aref y i) (row-major-aref a i))) y)))