aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/generic/math_functions.clj
diff options
context:
space:
mode:
authorKonrad Hinsen <konrad.hinsen@laposte.net>2009-03-13 13:23:53 +0000
committerKonrad Hinsen <konrad.hinsen@laposte.net>2009-03-13 13:23:53 +0000
commit9202e255b24716484492671f9ea85d412f983ed6 (patch)
tree3e307dd7b73504775266fd48dcf02d3fa9d713bf /src/clojure/contrib/generic/math_functions.clj
parentdba39786323602b548faad6088a5f2ef72b3430d (diff)
generic: modified arithmetic and added math-functions
Diffstat (limited to 'src/clojure/contrib/generic/math_functions.clj')
-rw-r--r--src/clojure/contrib/generic/math_functions.clj61
1 files changed, 61 insertions, 0 deletions
diff --git a/src/clojure/contrib/generic/math_functions.clj b/src/clojure/contrib/generic/math_functions.clj
new file mode 100644
index 00000000..d2d1fe97
--- /dev/null
+++ b/src/clojure/contrib/generic/math_functions.clj
@@ -0,0 +1,61 @@
+;; Generic interfaces for mathematical functions
+
+;; by Konrad Hinsen
+;; last updated March 13, 2009
+
+;; Copyright (c) Konrad Hinsen, 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 clojure.contrib.generic.math-functions
+ "Generic math function interface
+
+ NOTE: This library is VERY experimental. It WILL change significantly
+ with future release.
+
+ This library defines generic versions of common mathematical functions
+ such as sqrt or sin as multimethods that can be defined for any type."
+ (:use [clojure.contrib.def :only (defmacro-)]))
+
+(defmacro- defmathfn-1
+ [name]
+ (let [java-symbol (symbol "java.lang.Math" (str name))]
+ `(do
+ (defmulti ~name type)
+ (defmethod ~name java.lang.Number
+ [~'x]
+ (~java-symbol ~'x)))))
+
+(defn- two-types [x y] [(type x) (type y)])
+
+(defmacro- defmathfn-2
+ [name]
+ (let [java-symbol (symbol "java.lang.Math" (str name))]
+ `(do
+ (defmulti ~name two-types)
+ (defmethod ~name [java.lang.Number java.lang.Number]
+ [~'x ~'y]
+ (~java-symbol ~'x ~'y)))))
+
+; List of math functions taken from
+; http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html
+(defmathfn-1 abs)
+(defmathfn-1 acos)
+(defmathfn-1 asin)
+(defmathfn-1 atan)
+(defmathfn-2 atan2)
+(defmathfn-1 ceil)
+(defmathfn-1 cos)
+(defmathfn-1 exp)
+(defmathfn-1 floor)
+(defmathfn-1 log)
+(defmathfn-2 pow)
+(defmathfn-1 rint)
+(defmathfn-1 round)
+(defmathfn-1 sin)
+(defmathfn-1 sqrt)
+(defmathfn-1 tan)