| 1 |
;;; https://www.scheme.com/tspl3/examples.html |
| 2 |
;;; make-matrix creates a matrix (a vector of vectors). |
| 3 |
(define make-matrix |
| 4 |
(lambda (rows columns) |
| 5 |
(do ((m (make-vector rows)) |
| 6 |
(i 0 (+ i 1))) |
| 7 |
((= i rows) m) |
| 8 |
(vector-set! m i (make-vector columns))))) |
| 9 |
|
| 10 |
;;; matrix? checks to see if its argument is a matrix. |
| 11 |
;;; It isn't foolproof, but it's generally good enough. |
| 12 |
(define matrix? |
| 13 |
(lambda (x) |
| 14 |
(and (vector? x) |
| 15 |
(> (vector-length x) 0) |
| 16 |
(vector? (vector-ref x 0))))) |
| 17 |
|
| 18 |
;; matrix-rows returns the number of rows in a matrix. |
| 19 |
(define matrix-rows |
| 20 |
(lambda (x) |
| 21 |
(vector-length x))) |
| 22 |
|
| 23 |
;; matrix-columns returns the number of columns in a matrix. |
| 24 |
(define matrix-columns |
| 25 |
(lambda (x) |
| 26 |
(vector-length (vector-ref x 0)))) |
| 27 |
|
| 28 |
;;; matrix-ref returns the jth element of the ith row. |
| 29 |
(define matrix-ref |
| 30 |
(lambda (m i j) |
| 31 |
(vector-ref (vector-ref m i) j))) |
| 32 |
|
| 33 |
;;; matrix-set! changes the jth element of the ith row. |
| 34 |
(define matrix-set! |
| 35 |
(lambda (m i j x) |
| 36 |
(vector-set! (vector-ref m i) j x))) |
| 37 |
|
| 38 |
;;; mul is the generic matrix/scalar multiplication procedure |
| 39 |
(define mul |
| 40 |
(lambda (x y) |
| 41 |
;; mat-sca-mul multiplies a matrix by a scalar. |
| 42 |
(define mat-sca-mul |
| 43 |
(lambda (m x) |
| 44 |
(let* ((nr (matrix-rows m)) |
| 45 |
(nc (matrix-columns m)) |
| 46 |
(r (make-matrix nr nc))) |
| 47 |
(do ((i 0 (+ i 1))) |
| 48 |
((= i nr) r) |
| 49 |
(do ((j 0 (+ j 1))) |
| 50 |
((= j nc)) |
| 51 |
(matrix-set! r i j |
| 52 |
(* x (matrix-ref m i j)))))))) |
| 53 |
|
| 54 |
;; mat-mat-mul multiplies one matrix by another, after verifying |
| 55 |
;; that the first matrix has as many columns as the second |
| 56 |
;; matrix has rows. |
| 57 |
(define mat-mat-mul |
| 58 |
(lambda (m1 m2) |
| 59 |
(let* ((nr1 (matrix-rows m1)) |
| 60 |
(nr2 (matrix-rows m2)) |
| 61 |
(nc2 (matrix-columns m2)) |
| 62 |
(r (make-matrix nr1 nc2))) |
| 63 |
(if (not (= (matrix-columns m1) nr2)) |
| 64 |
(match-error m1 m2)) |
| 65 |
(do ((i 0 (+ i 1))) |
| 66 |
((= i nr1) r) |
| 67 |
(do ((j 0 (+ j 1))) |
| 68 |
((= j nc2)) |
| 69 |
(do ((k 0 (+ k 1)) |
| 70 |
(a 0 |
| 71 |
(+ a |
| 72 |
(* (matrix-ref m1 i k) |
| 73 |
(matrix-ref m2 k j))))) |
| 74 |
((= k nr2) |
| 75 |
(matrix-set! r i j a)))))))) |
| 76 |
|
| 77 |
;; type-error is called to complain when mul receives an invalid |
| 78 |
;; type of argument. |
| 79 |
(define type-error |
| 80 |
(lambda (what) |
| 81 |
(error 'mul |
| 82 |
"~s is not a number or matrix" |
| 83 |
what))) |
| 84 |
|
| 85 |
;; match-error is called to complain when mul receives a pair of |
| 86 |
;; incompatible arguments. |
| 87 |
(define match-error |
| 88 |
(lambda (what1 what2) |
| 89 |
(error 'mul |
| 90 |
"~s and ~s are incompatible operands" |
| 91 |
what1 |
| 92 |
what2))) |
| 93 |
|
| 94 |
;; body of mul; dispatch based on input types |
| 95 |
(cond |
| 96 |
((number? x) |
| 97 |
(cond |
| 98 |
((number? y) (* x y)) |
| 99 |
((matrix? y) (mat-sca-mul y x)) |
| 100 |
(else (type-error y)))) |
| 101 |
((matrix? x) |
| 102 |
(cond |
| 103 |
((number? y) (mat-sca-mul x y)) |
| 104 |
((matrix? y) (mat-mat-mul x y)) |
| 105 |
(else (type-error y)))) |
| 106 |
(else (type-error x))))) |