On this page:
top
up

7.6Polymorphic OperationsπŸ”— i

procedure

( matrix-ref Mij)A

M:(Matrix A)
Returns the entry on row i and column j.

Examples:
> (define M(matrix ([123][456])))
> (matrix-ref M02)

3

> (matrix-ref M12)

6

procedure

( matrix-row Mi)(Matrix A)

M:(Matrix A)

procedure

( matrix-col Mj)(Matrix A)

M:(Matrix A)
Returns the ith row or jth column as a matrix.

Examples:
> (define M(matrix ([123][456])))
> (matrix-row M1)

(array #[#[4 5 6]])

> (matrix-col M0)

(array #[#[1] #[4]])

procedure

( submatrix Misjs)(Array A)

M:(Matrix A)
Returns a submatrix or subarray of M, where is and js specify respectively the rows and columns to keep. Like array-slice-ref , but constrained so the result has exactly two axes.

Examples:
> (submatrix (identity-matrix 5)(:: 1#f2)(:: ))

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes (U One Zero)))

#<syntax:.../array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:.../array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:.../array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[#[0 1 0 0 0] #[0 0 0 1 0]])

> (submatrix (identity-matrix 5)'()'(124))

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes (U One Zero)))

#<syntax:.../array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:.../array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:.../array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(array #[])

Note that submatrix may return an empty array, which is not a matrix.

procedure

( matrix-diagonal M)(Array A)

M:(Matrix A)
Returns array of the entries on the diagonal of M.

Example:
(matrix ([123][456][789])))

(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
The function matrix-upper-triangle returns an upper triangular matrix (entries below the diagonal have the value zero) with entries from the given matrix. Likewise the function matrix-lower-triangle returns a lower triangular matrix.

Examples:
> (define M(array+ (array 1)(axis-index-array #(57)1)))
> M

- : #(struct:Array

(Indexes Index (Boxof Boolean) (-> Void) (-> Indexes Positive-Fixnum))

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:56:13 prop:equal+hash>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:55:13 prop:custom-write>

#<syntax:build/user/8.18/pkgs/math-lib/math/private/array/typed-array-struct.rkt:54:13 prop:custom-print-quotable>)

(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]])

- : (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]])

- : (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]])

- : (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)
The functions respectively returns a list of the rows or columns of the matrix.

Examples:
> (define M(matrix ([123][456])))

(list (array #[#[1 2 3]]) (array #[#[4 5 6]]))

(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))
The function matrix-augment returns a matrix whose columns are the columns of the matrices in Ms. The matrices in list must have the same number of rows.

The function matrix-stack returns a matrix whose rows are the rows of the matrices in Ms. The matrices in list must have the same number of columns.

Examples:
> (define M0(matrix ([11][11])))
> (define M1(matrix ([22][22])))
> (define M2(matrix ([33][33])))
> (matrix-augment (list M0M1M2))

(array #[#[1 1 2 2 3 3] #[1 1 2 2 3 3]])

> (matrix-stack (list M0M1M2))

(array #[#[1 1] #[1 1] #[2 2] #[2 2] #[3 3] #[3 3]])

procedure

( matrix-set-col Midxnew-col)(Matrix A)

M:(Matrix A)
idx:Integer
new-col:(Matrix A)

procedure

( matrix-set-row Midxnew-row)(Matrix A)

M:(Matrix A)
idx:Integer
new-row:(Matrix A)
The function matrix-set-col returns a matrix whose idxth column is new-col. The function matrix-set-row returns a matrix whose idxth row is new-row.

Examples:
> (define mat(matrix [[123]
[456]
[789]]))
> (define new-col(col-matrix [-1-2-3]))
> (define new-row(row-matrix [-1-2-3]))
> (matrix-set-col mat0new-col)

(array #[#[-1 2 3] #[-2 5 6] #[-3 8 9]])

> (matrix-set-row mat0new-row)

(array #[#[-1 -2 -3] #[4 5 6] #[7 8 9]])

procedure

( matrix-map-rows fM)(Matrix B)

f:((Matrix A)-> (Matrix B))
M:(Matrix A)
(matrix-map-rows fMfail)(U F(Matrix B))
f:((Matrix A)-> (U #f(Matrix B)))
M:(Matrix A)
fail:(-> F)
The two-argument case applies the function f to each row of M. If the rows are called r0, r1, ..., the result matrix has the rows (fr0), (fr1), ....

Examples:
> (define M(matrix ([123][456][789][101112])))
> (define (double-rowr)(matrix-scale r2))
> (matrix-map-rows double-rowM)

(array #[#[2 4 6] #[8 10 12] #[14 16 18] #[20 22 24]])

In the three argument case, if f returns #f, the result of (fail) is returned:
> (define Z(make-matrix 440))
> Z

- : (Array Zero)

(array #[#[0 0 0 0] #[0 0 0 0] #[0 0 0 0] #[0 0 0 0]])

(matrix-normalize r2(λ ()#f)))
Z
(λ ()'FAILURE))

- : (U 'FAILURE (Array Real))

'FAILURE

procedure

( matrix-map-cols fM)(Matrix B)

f:((Matrix A)-> (Matrix B))
M:(Matrix A)
(matrix-map-cols fMfail)(U F(Matrix B))
f:((Matrix A)-> (U #f(Matrix B)))
M:(Matrix A)
fail:(-> F)
Like matrix-map-rows , but maps f over columns.

top
up

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /