linear-algebra.lisp 1.22 KB
Newer Older
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
36
37
38
39
40
41
42
43
44
45
;; Define generic operators for GSL functions
;; Liam Healy 2011-01-01 09:51:47EST linear-algebra.lisp
;; Time-stamp: <2011-01-01 10:58:18EST linear-algebra.lisp>
;; $Id: $

(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)
	 (expt (gsl:invert-matrix a) (- b)))
	;; zero exponent; need to figure out non-square matrix
	(t (grid:identity-matrix (grid:dim0 a)))))