On this page:
gen:  linear
linear?
linear/  c
linear-add
linear-scale
linear-dot
scale
add
dot
gen-zero
3.1 Instances
3.2 Utilities for generic zero
zero
coerce-zero
car0
cdr0
null0?
foldl0
8.3

3 Linear generic interface

interface

gen:linear : any/c

procedure

(linear? v)  boolean?

  v : any/c

contract

linear/c : contract?

A generic interface representing a type or family of types that can be given the structure of a vector space (or an inner-product space, if dot is defined). Scalars are represented as any number?.

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:

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?:

procedure

(scale v a)  any/c

  v : any/c
  a : (or/c number? gen-zero?)
If v is linear? the result is the same as linear-scale.

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.

procedure

(add u v)  any/c

  u : any/c
  v : any/c
If v is linear? the result is the same as linear-add.

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.

procedure

(dot u v)  any/c

  u : any/c
  v : any/c
If v is linear? the result is the same as linear-dot.

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.

struct

(struct gen-zero ())

A type-generic representation of zero.

3.1 Instances

Two values conform if any of the following statements are true:

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

procedure

(zero u)  linear?

  u : linear?
The zero vector of the same type as u.

The same as (scale u (gen-zero)).

procedure

(coerce-zero u v)  linear?

  u : (or/c linear? gen-zero?)
  v : linear?
The same as (add u (zero v)).

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.

Examples:
> (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

(car0 p)  any/c

  p : (or/c pair? gen-zero?)

procedure

(cdr0 p)  any/c

  p : (or/c pair? gen-zero?)

procedure

(null0? v)  boolean?

  v : any/c

procedure

(foldl0 proc init lst*)  any/c

  proc : procedure?
  init : any/c
  lst* : (list*of any/c null0?)
Like car, cdr, null? and foldl (the latter for a single list argument only), but generalized to handle values produced with gen-zero.

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).

Examples:
> (car0 '(1 2 3))

1

> (cdr0 (gen-zero))

#<gen-zero>

> (foldl0 * 1 (gen-zero))

1

> (foldl0 cons '() (list* 1 2 3 (gen-zero)))

'(3 2 1)