Documentation for polymorphic matrix functions

This commit is contained in:
Jens Axel Søgaard 2013-01-02 18:02:03 +01:00
parent ad579d07d6
commit 715b0a7be7

View File

@ -316,67 +316,100 @@ tolerant to floating-point error.
@section[#:tag "matrix:poly"]{Polymorphic Operations} @section[#:tag "matrix:poly"]{Polymorphic Operations}
@defthing[matrix-ref Procedure]{ @defproc[(matrix-ref [M (Matrix A)] [i Integer] [j Integer]) A]{
@;{(: matrix-ref (All (A) (Matrix A) Integer Integer -> A))} Returns the entry on row @racket[i] and column @racket[j].
@examples[#:eval untyped-eval
(define A (matrix ([1 2 3] [4 5 6])))
(matrix-ref A 0 2)
(matrix-ref A 1 2)]
} }
@defthing[submatrix Procedure]{ @defthing[submatrix Procedure]{
@;{ @;{ TODO
(: submatrix (All (A) (Matrix A) Slice-Spec Slice-Spec -> (Matrix A))) (: submatrix (All (A) (Matrix A) Slice-Spec Slice-Spec -> (Matrix A)))
(define (submatrix a row-range col-range) (define (submatrix a row-range col-range)
(array-slice-ref (ensure-matrix 'submatrix a) (list row-range col-range))) (array-slice-ref (ensure-matrix 'submatrix a) (list row-range col-range)))
} }
} }
@deftogether[(@defthing[matrix-row Procedure] @deftogether[(@defproc[(matrix-row [M (Matrix A)] [i Integer]) (Matrix A)]
@defthing[matrix-col Procedure])]{ @defproc[(matrix-col [M (Matrix A)] [j Integer]) (Matrix A)])]{
@;{ Returns the given row or column.
(: matrix-row (All (A) (Matrix A) Integer -> (Matrix A))) @examples[#:eval untyped-eval
(: matrix-col (All (A) (Matrix A) Integer -> (Matrix A))) (define A (matrix ([1 2 3] [4 5 6])))
} (matrix-row A 1)
(matrix-col A 0)]
} }
@defthing[matrix-diagonal Procedure]{ @defproc[(matrix-diagonal [M (Matrix A)]) (Array A)]{
@;{(: matrix-diagonal (All (A) ((Matrix A) -> (Array A))))} Returns array of the elements on the diagonal of the square matrix.
@examples[#:eval untyped-eval
(define A (matrix ([1 2 3] [4 5 6] [7 8 9])))
(matrix-diagonal A)]
} }
@deftogether[(@defthing[matrix-upper-triangle Procedure] @deftogether[(@defproc[(matrix-upper-triangle [M (Matrix A)]) (Matrix A)]
@defthing[matrix-lower-triangle Procedure])]{ @defproc[(matrix-lower-triangle [M (Matrix A)]) (Matrix A)])]{
@;{ The function @racket[matrix-upper-triangle] returns an upper
(: matrix-upper-triangle (All (A) ((Matrix A) -> (Matrix (U A 0))))) triangular matrix (entries below the diagonal are zero) with
(: matrix-lower-triangle (All (A) ((Matrix A) -> (Matrix (U A 0))))) elements from the given matrix. Likewise the function
} @racket[matrix-lower-triangle] returns an lower triangular
matrix.
@examples[#:eval untyped-eval
(define A (matrix ([1 2 3] [4 5 6] [7 8 9])))
(matrix-upper-triangle A)
(matrix-lower-triangle A)]
} }
@deftogether[(@defthing[matrix-rows Procedure] @deftogether[(@defproc[(matrix-rows [M (Matrix A)]) (Listof (Matrix A))]
@defthing[matrix-cols Procedure])]{ @defproc[(matrix-cols [M (Matrix A)]) (Listof (Matrix A))])]{
@;{ The functions respectively returns a list of the rows or columns
(: matrix-rows (All (A) (Matrix A) -> (Listof (Matrix A)))) of the matrix.
(: matrix-cols (All (A) (Matrix A) -> (Listof (Matrix A)))) @examples[#:eval untyped-eval
} (define A (matrix ([1 2 3] [4 5 6])))
(matrix-rows A)
(matrix-cols A)]
} }
@deftogether[(@defthing[matrix-augment Procedure] @deftogether[(@defproc[(matrix-augment [Ms (Listof (Matrix A))]) (Matrix A)]
@defthing[matrix-stack Procedure])]{ @defproc[(matrix-stack [Ms (Listof (Matrix A))]) (Matrix A)])]{
@;{ The function @racket[matrix-augment] returns a matrix whose columns are
(: matrix-augment (All (A) (Listof (Matrix A)) -> (Matrix A))) the columns of the matrices in @racket[Ms]. This implies that the matrices
(: matrix-stack (All (A) (Listof (Matrix A)) -> (Matrix A))) in list must have the same number of rows.
}
The function @racket[matrix-stack] returns a matrix whose rows are
the rows of the matrices in @racket[Ms]. This implies that the matrices
in list must have the same number of columns.
@examples[#:eval untyped-eval
(define A (matrix ([1 1] [1 1])))
(define B (matrix ([2 2] [2 2])))
(define C (matrix ([3 3] [3 3])))
(matrix-augment (list A B C))
(matrix-stack (list A B C))]
} }
@deftogether[(@defthing[matrix-map-rows Procedure] @deftogether[
@defthing[matrix-map-cols Procedure])]{ (@defproc[(matrix-map-rows
@;{ [f ((Matrix A) -> (Matrix B))] [M (Matrix A)]) (Matrix B)]
(: matrix-map-rows @defproc[(matrix-map-rows
(All (A B F) (case-> (((Matrix A) -> (Matrix B)) (Matrix A) -> (Matrix B)) [f ((Matrix A) -> (U #f (Matrix B)))] [M (Matrix A)] [fail (-> F)]) (Matrix B)]
(((Matrix A) -> (U #f (Matrix B))) (Matrix A) (-> F) @defproc[(matrix-map-cols
-> (U F (Matrix B)))))) [f ((Matrix A) -> (Matrix B))] [M (Matrix A)]) (Matrix B)]
@defproc[(matrix-map-cols
[f ((Matrix A) -> (U #f (Matrix B)))] [M (Matrix A)] [fail (-> F)]) (Matrix B)])]{
(: matrix-map-cols In the simple case the function @racket[matrix-map-rows] applies the function @racket[f]
(All (A B F) (case-> (((Matrix A) -> (Matrix B)) (Matrix A) -> (Matrix B)) to each row of @racket[M]. If the rows are called @racket[r0], @racket[r1], ... then
(((Matrix A) -> (U #f (Matrix B))) (Matrix A) (-> F) the result matrix has the rows @racket[(f r0)], @racket[(f r1)], ... .
-> (U F (Matrix B)))))) In the three argument case, the result of @racket[(fail)] is used,
} if @racket[f] returns @racket[#f].
The function @racket[matrix-map-cols] works likewise but on rows.
@examples[#:eval untyped-eval
(define A (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 A)]
} }