moved listoflist infrastructure into xarray as another array-able structure.
[CommonLispStat.git] / src / data / listoflist.lisp
blob58110462f69b458e052533bf291c6a7a765f8f5f
1 ;;; -*- mode: lisp -*-
3 ;;; Time-stamp: <2009-08-27 08:36:35 tony>
4 ;;; Creation: <2008-09-08 08:06:30 tony>
5 ;;; File: listoflist.lisp
6 ;;; Author: AJ Rossini <blindglobe@gmail.com>
7 ;;; Copyright: (c) 2007-2008, AJ Rossini <blindglobe@gmail.com>. BSD.
8 ;;; Purpose: Manipulating structures which are lists of lists
9 ;;; with matrix-likes and dataframe-likes.
11 ;;; What is this talk of 'release'? Klingons do not make software
12 ;;; 'releases'. Our software 'escapes', leaving a bloody trail of
13 ;;; designers and quality assurance people in its wake.
15 (in-package :cls-data)
17 ;; We currently assume that the list-of-list representation is in
18 ;; row-major form, i.e. that lists represent rows and not columns.
19 ;; The original lisp-stat had the other way around. We could augment
20 ;; the top-level list with a property to check orientation
21 ;; (row-major/column-major), but this hasn't been done yet.
23 ;; See XARRAY's listoflist example for more details and support
24 ;; functions (migrated from here to there).
26 (defun listoflist->matrix-like (lol &key
27 (orientation :row-major)
28 (coerce-to 'double-float))
29 "From a listoflists structure of numbers, return a matrix-like.
31 FIXME: need to verify that the listoflists is a valid structure (same
32 size rows, typing if required, etc.
34 FIXME: need to grep special variables to make the right kind of
35 matrix-like.
37 <example>
38 (defparameter *lol-ml-test*
39 (list (list 1d0 1d0 2.1d0)
40 (list 2d0 2d0 1.1d0)))
41 (length *lol-ml-test*)
42 (length (elt *lol-ml-test* 0))
44 (defparameter *lol-ml-result* (listoflist->matrix-like *lol-ml-test*))
45 (matrix-dimensions *lol-ml-result*)
46 </example>"
47 (declare (ignorable coerce-to))
48 (let ((n (length lol))
49 (p (length (elt lol 0))))
50 (let ((result (make-matrix n p :initial-element 0d0)))
51 (dotimes (i n)
52 (dotimes (j p)
53 (if (equal orientation :row-major)
54 (setf (mref result i j) (coerce (elt (elt lol i) j) coerce-to))
55 (setf (mref result i j) (coerce (elt (elt lol j) i) coerce-to)))))
56 result)))
59 ;; the following will be handy to help out folks adjust. It should
60 ;; provide a means to write code faster and better.
61 (defun listoflist->dataframe (lol) ; &key (type :row-major))
62 "Create a cases-by-variables data frame consisting of numeric data,
63 from a ROW-MAJOR list-of-lists representation. A COLUMN-MAJOR
64 representation should be handled using the transpose-listoflists
65 function."
66 (if (lists-of-same-size lol)
67 (make-dataframe (listoflist->array lol))
68 (error "make-data-set-from-lists: no combining different length lists"))
69 (error "make-data-set-from-lists: proposed name exists"))