Outdated egg!

This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.

If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.

  1. Outdated egg!
  2. linear-algebra
    1. Line segments
    2. Vectors
    3. Matrices
    4. Sparse matrices
    5. Traversal
    6. Statistics
    7. Misc
    8. License

This page is maintained in the package's github repository.


This implements various linear algebra and other math operations. Much of this code is originally from Jeff Siskind's QobiScheme.

Line segments

[record] (define-structure line-segment p q)
[procedure] (p l)
[procedure] (q l)
[procedure] (collinear? l1 l2)
[procedure] (point-on-line-segment? r l)
[procedure] (intersection-point l1 l2)
[procedure] (cross? l1 l2)
[procedure] (intersect? l1 l2)
[procedure] (line-tangent l)
[procedure] (normal-2d l)
[procedure] (line-segment-length l)
[procedure] (read-line-segments-from-file pathname)
[procedure] (write-line-segments-to-file line-segments pathname)


[procedure] (x v)
[procedure] (y v)
[procedure] (z v)
[procedure] (dot u v)
[procedure] (cross-2d u v)
[procedure] (cross u v)
[procedure] (v+ u v)
[procedure] (v- u v)
[procedure] (k*v k v)
[procedure] (v*k v k)
[procedure] (k+v k v)
[procedure] (v+k v k)
[procedure] (v/k v k)
[procedure] (v* u v)
[procedure] (v/ u v)
[procedure] (v= u v)
[procedure] (rotate-90 u)
[procedure] (rotate-180 u)
[procedure] (rotate-270 u)
[procedure] (perpendicular? u v)
[procedure] (parallel? u v)
[procedure] (magnitude-squared v)
[procedure] (magnitude v)
[procedure] (unit v)
[procedure] (distance-squared u v)
[procedure] (distance u v)
[procedure] (clockwise-angle? u v w)
[procedure] (clockwise-or-same-angle? u v w)
[procedure] (append-vector vec1 vec2)


[procedure] (matrix? v)
[procedure] (list->matrix l)
[procedure] (make-matrix m n . &rest)
[procedure] (make-3-by-3-matrix a11 a12 a13 a21 a22 a23 a31 a32 a33)
[procedure] (matrix-copy m)
[procedure] (matrix-rows a)
[procedure] (matrix-columns a)
[procedure] (matrix-ref a i j)
[procedure] (matrix-set! a i j x)
[procedure] (matrix-row-ref a i)
[procedure] (matrix-column-ref a j)
[procedure] (matrix-row-set! a i v)
[procedure] (vector->row-matrix v)
[procedure] (vector->column-matrix v)
[procedure] (m+ a b)
[procedure] (m- a b)
[procedure] (m+k m k)
[procedure] (m+k-diagonal m k)
[procedure] (k+m k m)
[procedure] (m*v a v)
[procedure] (matrix-transpose a)
[procedure] (outer-product f u v)
[procedure] (self-outer-product f v)
[procedure] (m* a b)
[procedure] (m*. a b)
[procedure] (m/. a b)
[procedure] (v*m v a)
[procedure] (k*m k m)
[procedure] (m*k m k)
[procedure] (m/k m k)
[procedure] (v*m*v v m)
[procedure] (determinant a)
[procedure] (invert-matrix a)
[procedure] (simplex a m1 m2 m3)
[procedure] (jacobi a)
[procedure] (eigenvalues a)
[procedure] (eigenvectors a)
[procedure] (vector->diagonal-matrix v)
[procedure] (identity-matrix n)
[procedure] (clip-eigenvalues a v)
[procedure] (eigenvector-angle1 m)
[procedure] (eigenvector-angle2 m)
[procedure] (left-pseudo-inverse m)
[procedure] (right-pseudo-inverse m)
[procedure] (ref-1d m a)
[procedure] (ref-2d m a b)
[procedure] (ref-3d m a b c)
[procedure] (ref-4d m a b c d)
[procedure] (ref-5d m a b c d e)
[procedure] (map-n-vector-2d f m n)
[procedure] (map-n-vector-3d f m n p)
[procedure] (map-n-vector-4d f m n p q)
[procedure] (map-n-vector-5d f m n p q r)
[procedure] (matrix-sum f n i)
[procedure] (matrix-sum-2d f m n i)
[procedure] (shape-matrix v c)
[procedure] (unshape-matrix m)
[procedure] (crop m x y w h)
[procedure] (submatrix m x-offset y-offset x-size y-size)
[procedure] (matrix-ref-nd m . is)
[procedure] (matrix-3d-ref a s i j)
[procedure] (matrix-set-nd! m v . is)
[procedure] (matrix-3d-set! a v s i j)
[procedure] (map-matrix-nd f m n)
[procedure] (for-each-matrix-nd f m n)
[procedure] (map-matrix f m)
[procedure] (for-each-matrix f m)
[procedure] (map-matrix-3d f m)
[procedure] (for-each-matrix-3d f m)
[procedure] (map-n-matrix f i j)
[procedure] (for-each-n-matrix f i j)
[procedure] (map-indexed-matrix f m)
[procedure] (for-each-indexed-matrix f m)
[procedure] (map-indexed-matrix-3d f p)
[procedure] (for-each-indexed-matrix-3d f p)

Sparse matrices

[record] (define-structure sparse-matrix row column blank)
[record] (define-structure sparse-matrix-row element i up down)
[record] (define-structure sparse-matrix-column element j left right)
[record] (define-structure sparse-matrix-element value i up down j left right)
[procedure] (create-sparse-matrix blank)
[procedure] (sparse-matrix-ref sparse-matrix i j)


[procedure] (every-n-2d p v w)
[procedure] (every-n-3d p v w x)
[procedure] (every-n-4d p v w x y)
[procedure] (every-n-5d p v w x y z)
[procedure] (product-2d f m n)
[procedure] (sum-2d f m n)
[procedure] (sum-3d f m n p)
[procedure] (sum-4d f m n p q)
[procedure] (sum-pairs f m)
[procedure] (vector-sum f n i)
[procedure] (vector-sum-2d f m n i)
[procedure] (sum-f f l)
[procedure] (sum-vector v)
[procedure] (sum-vector-f f v)


[procedure] (list-mean p)
[procedure] (list-covariance l)
[procedure] (list-variance s)
[procedure] (list-skewness l)
[procedure] (list-kurtosis l)
[procedure] (list-correlation l1 l2)
[procedure] (vector-mean v)
[procedure] (vector-variance v)
[procedure] (vector-skewness v)
[procedure] (vector-kurtosis v)
[procedure] (vector-correlation v1 v2)
[procedure] (coefficient-of-bimodality v)
[procedure] (vectors-mean values)
[procedure] (vectors-variance mu values)
[procedure] (mahalanobis-distance val mu isigma)
[procedure] (frequencies l)


[parameter] *linear-algebra:epsilon*
[constant] pi
[constant] half-pi
[constant] two-pi
[constant] minus-pi
[constant] two-pi/360
[constant] three-sixty/two-pi
[procedure] (sqr x)
[procedure] (quadratic1 a b c)
[procedure] (quadratic2 a b c)


  Copyright 2010-2012 Purdue University. All rights reserved.
  Contact Andrei Barbu, andrei@0xab.com.
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  GNU Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public License
  along with this program.  If not, see http://www.gnu.org/licenses.