1 Beta distributions
make-binomial
make-beta
make-cumulative-beta
chebyshev-integral
2 YAML – Yet Another Matrix Library
2.1 Constructors
make-matrix
build-matrix
2.2 Accessors
matrix?
matrix-size
matrix-ref
matrix-set!
2.3 Operators
matrix-map
matrix*vector
matrix*matrix
transpose
Version: 5.1

SlowThought’s Math Library

This library contains functions related to various mathematical topics (currently probability and matrices), which might be of general interest.

1 Beta distributions

 (require (planet slowthought/math:1:1/beta))
Beta functions are sort of binomial distributions in reverse - given a set of data, how likely is it that a given binomial distribution produced it? Questions like this are pondered in Bayesian statistics and its many applications. They are handy in many areas where a family of functions with a domain [0,1] is needed. Note that (make-beta 0 0) is equivalent in many implementations to beta [1 ,1]. This is because many implementations build the beta function from the gamma function, and then in a typical application have expressions like beta [x-1 ,y-1]. This implementation was built around the Chebyshev integral, and so starting at zero came naturally. Also note that, other than make-binomial, these functions’ arguments are limited to non-negative integers. This allows chebyshev-integral to return an analytic solution, as opposed to the typical numerical one.

(make-binomial x)  procedure?
  x : real?
returns a thunk that returns either #t or #f, with x providing the expectation of #t. It is provided to generate data sets for testing the other functions described in this section.

(make-beta t f)  procedure?
  t : natural-number/c
  f : natural-number/c
returns a probability density function of the form (f x), which describes the likelihood that a function like those generated by make-binomial produced so many #t and #f.

(make-cumulative-beta t f)  procedure?
  t : natural-number/c
  f : natural-number/c
returns a function that calculates the integral of (make-beta t f).

(chebyshev-integral p q)  procedure?
  p : natural-number/c
  q : natural-number/c
returns a function which evaluates the expression

from 0 to x. This is the horsepower behind the other functions. It is provided for those who need to "roll their own" solutions for performance reasons.

2 YAML – Yet Another Matrix Library

 (require (planet slowthought/math:1:1/yaml))
Two dimensional matrices are implemented as vectors of vectors, and attempt to emulate vectors’ syntax and behaviour. The unique part of this implementation is the ability to easily substitute different arithmetic operators in some functions so that you can do operations on, for example, matrices of quaternions or polynomials (coming soon to a planet near you).
Also coming soon, a more complete implementation, including, for instance, determinants that can handle quaternions or polynomials. For now, there are Racket-like constructors, accessors, and operators, and some arithmetic operators.

2.1 Constructors

(make-matrix nrows ncolumns)  matrix?
  nrows : (and/c positive? integer?)
  ncolumns : (and/c positive? integer?)
(make-matrix nrows ncolumns initialize)  matrix?
  nrows : (and/c positive? integer?)
  ncolumns : (and/c positive? integer?)
  initialize : any
returns a matrix. Each value in the matrix is set to 0, or to initialize, if provided.

(build-matrix nrows ncolumns initialize)  matrix?
  nrows : (and/c positive? integer?)
  ncolumns : (and/c positive? integer?)
  initialize : procedure?
initialize is a function of the form (f irow icolumn) which is used to fill the returned matrix.

Immutable matrices are just as you’d expect:

  (define my-immutable-matrix #(#(1 2 3)
                                #(4 5 6)))

As are mutable matrices:

  (define my-mutable-matrix (vector (vector 6 5 4)
                                    (vector 3 2 1)))

Some sugar is provided in the name of implementation hiding and readability. The following is equivalent to the last example:

  (define my-pretty-matrix (matrix (row 6 5 4)
                                   (row 3 2 1)))

2.2 Accessors

YAML follows Scheme/Racket conventions for vectors.

(matrix? m)  boolean?
  m : any/c
... is not very robust. A well constructed matrix is a vector of vectors of identical size. If the library’s constructors are used, this is garunteed, but matrix? is not yet suspicious of the creative or the evil.

(matrix-size m)  
exact-positive-integer?
exact-positive-integer?
  m : matrix?
returns the numbers of rows and columns of the matrix m via values.

(matrix-ref m row column)  any
  m : matrix?
  row : natural-number/c
  column : natural-number/c
returns the contents of m.

(matrix-set! m row column value)  any
  m : matrix?
  row : natural-number/c
  column : natural-number/c
  value : any
changes the contents of m.

2.3 Operators

Again, YAML tries to follow Scheme/Racket convention.

(matrix-map proc m ...)  matrix?
  proc : procedure?
  m : matrix?
works in the same fashion as vector-map. Because it is so straight-forward, no seperate implementation is offered for addition, subtraction, or scalar multiplication.

Things start to get complicated when you introduce multiplication. Multipliers have optional arguments for * and + for the user to provide appropriate operators for his/her special type.

(matrix*vector m v)  vector?
  m : matrix?
  v : vector?
(matrix*vector m v multiply add)  vector?
  m : matrix?
  v : vector?
  multiply : *
  add : +
multiplies a matrix by (the transpose of) a vector to produce another vector.

(matrix*matrix m1 m2)  matrix?
  m1 : matrix?
  m2 : matrix?
(matrix*matrix m1 m2 multiply add)  matrix?
  m1 : matrix?
  m2 : matrix?
  multiply : *
  add : +
multiplies two matrices to produce another one.

(transpose m)  matrix?
  m : matrix?
returns the transpose of m.