From f28af0298df2d5e8274fc7ec7e9fe0129aec72a5 Mon Sep 17 00:00:00 2001 From: AJ Rossini Date: Wed, 11 Mar 2009 05:56:23 +0100 Subject: [PATCH] moved matrix making examples into ls-demo ; Signed-off-by: AJ Rossini --- TODO.lisp | 152 +++++++++++++++-------------------------------------------- ls-demo.lisp | 112 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 148 insertions(+), 116 deletions(-) diff --git a/TODO.lisp b/TODO.lisp index 39c565d..394eeb3 100644 --- a/TODO.lisp +++ b/TODO.lisp @@ -1,6 +1,6 @@ ;;; -*- mode: lisp -*- -;;; Time-stamp: <2009-03-08 12:29:51 tony> +;;; Time-stamp: <2009-03-10 21:38:11 tony> ;;; Creation: <2008-09-08 08:06:30 tony> ;;; File: TODO.lisp ;;; Author: AJ Rossini @@ -40,59 +40,6 @@ (progn ;; Data setup - ;; Making data-frames (i.e. cases (rows) by variables (columns)) - ;; takes a bit of getting used to. For this, it is important to - ;; realize that we can do the following: - ;; #1 - consider the possibility of having a row, and transposing - ;; it, so the list-of-lists is: ((1 2 3 4 5)) (1 row, 5 columns) - ;; #2 - naturally list-of-lists: ((1)(2)(3)(4)(5)) (5 rows, 1 column) - ;; see src/data/listoflist.lisp for code to process this particular - ;; data structure. - (defparameter *indep-vars-1-matrix* - (transpose (make-matrix 1 (length iron) - :initial-contents - (list (mapcar #'(lambda (x) (coerce x 'double-float)) - iron)))) - "creating iron into double float, straightforward") - - (documentation '*indep-vars-1-matrix* 'variable) - ;; *indep-vars-1-matrix* - - ;; or directly: - (defparameter *indep-vars-1a-matrix* - (make-matrix (length iron) 1 - :initial-contents - (mapcar #'(lambda (x) (list (coerce x 'double-float))) - iron))) - ;; *indep-vars-1a-matrix* - - ;; and mathematically, they seem equal: - (m= *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => T - ;; but of course not completely... - (eql *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL - (eq *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL - - ;; and verify... - (print *indep-vars-1-matrix*) - (print *indep-vars-1a-matrix*) - - (documentation 'lisp-matrix:bind2 'function) ; by which we mean: - (documentation 'bind2 'function) - (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :column) ; 2 col - (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :row) ; 1 long col - - ;; the weird way - (defparameter *indep-vars-2-matrix* - (transpose (make-matrix 2 (length iron) - :initial-contents - (list - (mapcar #'(lambda (x) (coerce x 'double-float)) - iron) - (mapcar #'(lambda (x) (coerce x 'double-float)) - aluminum))))) - ;; *indep-vars-2-matrix* - - ;; the "right"? way (defparameter *indep-vars-2-matrix* (make-matrix (length iron) 2 :initial-contents @@ -100,15 +47,8 @@ (list (coerce x 'double-float) (coerce y 'double-float))) iron aluminum))) - ;; *indep-vars-2-matrix* - - ;; The below FAILS due to coercion issues; it just isn't lispy, it's R'y. -#| - (defparameter *dep-var* (make-vector (length absorbtion) - :initial-contents (list absorbtion))) -|# - ;; BUT below, this should be the right type. + (defparameter *dep-var* (make-vector (length absorbtion) :type :row @@ -116,33 +56,42 @@ (list (mapcar #'(lambda (x) (coerce x 'double-float)) absorbtion)))) - ;; *dep-var* - (defparameter *dep-var-int* (make-vector (length absorbtion) :type :row :element-type 'integer :initial-contents (list absorbtion))) - - (typep *dep-var* 'matrix-like) ; => T - (typep *dep-var* 'vector-like) ; => T - - (typep *indep-vars-1-matrix* 'matrix-like) ; => T - (typep *indep-vars-1-matrix* 'vector-like) ; => T - (typep *indep-vars-2-matrix* 'matrix-like) ; => T - (typep *indep-vars-2-matrix* 'vector-like) ; => F - - iron - ;; following fails, need to ensure that we work on list elts, not just - ;; elts within a list: - ;; - ;; (coerce iron 'real) - ;; - ;; the following is a general list-conversion coercion approach -- is - ;; there a more efficient way? - ;; (coerce 1 'real) - ;; (mapcar #'(lambda (x) (coerce x 'double-float)) iron) + + + (defparameter *xv+1a* + (make-matrix + 8 2 + :initial-contents #2A((1d0 1d0) + (1d0 3d0) + (1d0 2d0) + (1d0 4d0) + (1d0 3d0) + (1d0 5d0) + (1d0 4d0) + (1d0 6d0)))) + + (defparameter *xv+1b* + (bind2 + (ones 8 1) + (make-matrix + 8 1 + :initial-contents '((1d0) + (3d0) + (2d0) + (4d0) + (3d0) + (5d0) + (4d0) + (6d0))) + :by :column)) + + (m= *xv+1a* *xv+1b*) ; => T (princ "Data Set up")) @@ -228,10 +177,11 @@ #+nil (progn - ;; FIXME: Data.Frames probably deserve to be related to lists -- - ;; either lists of cases, or lists of variables. We probably do not - ;; want to mix them, but want to be able to convert between such - ;; structures. + ;; FIXME: Barf'd version of Data.Frames. See data/data-clos.lisp + ;; for better philosophy. For example, we could say that they + ;; probably deserve to be related to lists -- either lists of cases, + ;; or lists of variables. We probably do not want to mix them, but + ;; want to be able to convert between such structures. (defparameter *my-case-data* '((:cases @@ -477,34 +427,6 @@ (1d0 4d0) (1d0 6d0)))) - (defparameter *xv+1a* - (make-matrix - 8 2 - :initial-contents #2A((1d0 1d0) - (1d0 3d0) - (1d0 2d0) - (1d0 4d0) - (1d0 3d0) - (1d0 5d0) - (1d0 4d0) - (1d0 6d0)))) - - (defparameter *xv+1b* - (bind2 - (ones 8 1) - (make-matrix - 8 1 - :initial-contents '((1d0) - (3d0) - (2d0) - (4d0) - (3d0) - (5d0) - (4d0) - (6d0))) - :by :column)) - - (m= *xv+1a* *xv+1b*) ; => T ;; so something like (NOTE: matrices are transposed to begin with, hence the incongruety) (defparameter *xtx-2* (m* (transpose *xv+1*) *xv+1*)) diff --git a/ls-demo.lisp b/ls-demo.lisp index 4a56d46..f837a75 100644 --- a/ls-demo.lisp +++ b/ls-demo.lisp @@ -3,7 +3,7 @@ ;;; See COPYRIGHT file for any additional restrictions (BSD license). ;;; Since 1991, ANSI was finally finished. Edited for ANSI Common Lisp. -;;; Time-stamp: <2009-02-04 17:58:19 tony> +;;; Time-stamp: <2009-03-10 20:45:49 tony> ;;; Creation: sometime in 2006... ;;; File: ls-demo.lisp ;;; Author: AJ Rossini @@ -651,3 +651,113 @@ my.lib ) +;;;;;;;;;;;;;;; Data stuff + +(progn ;; Data setup + + ;; Making data-frames (i.e. cases (rows) by variables (columns)) + ;; takes a bit of getting used to. For this, it is important to + ;; realize that we can do the following: + ;; #1 - consider the possibility of having a row, and transposing + ;; it, so the list-of-lists is: ((1 2 3 4 5)) (1 row, 5 columns) + ;; #2 - naturally list-of-lists: ((1)(2)(3)(4)(5)) (5 rows, 1 column) + ;; see src/data/listoflist.lisp for code to process this particular + ;; data structure. + (defparameter *indep-vars-1-matrix* + (transpose (make-matrix 1 (length iron) + :initial-contents + (list (mapcar #'(lambda (x) (coerce x 'double-float)) + iron)))) + "creating iron into double float, straightforward") + + (documentation '*indep-vars-1-matrix* 'variable) + ;; *indep-vars-1-matrix* + + ;; or directly: + (defparameter *indep-vars-1a-matrix* + (make-matrix (length iron) 1 + :initial-contents + (mapcar #'(lambda (x) (list (coerce x 'double-float))) + iron))) + ;; *indep-vars-1a-matrix* + + ;; and mathematically, they seem equal: + (m= *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => T + ;; but of course not completely... + (eql *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL + (eq *indep-vars-1-matrix* *indep-vars-1a-matrix*) ; => NIL + + ;; and verify... + (print *indep-vars-1-matrix*) + (print *indep-vars-1a-matrix*) + + (documentation 'lisp-matrix:bind2 'function) ; by which we mean: + (documentation 'bind2 'function) + (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :column) ; 2 col + (bind2 *indep-vars-1-matrix* *indep-vars-1a-matrix* :by :row) ; 1 long col + + ;; the weird way + (defparameter *indep-vars-2-matrix* + (transpose (make-matrix 2 (length iron) + :initial-contents + (list + (mapcar #'(lambda (x) (coerce x 'double-float)) + iron) + (mapcar #'(lambda (x) (coerce x 'double-float)) + aluminum))))) + ;; *indep-vars-2-matrix* + + ;; the "right"? way + (defparameter *indep-vars-2-matrix* + (make-matrix (length iron) 2 + :initial-contents + (mapcar #'(lambda (x y) + (list (coerce x 'double-float) + (coerce y 'double-float))) + iron aluminum))) + ;; *indep-vars-2-matrix* + + + ;; The below FAILS due to coercion issues; it just isn't lispy, it's R'y. +#| + (defparameter *dep-var* (make-vector (length absorbtion) + :initial-contents (list absorbtion))) +|# + ;; BUT below, this should be the right type. + (defparameter *dep-var* + (make-vector (length absorbtion) + :type :row + :initial-contents + (list + (mapcar #'(lambda (x) (coerce x 'double-float)) + absorbtion)))) + ;; *dep-var* + + + (defparameter *dep-var-int* + (make-vector (length absorbtion) + :type :row + :element-type 'integer + :initial-contents (list absorbtion))) + + (typep *dep-var* 'matrix-like) ; => T + (typep *dep-var* 'vector-like) ; => T + + (typep *indep-vars-1-matrix* 'matrix-like) ; => T + (typep *indep-vars-1-matrix* 'vector-like) ; => T + (typep *indep-vars-2-matrix* 'matrix-like) ; => T + (typep *indep-vars-2-matrix* 'vector-like) ; => F + + iron + ;; following fails, need to ensure that we work on list elts, not just + ;; elts within a list: + ;; + ;; (coerce iron 'real) + ;; + ;; the following is a general list-conversion coercion approach -- is + ;; there a more efficient way? + ;; (coerce 1 'real) + ;; (mapcar #'(lambda (x) (coerce x 'double-float)) iron) + + (princ "Data Set up")) + -- 2.11.4.GIT