-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeneral.lisp
1478 lines (1232 loc) · 48.2 KB
/
general.lisp
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
;;; -*- Mode:Lisp; Package:Weyli; Base:10; Lowercase:T; Syntax:Common-Lisp -*-
;;; ===========================================================================
;;; General Representation
;;; ===========================================================================
;;; (c) Copyright 1989, 1993 Cornell University
;;; general.lisp,v 1.14 1995/05/24 17:42:01 rz Exp
(in-package :weyli)
;;; DELETE (make::adjust-version-numbers Weyl "1.14")
(defgeneric set-memoization (domain key value)
(:documentation
"The purpose of this method is unknown."))
(defmethod set-memoization ((domain has-memoization) key value)
(with-slots (memos) domain
(setf (gethash key memos) value)
value))
;;; FIXME : Merge this into a single setf function.
(defgeneric get-memoization (domain key)
(:documentation
"The purpose of this method is unknown."))
(defmethod get-memoization ((domain has-memoization) key)
(with-slots (memos) domain
(gethash key memos)))
(defsetf get-memoization set-memoization)
(defmacro %memoize (domain expression &body body)
`(let ((.expr. ,expression))
(with-slots (memos) ,domain
(multiple-value-bind (value found?) (gethash .expr. memos)
(if found? value
(setf (get-memoization ,domain .expr.) (progn ,@body)))))))
(defmacro memoize (expression &body body)
`(%memoize *general* ,expression ,@body))
#+ignore
(defun fib-memo (n)
(memoize `(fib ,n)
(if (< n 2) 1
(+ (fib-memo (- n 1)) (fib-memo (- n 2))))))
(defgeneric display (expression &optional stream &rest ignore)
(:documentation
"The purpose of this method is unknown.")
(:method ((express general-expression) &optional stream &rest ignore)
(declare (ignore ignore))
(princ express stream)))
(defgeneric simplify (expression)
(:documentation
"Simplify the expression.")
(:method ((expression general-expression)) expression))
(defgeneric ge-equal (expression1 expression2)
(:documentation
"The purpose of this method is unknown.")
(:method (expression1 expression2)
(declare (ignore expression1 expression2))
nil))
(defmethod ge-equal ((x general-expression) (y general-expression))
(eql x y))
(defgeneric ge-great (expression1 expression2)
(:documentation
"The purpose of this method is unknown.")
(:method ((x general-expression) (y general-expression))
(declare (ignore x y))
nil))
;; Numbers and Variables
(defmethod make-quotient-element
((domain general-expressions) (x integer) (y integer))
(make-instance 'rational-number :domain domain
:numerator x :denominator y))
(defmethod make-element ((domain general-expressions) (x integer) &rest args)
(cond ((or (null args) (0? (first args)))
(make-instance 'rational-integer :domain domain :value x))
((number? (first args))
(make-instance 'complex-number :domain domain
:realpart x :imagpart (first args)))
(t (error "Can't deal with this yet: ~A" args))))
(defmethod make-element ((domain general-expressions) (x ratio) &rest args)
(cond ((or (null args) (0? (first args)))
(make-instance 'rational-number :domain domain
:numerator (cl:numerator x)
:denominator (cl:denominator x)))
((number? (first args))
(make-instance 'complex-number :domain domain
:realpart x :imagpart (first args)))
(t (error "Can't deal with this yet: ~A" args))))
(defmethod make-element ((domain general-expressions) (x float) &rest args)
(cond ((or (null args) (0? (first args)))
(make-instance 'floating-point-number :domain domain
:value x))
((number? (first args))
(make-instance 'complex-number :domain domain
:realpart x :imagpart (first args)))
(t (error "Can't deal with this yet: ~A" args))))
(defmethod make-element ((domain general-expressions) (x cl:complex)
&rest ignore)
(declare (ignore ignore))
(make-instance 'complex-number :domain domain
:realpart (cl:realpart x)
:imagpart (cl:imagpart x)))
(defmethod coerce ((num number) (domain general-expressions))
(make-element domain num))
(defmethod coerce ((num rational-integer) (domain general-expressions))
(if (eql (domain-of num) domain) num
(make-element domain (integer-value num))))
(defmethod coerce ((num rational-number) (domain general-expressions))
(if (eql (domain-of num) domain) num
(make-instance 'rational-number :domain domain
:numerator (qo-numerator num)
:denominator (qo-denominator num))))
(defmethod coerce ((num floating-point-number) (domain general-expressions))
(if (eql (domain-of num) domain) num
(make-element domain (fp-value num))))
(defmethod coerce ((num bigfloat) (domain general-expressions))
(if (eql (domain-of num) domain) num
(make-bigfloat domain (bigfloat-mantissa num) (bigfloat-exponent num))))
(defmethod coerce ((num cl:complex) (domain general-expressions))
(make-instance 'complex-number :domain domain
:realpart (realpart num)
:imagpart (imagpart num)))
(defmethod coerce ((num complex-number) (domain general-expressions))
(if (eql (domain-of num) domain) num
(make-instance 'complex-number :domain domain
:realpart (cn-realpart num)
:imagpart (cn-imagpart num))))
(defmethod simplify ((x number))
(make-element *general* x))
(defmethod simplify ((x numeric)) x)
;;; AUDIT : These methods were defined as a single method using an OR
;;; specializer. Might be useful to revisit that extension.
(defmethod ge-equal ((x number) (y number)) (= x y))
(defmethod ge-equal ((x numeric) (y number)) (= x y))
(defmethod ge-equal ((x number) (y numeric)) (= x y))
(defmethod ge-equal ((x numeric) (y numeric)) (= x y))
(defmethod ge-equal ((x number) y)
(declare (ignore y))
nil)
(defmethod ge-equal ((x numeric) y)
(declare (ignore y))
nil)
(defmethod ge-equal (x (y number))
(declare (ignore x))
nil)
(defmethod ge-equal (x (y numeric))
(declare (ignore x))
nil)
(defmethod ge-great ((x number) (y number)) (> x y))
(defmethod ge-great ((x numeric) (y number)) (> x y))
(defmethod ge-great ((x number) (y numeric)) (> x y))
(defmethod ge-great ((x numeric) (y numeric)) (> x y))
(defmethod ge-great ((x number) y)
(declare (ignore y))
nil)
(defmethod ge-great ((x numeric) y)
(declare (ignore y))
nil)
(defmethod ge-great (x (y number))
(declare (ignore x))
t)
(defmethod ge-great (x (y numeric))
(declare (ignore x))
t)
;; Variables
(defgeneric reparse-print-string (variable)
(:documentation
"The purpose of this method is unknown."))
(defmethod reparse-print-string ((var ge-variable))
(let ((string (cond ((atom (symbol-of var))
(string-downcase (symbol-of var)))
(t (format nil "[~A]" (symbol-of var)))))
temp)
(when (setq temp (getf var :subscripts))
(setq string
(format nil "~A(~S~{,~S~})"
string (first temp) (rest temp))))
(setf (string-of var) string)))
(defmethod initialize-instance :after ((var ge-variable) &rest ignore)
(declare (ignore ignore))
(reparse-print-string var))
(defgeneric make-ge-variable (domain variable)
(:documentation
"The purpose of this method is unknown."))
(defmethod make-ge-variable ((domain general-expressions) var)
(loop for v in (ge-variables domain)
do (when (equal (symbol-of v) var)
(return v))
finally
(setq var (make-instance 'ge-variable :domain domain :symbol var))
(push var (ge-variables domain))
(return var)))
(defmethod coerce ((var symbol) (domain general-expressions))
(make-ge-variable domain var))
(defmethod print-object ((var ge-variable) stream)
(let ((sym (string-of var)))
(cond ((and (not (null sym)) (atom sym))
#+Genera
(format stream "~'i~A~" sym)
#-Genera
(princ sym stream))
(t (princ (symbol-of var) stream)))))
;; This function is only to be applied to general expressions.
(defsubst ge-variable? (x)
(typep x 'ge-variable))
(defgeneric add-subscripts (variable &rest subscripts)
(:documentation
"The purpose of this method is unknown."))
(defmethod add-subscripts ((var ge-variable) &rest subscripts)
(setq var (coerce var *general*))
(let* ((symbol (symbol-of var))
(subscripts (append (getf var :subscripts) (copy-list subscripts)))
(canonical-var
(member symbol (ge-variables *general*)
:test #'(lambda (a b)
(and (equal a (symbol-of b))
(equal subscripts (getf b :subscripts)))))))
(cond (canonical-var
(first canonical-var))
(t (setq var (make-instance 'ge-variable :domain (domain-of var)
:symbol symbol))
(setf (getf var :subscripts) subscripts)
(reparse-print-string var)
(push var (ge-variables *general*))
var))))
(defmethod add-subscripts ((var symbol) &rest subscripts)
(%apply #'add-subscripts (coerce var *general*) subscripts))
(defmethod ge-equal ((x ge-variable) (y ge-variable))
(eql x y))
(defmethod ge-great ((x ge-variable) (y ge-variable))
(string-greaterp (string-of x) (string-of y)))
(defmethod ge-great ((x ge-variable) (y ge-plus))
(loop for w in (terms-of y)
unless (ge-great x w)
do (return nil)
finally (return t)))
(defmethod ge-great ((x ge-variable) (y ge-times))
(loop for w in (terms-of y)
unless (ge-great x w)
do (return nil)
finally (return t)))
(defmethod ge-great ((x ge-plus) (y ge-variable))
(loop for w in (terms-of x)
unless (ge-great w y)
do (return t)
finally (return nil)))
(defmethod ge-great ((x ge-times) (y ge-variable))
(loop for w in (terms-of x)
unless (ge-great w y)
do (return t)
finally (return nil)))
;; Functions
(defun search-for-function (list name nargs)
(loop for f in list
do (when (and (not (typep f 'ge-function-deriv))
(string= name (name-of f)))
(when (and nargs (not (cl:= nargs (nargs-of f))))
(error "Wrong number of arguments specified for function ~A"
name))
(return f))))
(defgeneric get-function (domain name &optional args)
(:documentation
"The purpose of this method is not known."))
(defmethod get-function ((domain general-expressions) name &optional nargs)
(setq name (string-downcase (string name)))
(or (search-for-function (ge-functions domain) name nargs)
(search-for-function *global-functions* name nargs)))
(defmethod get-function ((domain (eql nil)) name &optional nargs)
(setq name (string-downcase (string name)))
(search-for-function *global-functions* name nargs))
(defgeneric make-function (domain name &optional nargs)
(:documentation
"The purpose of this functions is unknown."))
(defmethod make-function ((domain general-expressions) name &optional nargs)
(setq name (string-downcase (string name)))
(let ((fun (or (search-for-function (ge-functions domain) name nargs)
(search-for-function *global-functions* name nargs))))
(unless fun
(when (null nargs)
(error "Number of arguments to ~A must be specified" name))
(setq fun (make-instance 'ge-function :domain domain
:name name :nargs nargs))
(push fun (ge-functions domain)))
fun))
(defmethod make-function ((domain (eql nil)) name &optional nargs)
(setq name (string-downcase (string name)))
(let ((fun (search-for-function *global-functions* name nargs)))
(unless fun
(when (null nargs)
(error "Number of arguments to ~A must be specified" name))
(setq fun (make-instance 'ge-function :domain domain
:name name :nargs nargs))
(push fun *global-functions*))
fun))
(defmethod derivs-of ((f ge-function)) nil)
(defun add-function-to-domain (domain name nargs &optional derivs)
(let ((function-class (if derivs 'ge-function-deriv 'ge-function))
deriv)
(loop for f in (ge-functions domain)
do (when (and (typep f function-class)
(eql (name-of f) name)
(equal (derivs-of f) derivs))
(setq deriv f)
(return t)))
(unless deriv
(setq deriv (make-instance function-class :domain domain
:name name :nargs nargs
:derivs derivs))
(push deriv (ge-functions domain)))
deriv))
;;; FIXME : This needs to be merged into the generic function.
(defmethod minus? ((x t))
(declare (ignore x))
nil)
;; For compatibility with Common Lisp
(defun minusp (x) (minus? x))
(defun plusp (x) (plus? x))
(defun zerop (x) (0? x))
(defgeneric make-function-deriv (function derivative)
(:documentation
"The purpose of this method is unknown."))
;; The copy-list's in the following functions is necessary because
;; sort destructively modifies its argument. --RZ
(defmethod make-function-deriv ((fun ge-function) (i integer))
(when (or (minusp i)
(not (< i (nargs-of fun))))
(error "Illegal derivative of ~S in position ~D" fun i))
(add-function-to-domain (domain-of fun)
(name-of fun)
(nargs-of fun)
(if (typep fun 'ge-function-deriv)
(sort (cons i (copy-list (derivs-of fun)))
#'cl:<)
(list i))))
(defmethod make-function-deriv ((fun ge-function) (derivs list))
(add-function-to-domain (domain-of fun)
(name-of fun)
(nargs-of fun)
(sort (if (typep fun 'ge-function-deriv)
(append derivs (copy-list (derivs-of fun)))
derivs)
#'cl:<)))
(defgeneric make-function-integrate (function integrand)
(:documentation
"The purpose of this function is unknown."))
(defmethod make-function-integrate ((fun ge-function) (i integer))
(when (or (minusp i)
(not (< i (nargs-of fun))))
(error "Illegal derivative of ~S in position ~D" fun i))
(let ((derivs (if (typep fun 'ge-function-deriv)
(derivs-of fun) nil)))
(cond ((member i derivs)
(setq derivs (remove i derivs :count 1)))
(t (error "Don't have representation for integrals yet")))
(add-function-to-domain (domain-of fun) (name-of fun) (nargs-of fun)
derivs)))
(defmethod print-object ((fun ge-function) stream)
#+Genera
(format stream "~'i~A~" (name-of fun))
#-Genera
(princ (name-of fun) stream))
(defmethod print-object ((fun ge-function-deriv) stream)
#+Genera
(format stream "~'i~A~" (name-of fun))
#-Genera
(princ (name-of fun) stream)
(princ "_{" stream)
(loop for n in (derivs-of fun)
do (princ n stream))
(princ "}" stream))
(defgeneric make-ge-funct (domain function &rest args)
(:documentation
"The purpose of this method is unknown."))
(defmethod make-ge-funct ((domain general-expressions) funct &rest args)
(make-instance 'ge-application :domain domain
:funct (if (ge-function? funct) funct
(make-function domain funct (length args)))
:args (copy-list args)))
(defmethod apply ((fun ge-function) &rest args)
(let ((domain (domain-of fun)))
(flet ((check-domain (dom)
(cond ((null domain)
(if (typep dom 'general-expressions)
(setq domain dom)
(error "GE function of ~D" dom)))
((not (eql domain dom))
(error "Incompatible domains apply ~S to ~S" fun args)))))
(setq args (accum-apply-args args))
(loop for arg in args
do (when (or (typep arg 'general-expression)
(typep arg 'numeric))
(check-domain (domain-of arg))))
(when (null domain)
(setq domain *general*))
(simplify
(make-instance 'ge-application :domain domain
:funct fun
:args (loop for arg in args
collect (coerce arg domain)))))))
(defmacro funct (function &rest args)
`(make-ge-funct *general* ',function
,@(mapcar #'(lambda (q) `(coerce ,q *general*))
args)))
(defgeneric display-list (objects &optional stream)
(:documentation "Display a list of objects, paying attention to
*print-length*. No surrounding delimiters. This is a method so that
we can define similar functions for sets of objects embedded in
arrays."))
(defmethod display-list
((objects list) &optional (stream *standard-output*))
(when objects
(let ((cnt (or *print-length* -1)))
(declare (fixnum cnt))
(print-object (first objects) stream)
(cl:decf cnt)
(loop for var in (rest objects)
do (princ ", " stream)
(when (cl:zerop cnt)
(princ "..." stream)
(return))
(print-object var stream)
(cl:decf cnt)))))
(defmethod print-object ((x ge-application) stream)
(print-object (funct-of x) stream)
(write-char #\( stream)
(display-list (args-of x) stream)
(write-char #\) stream))
(defmethod simplify ((x ge-application))
(let ((args (mapcar #'simplify (args-of x)))
(simplifier (getf (funct-of x) 'simplify))
new-x)
;; This is the function application with the simplified arguments.
(setq new-x (apply #'make-ge-funct (domain-of x) (funct-of x) args))
(if simplifier
(apply simplifier (domain-of x) new-x args)
new-x)))
;; Contexts
(defvar *initialize-contexts-funs* ())
(defun initialize-contexts ()
(setq *general* (make-instance 'general-expressions))
(loop for fun in *initialize-contexts-funs*
do (funcall fun)))
(defmacro with-new-context (&body body)
`(let ((*general* (make-instance 'general-expressions)))
,@body))
(defmacro check-point-context (&body body)
`(let ((.old-variables. (ge-variables *general*))
(.old-context. (ge-context *general*)))
(unwind-protect (progn ,@body)
(setf .old-variables. (ge-variables *general*))
(setf .old-context. (ge-context *general*)))))
(defgeneric make-ge-plus (domain terms)
(:documentation
"The purpose of this method is unknown."))
(defmethod make-ge-plus ((domain general-expressions) terms)
(make-instance 'ge-plus :domain domain :terms terms))
(defgeneric make-ge-times (domain terms)
(:documentation
"The purpose of this method is unknown."))
(defmethod make-ge-times ((domain general-expressions) terms)
(make-instance 'ge-times :domain domain :terms terms))
(defgeneric make-ge-expt (domain base exp)
(:documentation
"The purpose of this method is unknown."))
(defmethod make-ge-expt ((domain general-expressions) base exp)
(make-instance 'ge-expt :domain domain :base base :exp exp))
(defmethod coerce ((exp list) (domain general-expressions))
(flet ((coerce-obj (x)
(coerce x domain)))
(cond ((eql (first exp) '+)
(make-ge-plus domain
(mapcar #'coerce-obj (rest exp))))
((eql (first exp) '*)
(make-ge-times domain
(mapcar #'coerce-obj (rest exp))))
((eql (first exp) '-)
(if (null (rest (rest exp)))
(make-ge-times domain (list -1 (coerce-obj (second exp))))
(make-ge-plus domain
(list (coerce-obj (second exp))
(make-ge-times domain
(cons (make-element domain -1)
(mapcar #'coerce-obj (rest (rest exp)))))))))
((eql (first exp) '/)
(make-ge-times domain
(list (coerce-obj (second exp))
(make-ge-expt domain
(make-ge-times domain
(mapcar #'coerce-obj (rest (rest exp))))
(make-element domain -1)))))
(t (error "Don't know how to coerce ~S into ~S"
exp domain)))))
(defun parenthesized-display (expr stream)
(princ "(" stream)
(print-object expr stream)
(princ ")" stream))
(defun safe-display (expr stream)
(if (or (and (number? expr) (not (typep expr 'complex-number)))
(and (typep expr 'complex-number)
(0? (realpart expr)))
(ge-variable? expr)
(ge-expt? expr))
(print-object expr stream)
(parenthesized-display expr stream)))
;; Ordering functions for general expressions
;; Some operators may choose to ignore various parameters here.
(defun ge-lequal (x y)
(loop
(when (and (null x) (null y))
(return-from ge-lequal t))
(when (or (null x) (null y)
(not (ge-equal (first x) (first y))))
(return-from ge-lequal nil))
(pop x) (pop y)))
(defun ge-lgreat (x y)
(loop
(cond ((null x)
(return nil))
((null y)
(return t))
((ge-equal (first x) (first y)))
((ge-great (first x) (first y))
(return t))
(t (return nil)))
(pop x) (pop y)))
(defgeneric real? (object)
(:documentation
"Return true if the object is real valued.")
(:method ((object number))
(not (cl:complexp object)))
(:method ((object bigfloat))
(declare (ignore object))
t)
(:method ((object numeric))
(not (typep object 'complex-number))))
(defun ge-minus? (x)
(cond ((and (number? x) (real? x)) (minus? x))
((ge-times? x)
(let ((lead-term (first (terms-of x))))
(and (and (number? lead-term)
(real? lead-term)
(minus? lead-term)))))
(t nil)))
;; This works by converting the sum into a list of dotted pairs. The
;; first element of the list is a number, while the second is a list
;; of product terms. This makes combining new elements quite easy.
;; After the combination, everything is converted back to the standard
;; representation.
(defmacro merge-terms-in-sum (terms &body body)
`(let ((,terms (list nil)))
(labels ((add-term (base order)
(loop with terms = ,terms do
(cond ((or (null (rest terms))
(ge-lgreat base (rest (second terms))))
(push (cons order base) (rest terms))
(return t))
((ge-lequal base (rest (second terms)))
(setf (first (second terms))
(+ (first (second terms)) order))
(when (0? (first (second terms)))
(setf (rest terms) (rest (rest terms))))
(return t)))
(pop terms))))
,@body)))
(defun simp-plus-terms (domain old-terms)
(merge-terms-in-sum terms
(let ((const 0))
(labels ((loop-over-terms (terms)
(loop for term in terms
do (setq term (simplify term))
(cond ((number? term)
(setq const (+ const term)))
((ge-plus? term)
(loop-over-terms (terms-of term)))
((ge-times? term)
(setq term (terms-of term))
(cond ((number? (first term))
(add-term (rest term) (first term)))
(t (add-term term 1))))
(t (add-term (list term) 1))))))
(loop-over-terms old-terms)
(setq terms (loop for (c . term-l) in (rest terms)
collect
(if (or (eql c 1) (eql c 1.0))
(if (null (rest term-l))
(first term-l)
(simplify
(make-ge-times domain term-l)))
(simplify
(make-ge-times domain (cons c term-l))))))
(cond ((not (0? const))
(if (null terms) const
(make-ge-plus domain (cons const terms))))
((null terms)
(make-element domain 0))
((null (rest terms))
(first terms))
(t (make-ge-plus domain terms)))))))
(defun simp-times-terms (domain old-terms)
(merge-terms-in-sum terms
(let ((const 1))
(labels ((loop-over-terms (terms)
(loop for term in terms do
(setq term (simplify term))
(cond ((number? term)
(when (0? term)
(return-from simp-times-terms
(make-element domain 0)))
(setq const (* const term)))
((ge-times? term)
(loop-over-terms (terms-of term)))
((ge-expt? term)
(let ((exp (exponent-of term))
(base (base-of term)))
(cond ((number? (exponent-of term))
(add-term (list base) exp))
(t (add-term (list base)
(make-element domain 1))))))
(t (add-term (list term) 1))))))
(loop-over-terms old-terms)
(setq terms (loop for (exp base) in (rest terms)
collect
(if (1? exp) base
(make-ge-expt domain base exp))))
(cond ((not (1? const))
(if (null terms)
const
(make-ge-times domain (cons const terms))))
((null terms)
(make-element domain 1))
((null (rest terms))
(first terms))
(t (make-ge-times domain terms)))))))
(defmethod print-object ((sum ge-plus) stream)
(let ((terms (terms-of sum)))
(print-object (first terms) stream)
(loop for x in (rest terms)
do (cond ((and (number? x) (real? x))
(if (plus? x)
(format stream " + ~S" x)
(format stream " - ~S" (minus x))))
((ge-minus? x)
(princ " - " stream)
(safe-display
(simp-times-terms (domain-of sum) (list -1 x))
stream))
(t (princ " + " stream)
(print-object x stream))))))
(defmethod simplify ((x ge-plus))
(simp-plus-terms (domain-of x) (terms-of x)))
(defmethod ge-equal ((x ge-plus) (y ge-plus))
(ge-lequal (terms-of x) (terms-of y)))
(defmethod ge-great ((x ge-plus) (y ge-plus))
(ge-lgreat (terms-of x) (terms-of y)))
(defmethod print-object ((x ge-times) stream)
(let ((terms (terms-of x)))
(safe-display (first terms) stream)
(loop for x in (rest terms)
do (princ " " stream)
(safe-display x stream))))
(defmethod simplify ((x ge-times))
(simp-times-terms (domain-of x) (terms-of x)))
(defmethod ge-equal ((x ge-times) (y ge-times))
(ge-lequal (terms-of x) (terms-of y)))
(defmethod ge-great ((x ge-times) (y ge-times))
(ge-lgreat (terms-of x) (terms-of y)))
(defmethod simplify ((x ge-expt))
(let ((exp (simplify (exponent-of x)))
(base (base-of x)))
(cond ((0? exp) 1)
((1? exp) (simplify base))
((and (number? (setq base (simplify base)))
(number? exp))
(expt base exp))
((ge-expt? base)
(simplify
(make-ge-expt (domain-of x) (base-of base)
(* (exponent-of base) exp))))
(t (make-ge-expt (domain-of x) (simplify (base-of x)) exp)))))
(defmethod print-object ((expr ge-expt) stream)
(safe-display (base-of expr) stream)
(princ "^" stream)
(safe-display (exponent-of expr) stream))
(defmethod ge-equal ((x ge-expt) (y ge-expt))
(and (ge-equal (base-of x) (base-of y))
(ge-equal (exponent-of x) (exponent-of y))))
(defmethod ge-great ((x ge-expt) (y ge-expt))
(cond ((ge-great (base-of x) (base-of y))
t)
((ge-equal (base-of x) (base-of y))
(ge-great (exponent-of x) (exponent-of y)))
(t nil)))
(defmethod ge-equal ((x ge-application) (y ge-application))
(and (eql (funct-of x) (funct-of y))
(ge-lequal (args-of x) (args-of y))))
(defmethod ge-equal ((x ge-function) (y ge-function))
(eql x y))
(defgeneric get-variable-property (domain variable key)
(:documentation
"The purpose of this method is unknown."))
(defmethod get-variable-property ((domain domain) (var ge-variable) key)
(loop for var-prop in (ge-context domain)
do (when (eql (first var-prop) var)
(return (%getf (rest var-prop) key)))
finally (progn
(push (list var) (ge-context domain))
(return nil))))
(defgeneric set-variable-property (domain variable key value)
(:documentation
"The purpose of this method is unknown."))
(defmethod set-variable-property (domain (var ge-variable) key value)
(loop for var-prop in (ge-context domain)
do (when (eql (first var-prop) var)
(setf (%getf (rest var-prop) key) value)
(return value))
finally (progn
(push (list var key value) (ge-context domain))
(return value))))
(defsetf get-variable-property set-variable-property)
;; Variable dependencies and DEPENDS-ON?
(defgeneric declare-dependencies (variable &rest variables)
(:documentation
"The purpose of this method is unknown."))
(defmethod declare-dependencies ((var ge-variable) &rest vars)
(let ((depends (get-variable-property (domain-of var) var :dependencies))
(domain (domain-of var)))
(loop for v in vars
do (setq v (coerce v domain))
(unless (member v depends :test #'ge-equal)
(push v depends)))
(setf (get-variable-property (domain-of var) var :dependencies) depends)))
(defgeneric depends-on? (expression &rest variables)
(:documentation
"Return true if the expression depends on any of the variables"))
(defmethod depends-on? ((exp list) &rest vars)
(loop for arg in exp
do (when (apply #'depends-on? arg vars)
(return t))
finally (return nil)))
(defmethod depends-on? ((exp number) &rest vars)
(declare (ignore vars))
nil)
(defmethod depends-on? ((exp numeric) &rest vars)
(declare (ignore vars))
nil)
(defmethod depends-on? ((exp ge-variable) &rest vars)
(or (member exp vars :test #'ge-equal)
(loop
for var in (get-variable-property
(domain-of exp) exp :dependencies)
do (when (member var vars :test #'ge-equal)
(return t))
finally (return nil))))
(defmethod depends-on? ((exp ge-function) &rest vars)
(loop for var in vars
do (when (and (typep var 'ge-function)
(eql (name-of exp) (name-of var)))
(return t))
finally (return nil)))
(defmethod depends-on? ((exp ge-application) &rest vars)
(or (apply #'depends-on? (funct-of exp) vars)
(apply #'depends-on? (args-of exp) vars)))
(defmethod depends-on? ((exp ge-plus) &rest vars)
(apply #'depends-on? (terms-of exp) vars))
(defmethod depends-on? ((exp ge-times) &rest vars)
(apply #'depends-on? (terms-of exp) vars))
(defmethod depends-on? ((exp ge-expt) &rest vars)
(or (apply #'depends-on? (base-of exp) vars)
(apply #'depends-on? (exponent-of exp) vars)))
;; Derivatives
(defgeneric ge-deriv (expression variable)
(:documentation
"Return the derivate of the expression with respect to variable.")
(:method (expression variable)
(error "Don't know how to take the derivative of ~S wrt ~S."
expression variable)))
(defgeneric deriv (expression &rest variables)
(:documentation
"The purspose of this method is unknown."))
(defmethod deriv ((exp number) &rest vars)
(if (null vars)
(make-element *general* exp)
(make-element *general* 0)))
(defmethod deriv ((exp numeric) &rest vars)
(if (null vars) exp
(make-element (domain-of exp) 0)))
(defmethod deriv ((exp symbol) &rest vars)
(setq exp (coerce exp *general*))
(loop for v in vars
do (setq exp (ge-deriv exp (coerce v *general*))))
exp)
(defmethod deriv ((exp general-expression) &rest vars)
(setq exp (coerce exp *general*))
(loop for v in vars
do (setq exp (ge-deriv exp (coerce v *general*))))
exp)
(defmethod deriv ((fun ge-function) &rest args)
(loop for i in args
with nargs = (nargs-of fun)
do (when (or (minusp i) (not (< i nargs)))
(error "Illegal derivative of ~S in position ~D" fun i)))
(cond ((null args)
fun)
((getf fun 'deriv)
(apply #'deriv (nth (first args) (getf fun 'deriv))
(rest args)))
(t (make-function-deriv fun (copy-list args)))))
(defmethod ge-deriv ((exp general-expression) (var symbol))
(ge-deriv exp (coerce var (domain-of exp))))
(defmethod-sd ge-deriv ((exp numeric) (var ge-atom))
(make-element domain 0))
(defmethod-sd ge-deriv ((exp ge-atom) (var ge-atom))
(cond ((ge-equal exp var) (make-element domain 1))
#+ignore
((depends-on? exp var)
(make-ge-deriv domain exp `((,var 1))))
(t (make-element domain 0))))
(defmethod-sd ge-deriv ((exp ge-plus) (var ge-atom))
(simplify
(make-ge-plus domain (loop for x in (terms-of exp)
collect (ge-deriv x var)))))
(defmethod-sd ge-deriv ((exp ge-times) (var ge-atom))
(let ((terms (terms-of exp)))
(simplify
(make-ge-plus
domain
(loop for x in terms
collect
(simplify
(make-ge-times domain
(cons (ge-deriv x var) (remove x terms)))))))))
(defmethod-sd ge-deriv ((exp ge-expt) (var ge-atom))
(let ((base (base-of exp))
(power (exponent-of exp)))
(cond ((depends-on? power var)
(error "Not yet implemented"))
((and (number? power) (= power 2))
(* 2 base (ge-deriv base var)))
(t (* power (expt base (- power 1)))))))
(defmethod-sd ge-deriv ((exp ge-application) (var ge-atom))
(let* ((args (args-of exp))
(fun (funct-of exp))
(dargs (loop for arg in args
collect (ge-deriv arg var)))
(derivs (getf fun 'deriv))
ans)
(when (null derivs)
(setq derivs (loop for i below (nargs-of fun)
collect (make-function-deriv fun i)))
(setf (getf fun 'deriv) derivs))
(loop for darg in dargs