linear-algebra.lisp 2.8 KB
Newer Older
1 2
;; Define generic operators for GSL functions
;; Liam Healy 2011-01-01 09:51:47EST linear-algebra.lisp
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
;; Time-stamp: <2011-01-01 11:48:49EST linear-algebra.lisp>

;; Copyright 2011 Liam M. Healy
;; Distributed under the terms of the GNU General Public License
;;
;; 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 3 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, see <http://www.gnu.org/licenses/>.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

(in-package :antik)

(defmethod *i ((a grid:foreign-array) (b grid:foreign-array))
  (gsl:matrix-product a b))

(defmethod *i ((a grid:foreign-array) (b number))
  (gsl:scale b a))

(defmethod *i ((a number) (b grid:foreign-array))
  (gsl:scale a b))

(defmethod /i ((a grid:foreign-array) (b number))
  (gsl:scale (cl:/ b) a))

(defmethod +i ((a grid:foreign-array) (b grid:foreign-array))
  (gsl:elt+ a b))

(defmethod +i ((a grid:foreign-array) (b number))
  (gsl:elt+ a b))

(defmethod +i ((a number) (b grid:foreign-array))
  (gsl:elt+ b a))

(defmethod -i ((a grid:foreign-array) (b grid:foreign-array))
  (gsl:elt- a b))

(defmethod -i ((a grid:foreign-array) (b number))
  (gsl:elt+ a (cl:- b)))

(defmethod -i ((a number) (b grid:foreign-array))
  (gsl:elt+ (- b) a))

(defmethod expt ((a grid:matrix) (b integer))
  (cond ((= 1 b) a)
	((plusp b)		   ; use addition-chain exponentiation
	 (* a (expt a (1- b))))
	((minusp b)
58
	 (expt (invert-matrix a) (- b)))
59 60
	;; zero exponent; need to figure out non-square matrix
	(t (grid:identity-matrix (grid:dim0 a)))))
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

;;; Invert a matrix using LU
(export 'invert-matrix)
(defun invert-matrix (mat)
  "Invert the matrix."
  (let* ((dim (grid:dim0 mat))
	 (per (gsl:make-permutation dim))
	 (inv (grid:make-foreign-array 'double-float :dimensions (list dim dim))))
    (gsl:lu-decomposition mat per)
    (gsl:lu-invert mat per inv)))

#|
;;; Examples and unit test
;;; These are direct tests of matrix inversion
(save-test
 lu
 (grid:copy-to
  (invert-matrix
   (grid:make-foreign-array 'double-float
		:dimensions  '(2 2)
		:initial-contents '(1.0d0 2.0d0 3.0d0 4.0d0)))))

  (LISP-UNIT:ASSERT-NUMERICAL-EQUAL
   (LIST
    #2A((-1.9999999999999998d0 1.0d0)
	(1.4999999999999998d0 -0.49999999999999994d0)))
   (MULTIPLE-VALUE-LIST
    (GRID:COPY-TO
     (INVERT-MATRIX
      (GRID:MAKE-FOREIGN-ARRAY 'DOUBLE-FLOAT :DIMENSIONS '(2 2)
			       :INITIAL-CONTENTS
			       '((1.0d0 2.0d0) (3.0d0 4.0d0)))))))
|#