;;; Lisplab, level1-matrix.lisp ;;; Level1, matrix basic 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. ;;; Note: use the slot-value rather than the methods, since slot-value is faster. ;;; TODO: clean up (in-package :lisplab) (defmethod matrix-p ((x matrix-base)) t) (defmethod rank ((matrix matrix-base)) 2) (defmethod dim ((matrix matrix-base) &optional direction) (if direction (ecase direction (0 (rows matrix)) (1 (cols matrix))) (list (rows matrix) (cols matrix)))) (defun print-matrix-contents (m &key (stream *standard-output*) (pr #'princ) (rmax (rows m)) (cmax (cols m)) (indent 0) (braket-p nil)) "Utility function that prints the matrix elements in a human-friendly way." ;; TODO move among other utility functions? (let ((rows (min (rows m) rmax)) (cols (min (cols m) cmax)) (indfmt (if (zerop indent) "" (format nil "~~~aT" indent)))) (dotimes (i rows) (when (> i 0) (format stream indfmt)) (when braket-p (princ "(" stream)) (dotimes (j cols) (funcall pr (mref m i j) stream) (when (< j (1- cols)) (princ " " stream))) (when (< cols (cols m)) (format stream " ...")) (when braket-p (princ ")" stream)) (when (< i (1- rows)) (princ #\newline stream))) (when (< rows (rows m)) (format stream indfmt) (format stream "~& ...")))) (defmethod print-object ((matrix matrix-base) stream) "Prints matrix as an unreadable object. The number of printed rows and columns is limited by *lisplab-print-size* and format is given by *lisplab-element-printer*." (print-unreadable-object (matrix stream :type t :identity t) (format stream " ~ax~a" (rows matrix) (cols matrix)) (when *lisplab-print-size* (format stream "~&") (print-matrix-contents matrix :stream stream :pr (if *lisplab-element-printer* *lisplab-element-printer* #'princ) :rmax (if (eq *lisplab-print-size* t) (rows matrix) *lisplab-print-size*) :cmax (if (eq *lisplab-print-size* t) (cols matrix) *lisplab-print-size*) :indent 0) (format stream "~%")))) ;;; Spezialized for square matrices (defmethod rows ((matrix structure-square)) (slot-value matrix 'rowcols)) (defmethod cols ((matrix structure-square)) (slot-value matrix 'rowcols))