annotate 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
rev   line source
rlm@10 1 ;; Generic interfaces for mathematical functions
rlm@10 2
rlm@10 3 ;; by Konrad Hinsen
rlm@10 4 ;; last updated May 5, 2009
rlm@10 5
rlm@10 6 ;; Copyright (c) Konrad Hinsen, 2009. All rights reserved. The use
rlm@10 7 ;; and distribution terms for this software are covered by the Eclipse
rlm@10 8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
rlm@10 9 ;; which can be found in the file epl-v10.html at the root of this
rlm@10 10 ;; distribution. By using this software in any fashion, you are
rlm@10 11 ;; agreeing to be bound by the terms of this license. You must not
rlm@10 12 ;; remove this notice, or any other, from this software.
rlm@10 13
rlm@10 14 (ns
rlm@10 15 ^{:author "Konrad Hinsen"
rlm@10 16 :doc "Generic math function interface
rlm@10 17 This library defines generic versions of common mathematical
rlm@10 18 functions such as sqrt or sin as multimethods that can be
rlm@10 19 defined for any type."}
rlm@10 20 clojure.contrib.generic.math-functions
rlm@10 21 (:use [clojure.contrib.def :only (defmacro-)])
rlm@10 22 (:require [clojure.contrib.generic.arithmetic :as ga]
rlm@10 23 [clojure.contrib.generic.comparison :as gc]))
rlm@10 24
rlm@10 25 (defmacro- defmathfn-1
rlm@10 26 [name]
rlm@10 27 (let [java-symbol (symbol "java.lang.Math" (str name))]
rlm@10 28 `(do
rlm@10 29 (defmulti ~name
rlm@10 30 ~(str "Return the " name " of x.")
rlm@10 31 {:arglists '([~'x])}
rlm@10 32 type)
rlm@10 33 (defmethod ~name java.lang.Number
rlm@10 34 [~'x]
rlm@10 35 (~java-symbol ~'x)))))
rlm@10 36
rlm@10 37 (defn- two-types [x y] [(type x) (type y)])
rlm@10 38
rlm@10 39 (defmacro- defmathfn-2
rlm@10 40 [name]
rlm@10 41 (let [java-symbol (symbol "java.lang.Math" (str name))]
rlm@10 42 `(do
rlm@10 43 (defmulti ~name
rlm@10 44 ~(str "Return the " name " of x and y.")
rlm@10 45 {:arglists '([~'x ~'y])}
rlm@10 46 two-types)
rlm@10 47 (defmethod ~name [java.lang.Number java.lang.Number]
rlm@10 48 [~'x ~'y]
rlm@10 49 (~java-symbol ~'x ~'y)))))
rlm@10 50
rlm@10 51 ; List of math functions taken from
rlm@10 52 ; http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Math.html
rlm@10 53 (defmathfn-1 abs)
rlm@10 54 (defmathfn-1 acos)
rlm@10 55 (defmathfn-1 asin)
rlm@10 56 (defmathfn-1 atan)
rlm@10 57 (defmathfn-2 atan2)
rlm@10 58 (defmathfn-1 ceil)
rlm@10 59 (defmathfn-1 cos)
rlm@10 60 (defmathfn-1 exp)
rlm@10 61 (defmathfn-1 floor)
rlm@10 62 (defmathfn-1 log)
rlm@10 63 (defmathfn-2 pow)
rlm@10 64 (defmathfn-1 rint)
rlm@10 65 (defmathfn-1 round)
rlm@10 66 (defmathfn-1 sin)
rlm@10 67 (defmathfn-1 sqrt)
rlm@10 68 (defmathfn-1 tan)
rlm@10 69
rlm@10 70 ;
rlm@10 71 ; Sign
rlm@10 72 ;
rlm@10 73 (defmulti sgn
rlm@10 74 "Return the sign of x (-1, 0, or 1)."
rlm@10 75 {:arglists '([x])}
rlm@10 76 type)
rlm@10 77
rlm@10 78 (defmethod sgn :default
rlm@10 79 [x]
rlm@10 80 (cond (gc/zero? x) 0
rlm@10 81 (gc/> x 0) 1
rlm@10 82 :else -1))
rlm@10 83
rlm@10 84 ;
rlm@10 85 ; Conjugation
rlm@10 86 ;
rlm@10 87 (defmulti conjugate
rlm@10 88 "Return the conjugate of x."
rlm@10 89 {:arglists '([x])}
rlm@10 90 type)
rlm@10 91
rlm@10 92 (defmethod conjugate :default
rlm@10 93 [x] x)
rlm@10 94
rlm@10 95 ;
rlm@10 96 ; Square
rlm@10 97 ;
rlm@10 98 (defmulti sqr
rlm@10 99 "Return the square of x."
rlm@10 100 {:arglists '([x])}
rlm@10 101 type)
rlm@10 102
rlm@10 103 (defmethod sqr :default
rlm@10 104 [x]
rlm@10 105 (ga/* x x))
rlm@10 106
rlm@10 107 ;
rlm@10 108 ; Approximate equality for use with floating point types
rlm@10 109 ;
rlm@10 110 (defn approx=
rlm@10 111 "Return true if the absolute value of the difference between x and y
rlm@10 112 is less than eps."
rlm@10 113 [x y eps]
rlm@10 114 (gc/< (abs (ga/- x y)) eps))