1. Steve Losh
  2. khan

Commits

Steve Losh  committed 0c91112

first run.

  • Participants
  • Parent commits 80a077f
  • Branches default

Comments (0)

Files changed (8)

File .hgignore

View file
 syntax:glob
 pom.xml
 *jar
-/lib/
-/classes/
+lib/
+classes/
 .lein-deps-sum
 .cake

File README

View file
+# khan
+
+FIXME: write description
+
+## Usage
+
+FIXME: write
+
+## License
+
+Copyright (C) 2011 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.

File core.clj

Empty file added.

File project.clj

View file
+(defproject khan "1.0.0-SNAPSHOT"
+  :description "FIXME: write description"
+  :dependencies [[org.clojure/clojure "1.2.1"]])

File src/khan/core.clj

View file
+(ns khan.core)

File src/khan/linear.clj

View file
+(ns khan.linear)
+
+(defn row-count [m]
+  (count m))
+
+(defn col-count [m]
+  (count (first m)))
+
+
+(defn rows [m]
+  m)
+
+(defn cols [m]
+  (let [ncols (col-count m)]
+    (map (fn [c] (apply vector (map #(get % c) m))) (range ncols))))
+
+
+(defn vector+ [v1 v2]
+  (apply vector (map + v1 v2)))
+
+(defn vector- [v1 v2]
+  (apply vector (map - v1 v2)))
+
+
+(defn dot-product [v1 v2]
+  (apply + (map * v1 v2)))
+
+
+(defn matrix+ [m n]
+  (apply vector (map vector+ m n)))
+(defn matrix- [m n]
+  (apply vector (map vector- m n)))
+(defn matrix* [m n]
+  {:pre [(= (col-count m)
+            (row-count n))]}
+  (let [nrows (row-count m)
+        ncols (col-count n)]
+    (apply vector
+           (map (fn [r]
+                  (apply vector
+                         (map (fn [c]
+                                (dot-product (nth (rows m) r)
+                                             (nth (cols n) c)))
+                              (range ncols))))
+                (range nrows)))))

File test/khan/test/core.clj

View file
+(ns khan.test.core
+  (:use [khan.core])
+  (:use [clojure.test]))
+
+; (deftest replace-me ;; FIXME: write
+;   (is false "No tests have been written."))

File test/khan/test/linear.clj

View file
+(ns khan.test.linear
+  (:use [khan.linear])
+  (:use [clojure.test]))
+
+(deftest test-row-count
+         (is (= (row-count [[1 2]
+                            [3 4]]) 2)
+             "Incorrect number of rows.")
+         (is (= (row-count [[1 2]
+                            [3 4]
+                            [5 6]]) 3)
+             "Incorrect number of rows."))
+
+(deftest test-col-count
+         (is (= (col-count [[1 2]
+                            [3 4]]) 2)
+             "Incorrect number of columns.")
+         (is (= (col-count [[1 2]
+                            [3 4]
+                            [5 6]]) 2)
+             "Incorrect number of columns."))
+
+(deftest test-rows
+         (is (= [[1 2 3] [0 0 0]]
+                (rows [[1 2 3]
+                       [0 0 0]]))
+             "Incorrect rows.")
+         (is (= [[0 0 0]]
+                (rows [[0 0 0]]))
+             "Incorrect rows."))
+
+(deftest test-cols
+         (is (= [[1 0] [2 0] [3 0]]
+                (cols [[1 2 3]
+                       [0 0 0]]))
+             "Incorrect columns.")
+         (is (= [[0] [0] [0]]
+                (cols [[0 0 0]]))
+             "Incorrect columns."))
+
+(deftest test-matrix+
+         (is (= (matrix+ [[1 2]
+                          [3 4]]
+                         [[5 6]
+                          [7 8]])
+                [[6 8]
+                 [10 12]])
+             "Invalid addition."))
+
+(deftest test-matrix-
+         (is (= (matrix- [[1 2]
+                          [3 4]]
+                         [[5 6]
+                          [7 8]])
+                [[-4 -4]
+                 [-4 -4]])
+             "Invalid subtraction."))
+
+(deftest test-dot-product
+         (is (= (dot-product [1 2 3] [10 100 1000])
+                (+ (* 1 10)
+                   (* 2 100)
+                   (* 3 1000)))
+             "Invalid dot product."))