Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove Numerical interface #164

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/emmy/abstract/number.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@
(:import (clojure.lang Symbol))))

(extend-type Symbol
v/Numerical
(numerical? [_] true)

v/IKind
(kind [_] Symbol))

Expand Down
3 changes: 0 additions & 3 deletions src/emmy/complex/impl.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@

v/INumericTower

v/Numerical
(numerical? [_] true)

#?@(:clj [Object
(equals [a b] (equal? a b))
(toString [a] (->string a))]
Expand Down
2 changes: 1 addition & 1 deletion src/emmy/env.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -581,4 +581,4 @@
[emmy.special.elliptic elliptic-f]
[emmy.special.factorial factorial]
[emmy.value = compare
numerical? kind kind-predicate principal-value])
kind kind-predicate principal-value])
3 changes: 0 additions & 3 deletions src/emmy/expression.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
;; other abstract structures referenced in [[abstract-types]].

(deftype Literal [type expression m]
v/Numerical
(numerical? [_] (= type ::numeric))

v/IKind
(kind [_] type)

Expand Down
4 changes: 2 additions & 2 deletions src/emmy/generic.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@
([] 1)
([x] x)
([x y]
(let [numx? (v/numerical? x)
numy? (v/numerical? y)]
(let [numx? (v/number? x)
numy? (v/number? y)]
(cond (and numx? (zero? x)) (zero-like y)
(and numy? (zero? y)) (zero-like x)
(and numx? (one? x)) y
Expand Down
10 changes: 1 addition & 9 deletions src/emmy/ratio.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@

#?(:clj
(extend-type Ratio
v/Numerical
(numerical? [_] true)

v/IKind
(kind [_] Ratio)))

Expand Down Expand Up @@ -127,11 +124,6 @@
(extend-type bf/Fraction
v/INumericTower

v/IReal

v/Numerical
(numerical? [_] true)

#?@(:cljs [IEquiv
(-equiv [x other]
(cond (instance? bf/Fraction other) (bf/eq x other)
Expand Down Expand Up @@ -192,7 +184,7 @@
(bf/remainder (bf/add (bf/remainder a b) b) b)))
(defmethod g/gcd [bf/Fraction bf/Fraction] [a b] (bf/promote (bf/gcd a b)))

;; Cross-compatibility with numbers in CLJS.
;; Cross-compatibility with numbers in CLJS.
(defn- downcast-fraction
"Anything that `upcast-number` doesn't catch will hit this and pull a floating
point value out of the ratio."
Expand Down
7 changes: 0 additions & 7 deletions src/emmy/tape.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,6 @@
v/IKind
(kind [_] ::tape)

;; A [[TapeCell]] has to respond `false` to all [[emmy.value/numerical?]]
;; inquiries; if we didn't do this, then [[emmy.generic/*]] and friends would
;; attempt to apply shortcuts like `(* x <tape-with-1>) => x`, stripping off
;; the [[TapeCell]] identity of the result and ruining the derivative.
v/Numerical
(numerical? [_] false)

Object
;; Comparing [[TapeCell]] objects using `equals` defaults to [[equiv]], which
;; compares instances only using their non-tagged ('finite') components. If
Expand Down
55 changes: 9 additions & 46 deletions src/emmy/value.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,8 @@
(:import
(clojure.lang BigInt Sequential))))

(defprotocol Numerical
(^boolean numerical? [x]
"Returns true if `x` is a purely numerical value and should be considered for
numerical simplifications, such as $x * 1 == x$ or $x * 0 ==
0$.

[[numerical?]] should return `false` if `x` has additional, non-numerical
structure that should be preserved."))

(defprotocol INumericTower)

(defprotocol IReal)

(extend-protocol Numerical
#?(:clj Object :cljs default)
(numerical? [_] false))

(defprotocol IKind
(kind [this]))

Expand Down Expand Up @@ -140,17 +125,6 @@
(derive goog.math.Integer ::integral)
(derive goog.math.Long ::integral)))

(extend-protocol Numerical
#?(:clj Number :cljs number)
(numerical? [_] true)

#?@(:clj
[java.lang.Double
(numerical? [_] true)

java.lang.Float
(numerical? [_] true)]))

(extend-protocol IKind
#?(:clj Number :cljs number)
(kind [x] #?(:clj (type x)
Expand All @@ -172,8 +146,8 @@
nil
(kind [_] nil)

;; Var
;; (kind [v] (type v))
;; Var
;; (kind [v] (type v))

#?(:clj Object :cljs default)
(kind [o] (:type o (type o))))
Expand Down Expand Up @@ -311,26 +285,15 @@

#?(:cljs
;; ClojureScript-specific implementations of Value.
(do
(extend-protocol Numerical
js/BigInt
(numerical? [_] true)

goog.math.Integer
(numerical? [_] true)

goog.math.Long
(numerical? [_] true))

(extend-protocol IKind
js/BigInt
(kind [_] js/BigInt)
(extend-protocol IKind
js/BigInt
(kind [_] js/BigInt)

goog.math.Integer
(kind [_] goog.math.Integer)
goog.math.Integer
(kind [_] goog.math.Integer)

goog.math.Long
(kind [_] goog.math.Long))))
goog.math.Long
(kind [_] goog.math.Long)))

#?(:cljs
;; We find it convenient to be able to decorate "vanilla" JavaScript
Expand Down
3 changes: 0 additions & 3 deletions test/emmy/abstract/function_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@
(testing "g/one? returns false for fns"
(is (not (g/one? (af/literal-function 'f)))))

(testing "v/numerical? returns false for fns"
(is (not (v/numerical? (af/literal-function 'f)))))

(let [f (af/literal-function 'f)]
(checking "zero-like, one-like passes through for literal fns"
100 [n sg/real]
Expand Down
3 changes: 0 additions & 3 deletions test/emmy/abstract/number_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
(is (g/one? (g/one-like n)))
(is (g/identity? (g/identity-like n))))

(checking "v/numerical? returns true for all literal-number instances" 100 [n gen-literal]
(is (v/numerical? n)))

(checking "exact? mirrors input" 100 [n gen-literal-element]
(if (g/exact? n)
(is (g/exact? (an/literal-number n)))
Expand Down
12 changes: 4 additions & 8 deletions test/emmy/complex_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@

(is (= 10 (g/freeze (c/complex 10)))
"If the imaginary piece is 0, freeze will return only the real part.")
(is (v/numerical? (c/complex 10)))

(testing "exact?"
(is (not (g/exact? (c/complex 0 10.1))))
Expand Down Expand Up @@ -267,9 +266,9 @@
(checking "floor pushes through to complex components" 100
[x sg/complex]
(is (v/= (g/floor x)
(g/make-rectangular
(g/floor (g/real-part x))
(g/floor (g/imag-part x))))))
(g/make-rectangular
(g/floor (g/real-part x))
(g/floor (g/imag-part x))))))

(testing "ceiling"
(is (= (c/complex 1 2) (g/ceiling (c/complex 1 2))))
Expand Down Expand Up @@ -328,10 +327,7 @@
(is (near z (g/square (g/sqrt z))))))

(testing "sqrt"
(is (near (c/complex 10 10) (g/sqrt (g/mul c/I 200)))))

(testing "arithmetic"
(is (v/numerical? c/I)))))
(is (near (c/complex 10 10) (g/sqrt (g/mul c/I 200)))))))

(defn fourth-power-is-one?
"Checks if x^4 is 1+0i. This is needed because the gcd-complex function returns
Expand Down
2 changes: 1 addition & 1 deletion test/emmy/differential_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
(zero? compare-bit) (is (and (<= l r) (= l r) (>= l r)))
:else (is (> l r))))))))

(checking "v/numerical?, v/scalar?" 100 [diff (sg/dual sg/real)]
(checking " v/scalar?" 100 [diff (sg/dual sg/real)]
(is (v/scalar? diff)
"True for all duals"))

Expand Down
6 changes: 0 additions & 6 deletions test/emmy/function_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@
(is (not (g/identity? identity))
"We go conservative and say that EVEN the actual identity function is not identity."))

(testing "v/numerical? returns false for fns"
(is (not (v/numerical? neg?)))
(is (not (v/numerical? #'neg?)))
(is (not (v/numerical? g/add)))
(is (not (v/numerical? identity))))

(checking "zero-like, one-like returns 0, 1 for fns, vars" 100
[f (gen/elements [g/abs g/sin g/cos
#'g/abs #'g/sin #'g/cos])
Expand Down
2 changes: 1 addition & 1 deletion test/emmy/generic_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@
(is (= 1 (g/*)) "No args returns the multiplicative identity.")
(checking "g/*" 100 [x gen/any-equatable]
(is (v/= x (g/* x)) "single arg returns itself.")
(is (v/= (if (and (v/numerical? x) (g/one? x)) 1 x)
(is (v/= (if (and (v/number? x) (g/one? x)) 1 x)
(g/* x 1)) "First unity gets returned.")
(is (v/= x (g/* 1 x)) "Anything times a 1 returns itself.")))

Expand Down
6 changes: 0 additions & 6 deletions test/emmy/matrix_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,6 @@
(g/identity-like (m/by-rows [1 2 3 4])))
"identity-like is only supported on square matrices."))

(testing "numerical? returns false, always"
(is (not (v/numerical? (m/by-rows [1] [2]))))
(is (not (v/numerical? (m/by-rows [1.2] [3] [4]))))
(is (not (v/numerical? (m/by-rows [0] [0] [0.00001]))))
(is (not (v/numerical? (m/by-rows [0 1 (g// 3 2)])))))

(testing "exact?"
(is (g/exact? (m/by-rows [1] [2])))
(is (not (g/exact? (m/by-rows [1.2] [3] [4]))))
Expand Down
3 changes: 0 additions & 3 deletions test/emmy/operator_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
(is (not (g/one? (g/one-like x2))))
(is (g/identity? (g/identity-like x2))))

(testing "v/numerical?"
(is (not (v/numerical? x2))))

(testing "g/freeze"
(is (= 'double (g/freeze x2)))
(is (= '(- double) (g/freeze (g/negate x2))))
Expand Down
5 changes: 0 additions & 5 deletions test/emmy/quaternion_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
[emmy.matrix :as m]
[emmy.quaternion :as q]
[emmy.simplify]
[emmy.structure :as s]
[emmy.util.logic :as ul]
[emmy.value :as v]
[same.core :refer [ish? with-comparator]]))
Expand Down Expand Up @@ -182,10 +181,6 @@
(is (g/exact? (q/make 1 2 3 #emmy/ratio 3/2)))
(is (not (g/exact? (q/make 0 0 0 0.00001)))))

(testing "numerical?"
(is (not (v/numerical? (s/up 1 2 3 4)))
"no structure is numerical."))

(testing "freeze"
(is (= '(quaternion (/ 1 2) 2 3 x)
(g/freeze (q/make #emmy/ratio 1/2
Expand Down
3 changes: 0 additions & 3 deletions test/emmy/rational_function_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@
(polynomial 1 [[{} -1] [{0 1} 1]]))
(g/freeze x+1:x-1))))

(testing "v/numerical?"
(is (not (v/numerical? x+1:x-1))))

(testing "v/kind"
(is (= ::rf/rational-function
(v/kind x+1:x-1))))
Expand Down
3 changes: 0 additions & 3 deletions test/emmy/series_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
(= ::s/power-series (v/kind series))
(= ::s/series (v/kind series))))

(testing "v/numerical?"
(is (not (v/numerical? series))))

(testing "g/exact?"
(is (not (g/exact? series))))

Expand Down
14 changes: 1 addition & 13 deletions test/emmy/structure_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@
(is (g/exact? (constructor 0 1 #emmy/ratio 3/2)))
(is (not (g/exact? (constructor 0 0 0.00001))))))

(testing "numerical?"
(checking "no structure is numerical." 100
[s (sg/structure sg/real)]
(is (not (v/numerical? s)))))

(testing "freeze"
(is (= '(up 1 2 3) (g/freeze (s/up 1 2 3))))
(is (= '(down 1 2 3) (g/freeze (s/down 1 2 3)))))
Expand Down Expand Up @@ -713,16 +708,9 @@

(checking "typical-object vs compatible-shape" 100
[s (sg/structure sg/real 3)]
(is (v/numerical?
(g/* s (s/compatible-shape s)))
"structures collapse to numerical expressions when multiplied by
a compatible shape.")

(is (= (s/structure->prototype 'x (s/typical-object s))
(s/structure->prototype 'x (s/transpose
(s/compatible-shape s))))
"structures collapse to numerical expressions when multiplied by
a compatible shape."))
(s/compatible-shape s))))))

(checking "s/compatible-zero works" 100
[s (sg/structure sg/real)]
Expand Down
9 changes: 0 additions & 9 deletions test/emmy/tape_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@

(defmethod g/zero? [#?(:clj String :cljs js/String)] [_] false)

(testing "v/numerical? is always false"
(checking "tapecell with numerical primal is STILL not numerical, so we stay
excluded from numerical simplifications." 100
[t (sg/tapecell sg/real)]
(is (not (v/numerical? t))))

(is (not (v/numerical? (t/make 0 "face")))
"tapecell with non-numerical primal is not numerical"))

(checking "native comparison operators work with tapecell" 100
[l sg/real, r sg/real]
(is (= (v/compare l r)
Expand Down
7 changes: 0 additions & 7 deletions test/emmy/value_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,6 @@
[n (gen/one-of [sg/any-integral (sg/reasonable-double)])]
(is (= n (g/freeze n))))

(checking "all numbers are numerical" 100
[n sg/number]
(is (v/numerical? n)))

(is (v/numerical? 'x)
"Symbols are abstract numerical things.")

(is (isa? (v/kind 10) ::v/real))
(is (g/exact? 10))
(is (not (g/exact? 10.1))))
Expand Down
Loading