-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcos.sls
151 lines (108 loc) · 3.74 KB
/
cos.sls
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!r6rs
(library (mpl cos)
(export cos)
(import (mpl rnrs-sans)
(rename (only (rnrs) cos) (cos rnrs:cos))
(mpl match)
(mpl arithmetic)
(mpl numerator)
(mpl denominator)
(mpl sqrt)
(mpl misc))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define pi 'pi)
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (simplify-cos-first-quadrant a/b)
(cond ( (> a/b 2) (cos (* (mod a/b 2) pi)) )
( (> a/b 1) (- (cos (- (* a/b pi) pi))) )
( (> a/b 1/2) (- (cos (- pi (* a/b pi)))) )
( else `(cos ,(* a/b pi)) )))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (simplify-cos-k/n*pi k/n)
(let ((k (numerator k/n))
(n (denominator k/n)))
(case n
((1) (case (mod k 2)
((1) -1)
((0) 1)))
((2) (case (mod k 2)
((1) 0)))
((3) (case (mod k 6)
((1 5) 1/2)
((2 4) -1/2)))
((4) (case (mod k 8)
((1 7) (/ 1 (sqrt 2)))
((3 5) (- (/ 1 (sqrt 2))))))
((6) (case (mod k 12)
((1 11) (/ (sqrt 3) 2))
((5 7) (- (/ (sqrt 3) 2))))))))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (n*pi? elt)
(define (n? x)
(and (number? x)
(exact? x)
(>= (abs x) 2)))
(match elt
( ('* (? n?) 'pi) #t )
( else #f )))
(define (simplify-cos-sum-with-pi elts)
(let ((pi-elt (find n*pi? elts)))
(let ((n (list-ref pi-elt 1)))
(cos (+ (- (apply + elts) pi-elt)
(* (mod n 2) pi))))))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (n/2*pi? elt)
(define (n/2? x)
(and (number? x)
(exact? x)
(equal? (denominator x) 2)))
(match elt
( ('* (? n/2?) 'pi) #t )
( else #f )))
(define (simplify-cos-sum-with-n/2*pi elts)
(let ((n/2*pi (find n/2*pi? elts)))
(let ((other-elts (- (apply + elts) n/2*pi)))
(let ((n (numerator (list-ref n/2*pi 1))))
(case (mod n 4)
((1) (- `(sin ,other-elts)))
((3) `(sin ,other-elts)))))))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (simplify-cos u)
(match u
( ('cos 0) 1 )
( ('cos 'pi) -1 )
( ('cos (? inexact-number? n)) (rnrs:cos n) )
( (and ('cos n)
(? (lambda (_)
(and (number? n)
(negative? n)))))
(cos (- n)) )
( (and ('cos ('* n . elts))
(? (lambda (_)
(and (number? n)
(negative? n)))))
(cos (apply * (append (list -1 n) elts))) )
( (and ('cos ('* a/b 'pi))
(? (lambda (_)
(and (number? a/b)
(exact? a/b)
(> a/b 1/2)))))
(simplify-cos-first-quadrant a/b) )
( (and ('cos ('* k/n 'pi))
(? (lambda (_)
(and (member (denominator k/n) '(1 2 3 4 6))
(integer? (numerator k/n))))))
(simplify-cos-k/n*pi k/n) )
( (and ('cos ('+ . elts))
(? (lambda (_)
(find n*pi? elts))))
(simplify-cos-sum-with-pi elts) )
( (and ('cos ('+ . elts))
(? (lambda (_)
(find n/2*pi? elts))))
(simplify-cos-sum-with-n/2*pi elts) )
( else u )))
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (cos x)
(simplify-cos `(cos ,x)))
)