OpenGL Mathematics (GLM) for Racket
OpenGL Mathematics (GLM) for Racket is a Racket port of OpenGL Mathematics (GLM), a mathematics library for graphics software based on the OpenGL Shading Language (GLSL) specifications.
GLM for Racket provides GLM’s core functions and data types along with support for popular Racket idioms such as sequence-based looping, variadic keyword-based constructors, and match-based de-structuring.
1 Swizzling
#lang glm/swizzle | package: glm |
The glm/swizzle meta-language customizes the syntax of identifiers to enable component swizzling.
#lang glm/swizzle racket/base (define v (vec4 1 2 3 4)) v.x ;; expands to (vec-ref v 0) v.xy ;; expands to (vec2 (vec-ref v 0) (vec-ref v 1))
All of the bindings exported by glm/vector-types are available whenever swizzling is enabled.
2 GLM Core
(require glm) | package: glm |
This module re-exports glm/matrix-types and glm/vector-types.
2.1 Vector Types
(require glm/vector-types) | package: glm |
This module re-exports glm/bvec, glm/dvec, glm/ivec, glm/uvec, and glm/vec.
2.1.1 Single Precision Floats
(require glm/vec) | package: glm |
A vector is an array of 32-bit floating point component values.
Two vectors are equal? iff they have the same number of components and each pair of consecutive components are =.
procedure
len : (or/c exact-positive-integer? #f) = #f fill : (or/c real? 'no-fill) = 0 x : (or/c vec? real?)
> (vec 1 2/3 4.5 -670000000.0) (vec 1 0.67 4.5 -670000000)
The components of any vectors in the xs are spliced into the final component list.
> (vec (vec 1 2) 3 4 (vec 5 6 (vec 7 8)) 9) (vec 1 2 3 4 5 6 7 8 9)
When both len and fill are numbers, the final component list can have any number of values. If its length exceeds len, the excess values are truncated; otherwise, any missing values default to fill.
> (vec #:length 5 1 2 3) (vec 1 2 3 0 0)
> (vec #:length 5 1 2 3 4 5 6 7) (vec 1 2 3 4 5)
When only len is a number, the final component list must have exactly len values. Giving more or less than len values is an error.
> (vec #:length 3 #:fill 'no-fill (vec 1 2) (vec 3 4)) vec3: contract violation
expected: exactly 3 values
given: '(1.0 2.0 3.0 4.0)
> (vec #:length 3 #:fill 'no-fill) vec3: contract violation
expected: exactly 3 values
given: '()
If len ≥ 2 and only one component value is given, every value defaults to it.
> (vec #:length 3 7) (vec 7 7 7)
vec is also a match pattern identifier for deconstructing vectors.
> (match (vec 1 2 3) [(vec a b c) (+ a b c)]) 6.0
Vectors of indeterminate length can be matched with a final #:rest pattern.
> (match (vec 1 2 3 4 5) [(vec x y z #:rest tail) (list x y z (apply + tail))]) '(1.0 2.0 3.0 9.0)
procedure
data : array? len : exact-positive-integer? fixed? : boolean? fill : (or/c real? 'no-fill)
If fixed? is true and len is between 0 and 4, the vector displays its length-suffixed name. Otherwise, it uses the more general (vec ...) form.
> (define v1 (vec #:length 5 1)) > (define v2 (make-vec (vec-data v1) 3 #t 0)) > v2 (vec3 1 1 1)
> (vec-set! v2 2 0) > v1 (vec 1 1 0 1 1)
> (define v (vec 1 2 3)) > (sequence->list (in-array (vec-data v))) '(1.0 2.0 3.0)
procedure
v : vec?
> (vec-length (vec 0 1 2)) 3
procedure
(make-vec-data x ...) → array?
x : real?
> (array-ref (make-vec-data 9 8 7) 2) 7.0
> (define v1 (vec 1 2 3)) > (eq? (vec-data v1) (vec-data v1)) #t
> (define v2 (vec-copy v1)) > (eq? (vec-data v1) (vec-data v2)) #f
procedure
v : vec? i : exact-nonnegative-integer?
procedure
v : vec? i : exact-nonnegative-integer? x : real?
procedure
(vec->f32vector v) → f32vector?
v : vec?
> (define v (vec 9 8 7)) > (define fv (vec->f32vector v)) > (f32vector-set! fv 1 0.0) > v (vec 9 0 7)
procedure
v : vec? start : exact-nonnegative-integer? = 0 stop : (or/c exact-integer? #f) = #f step : (and/c exact-integer? (not/c zero?)) = 1
The optional arguments start, stop, and step are as in in-vector.
syntax
(for/vec maybe-length (for-clause ...) body-or-break ... body)
syntax
(for*/vec maybe-length (for-clause ...) body-or-break ... body)
> (for/vec ([x (in-list '(9 8 7))]) (sub1 x)) (vec 8 7 6)
> (for*/vec ([x (in-range 3)] [y (in-range 3)]) (* x (+ x y))) (vec 0 0 0 1 2 3 4 6 8)
If the optional #:length clause is specified, it determines the length of the result vector.
> (for/vec #:length 5 ([x (in-list '(1 2 3))]) (add1 x)) (vec 2 3 4 0 0)
If an optional #:fill clause is specified and its value is not 'no-fill, it determines the value of any unspecified components.
> (for/vec #:length 4 #:fill -1 ([x (in-naturals)] #:break (= x 2)) x) (vec 0 1 -1 -1)
> (for*/vec #:length 10 #:fill -1 ([x (in-range 3)] [y (in-range 3)]) (+ x (* x y))) (vec 0 0 0 1 2 3 2 4 6 -1)
procedure
v : vec?
> ((vec-constructor (vec 1 2 3)) 4 5) (vec 4 5 0)
> (vec=! v (vec 7 8)) > v (vec 7 8 6)
> (vec=! v (vec 9 10 11 12)) > v (vec 9 10 11)
procedure
a : (or/c vec? real?)
procedure
a : (or/c vec? real?)
procedure
a : (or/c vec? real?)
procedure
a : (or/c vec? real?)
> (vec+ 1 2 3) 6
> (vec- 3 (vec 4 5)) (vec -1 -2)
> (vec* (vec 6 7) 8) (vec 48 56)
> (vec/ (vec 9 0) (vec 3 11)) (vec 3 0)
procedure
a : vec? b : (or/c vec? real?)
procedure
a : vec? b : (or/c vec? real?)
procedure
a : vec? b : (or/c vec? real?)
procedure
a : vec? b : (or/c vec? real?)
> (define v (vec 1 2)) > (vec+=! v (vec 3 4)) > v (vec 4 6)
> (vec-=! v 5) > v (vec -1 1)
> (vec*=! v (vec 6 7 8)) > v (vec -6 7)
> (vec/=! v (vec 9 10)) > v (vec -0.67 0.7)
parameter
(current-vec-precision precision) → void? precision : exact-nonnegative-integer?
= 2
> (current-vec-precision 3) > (vec 1/2 2/3 3/4) (vec 0.5 0.667 0.75)
> (parameterize ([current-vec-precision 6]) (println (vec 1/2 2/3 3/4))) (vec 0.5 0.666667 0.75)
procedure
len : exact-positive-integer?
= (_array _float len)
syntax
(define-vec-type id #:length len)
A define-vec-type form defines 2 names:
id, a vector constructor function that takes up to len arguments and returns a new vector.
id?, a predicate procedure that returns #t for vectors of length len and #f for any other value.
> (define-vec-type vec5 #:length 5) > (vec5? (vec5)) #t
> (vec5? (vec 1 2 3)) #f
id is also a match pattern identifier similar to vec, except it only matches vectors with exactly len components.
> (match (vec5 1 2 3 4 5) [(vec5 a b c d e) (reverse (list a b c d e))]) '(5.0 4.0 3.0 2.0 1.0)
> (match (vec5 1) [(vec5 _ _ #:rest xs) (apply + xs)]) 3.0
> (match (vec 1 2 3) [(vec5 #:rest xs) (apply + xs)]) match: no matching clause for (vec 1 2 3)
> (match (vec 1 2 3 4 5 6 7) [(vec5 #:rest xs) (apply + xs)]) match: no matching clause for (vec 1 2 3 4 5 6 7)
2.1.1.1 vec Types
procedure
procedure
procedure
procedure
procedure
x : real?
procedure
x : real? y : real?
procedure
x : real? y : real? z : real?
procedure
x : real? y : real? z : real? w : real?
2.1.2 Double Precision Floats
(require glm/dvec) | package: glm |
Arrays of 64-bit floating point numbers.
procedure
v : any/c
procedure
len : (or/c exact-positive-integer? #f) = #f fill : (or/c real? 'no-fill) = 0 x : (or/c dvec? real?)
procedure
data : array? len : exact-positive-integer? fixed? : boolean? fill : (or/c real? 'no-fill)
procedure
v : dvec?
procedure
v : dvec?
procedure
(make-dvec-data x ...) → array?
x : real?
procedure
v : dvec?
procedure
v : dvec?
procedure
v : dvec? i : exact-nonnegative-integer?
procedure
v : dvec? i : exact-nonnegative-integer? x : number?
procedure
(dvec->list v) → (listof flonum?)
v : dvec?
procedure
(dvec->f64vector v) → f64vector?
v : dvec?
procedure
v : dvec? start : exact-nonnegative-integer? = 0 stop : (or/c exact-integer? #f) = #f step : (and/c exact-integer? (not/c zero?)) = 1
syntax
(for/dvec maybe-length (for-clause ...) body-or-break ... body)
syntax
(for*/dvec maybe-length (for-clause ...) body-or-break ... body)
procedure
v : dvec?
procedure
v : dvec? u : dvec?
procedure
a : (or/c dvec? real?)
procedure
a : (or/c dvec? real?)
procedure
a : (or/c dvec? real?)
procedure
a : (or/c dvec? real?)
procedure
a : dvec? b : (or/c dvec? real?)
procedure
a : dvec? b : (or/c dvec? real?)
procedure
a : dvec? b : (or/c dvec? real?)
procedure
a : dvec? b : (or/c dvec? real?)
procedure
v : dvec?
procedure
v : dvec?
procedure
v : dvec?
procedure
v : dvec?
parameter
(current-dvec-precision precision) → void? precision : exact-nonnegative-integer?
= 2
procedure
len : exact-nonnegative-integer?
= (_array _double len)
syntax
(define-dvec-type id #:length len)
2.1.2.1 dvec Types
procedure
procedure
procedure
procedure
procedure
x : real?
procedure
x : real? y : real?
procedure
x : real? y : real? z : real?
procedure
x : real? y : real? z : real? w : real?
2.1.3 Signed Integers
(require glm/ivec) | package: glm |
Arrays of signed integers.
procedure
v : any/c
procedure
len : (or/c exact-integer? #f) = #f fill : (or/c integer? 'no-fill) = 0 x : (or/c ivec? integer?)
procedure
data : array? len : exact-positive-integer? fixed? : boolean? fill : (or/c integer? 'no-fill)
procedure
v : ivec?
procedure
v : ivec?
procedure
(make-ivec-data x ...) → array?
x : integer?
procedure
v : ivec?
procedure
v : ivec?
procedure
v : ivec? i : exact-nonnegative-integer?
procedure
v : ivec? i : exact-nonnegative-integer? x : number?
procedure
(ivec->list v) → (listof flonum?)
v : ivec?
procedure
(ivec->s32vector v) → s32vector?
v : ivec?
procedure
v : ivec? start : exact-nonnegative-integer? = 0 stop : (or/c exact-integer? #f) = #f step : (and/c exact-integer? (not/c zero?)) = 1
syntax
(for/ivec maybe-length (for-clause ...) body-or-break ... body)
syntax
(for*/ivec maybe-length (for-clause ...) body-or-break ... body)
procedure
v : ivec?
procedure
v : ivec? u : ivec?
procedure
a : (or/c ivec? integer?)
procedure
a : (or/c ivec? integer?)
procedure
a : (or/c ivec? integer?)
procedure
a : (or/c ivec? integer?)
procedure
a : ivec? b : (or/c ivec? integer?)
procedure
a : ivec? b : (or/c ivec? integer?)
procedure
a : ivec? b : (or/c ivec? integer?)
procedure
a : ivec? b : (or/c ivec? integer?)
procedure
v : ivec?
procedure
v : ivec?
procedure
v : ivec?
procedure
v : ivec?
procedure
len : exact-nonnegative-integer?
= (_array _int len)
syntax
(define-ivec-type id #:length len)
2.1.3.1 ivec Types
procedure
procedure
procedure
procedure
procedure
x : integer?
procedure
x : integer? y : integer?
procedure
x : integer? y : integer? z : integer?
procedure
x : integer? y : integer? z : integer? w : integer?
2.1.4 Unsigned Integers
(require glm/uvec) | package: glm |
Arrays of unsigned integers.
procedure
v : any/c
procedure
len : (or/c exact-positive-integer? #f) = #f fill : (or/c nonnegative-integer? 'no-fill) = 0 x : (or/c uvec? nonnegative-integer?)
procedure
data : array? len : exact-positive-integer? fixed? : boolean? fill : (or/c nonnegative-integer? 'no-fill)
procedure
v : uvec?
procedure
v : uvec?
procedure
(make-uvec-data x ...) → array?
x : nonnegative-integer?
procedure
v : uvec?
procedure
v : uvec?
procedure
(uvec-ref v i) → nonnegative-integer?
v : uvec? i : exact-nonnegative-integer?
procedure
v : uvec? i : exact-nonnegative-integer? x : nonnegative-integer?
procedure
(uvec->list v) → (listof flonum?)
v : uvec?
procedure
(uvec->u32vector v) → u32vector?
v : uvec?
procedure
v : uvec? start : exact-nonnegative-integer? = 0 stop : (or/c exact-integer? #f) = #f step : (and/c exact-integer? (not/c zero?)) = 1
syntax
(for/uvec maybe-length (for-clause ...) body-or-break ... body)
syntax
(for*/uvec maybe-length (for-clause ...) body-or-break ... body)
procedure
v : uvec?
procedure
v : uvec? u : uvec?
procedure
(uvec+ a ...) → (or/c uvec? nonnegative-integer?)
a : (or/c uvec? nonnegative-integer?)
procedure
(uvec- a ...) → (or/c uvec? nonnegative-integer?)
a : (or/c uvec? nonnegative-integer?)
procedure
(uvec* a ...) → (or/c uvec? nonnegative-integer?)
a : (or/c uvec? nonnegative-integer?)
procedure
(uvec/ a ...) → (or/c uvec? nonnegative-integer?)
a : (or/c uvec? nonnegative-integer?)
procedure
a : uvec? b : (or/c uvec? nonnegative-integer?)
procedure
a : uvec? b : (or/c uvec? nonnegative-integer?)
procedure
a : uvec? b : (or/c uvec? nonnegative-integer?)
procedure
a : uvec? b : (or/c uvec? nonnegative-integer?)
procedure
v : uvec?
procedure
v : uvec?
procedure
v : uvec?
procedure
v : uvec?
procedure
len : exact-nonnegative-integer?
= (_array _uint len)
syntax
(define-uvec-type id #:length len)
2.1.4.1 uvec Types
procedure
procedure
procedure
procedure
procedure
x : nonnegative-integer?
procedure
x : nonnegative-integer? y : nonnegative-integer?
procedure
x : nonnegative-integer? y : nonnegative-integer? z : nonnegative-integer?
procedure
x : nonnegative-integer? y : nonnegative-integer? z : nonnegative-integer? w : nonnegative-integer?
2.1.5 Booleans
(require glm/bvec) | package: glm |
Arrays of boolean values.
procedure
v : any/c
procedure
len : (or/c exact-integer? #f) = #f fill : (or/c boolean? 'no-fill) = #f x : (or/c bvec? boolean?)
procedure
data : array? len : exact-nonnegative-integer? fixed? : boolean? fill : (or/c boolean? 'no-fill)
procedure
v : bvec?
procedure
v : bvec?
procedure
(make-bvec-data x ...) → array?
x : boolean?
procedure
v : bvec?
procedure
v : bvec?
procedure
v : bvec? i : exact-nonnegative-integer?
procedure
v : bvec? i : exact-nonnegative-integer? x : boolean?
procedure
(bvec->list v) → (listof boolean?)
v : bvec?
procedure
(bvec->s32vector v) → s32vector?
v : bvec?
procedure
v : bvec? start : exact-nonnegative-integer? = 0 stop : (or/c exact-integer? #f) = #f step : (and/c exact-integer? (not/c zero?)) = 1
syntax
(for/bvec maybe-length (for-clause ...) body-or-break ... body)
syntax
(for*/bvec maybe-length (for-clause ...) body-or-break ... body)
procedure
v : bvec?
procedure
v : bvec? u : bvec?
procedure
len : exact-nonnegative-integer?
= (_array _int len)
syntax
(define-bvec-type id #:length len)
2.1.5.1 bvec Types
procedure
procedure
procedure
procedure
procedure
x : boolean?
procedure
x : boolean? y : boolean?
procedure
x : boolean? y : boolean? z : boolean?
procedure
x : boolean? y : boolean? z : boolean? w : boolean?
2.2 Matrix Types
(require glm/matrix-types) | package: glm |
This module re-exports glm/dmat and glm/mat.
2.2.1 Single Precision Floats
(require glm/mat) | package: glm |
A matrix is a two-dimensional array of 32-bit floating point component values.
Two matrices are equal? iff they have the same dimensions, the same number of components, and each pair of consecutive components are =.
procedure
N : (or/c exact-positive-integer? #f) = #f M : exact-positive-integer? fill : (or/c real? #f) = 0 x : (or/c mat? vec? real?)
> (mat #:rows 2 1 2 3 4 5 6)
[ 1 | 3 | 5 ]
[ 2 | 4 | 6 ]
> (mat #:rows 3 1 2 3 4 5 6)
[ 1 | 4 ]
[ 2 | 5 ]
[ 3 | 6 ]
When no xs are given, the result is an N×M (cols×rows) matrix with 1’s along its diagonal and 0’s everywhere else.
> (mat #:cols 3 #:rows 2)
[ 1 | 0 | 0 ]
[ 0 | 1 | 0 ]
> (mat #:cols 2 #:rows 3)
[ 1 | 0 ]
[ 0 | 1 ]
[ 0 | 0 ]
When N is given and the xs consist of a lone scalar value x, the result is an N×M matrix with x along its diagonal and fill everywhere else.
> (mat #:cols 3 #:rows 2 -1/2)
[ -0.5 | 0 | 0 ]
[ 0 | -0.5 | 0 ]
If the lone x is a matrix, the result is an N×M matrix with x embedded in its upper-left corner.
> (mat #:cols 3 #:rows 4 #:fill -3 (mat #:rows 2 2))
[ 2 | 0 | -3 ]
[ 0 | 2 | -3 ]
[ -3 | -3 | 1 ]
[ -3 | -3 | -3 ]
> (mat #:cols 3 #:rows 2 #:fill -3 (mat #:rows 4 2))
[ 2 | 0 | 0 ]
[ 0 | 2 | 0 ]
When N = (length '(x ...)), each of the xs becomes the sole argument to a column vector constructor.
> (mat #:cols 3 #:rows 2 9 8 7)
[ 9 | 8 | 7 ]
[ 9 | 8 | 7 ]
> (mat #:cols 2 #:rows 3 (vec2 1 2) (vec3 3 4 5))
[ 1 | 3 ]
[ 2 | 4 ]
[ 0 | 5 ]
When N is #f and (length '(x ...)) ≤ 1, the result is an M×M matrix.
> (mat #:rows 2)
[ 1 | 0 ]
[ 0 | 1 ]
> (mat #:rows 3 2)
[ 2 | 0 | 0 ]
[ 0 | 2 | 0 ]
[ 0 | 0 | 2 ]
If 2 ≤ (length '(x ...)) ≤ M, the result is an 1×M matrix.
> (mat #:rows 3 1 2)
[ 1 ]
[ 2 ]
[ 0 ]
If L = (length '(x ...)) > M, the result is an L’×M matrix, where L’ = L mod M + (0 if M divides L else 1).
> (mat #:rows 2 1 2 3 4)
[ 1 | 3 ]
[ 2 | 4 ]
> (mat #:rows 3 1 2 3 4)
[ 1 | 4 ]
[ 2 | 0 ]
[ 3 | 0 ]
mat is also a match pattern identifier for deconstructing matrices.
> (match (mat #:rows 3 1 2 3 4 5 6) [(mat x y z r g b) (list (list r g b) (list x y z))]) '((4.0 5.0 6.0) (1.0 2.0 3.0))
Optional #:cols or #:rows patterns may be given, and matrices of indeterminate length can be matched with a final #:rest pattern.
> (match (mat #:rows 3 1 2 3 4 5 6) [(mat #:cols cols #:rows rows x y z #:rest rgb) (list cols rows rgb x y z)]) '(2 3 (4.0 5.0 6.0) 1.0 2.0 3.0)
> (match (mat #:rows 3 1 2 3 4 5 6) [(mat #:cols cols #:rows rows #:rest _) (* cols rows)]) 6
procedure
data : array? num-cols : exact-positive-integer? num-rows : exact-positive-integer?
> (define m1 (mat #:rows 5)) > (define m2 (make-mat (mat-data m1) 3 3)) > m2
[ 1 | 0 | 0 ]
[ 0 | 1 | 0 ]
[ 0 | 0 | 1 ]
> (mat-set! m1 1 1 -1) > m2
[ 1 | 0 | 0 ]
[ 0 | -1 | 0 ]
[ 0 | 0 | 1 ]
> (define m (mat #:rows 2 1 2 3 4 5 6)) > (sequence->list (in-array (mat-data m))) '(#<array> #<array> #<array>)
> (define m (mat #:rows 2 1 2 3 4 5 6)) > m
[ 1 | 3 | 5 ]
[ 2 | 4 | 6 ]
> (mat-length m) 3
> (mat-num-cols m) 3
> (mat-num-rows m) 2
procedure
(make-mat-data num-cols num-rows x ...+) → array?
num-cols : exact-positive-integer? num-rows : exact-positive-integer? x : real?
> (array-ref (make-mat-data 3 2 1 2 3 4 5 6) 2 1) 6.0
> (define m1 (mat #:rows 3 1 2 3)) > (eq? (mat-data m1) (mat-data m1)) #t
> (define m2 (mat-copy m1)) > (eq? (mat-data m1) (mat-data m2)) #f
> (mat-name (mat #:rows 5)) 'mat5
> (mat-name (mat #:cols 5 #:rows 4)) 'mat5x4
> (mat-name (mat #:cols 4 #:rows 5)) 'mat4x5
procedure
m : mat? col : exact-nonnegative-integer? row : exact-nonnegative-integer? (mat-ref m i) → vec? m : mat? i : exact-nonnegative-integer?
> (define m (mat #:rows 2 1 2 3 4 5 6)) > m
[ 1 | 3 | 5 ]
[ 2 | 4 | 6 ]
> (mat-ref m 5) 6.0
> (mat-ref m 2 1) 6.0
procedure
m : mat? i : exact-nonnegative-integer?
procedure
(mat-column m i) → vec?
m : mat? i : exact-nonnegative-integer?
> (define m (mat #:rows 2 1 2 3 4 5 6)) > m
[ 1 | 3 | 5 ]
[ 2 | 4 | 6 ]
> (mat-column m 1) (vec2 3 4)
> (vec-set! (mat-column m 1) 0 -1) > m
[ 1 | -1 | 5 ]
[ 2 | 4 | 6 ]
procedure
m : mat? col : exact-nonnegative-integer? row : exact-nonnegative-integer? x : real? (mat-set! m i x) → void? m : mat? i : exact-nonnegative-integer? x : real?
> (define m (mat #:rows 2 1 2 3 4 5 6)) > (mat-set! m 5 -1) > (mat-set! m 2 0 -2) > m
[ 1 | 3 | -2 ]
[ 2 | 4 | -1 ]
procedure
(mat-set-row! m i v) → void?
m : mat? i : exact-nonnegative-integer? v : vec?
> (define m (mat #:rows 2 1 2 3 4 5 6)) > (mat-set-row! m 1 (vec -2 -4 -6)) > m
[ 1 | 3 | 5 ]
[ -2 | -4 | -6 ]
procedure
(mat-set-column! m i v) → void?
m : mat? i : exact-nonnegative-integer? v : vec?
> (define m (mat #:rows 2 1 2 3 4 5 6)) > (mat-set-column! m 1 (vec -3 -4)) > m
[ 1 | -3 | 5 ]
[ 2 | -4 | 6 ]
> (define m (mat #:rows 3 1 2 3 4 5 6)) > m
[ 1 | 4 ]
[ 2 | 5 ]
[ 3 | 6 ]
> (mat-columns m) '((vec3 1 2 3) (vec3 4 5 6))
> (mat-rows m) '((vec 1 4) (vec 2 5) (vec 3 6))
> (mat->list m) '(1.0 2.0 3.0 4.0 5.0 6.0)
mat-columns is also a match pattern identifier for deconstructing the columns of a matrix.
> (match m [(mat-columns xyz rgb) (list rgb xyz)]) '((vec3 4 5 6) (vec3 1 2 3))
Matrices of indeterminate length can be matched with a final #:rest pattern.
> (match m [(mat-columns (vec x y z) #:rest vs) (list vs z y x)]) '(((vec3 4 5 6)) 3.0 2.0 1.0)
procedure
(mat->f32vector m) → f32vector?
m : mat?
> (define m (mat #:rows 2 1 2 3 4 5 6)) > (define fv (mat->f32vector m)) > (f32vector-set! fv 5 0.0) > m
[ 1 | 3 | 5 ]
[ 2 | 4 | 0 ]
procedure
(in-mat-rows m) → sequence?
m : mat?
> (for ([v (in-mat-rows (mat #:rows 2))]) (println v))
(vec 1 0)
(vec 0 1)
procedure
(in-mat-columns m) → sequence?
m : mat?
> (for ([v (in-mat-columns (mat #:rows 2))]) (println v))
(vec2 1 0)
(vec2 0 1)
syntax
(for/mat maybe-cols #:rows length-expr maybe-fill (for-clause ...) body-or-break ... body)
maybe-cols =
| #:cols length-expr maybe-fill =
| #:fill length-expr
syntax
(for*/mat maybe-cols #:rows length-expr maybe-fill (for-clause ...) body-or-break ... body)
> (for/mat #:rows 2 ([i (in-range 6)]) (add1 i))
[ 1 | 3 | 5 ]
[ 2 | 4 | 6 ]
> (for*/mat #:rows 3 ([i (in-range 2)] [j (in-range 3)]) (+ j (* i 100)))
[ 0 | 100 ]
[ 1 | 101 ]
[ 2 | 102 ]
If the optional #:cols clause is specified, it determines the shape of the result matrix.
> (for/mat #:cols 5 #:rows 3 ([x (in-range 5)]) (* x x))
[ 0 | 1 | 4 | 9 | 16 ]
[ 0 | 1 | 4 | 9 | 16 ]
[ 0 | 1 | 4 | 9 | 16 ]
If the optional #:fill clause is specified, it determines the value of any unspecified components.
> (for/mat #:rows 3 #:fill -1 ([x (in-range 4)]) x)
[ 0 | 3 ]
[ 1 | -1 ]
[ 2 | -1 ]
procedure
m : mat?
procedure
(mat-row-predicate m) → predicate/c
m : mat?
procedure
(mat-predicate m) → predicate/c
m : mat?
> (define m (mat #:cols 5 #:rows 4)) > (define mat4x5? (mat-predicate m)) > (define vec5? (mat-row-predicate m)) > (mat4x5? m) #t
> (vec5? (mat-row m 0)) #t
> (vec5? (mat-column m 0)) #f
> (define m (mat #:cols 5 #:rows 4)) > (define mat4x5 (mat-constructor m)) > (define vec5 (mat-row-constructor m)) > (mat4x5)
[ 1 | 0 | 0 | 0 | 0 ]
[ 0 | 1 | 0 | 0 | 0 ]
[ 0 | 0 | 1 | 0 | 0 ]
[ 0 | 0 | 0 | 1 | 0 ]
> (vec5) (vec 0 0 0 0 0)
> (define m (mat #:rows 2 1 2 3 4 5 6)) > m
[ 1 | 3 | 5 ]
[ 2 | 4 | 6 ]
> (mat=! m (mat #:rows 2 9 8 7 6 5 4)) > m
[ 9 | 7 | 5 ]
[ 8 | 6 | 4 ]
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
> (mat+ 1 2 3) 6
> (mat- 3 (vec 4 5)) (vec -1 -2)
> (mat* (mat #:rows 3 2) (vec 1 2 3)) (vec 2 4 6)
> (mat/ (mat #:rows 3) 2)
[ 0.5 | 0 | 0 ]
[ 0 | 0.5 | 0 ]
[ 0 | 0 | 0.5 ]
> (mat/ (mat #:rows 3) (mat #:rows 3 2))
[ 0.5 | 0 | 0 ]
[ 0 | 0.5 | 0 ]
[ 0 | 0 | 0.5 ]
procedure
m : mat? a : (or/c mat? vec? real?)
procedure
m : mat? a : (or/c mat? vec? real?)
procedure
m : mat? a : (or/c mat? vec? real?)
procedure
m : mat? a : (or/c mat? vec? real?)
> (define m (mat #:cols 3 #:rows 2 1 2 3)) > m
[ 1 | 2 | 3 ]
[ 1 | 2 | 3 ]
> (mat+=! m (mat #:cols 3 #:rows 2 10 10 10)) > m
[ 11 | 12 | 13 ]
[ 11 | 12 | 13 ]
> (mat-=! m (mat #:cols 3 #:rows 2 1 2 3)) > m
[ 10 | 10 | 10 ]
[ 10 | 10 | 10 ]
> (define m (mat #:rows 3 1 2 3 4 5 6 7 8 9)) > m
[ 1 | 4 | 7 ]
[ 2 | 5 | 8 ]
[ 3 | 6 | 9 ]
> (mat*=! m (mat #:rows 3 2)) > m
[ 2 | 4 | 6 ]
[ 8 | 10 | 12 ]
[ 14 | 16 | 18 ]
> (define m (mat #:rows 3 1 2 3 4 5 6 7 8 9)) > m
[ 1 | 4 | 7 ]
[ 2 | 5 | 8 ]
[ 3 | 6 | 9 ]
> (mat/=! m (mat #:rows 3 2)) > m
[ 0.5 | 1 | 1.5 ]
[ 2 | 2.5 | 3 ]
[ 3.5 | 4 | 4.5 ]
procedure
cols : exact-positive-integer? rows : exact-positive-integer?
syntax
(define-mat-type id #:cols N #:rows M)
A define-mat-type form defines 2 names:
id, a matrix constructor function that takes up to N×M arguments and returns a new matrix.
id?, a predicate procedure that returns #t for matrices with N columns and M rows, and #f for any other value.
> (define-mat-type mat5 #:cols 5 #:rows 5) > (mat5? (mat5)) #t
> (mat5? (mat #:rows 3)) #f
id is also a match patterns similar to mat, except it only matches matrices with exactly N columns and M rows.
> (match (mat5 1 2 3 4 5) [(mat5 a b c #:rest xs) (list a b c (apply + xs))]) '(1.0 1.0 1.0 72.0)
> (match (mat #:rows 3) [(mat5 #:rest xs) (apply + xs)]) match: no matching clause for [ 1 | 0 | 0 ]
[ 0 | 1 | 0 ]
[ 0 | 0 | 1 ]
2.2.1.1 mat Types
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
procedure
a : (or/c mat? vec? real?)
value
value
value
value
value
value
value
value
value
value
value
value
2.2.2 Double Precision Floats
(require glm/dmat) | package: glm |
Two-dimensional arrays of 64-bit floating point numbers.
procedure
m : any/c
procedure
N : (or/c exact-positive-integer? #f) = #f M : exact-positive-integer? fill : (or/c real? #f) = 0 x : (or/c dmat? dvec? real?)
procedure
data : array? num-cols : exact-positive-integer? num-rows : exact-positive-integer?
procedure
m : dmat?
procedure
m : dmat?
procedure
m : dmat?
procedure
m : dmat?
procedure
(make-dmat-data num-cols num-rows x ...+) → array?
num-cols : exact-positive-integer? num-rows : exact-positive-integer? x : real?
procedure
m : dmat?
procedure
m : dmat?
procedure
m : dmat? col : exact-nonnegative-integer? row : exact-nonnegative-integer? (dmat-ref m i) → dvec? m : dmat? i : exact-nonnegative-integer?
procedure
m : dmat? i : exact-nonnegative-integer?
procedure
(dmat-column m i) → dvec?
m : dmat? i : exact-nonnegative-integer?
procedure
(dmat-set-column! m i v) → void?
m : dmat? i : exact-nonnegative-integer? v : dvec?
procedure
(dmat-set-row! m i v) → void?
m : dmat? i : exact-nonnegative-integer? v : dvec?
procedure
m : dmat? col : exact-nonnegative-integer? row : exact-nonnegative-integer? x : real? (dmat-set! m i x) → void? m : dmat? i : exact-nonnegative-integer? x : real?
procedure
(dmat->list m) → (listof real?)
m : dmat?
procedure
m : dmat?
procedure
(dmat-columns m) → (listof dvec?)
m : dmat?
procedure
(in-dmat-columns m) → sequence?
m : dmat?
procedure
(in-dmat-rows m) → sequence?
m : dmat?
procedure
m : dmat?
syntax
(for/dmat maybe-cols #:rows length-expr maybe-fill (for-clause ...) body-or-break ... body)
maybe-cols =
| #:cols length-expr maybe-fill =
| #:fill length-expr
syntax
(for*/dmat maybe-cols #:rows length-expr maybe-fill (for-clause ...) body-or-break ... body)
procedure
m : dmat?
procedure
m : dmat?
procedure
(dmat-predicate m) → predicate/c
m : dmat?
procedure
(dmat-column-constructor m) → (unconstrained-domain-> dvec?)
m : dmat?
procedure
m : dmat?
procedure
m : dmat?
procedure
m : dmat? n : dmat?
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
m : dmat? a : (or/c dmat? dvec? real?)
procedure
m : dmat? a : (or/c dmat? dvec? real?)
procedure
m : dmat? a : (or/c dmat? dvec? real?)
procedure
m : dmat?
procedure
m : dmat?
procedure
m : dmat?
procedure
m : dmat?
procedure
rows : exact-positive-integer? cols : exact-positive-integer?
syntax
(define-dmat-type id #:cols N #:rows M)
2.2.2.1 dmat Types
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
procedure
a : (or/c dmat? dvec? real?)
value
value
value
value
value
value
value
value
value
value
value
value