3 Linear generic interface
Values of a special type gen-zero can be used with this interface to represent a zero vector of the appropriate type, or the scalar 0.
The interface contains following methods, that a type t can implement to support the interface:
procedure
(linear-add linear other) → t?
linear : t? other : (or/c t? gen-zero?) Addition of two vectors.See add.
procedure
(linear-scale linear scalar) → t?
linear : t? scalar : (or/c number? gen-zero?) Multiplication of a vector by a scalar.See scale.
procedure
(linear-dot linear other) → number?
linear : t? other : (or/c t? gen-zero?) Inner product of two vectors.See dot.
These methods are assumed to satisfy the axioms of a vector space (or inner-product space), but this is not checked.
In addition, the following should hold, for any v : linear?:
(equal? (linear-add v (gen-zero)) v)
(equal? (linear-scale v (gen-zero)) (linear-scale v 0))
(equal? (linear-dot v (gen-zero)) (gen-zero))
Otherwise, if a is gen-zero, the result is also gen-zero, and an error is raised for any other value of a.
This function should be used for a recursive method definition of linear-scale.
Otherwise, if v is gen-zero, u is returned, and an error is raised for any other value of a.
This function should be used for a recursive method definition of linear-add.
Otherwise, if v is gen-zero, the result is gen-zero, and an error is raised for any other value of a.
This function should be used for a recursive method definition of linear-dot.
3.1 Instances
Two values conform if any of the following statements are true:
They are both numbers
They are both null
They are both vectors, where each element of one vector conforms with the corresponding element of the other
They are both arrays of compatible shape and with conforming elements
They are both proc-result structs, with both slots containing conforming values
They are both boxes, with conforming contents
They are both hash tables with the same keys, and where the values mapped to by each key conform.
Instances of gen:linear are defined for number?, null?, pair?, vector?, array?, proc-result?, box? and hash? so that any equivalence class of conforming values forms an inner-product space, where addition and scalar multiplication defined elementwise, and an inner product as the sum of the elementwise product.
3.2 Utilities for generic zero
The same as (scale u (gen-zero)).
Returns a value like u, but where any gen-zero occuring as all or part of u is replaced with a concrete zero of the appropriate kind, such that it conforms to v.
If u is not (or does not contain) gen-zero, the result is identically u. Notice too the effect of gen-zero in the last tail of a dotted list.
> (define z (gen-zero)) > (coerce-zero '(1 2 3) '(100 200 300)) '(1 2 3)
> (coerce-zero z '(100 200 300)) '(0 0 0)
> (coerce-zero `(1 ,z 3) '(100 200 300)) '(1 0 3)
> (coerce-zero `(,z ,z 3 4 . ,z) '(100 (200 201) 300 400)) '(0 (0 0) 3 4)
> (coerce-zero `(,z ,z 3 4 . ,z) '(100 (200 201) 300 400 . 500)) '(0 (0 0) 3 4 . 0)
procedure
p : (or/c pair? gen-zero?)
procedure
p : (or/c pair? gen-zero?)
procedure
v : any/c
procedure
proc : procedure? init : any/c lst* : (list*of any/c null0?)
When applying foldl0, an improper list argument lst* with a value of gen-zero in its final position is accepted and is treated the same as if it were a proper list (that is, as if it’s final tail were null instead).