This page is hosted on AFS file server space, which is being shut down on November 13, 2018. If you are seeing this message, your service provider needs to take steps now. Visit afs.unc.edu for more information.

Psych 285: Computational Statistics
and Statistical Visualization

Professor Forrest Young

LispStat Programming Examples

These short examples follow Luke Tierney's text. You may copy this page and work your way through these examples without having to type them in.


  • Matrix Examples from Chapter 4, Section 4.4.7
  • 
    ;Arrays - multidimensional arrays (vectors are unidimensional arrays)
    ;note the #2A( ...  ) syntax for two-dimensional arrays (matrices)
    ;and the #3A( ... ) syntax for three-dimensional arrays, etc...
    
    (setf mat (make-array '(2 3)))
    (setf mat (make-array '(2 3) :initial-element 1))
    (setf mat (make-array '(3 2) :initial-contents '((1 2) (3 4) (5 6))))
    
    (make-array '(30 1) :initial-element 1)
    
    (print-matrix mat)
    (pm mat) ;ViSta only
    
    (setf mat (matrix '(2 3) '(1 2 3 4 5 6)))
    
    (aref mat 1 1)
    (setf (aref mat 1 0) "A")
    
    (select mat 1 1)
    (select mat '(1 2) '(2 3))
    (setf (select mat 1 0) 'B)
    
    (array-rank mat)
    
    (array-dimensions mat)
    (array-dimension mat 0)
    (array-total-size mat)
    
    (setf abrasion (matrix (list 3 (length hardness))
                             (combine hardness tensile-strength abrasion-loss)))
    
    


  • Matrix Examples from Chapter 5, Section 4
  • 
    (setf abrasion (transpose abrasion))
    (print-matrix abrasion)
    (pm abrasion) ;ViSta only
    
    (print-matrix (matmult (transpose abrasion) abrasion ))
    
    ;bind-rows and bind-columns create matrices from lists, vectors, or matrices
    (setf abrasion (bind-columns hardness tensile-strength abrasion-loss))
    (pm abrasion)
    
    ;column-list and row-list break up a matrix into a list of rows or columns
    (column-list abrasion)
    (row-list abrasion
    
    (defun column-means (mat)
      (mapcar #'mean (column-list mat)))
    (column-means abrasion)
    
    ;transpose, identity-matrix
    
    (pm (identity-matrix 4))
    (pm (transpose abrasion))
    
    ;matmult and %* (undocumented)
    (pm (%* (transpose abrasion) abrasion))
    (setf one (make-array '(3) :initial-element 1) )
    (%* abrasion one) ; sums up the values in each row
    
    ;inner-product, outer-product, cross-product
    (%* '(1 2 3) '(4 5 6))
    (inner-product '(1 2 3) '(4 5 6))
    (pm (outer-product '(1 2 3) '(4 5 6)))
    (pm (cross-product abrasion)) ; same as (%* (transpose abrasion) abrasion))
    
    , diagonal (used in two ways)
    (pm (diagonal (matmult(transpose abrasion) abrasion )))
    (pm (diagonal (diagonal (matmult(transpose abrasion) abrasion ))))
    
    ;inverse
    (setf cp (cross-product abrasion))
    (pm (%* (inverse cp) cp))
    
    ;eigen, eigenvalues, eigenvectors, sv-decomp
    (eigen cp)
    (eigenvalues cp)
    (eigenvectors cp)
    (sv-decomp abrasion)
    
    ;chol-decomp, lu-decomp, qr-decomp