-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exercise 2.92 polynomials in different variables.rkt
3300 lines (2867 loc) · 125 KB
/
Exercise 2.92 polynomials in different variables.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#lang racket
; Exercise 2.92. By imposing an ordering on variables, extend the polynomial package so that addition and
; multiplication of polynomials works for polynomials in different variables. (This is not easy!)
; S O L U T I O N
; Assumed ordering of variables is (in increasing order of priority):
; p, q, r, s, t, u, v, w, x, y, z
; I have implemented a generic print procedure so that polynomials and other types can be printed
; in an easy to read manner.
; The main work in this exercise lies in the 'convert-polynomial' procedure that transforms a polynomial
; from one variable to another
; This problem was quite hard and took me several days to solve correctly. Right in the beginning, I decided to write a 'convert' procedure that 'casts' a polynomial in one variable to the same polynomial in a different variable by re-arranging the terms. In this procedure I needed to account for the fact that the coefficients of a polynomial can themselves be polynomials. This 'polynomials inside a polynomial' can go to any depth.
; The hardest part was to find a clean, generic way to do the multiplying and re-arranging of variables that we so easily do by hand.
; At the very beginning I decided that a pretty-print procedure was needed for the polynomials because it is very hard to read the internal list representation (sparse or dense) of a polynomial. This print-poly procedure really saved my life especially while testing my program.
; I knew that the convert-polynomial procedure had to be recursive. If a polynomial in x needs to be converted to a polynomial in y, then we can convert each term of the x-polynomial to a y-polynomial and then add them all together using ordinary polynomial addition. So this was the main driving recursive logic for the conversion process. The 'first-terms' and 'rest-terms' procedures came in handy here.
; Once this main driving logic was in place, I started thinking about the conditions where the recursion terminates and the call-stack unwinds. These conditions are:
; 1. If we have an empty polynomial in x, then convert-polynomial simply returns an empty polynomial in y.
; 2. If the polynomial we are trying to convert is already in the target variable, then there is nothing to do. Simply return the same polynomial.
; 3. If the polynomial has only one term then we have to account for the following possibilities:
; 3a. The coefficient of this term is a non-empty polynomial: In this case, first convert the coefficient so that it is in the target variable. Then do something like this (In this example, the converted coefficient is Cx^2):
; Cx^2y^4 needs to be transformed to Cy^4x^2 i.e.
; ('polynomial y (4 (polynomial x (2 C))))
; needs to become
; ('polynomial x (2 (polynomial y (4 C))))
; Note that the converted coefficient can be a polynomial with multiple terms itself so the above variable swapping transformation needs to be done for all the terms and after that these terms need to be added back together. Again, the 'first-term' and 'rest-terms' procedures are useful for this.
; 3b. Coefficient is an empty polynomial: Return an empty polynomial in the new variable.
; 3c. Coefficient in the term is not a polynomial: Example: In 7y^4, the coefficient is not a polynomial but just a constant. Assuming that we are converting polynomials in y to polynomials in x, this needs to be transformed to (7y^4)x^0.
; It took multiple rounds of analysis and thought to arrive at this structure.
; Other procedures I needed to write to support this conversion process:
; higher-in-hierarchy?: This is a yes/no procedure that tells me if the variable specified by the first argument is higher in the hierarchy than the variable specified by the second argument. When we have to combine two polynomials in different variables, this procedure is used to determine which one to convert to the other.
; Generic =zero? procedure that checks if a polynomial is equal t zero. For this we basically check if the term list is empty.
; equal-polynomial-real?: Aids in simplifying polynomials. I found that when 'convert-polynomial' runs, it invariably ends up with 'polynomials' that are really just ordinary numbers. This procedure can determine if this is the case and after that we can just replace the polynomial with the number it really is.
; After this, I had to do the following:
; 1. Modify the 'add' and 'mul' procedures for polynomial to polynomial operations to check if the variables are different and if they are, simply call the old 'add-poly' or 'mul-poly' and if not, then determine which variable is higher in the hierarchy, do polynomial conversion in the right direction and then call 'add-poly' or 'mul-poly'.
; 2. I found that there is a need to support the addition and multiplication of polynomials to real numbers and complex numbers. This is because, when we combine terms, the coefficient of one term may be a polynomial and the coefficient of the other term may be an ordinary number. So I implemented a generic procedure for this. Again, the method is to convert the real number to a polynomial in the same variable as the other polynomial and then do ordinary polynomial addition or multiplication.
; During testing, I encountered infinite loop issues multiple times because the 'raises' and 'drops' in the generic procedure framework cause this when crucial combinations are missing in the op-table. I broke these infinite loops by supporting operations/type combinations that I had missed earlier.
; GENERIC PROCEDURES
; Generic Polynomial procedures
; Note: I have designed this with the assumption that the procedures adjoin-term, first-term and rest-terms
; though generic, will still be used only internally by the polynomial procedures.
; These three procedures are generic but not exposed to the outside world.
(define (variable p) (apply-generic 'variable p))
(define (term-list p) (apply-generic 'term-list p))
(define (adjoin-term term term-list) (apply-generic 'adjoin-term term term-list))
(define (first-term term-list) (apply-generic 'first-term term-list))
(define (rest-terms term-list) (apply-generic 'rest-terms term-list))
(define (order term) (apply-generic 'order term))
(define (coeff term) (apply-generic 'coeff term))
(define (negate-term term) (apply-generic 'negate-term term))
; Generic Print procedure
(define (print x) (apply-generic 'print x))
; Generic Logical procedures
(define (ABS x) (apply-generic 'abs x))
(define (=zero? x) (apply-generic '=zero? x))
(define (equ? x y) (apply-generic 'equal? x y))
(define (greater? x y) (apply-generic 'greater? x y))
(define (lesser? x y) (apply-generic 'lesser? x y))
(define (empty-term? t) (apply-generic 'empty-term? t))
(define (square x) (apply-generic 'square x))
(define (square-root x) (apply-generic 'square-root x))
(define (NEGATE x) (apply-generic 'negate x))
; Generic Arithmetic procedures with two arguments
(define (add x y) (apply-generic 'add x y))
(define (sub x y) (apply-generic 'sub x y))
(define (mul x y) (apply-generic 'mul x y))
(define (div x y) (apply-generic 'div x y))
(define (exp x y) (apply-generic 'exp x y))
; Generic Trigonometric procedures
(define (sine x) (apply-generic 'sine x))
(define (cosine x) (apply-generic 'cosine x))
(define (tan-inverse x y) (apply-generic 'tan-inverse x y))
; Generic Contrived procedures
(define (add-four-quantities w x y z) (apply-generic 'add-four-quantities w x y z))
(define (mul-and-scale x y factor) (apply-generic 'mul-and-scale x y factor))
(define (mul-five-quantities v w x y z) (apply-generic 'mul-five-quantities v w x y z))
; Generic operation that 'projects' x one level lower in the tower
(define (project x) (apply-generic 'project x))
; Generic operation that raises x one level in the tower
(define (raise x) (apply-generic 'raise x))
; Generic Constructions of specific types of entities (ordinary, rational, complex etc.)
(define (make-scheme-number n) ((get 'make 'scheme-number) n))
(define (make-natural n) ((get 'make 'natural) n))
(define (make-integer n) ((get 'make 'integer) n))
(define (make-rational n d) ((get 'make 'rational) n d))
(define (make-real r) ((get 'make 'real) r))
(define (make-complex-from-real-imag x y) ((get 'make-from-real-imag 'complex) x y))
(define (make-complex-from-mag-ang r a) ((get 'make-from-mag-ang 'complex) r a))
(define (make-polynomial var terms) ((get 'make 'polynomial) var terms))
(define (make-polynomial-dense-terms terms) ((get 'make 'polynomial-dense-terms) terms))
(define (make-polynomial-sparse-terms terms) ((get 'make 'polynomial-sparse-terms) terms))
(define (make-term order coeff) ((get 'make 'polynomial-term) order coeff))
(define (make-from-real-imag x y) ((get 'make-from-real-imag 'rectangular) x y))
(define (make-from-mag-ang r a) ((get 'make-from-mag-ang 'polar) r a))
; POLYNOMIAL PROCEDURES
(define (make-poly variable term-list) (cons variable term-list))
(define (make-dense-terms terms) terms)
(define (make-sparse-terms terms) terms)
(define (variable-poly p) (car p))
(define (term-list-poly p) (cdr p))
; Returns the polynomial constructed with the first-term of the supplied polynomial
(define (first-term-part-poly p)
(make-polynomial (variable p) (make-polynomial-sparse-terms (list (contents (first-term (term-list p))))))
)
; Returns the polynomial constructed with the rest-terms of the supplied polynomial
(define (rest-terms-part-poly p)
(let ((rt (rest-terms (term-list p))))
(if (empty-termlist? rt)
(make-polynomial (variable p) (the-empty-poly-termlist))
(make-polynomial (variable p) (rest-terms (term-list p)))
)
)
)
(define (print-poly p)
(define (print-poly-internal var terms first-time?)
(cond
((empty-termlist? terms) (display "}"))
(else
(let ((coeff-first-term (coeff (first-term terms))) (order-first-term (order (first-term terms))))
; Print + or -
(cond
((=zero? coeff-first-term) (display ""))
((and (pair? coeff-first-term) (lesser? coeff-first-term 0)) (display " ") (display '-) (display " "))
((and (pair? coeff-first-term) (not first-time?)) (display " ") (display '+) (display " "))
((and (not (pair? coeff-first-term)) (> coeff-first-term 0) (not first-time?)) (display " ") (display '+) (display " "))
((and (not (pair? coeff-first-term)) (< coeff-first-term 0)) (display " ") (display '-) (display " "))
)
; Print the coefficient
(cond
((=zero? coeff-first-term) (display ""))
((and (pair? coeff-first-term) (not (or (equ? coeff-first-term 1) (equ? coeff-first-term -1))))
; Expecting this to be a polynomial
(print (ABS coeff-first-term))
)
((not (equ? coeff-first-term 1))
(display (ABS coeff-first-term))
)
)
; Print the variable and order
(cond
((=zero? coeff-first-term) (display ""))
((equal? order-first-term 1)
(display var)
)
((not (=zero? order-first-term))
(display var)
(display '^)
(print order-first-term)
)
)
(if (and (=zero? coeff-first-term) first-time?)
(print-poly-internal var (rest-terms terms) true)
(print-poly-internal var (rest-terms terms) false)
)
)
)
)
)
; (display "Printing: ")
; (display p)
; (newline)
(display "{")
(print-poly-internal (variable-poly p) (term-list-poly p) true)
)
(define (is-poly? p)
(if (pair? p)
(equal? (type-tag p) 'polynomial)
false
)
)
(define (the-empty-poly-termlist) '())
; (define (the-empty-poly-termlist) (list 'polynomial-sparse-terms (list 0 0)))
(define (convert-polynomial p new-var)
; Converts the polynomial by expanding and rearranging the terms so that 'new-var' becomes the variable
; of the polynomial
; Assumptions made in this implementation:
; 1. The supplied object p should be tagged with the tag 'polynomial
; 2. We expect that a polynomial in a variable say z, will not have any coefficient that itself contains z
(cond
; If the polynomial is empty, then return a new empty polynomial in the new-variable
((empty-termlist? (term-list p)) (make-polynomial new-var (the-empty-poly-termlist)))
; If the polynomial is empty, then return zero
; ((empty-termlist? (term-list p)) 0)
; if new-var is not really new, return the polynomial as it is
((equal? (variable p) new-var) p)
; If the polynomial has only one term
((empty-termlist? (rest-terms (term-list p)))
(let ((the-term (first-term (term-list p))))
(cond
; Coefficient is a non-empty polynomial
((and (is-poly? (coeff the-term)) (not (empty-termlist? (term-list (coeff the-term)))))
; coeff in the term is a polynomial by itself
; First, convert the coefficient to be in the target variable.
; Then do something like this (In this example, the converted coefficient is Cx^2):
; Cx^2y4 needs to become Cy^4x^2
; i.e.
; ('polynomial y (4 (polynomial x (2 C)))) needs to become
; ('polynomial x (2 (polynomial y (4 C))))
(let ((converted-inner-poly (convert-polynomial (coeff the-term) new-var)))
(let (
; (cip-first-term-poly (first-term-part-poly converted-inner-poly))
(cip-rest-terms-poly (rest-terms-part-poly converted-inner-poly))
(cip-first-term-order (order (first-term (term-list converted-inner-poly))))
(cip-first-term-coeff (coeff (first-term (term-list converted-inner-poly))))
)
(add
(make-polynomial
new-var
(make-polynomial-sparse-terms
(list
(list
; ORDER
cip-first-term-order
; COEFFICIENT
(make-polynomial
(variable p)
(make-polynomial-sparse-terms
(list
(list
(order the-term)
cip-first-term-coeff
)
)
)
)
)
)
)
)
; LOGIC PROBLEM HERE
(convert-polynomial
; cip-rest-terms-poly
(make-polynomial
(variable p)
(make-polynomial-sparse-terms
(list
(list
; ORDER
(order the-term)
; COEFFICIENT
cip-rest-terms-poly
)
)
)
)
new-var
)
)
)
)
; coeff in the term is not a polynomial
; Example: 7y^4 becomes (7y^4)x^0
)
; Coefficient is an empty polynomial, so return an empty polynomial in the new variable
((and (is-poly? (coeff the-term)) (empty-termlist? (term-list (coeff the-term))))
(make-polynomial new-var (the-empty-poly-termlist))
)
(else
(make-polynomial new-var (make-polynomial-sparse-terms (list (list 0 p))))
)
)
)
)
; If the polynomial has more than one term
; This is the high level driving logic
((not (equal? (variable p) new-var))
(add
(convert-polynomial
(make-polynomial (variable p) (make-polynomial-sparse-terms (list (contents (first-term (term-list p))))))
new-var
)
(convert-polynomial
(make-polynomial (variable p) (rest-terms (term-list p)))
new-var
)
)
)
(else
(error "Unable to convert " (list p new-var))
)
)
)
; Term-list manipulation for dense polynomials
(define (adjoin-term-dense term term-list)
; Structure of term-list
; [OrderOfPolynomial, (list of coefficients)]
; Maintaining the order of the polynomial as the first element allows us to avoid repeated
; expensive 'length' calls on the coefficient list
; Reminder: We assume that term lists are represented as lists of terms,
; arranged from highest-order to lowest-order term.
(if (=zero? (coeff-poly-term term))
term-list
(cond
((= (order-poly-term term) (+ (polynomial-order-dense term-list) 1))
; no need to insert a zero in the coefficient list
(cons (+ 1 (polynomial-order-dense term-list)) (cons (coeff-poly-term term) (coefficients-dense term-list)))
)
((and (empty-termlist? term-list) (= 0 (order-poly-term term)))
(list 0 (coeff-poly-term term))
)
((and (empty-termlist? term-list) (> (order-poly-term term) 0))
(adjoin-term-dense term (list 0 0))
)
(else
; we need to supply zero(s) if there are gaps
(adjoin-term-dense term (cons (+ 1 (polynomial-order-dense term-list)) (cons 0 (coefficients-dense term-list))))
)
)
)
)
(define (first-term-dense term-list)
; Since we are using the term-list representation that is appropriate for
; dense polynomials (see SICP text), we need to do some extra processing
; to retrieve both the order and coefficient
(if (pair? term-list)
(if (pair? (coefficients-dense term-list))
(list (polynomial-order-dense term-list) (car (coefficients-dense term-list)))
null
)
null
)
)
(define (rest-terms-dense term-list)
; Since we are using the term-list representation that is appropriate for
; dense polynomials (see SICP text), we need to do some extra processing
; to retrieve both the order and coefficient
(if (> (polynomial-order-dense term-list) 0)
(cons (- (polynomial-order-dense term-list) 1) (cdr (coefficients-dense term-list)))
(the-empty-poly-termlist)
)
)
(define (polynomial-order-dense term-list)
(if (pair? term-list)
(car term-list)
0
)
)
(define (coefficients-dense term-list)
(if (pair? term-list)
(cdr term-list)
(list 0)
)
)
; Term-list manipulation for sparse polynomials
(define (adjoin-term-sparse term term-list)
(if (=zero? (coeff-poly-term term))
term-list
(cons term term-list)
)
)
(define (first-term-sparse term-list)
(cond
((null? term-list) null)
((not (pair? term-list)) (error "Procedure first-term-sparse: term-list is not a pair!"))
(else
(car term-list)
)
)
)
(define (rest-terms-sparse term-list)
(cond
((null? term-list) (the-empty-poly-termlist))
((not (pair? term-list)) (error "Procedure rest-terms-sparse: term-list is not a pair!"))
(else
(cdr term-list)
)
)
)
; Term manipulation (used in both dense and sparse polynomial representations)
(define (make-poly-term order coeff) (list order coeff))
(define (order-poly-term term)
(if (pair? term)
(car term)
0
)
)
(define (coeff-poly-term term)
(if (pair? term)
(cadr term)
0
)
)
(define (higher-in-hierarchy? v1 v2)
; returns true if v1 is higher than v2, otherwise false
; Items in the below list are arranged from low to high
(define var-hierarchy (list 'p 'q 'r 's 't 'u 'v 'w 'z 'y 'x))
(define (find-position item items current-position)
(cond
((pair? items)
(if (equal? item (car items))
current-position
(find-position item (cdr items) (+ current-position 1))
)
)
(else
-1
)
)
)
(let ((v1-pos (find-position v1 var-hierarchy 1)) (v2-pos (find-position v2 var-hierarchy 1)))
(cond
((< v1-pos v2-pos) false)
((> v1-pos v2-pos) true)
(else
; default treatment
false
)
)
)
)
; Polynomial Operations
(define (add-poly p1 p2)
(if (same-variable? (variable-poly p1) (variable-poly p2))
(make-poly
(variable-poly p1)
(add-terms (term-list-poly p1) (term-list-poly p2))
)
(error "Polys not in same var -- ADD-POLY"
(list p1 p2)
)
)
)
(define (sub-poly p1 p2)
(add-poly p1 (negate-poly p2))
)
(define (mul-poly p1 p2)
(if (same-variable? (variable-poly p1) (variable-poly p2))
(make-poly
(variable-poly p2)
(mul-terms (term-list-poly p1) (term-list-poly p2))
)
(error "Polys not in same var -- MUL-POLY"
(list p1 p2)
)
)
)
(define (add-terms L1 L2)
(cond
((empty-termlist? L1) L2)
((empty-termlist? L2) L1)
(else
(let ((t1 (first-term L1)) (t2 (first-term L2)))
(cond
((greater? (order t1) (order t2))
(adjoin-term t1 (add-terms (rest-terms L1) L2))
)
((lesser? (order t1) (order t2))
(adjoin-term t2 (add-terms L1 (rest-terms L2)))
)
(else
(adjoin-term
(make-term (order t1) (add (coeff t1) (coeff t2)))
(add-terms (rest-terms L1) (rest-terms L2))
)
)
)
)
)
)
)
(define (mul-terms L1 L2)
(if (or (empty-termlist? L1) (empty-termlist? L2))
(the-empty-poly-termlist)
(add-terms
(mul-term-by-all-terms (first-term L1) L2)
(mul-terms (rest-terms L1) L2)
)
)
)
(define (mul-term-by-all-terms t1 L)
(if (or (empty-termlist? L) (empty-term? t1))
; Returning an empty list with an explicit tag so that the generic procedure
; 'adjoin-term' below does not fail. Generic procedures expect a data tag for each of their arguments
(list 'polynomial-sparse-terms (list 0 0))
(let ((t2 (first-term L)))
(adjoin-term
(make-term (add (order t1) (order t2)) (mul (coeff t1) (coeff t2)))
(mul-term-by-all-terms t1 (rest-terms L))
)
)
)
)
(define (div-poly p-dividend p-divisor)
(if (same-variable? (variable-poly p-dividend) (variable-poly p-divisor))
(let ((result (div-terms (term-list-poly p-dividend) (term-list-poly p-divisor))))
(list
(make-poly (variable-poly p-dividend) (car result))
(make-poly (variable-poly p-dividend) (cadr result))
)
)
(error "Polys not in same var -- DIV-POLY"
(list p-dividend p-divisor)
)
)
)
(define (div-terms L-dividend L-divisor)
(if (empty-termlist? L-dividend)
(list (the-empty-poly-termlist) (the-empty-poly-termlist))
(let ((ft-dividend (first-term L-dividend)) (ft-divisor (first-term L-divisor)))
(if (greater? (order ft-divisor) (order ft-dividend))
(list (the-empty-poly-termlist) L-dividend)
(let ((new-c (div (coeff ft-dividend) (coeff ft-divisor)))
(new-o (sub (order ft-dividend) (order ft-divisor))))
(let ((rest-of-result
(div-terms
(term-list-poly
(sub-poly
(make-poly 'x L-dividend)
(mul-poly
(make-poly 'x L-divisor)
(make-poly 'x (make-polynomial-sparse-terms (list (list new-o new-c))))
)
)
)
L-divisor
)
))
; <form complete result>
(list
(add-terms
(make-polynomial-sparse-terms (list (list new-o new-c)))
(car rest-of-result)
)
(cadr rest-of-result)
)
)
)
)
)
)
)
(define (=zero-polynomial? p)
; A polynomial is zero if its term-list is empty
(empty-termlist? (term-list-poly p))
)
(define (lesser-polynomial-real? p r)
; returns true if the polynomial is just a constant which is less than r
(and (=zero? (order (first-term (term-list-poly p)))) (lesser? (coeff (first-term (term-list-poly p))) r))
)
(define (negate-poly p)
(make-poly
(variable-poly p)
(negate-term-list (term-list-poly p))
)
)
(define (negate-term-list t)
(if (empty-termlist? t)
t
(adjoin-term (negate-term (first-term t)) (negate-term-list (rest-terms t)))
)
)
(define (negate-poly-term t)
(list (order-poly-term t) (mul -1 (coeff-poly-term t)))
)
(define (empty-termlist? t)
; consider both with tag and without tag
; This procedure will return true for the following data:
; '()
; ('polynomial-sparse-terms)
; ('polynomial-dense-terms)
; ('polynomial-sparse-terms (0 0))
; ('polynomial-dense-terms 0 0)
(or
(null? t)
(and (symbol? (car t)) (null? (cdr t)))
(and (eq? (car t) 'polynomial-sparse-terms) (eq? (caadr t) 0) (eq? (cadadr t) 0))
(and (eq? (car t) 'polynomial-dense-terms) (eq? (cadr t) 0) (eq? (caddr t) 0))
)
)
(define (equal-polynomial-real? p r)
; returns true if the polynomial is just a constant which is less than r
(and (=zero? (order (first-term (term-list-poly p)))) (equ? (coeff (first-term (term-list-poly p))) r))
)
(define (empty-poly-term? t)
(cond
((null? t) true)
((not (pair? t)) (error "Procedure empty-term: t not a pair!"))
(else
(eq? (cadr t) 0)
)
)
)
(define (same-variable? v1 v2)
(and (variable? v1) (variable? v2) (eq? v1 v2))
)
(define (variable? x) (symbol? x))
; COMPLEX NUMBER PROCEDURES
(define (magnitude z) (apply-generic 'magnitude z))
(define (angle z) (apply-generic 'angle z))
(define (REAL-PART z) (apply-generic 'REAL-PART z))
(define (IMAG-PART z) (apply-generic 'IMAG-PART z))
(define (add-complex z1 z2)
(make-from-real-imag
(add (REAL-PART z1) (REAL-PART z2))
(add (IMAG-PART z1) (IMAG-PART z2))
)
)
(define (sub-complex z1 z2)
(make-from-real-imag
(sub (REAL-PART z1) (REAL-PART z2))
(sub (IMAG-PART z1) (IMAG-PART z2))
)
)
(define (mul-complex z1 z2)
(make-from-mag-ang
(mul (magnitude z1) (magnitude z2))
(add (angle z1) (angle z2))
)
)
(define (div-complex z1 z2)
(make-from-mag-ang
(div (magnitude z1) (magnitude z2))
(sub (angle z1) (angle z2))
)
)
(define (mul-and-scale-complex z1 z2 factor)
(display "Entered proc mul-and-scale-complex")
(newline)
(let ((prod (mul-complex z1 z2)))
(make-complex-from-real-imag (mul (REAL-PART prod) factor) (mul (IMAG-PART prod) factor))
)
)
(define (add-four-complex-numbers z1 z2 z3 z4)
(display "Entered proc add-four-complex-numbers")
(newline)
(make-from-real-imag
(add (REAL-PART z1) (REAL-PART z2) (REAL-PART z3) (REAL-PART z4))
(add (IMAG-PART z1) (IMAG-PART z2) (IMAG-PART z3) (IMAG-PART z4))
)
)
(define (equal-complex? c1 c2)
(and (equ? (REAL-PART c1) (REAL-PART c2)) (equ? (IMAG-PART c1) (IMAG-PART c2)))
)
(define (=zero-complex? c) (equ? 0 (magnitude c)))
(define (print-complex c)
(if (=zero? (IMAG-PART c))
(print (REAL-PART c))
(begin
(print (REAL-PART c))
(display '+)
(print (IMAG-PART c))
(display 'i)
)
)
)
; (define (project-complex c) (make-real (REAL-PART c)))
(define (project-complex c) (REAL-PART c))
(define (square-complex c)
(mul-complex c c)
)
; Rectangular (Complex) Number procedures
(define (make-from-real-imag-rectangular x y) (cons x y))
(define (make-from-mag-ang-rectangular r a) (cons (mul r (cosine a)) (mul r (sine a))))
(define (magnitude-rectangular z)
(square-root (add (square (real-part-rectangular z)) (square (imag-part-rectangular z))))
)
(define (angle-rectangular z) (tan-inverse (imag-part-rectangular z) (real-part-rectangular z)))
(define (real-part-rectangular z) (car z))
(define (imag-part-rectangular z) (cdr z))
(define (raise-rectangular r)
(make-complex-from-real-imag (real-part-rectangular r) (imag-part-rectangular r))
)
; Polar (Complex) Number procedures
(define (make-from-real-imag-polar x y) (cons (square-root (add (square x) (square y))) (tan-inverse y x)))
(define (make-from-mag-ang-polar r a) (cons r a))
(define (magnitude-polar z) (car z))
(define (angle-polar z) (cdr z))
(define (real-part-polar z) (mul (magnitude-polar z) (cosine (angle-polar z))))
(define (imag-part-polar z) (mul (magnitude-polar z) (sine (angle-polar z))))
(define (raise-polar p)
(make-complex-from-mag-ang (magnitude-polar p) (angle-polar p))
)
; REAL NUMBER PROCEDURES
(define (make-real-specific r)
(if (real? r)
r
(error "Cannot make real with: " r)
)
)
(define (abs-real r)
(abs r)
)
(define (=zero-real? r)
(= 0 r)
)
(define (print-real r)
(display r)
)
(define (raise-real r)
(make-complex-from-real-imag r 0)
)
(define (project-real r)
(make-rational r 1)
)
(define (square-root-real r)
(if (>= r 0)
(sqrt r)
(make-complex-from-real-imag 0 (sqrt (abs r)))
)
)
(define (square-real r)
(* r r)
)
(define (cosine-real r)
(cos r)
)
(define (tan-inverse-real r1 r2)
(atan r1 r2)
)
(define (sine-real r)
(sin r)
)
; RATIONAL NUMBER PROCEDURES
(define (numer x) (car x))
(define (denom x) (cdr x))
(define (make-rational-specific n d)
(define (construct-rational n d)
(let ((g (gcd n d)))
(cond
((= d 0) (error "Denominator in a rational number cannot be zero"))
((= n 0) (cons n d))
((and (< n 0) (< d 0)) (cons (/ (abs n) g) (/ (abs d) g)))
((and (< n 0) (> d 0)) (cons (/ n g) (/ d g)))
((and (> n 0) (< d 0)) (cons (/ (* -1 n) g) (/ (abs d) g)))
((and (> n 0) (> d 0)) (cons (/ n g) (/ d g)))
)
)
)
(cond
((and (integer? n) (integer? d))
(construct-rational (exact-round n) (exact-round d))
)
; if both supplied arguments are not integers, try combining them and try to make a rational
; Example: 4 / 0.5 gives us 8 which is rational
((integer? (/ n d))
(construct-rational (exact-round (/ n d)) 1)
)
(else
(error "Rational number cannot be made with: " n d)
)
)
)
(define (add-rational x y) (make-rational-specific (+ (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y))))
(define (sub-rational x y) (make-rational-specific (- (* (numer x) (denom y)) (* (numer y) (denom x))) (* (denom x) (denom y))))
(define (mul-rational x y) (make-rational-specific (* (numer x) (numer y)) (* (denom x) (denom y))))
(define (div-rational x y) (make-rational-specific (* (numer x) (denom y)) (* (denom x) (numer y))))
(define (equal-rational? x y)
; (display "Entered equal-rational?")
; (newline)
(and (= (numer x) (numer y)) (= (denom x) (denom y)))
)
(define (greater-rational? x y)
(> (* (numer x) (denom y)) (* (denom x) (numer y)))
)
(define (lesser-rational? x y)
(< (* (numer x) (denom y)) (* (denom x) (numer y)))
)
(define (=zero-rational? x)
(= 0 (numer x))
)
(define (square-root-rational rat)
(if (>= (numer rat) 0)
(sqrt (/ (numer rat) (denom rat)))
(make-complex-from-real-imag 0 (sqrt (abs (/ (numer rat) (denom rat)))))
)
)
(define (square-rational rat)
(cons (* (numer rat) (numer rat)) (* (denom rat) (denom rat)))
)
(define (cosine-rational rat)
(cos (/ (numer rat) (denom rat)))
)
(define (tan-inverse-rational rat1 rat2)
(atan (/ (numer rat1) (denom rat1)) (/ (numer rat2) (denom rat2)))
)
(define (sine-rational rat)
(sin (/ (numer rat) (denom rat)))
)
(define (raise-rational r)
(make-real (* 1.0 (/ (numer r) (denom r))))
)
(define (project-rational r)
; tries to push this object down one step in the tower
(make-integer (numer r))
)
(define (mul-five-rationals v w x y z)
(mul-rational (mul-rational (mul-rational (mul-rational v w) x) y) z)
)
; INTEGER PROCEDURES
(define (abs-integer i)
(abs i)
)
(define (make-integer-specific n)
(if (integer? n)
n
(error "Cannot make integer with: " n)
)
)
(define (print-int i)
(display i)
)
(define (raise-int n)
(make-rational n 1)
)
(define (project-int n)
; tries to push this object down one step in the tower
(make-natural n)
)
(define (square-root-integer i)
(if (>= i 0)
(sqrt i)
(make-complex-from-real-imag 0 (sqrt (abs i)))
)
)
(define (square-integer n)
(* n n)
)
(define (cosine-integer i)
(cos i)
)
(define (tan-inverse-integer i1 i2)
(atan i1 i2)
)
(define (sine-integer i)
(sin i)
)
; NATURAL NUMBER PROCEDURES
(define (make-natural-specific n)
(if (and (>= n 0) (or (natural? n) (= n (exact-round n))))
n
(error "Cannot make natural with: " n)
)
)
(define (abs-natural n)
(abs n)
)
(define (print-natural n)
(display n)
)
(define (raise-natural n)
(make-integer n)
)
(define (square-root-natural n)
(sqrt n)
)
(define (square-natural n)
(* n n)
)
(define (cosine-natural n)
(cos n)
)
(define (tan-inverse-natural n1 n2)
(atan n1 n2)
)