diff src/clojure/contrib/generic/math_functions.clj @ 10:ef7dbbd6452c

added clojure source goodness
author Robert McIntyre <rlm@mit.edu>
date Sat, 21 Aug 2010 06:25:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojure/contrib/generic/math_functions.clj	Sat Aug 21 06:25:44 2010 -0400
     1.3 @@ -0,0 +1,114 @@
     1.4 +;; Generic interfaces for mathematical functions
     1.5 +
     1.6 +;; by Konrad Hinsen
     1.7 +;; last updated May 5, 2009
     1.8 +
     1.9 +;; Copyright (c) Konrad Hinsen, 2009. All rights reserved.  The use
    1.10 +;; and distribution terms for this software are covered by the Eclipse
    1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
    1.12 +;; which can be found in the file epl-v10.html at the root of this
    1.13 +;; distribution.  By using this software in any fashion, you are
    1.14 +;; agreeing to be bound by the terms of this license.  You must not
    1.15 +;; remove this notice, or any other, from this software.
    1.16 +
    1.17 +(ns
    1.18 +  ^{:author "Konrad Hinsen"
    1.19 +     :doc "Generic math function interface
    1.20 +           This library defines generic versions of common mathematical
    1.21 +           functions such as sqrt or sin as multimethods that can be
    1.22 +           defined for any type."}
    1.23 +  clojure.contrib.generic.math-functions
    1.24 +  (:use [clojure.contrib.def :only (defmacro-)])
    1.25 +  (:require [clojure.contrib.generic.arithmetic :as ga]
    1.26 +	    [clojure.contrib.generic.comparison :as gc]))
    1.27 +
    1.28 +(defmacro- defmathfn-1
    1.29 +  [name]
    1.30 +  (let [java-symbol (symbol "java.lang.Math" (str name))]
    1.31 +    `(do
    1.32 +       (defmulti ~name
    1.33 +	 ~(str "Return the " name " of x.")
    1.34 +	 {:arglists '([~'x])}
    1.35 +	 type)
    1.36 +       (defmethod ~name java.lang.Number
    1.37 +	 [~'x]
    1.38 +	 (~java-symbol ~'x)))))
    1.39 +
    1.40 +(defn- two-types [x y] [(type x) (type y)])
    1.41 +
    1.42 +(defmacro- defmathfn-2
    1.43 +  [name]
    1.44 +  (let [java-symbol (symbol "java.lang.Math" (str name))]
    1.45 +    `(do
    1.46 +       (defmulti ~name
    1.47 +	 ~(str "Return the " name " of x and y.")
    1.48 +	 {:arglists '([~'x ~'y])}
    1.49 +	 two-types)
    1.50 +       (defmethod ~name [java.lang.Number java.lang.Number]
    1.51 +	 [~'x ~'y]
    1.52 +	 (~java-symbol ~'x ~'y)))))
    1.53 +
    1.54 +; List of math functions taken from
    1.55 +; http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html
    1.56 +(defmathfn-1 abs)
    1.57 +(defmathfn-1 acos)
    1.58 +(defmathfn-1 asin)
    1.59 +(defmathfn-1 atan)
    1.60 +(defmathfn-2 atan2)
    1.61 +(defmathfn-1 ceil)
    1.62 +(defmathfn-1 cos)
    1.63 +(defmathfn-1 exp)
    1.64 +(defmathfn-1 floor)
    1.65 +(defmathfn-1 log)
    1.66 +(defmathfn-2 pow)
    1.67 +(defmathfn-1 rint)
    1.68 +(defmathfn-1 round)
    1.69 +(defmathfn-1 sin)
    1.70 +(defmathfn-1 sqrt)
    1.71 +(defmathfn-1 tan)
    1.72 +
    1.73 +;
    1.74 +; Sign
    1.75 +;
    1.76 +(defmulti sgn
    1.77 +  "Return the sign of x (-1, 0, or 1)."
    1.78 +  {:arglists '([x])}
    1.79 +  type)
    1.80 +
    1.81 +(defmethod sgn :default
    1.82 +  [x]
    1.83 +  (cond (gc/zero? x) 0
    1.84 +	(gc/> x 0) 1
    1.85 +	:else -1))
    1.86 +
    1.87 +;
    1.88 +; Conjugation
    1.89 +;
    1.90 +(defmulti conjugate
    1.91 +  "Return the conjugate of x."
    1.92 +  {:arglists '([x])}
    1.93 +  type)
    1.94 +
    1.95 +(defmethod conjugate :default
    1.96 +  [x] x)
    1.97 +
    1.98 +;
    1.99 +; Square
   1.100 +;
   1.101 +(defmulti sqr
   1.102 +  "Return the square of x."
   1.103 +  {:arglists '([x])}
   1.104 +  type)
   1.105 +
   1.106 +(defmethod sqr :default
   1.107 +  [x]
   1.108 +  (ga/* x x))
   1.109 +
   1.110 +;
   1.111 +; Approximate equality for use with floating point types
   1.112 +;
   1.113 +(defn approx=
   1.114 +  "Return true if the absolute value of the difference between x and y
   1.115 +   is less than eps."
   1.116 +  [x y eps]
   1.117 +  (gc/< (abs (ga/- x y)) eps))