   iters iterations and starting vector start-vector (defaulting to 100 and 111111 resp)."
   ; need to clean up some of these variables names to be more descriptive
   (let [iters (or iters 100)
-        n-cols (count (first data))
+        n-cols (dimension-count data 1)
         start-vector (or start-vector (repeatv n-cols 1))
         ; XXX - this add extra cols to the start vector if we have new comments... should test
         start-vector (matrix

  (matrix (mapv #(- % (proj-vec xs %)) data)))


(defn centered-data [data]
  (- data (mean data)))


; Will eventually also want to add last-pcs
(defn powerit-pca [data n-comps & {:keys [iters start-vectors]}]
  "Find the first n-comps principal components of the data matrix; iters defaults to iters of
  power-iteration"
  (let [cntrd-data (centered-data data)
        start-vectors (or start-vectors [])
        data-dim (min (row-count cntrd-data) (column-count cntrd-data))]
    (loop [data' cntrd-data n-comps' (min n-comps data-dim) pcs [] start-vectors start-vectors]
      (let [pc (power-iteration data' iters (first start-vectors)) ; may eventually want to return eigenvals...
            pcs (conj pcs pc)]
        (if (= n-comps' 1)
          pcs ; return if done
          (let [data' (factor-matrix data' pc)
                n-comps' (dec n-comps')]
            (recur data' n-comps' pcs (rest start-vectors))))))))


;(defn wrapped-pca [data n-comps & {:keys [iters start-vectors] :as kwargs}]
  ;(let [[row-cnt col-cnt] (dim data)]
    ;(case [(> row-cnt 1) (> col-cnt 1)]
      ;[true true] (apply powerit-pca data n-comps kwargs)


(defn pca-project [data pcs]
  "Apply the principal component projection specified by pcs to the data"
  ; Here we map each row of data to it's projection
  (mmul data (transpose pcs)))



(ns polismath.pca
  (:refer-clojure :exclude [* - + == /])
  (:use polismath.utils
        [clojure.core.match :only (match)]
        clojure.core.matrix 
        clojure.core.matrix.stats
        clojure.core.matrix.operators))

(set-current-implementation :vectorz)
(require '[clojure.tools.trace :as tr])
(use 'alex-and-georges.debug-repl)

(set! *unchecked-math* true)

