-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.3.rkt
293 lines (233 loc) · 10.5 KB
/
2.3.rkt
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#lang sicp
;Seciton 2.3
;Symbolic Data
;Ex 2.54
(define (equal? a b)
(cond ((and (not (pair? a)) (not (pair? b))) (eq? a b))
((and (pair? a) (pair? b)) (and (equal? (car a) (car b)) (equal? (cdr a) (cdr b))))
(else #f)))
;Symbolic differentiation
(define (deriv exp var)
(cond ((number? exp) 0)
((variable? exp) (if (same-variable? exp var) 1 0))
((sum? exp) (make-sum (deriv (addend exp) var)
(deriv (augend exp) var)))
((product? exp)
(make-sum
(make-product (multiplier exp)
(deriv (multiplicand exp) var))
(make-product (deriv (multiplier exp) var)
(multiplicand exp))))
((exponentiation? exp)
;Here we can check if the exponent is an expression or just a number
;Now we assume it's just a number
(make-product
(exponent exp)
(make-product
(make-exp (base exp) (- (exponent exp) 1))
(deriv (base exp) var))
))
(else
(error "unknown expression type: DERIV" exp))))
(define (variable? x) (symbol? x))
(define (same-variable? v1 v2)
(and (variable? v1) (variable? v2) (eq? v1 v2)))
(define (=number? exp num) (and (number? exp) (= exp num)))
(define (make-sum a1 a2)
(cond ((=number? a1 0) a2)
((=number? a2 0) a1)
((and (number? a1) (number? a2))
(+ a1 a2))
((null? a2) a1)
;Make it accept arbitrary numbers of terms
;((pair? a2) (if (not (null? (cdr a2)))
; (list '+ a1 (make-sum (car a2) (cdr a2)))
; (list '+ a1 (car a2))))
;Additional conditions for symplifying expressions
((same-variable? a1 a2) (list 2 '* a1))
((same-variable-summed? a1 a2) (list (+ 1 (oprt1 a1)) '* a2))
((same-variable-summed? a2 a1) (list (+ 1 (oprt1 a2)) '* a1))
((equal? a1 a2) (make-product 2 a1))
((same-expression-scaled? a1 a2) (combiner-list (+ (oprt1 a2) 1) a1 '*))
((same-expression-scaled? a2 a1) (combiner-list (+ (oprt1 a1) 1) a2 '*))
;############
;(else (list a1 '+ a2))
(else (combiner-list a1 a2 '+))
))
(define (make-product m1 m2)
(cond ((or (=number? m1 0) (=number? m2 0)) 0)
((=number? m1 1) (if (product? m2) (make-product (multiplier m2) (multiplicand m2)) m2)) ;m2)
((=number? m2 1) (if (product? m1) (make-product (multiplier m1) (multiplicand m1)) m1))
((and (number? m1) (number? m2)) (* m1 m2))
((null? m2) m1)
;Additional conditions for symplifying expressions
((same-variable? m1 m2) (list m1 '^ 2))
((same-variable-multiplied? m1 m2) (make-product (get-multiplied-var m1 m2) (list m1 '^ 2)))
((same-variable-exp? m1 m2) (list m2 '^ (+ 1 (exponent m1))))
((same-variable-exp? m2 m1) (list m1 '^ (+ 1 (exponent m2))))
((and (product? m2) (same-variable-exp-scaled? m1 m2))
(list (multiplier m2) '* (make-exp m1 (+ 1 (exponent (multiplicand m2))))))
((same-variable-exp-both? m1 m2)
(list (base m1) '^ (+ (exponent m1) (exponent m2))))
((or (product? m1) (product? m2))
(let ((is-prod-m1 (product? m1))
;(same-variable? (first-multiplicand-item m1) m2)))
(is-prod-m2 (product? m2)))
;(same-variable? (first-multiplicand-item m2) m1))))
(if (and ;(or is-prod-m1 is-prod-m2)
(same-variable?
(if is-prod-m1 (first-multiplicand-item m1) m1)
(if is-prod-m2 (first-multiplicand-item m2) m2))
(same-variable?
(if is-prod-m1 (multiplier m1) m1)
(if is-prod-m2 (multiplier m2) m2))
)
(make-product
(if is-prod-m1 (make-product (multiplier m1) (multiplicand m1)) m1)
(if is-prod-m2 (make-product (multiplier m2) (multiplicand m2)) m2))
(combiner-list (if (pair? m1) (list m1) m1) m2 '*))))
;((and (product? m1)
; (same-variable? (first-multiplicand-item m1) m2))
; (make-product (make-product (multiplier m1) (multiplicand m1)) m2))
;((and (product? m2)
; (same-variable? (first-multiplicand-item m2) m1))
; (make-product m1 (make-product (multiplier m2) (multiplicand m2))))
;####################################
;(else (list m1 '* m2))
(else (combiner-list (if (pair? m1) (list m1) m1) m2 '*))
))
;Predicate
(define (sum? x)
;
(and (pair? x)
(or (eq? (operation x) '+)
(takes-precedence x '+))))
(define (addend s)
(cond ((equal? (operation s) '+) (oprt1 s))
(else (first-operands s '+))))
;((takes-precedence s '+) (list (oprt1 s) (operation s) (oprt2 s)))))
(define (augend s)
;(if (null? (cdddr s)) (oprt2 s)
; (if (equal? (operation s) '+)
; (multi-term s '+);Make it accept arbitrary numbers of terms if there is more than one item left to be summed
; (next-operands s)))
(cond ((null? (cdddr s)) (oprt2 s))
((equal? (operation s) '+)
(multi-term s '+));Make it accept arbitrary numbers of terms if there is more than one item left to be summed
(else (second-operands s '+))))
;(list-to-oper-groups (cddr s) '+)))
;(make-sum (caddr s) (cdddr s)))); (make-sum (cadddr s) (augend (list '+ (cdddr s))))))
(define (product? x)
;Implement order of operations here. multiplication takes precedence over summutation
(and (pair? x) (eq? (operation x) '*)))
(define (multiplier p) (oprt1 p))
(define (multiplicand p)
(if (null? (cdddr p)) (oprt2 p)
;Make it accept arbitrary numbers of terms
;If there is more than one item left to be multiplied
(multi-term p '*)))
;(list-to-oper-groups (cddr p) '*)))
;Ex 2.57
(define (exp a b)
(define (iter n res)
(if (= n 1) res
(iter (- n 1) (* res a))))
(iter b a))
(define (make-exp a b)
(cond ((=number? a 1) 1)
((=number? b 0) 1)
((=number? b 1) a)
((and (number? a) (number? b)) (exp a b))
;(else (combiner-list a b '^))))
(else (list a '^ b))))
(define (exponentiation? x) (and (pair? x) (eq? (operation x) '^)))
(define (base e) (oprt1 e))
(define (exponent e) (oprt2 e))
;Ex 2.57
;Use this inside augend and multiplicand if there are more than two items to be summed (or multiplied)
(define (list-to-oper-groups l op)
(if (null? (cdr l)) (car l)
(list op (car l) (list-to-oper-groups (cdr l) op))))
;
;(define (sum-in? expr) (= '+ (cadr expr)))
;Prefix notation
;(define (oprt1 expr) (cadr expr))
;(define (operation expr) (car expr))
;(define (oprt2 expr) (caddr expr))
;Infix notation
(define (oprt1 expr) (car expr))
(define (operation expr) (cadr expr))
(define (oprt2 expr) (caddr expr))
(define (multi-term expr op)
(cddr expr);For infex
;(list-to-oper-groups (cddr p) op)For prefix
)
;Order of operations
(define (takes-precedence expr op)
(cond ((not (pair? expr)) #f)
((not (pair? (cdddr expr))) #f)
((equal? (operation (cddr expr)) op) #t);cddr gives the second expression (after the first operation)
;((pair? (cddr expr)(takes
(else (takes-precedence (cddr expr) op))));(cddr (cdddr expr)) gives the expressions following the second expression
;(else #f)))
(define (first-operands expr op)
(define (iter exp result)
(cond ((not (pair? (cdddr exp))) result)
((equal? (operation exp) op) result)
(else (iter (cddr exp) (append result (list (operation exp) (oprt2 exp)))))))
;(else (cons result (cons (operation exp) (cons (oprt2 exp) (iter (cddr exp) nil)))))))
(iter expr (oprt1 expr)))
(define (second-operands expr op)
(define (iter exp result)
(cond ((null? (cdr result)) (car result))
((not (pair? (cdddr exp))) result)
((equal? (operation exp) op) result)
(else (iter (cddr exp) (cddr result)))))
;(else (cons result (cons (operation exp) (cons (oprt2 exp) (iter (cddr exp) nil)))))))
(iter expr (cddr expr)))
(define (append list1 list2)
(if (null? list1) list2
(if (not (pair? list1)) (cons list1 list2)
(cons (car list1) (append (cdr list1) list2)))))
(define (combiner a1 a2 op)
;(list a1 op a2))
(append a1 (list op a2)))
;(append a1 (if (pair? a2) (cons op a2) (list op a2))))
(define (combiner-list a1 a2 op)
;(append a1 (if (pair? a2) (cons op a2) (list op a2))))
(append a1 (list op a2)))
; ---------- Additional procedures for symplifying expressions ---------------
;Note :
;Use oprt2 instead of multiplicand because multiplicand gives all the remaining items while oprt2 doesn't which is desirable sometimes
(define (first-multiplicand-item expr)
(let ((multiplicand (multiplicand expr)))
(if (pair? multiplicand) (car multiplicand) multiplicand)))
;Checks if two variables are the same, but the second is multiplied by a constant factor
(define (same-variable-summed? v1 v2)
(or (and (variable? v1) (variable? v2) (eq? v1 v2))
(and (product? v1) (number? (multiplier v1)) (same-variable? (oprt2 v1) v2))))
;Checks if two expressions are the same, but the second is multiplied by a constant factor
(define (same-expression-scaled? v1 v2)
(and (product? v2) (number? (multiplier v2)) (equal? v1 (oprt2 v2))))
;Checks two variables are the same but the second multiplied with a constant factor
(define (same-variable-multiplied? v1 v2)
(and (product? v2)
(or (and
(not (same-variable? (multiplier v2) v1))
(same-variable? (oprt2 v2) v1))
(and
(not (same-variable? (oprt2 v2) v1))
(same-variable? (multiplier v2) v1)))))
(define (get-multiplied-var v1 v2)
(if (not (same-variable? (multiplier v2) v1)) (multiplier v2) (oprt2 v2)))
(define (same-variable-exp? v1 v2)
(or (and (variable? v1) (variable? v2) (eq? v1 v2))
(and (exponentiation? v1) (number? (exponent v1)) (same-variable? (base v1) v2))))
(define (same-variable-exp-scaled? v1 v2)
(and (exponentiation? (multiplicand v2)) (not (same-variable? v1 (multiplier v2)))
(same-variable? v1 (base (multiplicand v2)))))
(define (same-variable-exp-both? v1 v2)
;(or (and (variable? v1) (variable? v2) (eq? v1 v2))
(and (exponentiation? v1) (exponentiation? v2)
(number? (exponent v1)) (number? (exponent v2))
(same-variable? (base v1) (base v2))))