migrate all listoflist support with matrix/array/dataframe type objects to the result...
[CommonLispStat.git] / src / data / dataframe-matrixlike.lisp
blob6839f76cd60e6b89864837d359eed440252eebc9
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2009-09-25 08:02:52 tony>
4 ;;; Creation: <2009-03-12 17:14:56 tony>
5 ;;; File: dataframe-matrixlike.lisp
6 ;;; Author: AJ Rossini <blindglobe@gmail.com>
7 ;;; Copyright: (c)2009--, AJ Rossini. BSD, LLGPL, or GPLv2, depending
8 ;;; on how it arrives.
9 ;;; Purpose: Uses the lisp-matrix dataframe instance for storage.
10 ;;; Useful if we only have numerical data and no missing
11 ;;; data, strings, or categorical stuff...
13 ;;; What is this talk of 'release'? Klingons do not make software
14 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
15 ;;; designers and quality assurance people in its wake.
17 ;;; This organization and structure is new to the 21st Century
18 ;;; version.. Think, "21st Century Schizoid Man".
20 (in-package :cls-dataframe)
22 ;;; DATAFRAME-MATRIXLIKE
23 ;;;
24 ;;; example/implementatin of using lisp-matrix datastructures for
25 ;;; dataframe storage.
28 (defclass dataframe-matrixlike (dataframe-like)
29 ((store :initform nil
30 :initarg :storage
31 :type matrix-like
32 :accessor dataset
33 :documentation "Data storage: typed as matrix-like
34 (numerical only)."))
35 (:documentation "example implementation of dataframe-like using storage
36 based on lisp-matrix structures."))
38 (defmethod nrows ((df dataframe-matrixlike))
39 "specializes on inheritance from matrix-like in lisp-matrix."
40 (matrix-dimension (dataset df) 0))
42 (defmethod ncols ((df dataframe-matrixlike))
43 "specializes on inheritance from matrix-like in lisp-matrix."
44 (matrix-dimension (dataset df) 1))
46 ;;; *** FIXME: change mref to xref when we establish lisp-matrix
47 ;;; change to use xarray access facility. Need to dummy-proof the
48 ;;; following.
49 (defmethod xref ((df dataframe-matrixlike) &rest subscripts)
50 "Returns a scalar in array, in the same vein as aref, mref, vref, etc.
51 idx1/2 is row/col or case/var."
52 (mref (dataset df) (elt subscripts 0) (elt subscripts 1)))
54 (defmethod (setf xref) (value (df dataframe-matrixlike) &rest subscripts)
55 "Sets a value for df-ml."
56 ;; NEED TO CHECK TYPE!
57 ;; (check-type val (elt (vartype df) index2))
58 (setf (mref (dataset df) (elt subscripts 0) (elt subscripts 1)) value))
61 ;;; matrix-like <-> listoflist
62 ;; See XARRAY's listoflist example for more details and support
63 ;; functions (migrated from here to there).
64 (defun listoflist->matrix-like (lol &key
65 (orientation :row-major)
66 (coerce-to 'double-float))
67 "From a listoflists structure of numbers, return a matrix-like.
69 FIXME: need to verify that the listoflists is a valid structure (same
70 size rows, typing if required, etc.
72 FIXME: need to grep special variables to make the right kind of
73 matrix-like.
75 <example>
76 (defparameter *lol-ml-test*
77 (list (list 1d0 1d0 2.1d0)
78 (list 2d0 2d0 1.1d0)))
79 (length *lol-ml-test*)
80 (length (elt *lol-ml-test* 0))
82 (defparameter *lol-ml-result* (listoflist->matrix-like *lol-ml-test*))
83 (matrix-dimensions *lol-ml-result*)
84 </example>"
85 (declare (ignorable coerce-to))
86 (let ((n (length lol))
87 (p (length (elt lol 0))))
88 (let ((result (make-matrix n p :initial-element 0d0)))
89 (dotimes (i n)
90 (dotimes (j p)
91 (if (equal orientation :row-major)
92 (setf (mref result i j) (coerce (elt (elt lol i) j) coerce-to))
93 (setf (mref result i j) (coerce (elt (elt lol j) i) coerce-to)))))
94 result)))