-
Notifications
You must be signed in to change notification settings - Fork 16
/
math.cljc
130 lines (107 loc) · 2.72 KB
/
math.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
(ns dali.math)
#?(:cljs
(def PI (.-PI js/Math)))
#?(:clj
(defn abs [x]
(java.lang.Math/abs x))
:cljs
(defn abs [x]
(.abs js/Math x)))
(defn within
"Tests whether x is within the range of a and b. a can be less or
more than b. The optional error parameter extends both sides of the
range by error."
([x [a b]]
(or (and (>= x a) (<= x b))
(and (>= x b) (<= x a))))
([x [a b] error]
(or (and (>= x (- a error)) (<= x (+ b error)))
(and (>= x (- b error)) (<= x (+ a error))))))
#?(:clj
(defn radians->degrees [x]
(java.lang.Math/toDegrees x))
:cljs
(defn radians->degrees [x]
(/ (* x 180) PI)))
#?(:clj
(defn degrees->radians [x]
(java.lang.Math/toRadians x))
:cljs
(defn degrees->radians [x]
(* (/ x 180) PI)))
#?(:clj
(defn sin
"The sine of angle x (in degrees)"
[x]
(java.lang.Math/sin (degrees->radians x)))
:cljs
(defn sin
"The sine of angle x (in degrees)"
[x]
(.sin js/Math (degrees->radians x))))
#?(:clj
(defn cos
"The cosine of angle x (in degrees)"
[x]
(java.lang.Math/cos (degrees->radians x)))
:cljs
(defn cos
"The cosine of angle x (in degrees)"
[x]
(.cos js/Math (degrees->radians x))))
#?(:clj
(defn atan2 [x y]
(java.lang.Math/atan2 x y))
:cljs
(defn atan2 [x y]
(.atan2 js/Math x y)))
#?(:clj
(defn sqrt
[x]
(java.lang.Math/sqrt x))
:cljs
(defn sqrt
[x]
(.sqrt js/Math x)))
(defn polar-angle
[[x y]]
(radians->degrees (atan2 y x)))
(defn cartesian->polar
[[x y]]
[(sqrt (+ (* x x) (* y y))) (polar-angle [x y])])
(defn polar->cartesian
[[r theta]]
[(* r (cos theta))
(* r (sin theta))])
(defn minus
"Makes all the numbers of a seq negative and returns it as a vector"
[x]
(vec (map - x)))
(defn determinant-2x2
"The determinant of a 2x2 matrix."
[[[a b] [c d]]]
(- (* a d)
(* b c)))
(defn solve-2x2-system
"Solves a 2x2 system of equations. cx is the coeffient of x, cy is
the coefficient of y, and the con variables are the constant
values."
[[cx1 cy1 con1]
[cx2 cy2 con2]]
(let [denom (determinant-2x2 [[cx1 cy1]
[cx2 cy2]])]
(when (not (zero? denom))
{:x (/ (determinant-2x2 [[con1 cy1]
[con2 cy2]])
denom)
:y (/ (determinant-2x2 [[cx1 con1]
[cx2 con2]])
denom)})))
(defn linear-equation-at-x
"Calculates the y value according to a linear equation based on the
passed x value. The equation information is passed as a map
with :slope and :offset keys."
[{:keys [slope offset]} x]
(-> x
(* slope)
(+ offset)))