Skip to content

Commit

Permalink
Add tests for infix notation, from http://data-sorcery.org/2010/05/14…
Browse files Browse the repository at this point in the history
  • Loading branch information
mtnygard committed Sep 16, 2011
1 parent c0d4b5a commit 62039cf
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 26 deletions.
60 changes: 34 additions & 26 deletions README.textile
Expand Up @@ -131,32 +131,6 @@ h2. Incanter dependencies

h2. Problems Moving to Clojure 1.3

h3. Leiningen

* Complains that class clojure.set not found (called from
incanter.core as clojure.set/difference). This didn't happen when
building under cake, for some reason. Workaround: change call from
clojure.set/difference to just difference, add :use in ns decl.

h3. Contrib migration

* From clojure.contrib.core, defvar and defvar- didn't make it into
core.incubator. Scope: only used by distributions.clj. Workaround:
changed defvar- calls to just def.

* clojure.contrib.combinatorics: moved to math.combinatorics, but no
release made yet. Workaround: added pom.xml to local clone of
https://github.com/clojure/math.combinatorics.git. Built using "mvn
install" to put jar in local repository.

h3. Sequences

* Matrix.java no longer accepted as seq. Previously, implementing ISeq
was sufficient, now it appears the marker interface
clojure.lang.Sequential is also needed. Workaround: added Sequential
to Matrix's implements clause. Open question: Should this be
Sequential vs. Seqable?

h3. Numerics

* Integer overflow in distributions_tests.clj, function
Expand All @@ -176,6 +150,23 @@ h3. Numerics
incanter.core/get-dummies, which was passing a double but didn't
need to. Have made that explicitly an integer.

* @(matrix)@ promotes to double.

* Equality tests are now problematic. Either use = and double or
bigint literals, or use == and don't force precision on literals.
* However, matrix-to-list or lazy-seq-to-list compares don't work with
==. Symmetry is broken and and clojure lists don't look like numeric
lists, and clojure vectors don't look like algebraic vectors.


h3. Sequences

* Matrix.java no longer accepted as seq. Previously, implementing ISeq
was sufficient, now it appears the marker interface
clojure.lang.Sequential is also needed. Workaround: added Sequential
to Matrix's implements clause. Open question: Should this be
Sequential vs. Seqable?

h3. Dynamic vars

* Compiler complains about @*test-statistic-iterations*@ and
Expand All @@ -187,6 +178,23 @@ h3. Dynamic vars
added ^{:dynamic true} to metadata. Open question: does rebinding
this fit in the 1.3 model for vars? Check with Stu about threading.

h3. Leiningen

* Complains that class clojure.set not found (called from
incanter.core as clojure.set/difference). This didn't happen when
building under cake, for some reason. Workaround: change call from
clojure.set/difference to just difference, add :use in ns decl.

h3. Contrib migration

* From clojure.contrib.core, defvar and defvar- didn't make it into
core.incubator. Scope: only used by distributions.clj. Workaround:
changed defvar- calls to just def.

* clojure.contrib.combinatorics: moved to math.combinatorics, but no
release made yet. Workaround: added pom.xml to local clone of
https://github.com/clojure/math.combinatorics.git. Built using "mvn
install" to put jar in local repository.

h3. Missing tests

Expand Down
71 changes: 71 additions & 0 deletions modules/incanter-core/test/incanter/infix_tests.clj
@@ -0,0 +1,71 @@
;;; infix-tests.clj -- Unit tests of Incanter infix expression functions

;; by Michael Nygard http://incanter.org
;; Sept 16 2011

;; Copyright (c) David Edgar Liebke, 2009. All rights reserved. The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution. By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license. You must not
;; remove this notice, or any other, from this software.



(ns incanter.infix-tests
(:use clojure.test
(incanter core infix)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; UNIT TESTS FOR incanter.infix.clj
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(deftest basic-arithmetic
(is (= ($= 7 + 8 - 2 * 6 / 2) 9))
(is (= ($= [1 2 3] + 5) '(6 7 8)))
(is (= ($= [1 2 3] + [4 5 6]) '(5 7 9)))
(is (= ($= [1 2 3] * [1 2 3]) '(1 4 9)))
(is (= ($= [1 2 3] / [1 2 3]) '(1 1 1)))
(is (= ($= (matrix [[1 2] [4 5]]) + 6) (matrix [[7 8] [10 11]])))
(is (= ($= (trans [[1 2] [4 5]]) + 6) (matrix [[7 10] [8 11]])))
(is (= ($= 8 ** 3) 512.0))
(is (= ($= 8 ** 1/2) 2.8284271247461903))
(is (= ($= 2 ** -2) 0.25))
(is (= ($= [1 2 3] ** 2) '(1.0 4.0 9.0)))
(is (= ($= 10 + 20 * (4 - 5) / 6) 20/3))
(is (= ($= (10 + 20) * 4 - 5 / 6) 715/6))
(is (= ($= 10 + 20 * (4 - 5 / 6)) 220/3))
(is (= ($= ((((5 + 4) * 5)))) 45)))

(deftest functions-in-infix-expressions
(is (= (let [x 10 y -5] ($= x + y / -10)) 21/2))
(is (= ($= (sqrt 5) * 5 + 3 * 3) 20.18033988749895))
(is (= ($= sq [1 2 3] + [1 2 3]) [2 6 12]))
(is (= ($= sin 2 * Math/PI * 2) 5.713284232087328))
(is (= ($= (cos 0) * 10) 10.0))
(is (= ($= (tan 2) * Math/PI * 10) -68.64505182223235))
(is (= ($= (asin 1/2) * 10) 5.23598775598299))
(is (= ($= (acos 1/2) * 10) 10.47197551196598))
(is (= ($= (atan 1/2) * 10) 4.636476090008061))
(is (= ($= [1 2 3] / (sq [1 2 3]) + [5 6 7]) '(6 13/2 22/3)))
(is (= ($= [1 2 3] + (sin [4 5 6])) '(0.2431975046920718 1.0410757253368614 2.720584501801074)))
(is (= ($= 3 > (5 * 2/7)) true))
(is (= ($= 3 <= (5 * 2/7)) false))
(is (= ($= 3 != (5 * 2/7)) true))
(is (= ($= 3 == (5 * 2/7)) false))
(is (= ($= 3 != 8 || 6 > 12) true)))

(deftest matrix-products-in-infix
(is (= ($= [1 2 3] <*> (trans [1 2 3])) (matrix [[1 2 3] [2 4 6] [3 6 9]])))
(is (= ($= (trans [[1 2] [4 5]]) <*> (matrix [[1 2] [4 5]])) (matrix [[17 22] [22 29]])))
(is (= ($= (trans [1 2 3 4]) <*> [1 2 3 4]) 30.0))
(is (= ($= [1 2 3 4] <*> (trans [1 2 3 4])) (matrix [[1 2 3 4] [2 4 6 8] [3 6 9 12] [4 8 12 16]]))))

(deftest kronecker-product-in-infix
(is (= ($= [1 2 3] <x> [1 2 3]) (matrix [1 2 3 2 4 6 3 6 9])))
(is (= ($= (matrix [[1 2] [3 4] [5 6]]) <x> 4) (matrix [[4 8] [12 16] [20 24]])))
(is (= ($= (matrix [[1 2] [3 4] [5 6]]) <x> (matrix [[1 2] [3 4]])) (matrix [[1 2 2 4] [3 4 6 8] [3 6 4 8] [9 12 12 16] [5 10 6 12] [15 20 18 24]])))
(is (= ($= [1 2 3 4] <x> 4) (matrix [4 8 12 16])))
(is (= ($= [1 2 3 4] <x> (trans [1 2 3 4])) (matrix [[1 2 3 4] [2 4 6 8] [3 6 9 12] [4 8 12 16]]))))

0 comments on commit 62039cf

Please sign in to comment.