Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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