7.6 Polymorphic Operations
procedure
(matrix-ref M i j) → A
M : (Matrix A) i : Integer j : Integer
> (define M (matrix ([1 2 3] [4 5 6]))) > (matrix-ref M 0 2) 3
> (matrix-ref M 1 2) 6
procedure
(matrix-row M i) → (Matrix A)
M : (Matrix A) i : Integer
procedure
(matrix-col M j) → (Matrix A)
M : (Matrix A) j : Integer
> (define M (matrix ([1 2 3] [4 5 6]))) > (matrix-row M 1) (array #[#[4 5 6]])
> (matrix-col M 0) (array #[#[1] #[4]])
procedure
M : (Matrix A) is : (U Slice (Sequenceof Integer)) js : (U Slice (Sequenceof Integer))
> (submatrix (identity-matrix 5) (:: 1 #f 2) (::)) - : (Array (U One Zero))
(array #[#[0 1 0 0 0] #[0 0 0 1 0]])
> (submatrix (identity-matrix 5) '() '(1 2 4)) - : (Array (U One Zero))
(array #[])
procedure
(matrix-diagonal M) → (Array A)
M : (Matrix A)
> (matrix-diagonal (matrix ([1 2 3] [4 5 6] [7 8 9]))) (array #[1 5 9])
procedure
(matrix-upper-triangle M [zero]) → (Matrix A)
M : (Matrix A) zero : A = 0
procedure
(matrix-lower-triangle M [zero]) → (Matrix A)
M : (Matrix A) zero : A = 0
> (define M (array+ (array 1) (axis-index-array #(5 7) 1))) > M - : (Array Positive-Fixnum)
(array
#[#[1 2 3 4 5 6 7]
#[1 2 3 4 5 6 7]
#[1 2 3 4 5 6 7]
#[1 2 3 4 5 6 7]
#[1 2 3 4 5 6 7]])
> (matrix-upper-triangle M) - : (Array Nonnegative-Fixnum)
(array
#[#[1 2 3 4 5 6 7]
#[0 2 3 4 5 6 7]
#[0 0 3 4 5 6 7]
#[0 0 0 4 5 6 7]
#[0 0 0 0 5 6 7]])
> (matrix-lower-triangle M) - : (Array Nonnegative-Fixnum)
(array
#[#[1 0 0 0 0 0 0]
#[1 2 0 0 0 0 0]
#[1 2 3 0 0 0 0]
#[1 2 3 4 0 0 0]
#[1 2 3 4 5 0 0]])
> (matrix-lower-triangle (array->flarray M) 0.0) - : (Array Flonum)
(array
#[#[1.0 0.0 0.0 0.0 0.0 0.0 0.0]
#[1.0 2.0 0.0 0.0 0.0 0.0 0.0]
#[1.0 2.0 3.0 0.0 0.0 0.0 0.0]
#[1.0 2.0 3.0 4.0 0.0 0.0 0.0]
#[1.0 2.0 3.0 4.0 5.0 0.0 0.0]])
procedure
(matrix-rows M) → (Listof (Matrix A))
M : (Matrix A)
procedure
(matrix-cols M) → (Listof (Matrix A))
M : (Matrix A)
> (define M (matrix ([1 2 3] [4 5 6]))) > (matrix-rows M) (list (array #[#[1 2 3]]) (array #[#[4 5 6]]))
> (matrix-cols M) (list (array #[#[1] #[4]]) (array #[#[2] #[5]]) (array #[#[3] #[6]]))
procedure
(matrix-augment Ms) → (Matrix A)
Ms : (Listof (Matrix A))
procedure
(matrix-stack Ms) → (Matrix A)
Ms : (Listof (Matrix A))
> (define M0 (matrix ([1 1] [1 1]))) > (define M1 (matrix ([2 2] [2 2]))) > (define M2 (matrix ([3 3] [3 3]))) > (matrix-augment (list M0 M1 M2)) (array #[#[1 1 2 2 3 3] #[1 1 2 2 3 3]])
> (matrix-stack (list M0 M1 M2)) (array #[#[1 1] #[1 1] #[2 2] #[2 2] #[3 3] #[3 3]])
procedure
(matrix-map-rows f M) → (Matrix B)
f : ((Matrix A) -> (Matrix B)) M : (Matrix A) (matrix-map-rows f M fail) → (U F (Matrix B)) f : ((Matrix A) -> (U #f (Matrix B))) M : (Matrix A) fail : (-> F)
> (define M (matrix ([1 2 3] [4 5 6] [7 8 9] [10 11 12]))) > (define (double-row r) (matrix-scale r 2)) > (matrix-map-rows double-row M) (array #[#[2 4 6] #[8 10 12] #[14 16 18] #[20 22 24]])
> (define Z (make-matrix 4 4 0)) > Z - : (Array Zero)
(array #[#[0 0 0 0] #[0 0 0 0] #[0 0 0 0] #[0 0 0 0]])
> (matrix-map-rows (λ: ([r : (Matrix Real)]) (matrix-normalize r 2 (λ () #f))) Z (λ () 'FAILURE)) - : (U 'FAILURE (Array Real))
'FAILURE
procedure
(matrix-map-cols f M) → (Matrix B)
f : ((Matrix A) -> (Matrix B)) M : (Matrix A) (matrix-map-cols f M fail) → (U F (Matrix B)) f : ((Matrix A) -> (U #f (Matrix B))) M : (Matrix A) fail : (-> F)