File: seqopt.lsp

package info (click to toggle)
maxima 5.6-17
  • links: PTS
  • area: main
  • in suites: woody
  • size: 30,572 kB
  • ctags: 47,715
  • sloc: ansic: 154,079; lisp: 147,553; asm: 45,843; tcl: 16,744; sh: 11,057; makefile: 7,198; perl: 1,842; sed: 334; fortran: 24; awk: 5
file content (873 lines) | stat: -rw-r--r-- 47,142 bytes parent folder | download | duplicates (2)
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
;;; -*- Mode: Lisp; Package: Macsyma -*-                                 ;;;
;;;    (c) Copyright 1984 the Regents of the University of California.   ;;;
;;;        All Rights Reserved.                                          ;;;
;;;        This work was produced under the sponsorship of the           ;;;
;;;        U.S. Department of Energy.  The Government retains            ;;;
;;;        certain rights therein.                                       ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(macsyma-module seqopt)

(eval-when (load eval) (or (get 'expens 'version)
			   (load "expense")))

(defmvar $sequence_optim_prefix '$opt
         "String used to prefix all optimized temporaries arising from a
   call to SEQUENCE_OPTIMIZE."
         modified-commands '$sequence_optimize)

(defmvar $sequence_optim_counter 1
         "Integer index used to uniquely identify all optimized temporaries
   arising from a call to SEQUENCE_OPTIMIZE."
         fixnum
         modified-commands '$sequence_optimize)

(defmvar $sequence_optim_suffix 's
         "String used to suffix all optimized temporaries arising from a
   call to SEQUENCE_OPTIMIZE, as well as names generated by CRAY_FORTRAN for
   subexpressions which have been broken out of an expression which is too
   large for the CFT compiler."
         modified-commands '($sequence_optimize $cray_fortran))

(defmvar $save_optim_info nil
         "Flag which, if TRUE, causes the common subexpressions which
  SEQUENCE_OPTIMIZE finds to be saved as equations on the MACSYMA list
  OPTIM_EQUIVS."
         boolean
         modified-commands '$sequence_optimize)

(defmvar $optim_equivs (list '(mlist simp))
         "Macsyma list of equations for the common subexpressions which
  SEQUENCE_OPTIMIZE finds when SAVE_OPTIM_INFO is TRUE."
         modified-commands '$sequence_optimize)

(defmvar $optim_additions (list '(mlist simp))
         "Macsyma list of equations for the subexpressions which it is known
  a priori will occur more than once in a sequence of code to be optimized."
         modified-commands '$pre_optimize)

(defmvar $merge_ops (list '(mlist simp) '$cvmgp '$cvmgt)
         "A MACSYMA list of currently known CRAY-1 vector merge operations."
         modified-commands '($sequence_optimize $expense))

(defmvar $cost_float_power (+ $cost_exp $cost_sin_cos_log)
         "The expense of computing a floating point power in terms of scalar
  floating point additions on the CRAY-1(For further discussion do:
  DESCRIBE(COST_RECIPROCAL) )."
         fixnum
         modified-commands '($expense $gather_exponents))

(defvar optim-vars nil
  "MACSYMA list of generated names for common subexpressions(Not used if
   a list equations is passed to SEQUENCE_OPTIMIZE).")

(declare (special setqs)
         (array* (notype (subexp 1)))
         (fixnum n (alike1-hash)))

(array subexp t 64.)

(defmacro make-expt (base exponent) ``((mexpt simp) ,,base ,,exponent))

(defmacro base (x) `(cadr ,x))

(defmacro exponent (x) `(caddr ,x))

(defmacro mquotientp (x) `(and (not (atom ,x)) (eq (caar ,x) 'mquotient)))

;; $SEQUENCE_OPTIMIZE takes a Macsyma expression or list of simple equations
;; and returns a LIST which contains a series of equivalences for the common
;; subexpressions and the reduced equations or expression.
;; These subexpressions are found by hashing them.

(defun alike1-hash (exp)
  (\ (if (atom exp)
         (sxhash exp)
         (do ((n (alike1-hash (caar exp))
                 (+ n (alike1-hash (car arg_list))))
              (arg_list (cdr exp) (cdr arg_list)))
             ((null arg_list) n)))
     27449.))  ; a prime number < 2^15 = PRIME(3000)

(defun $sequence_optimize (x)
  (prog (setqs)
    (fillarray 'subexp (list nil))
    (if ($listp x)
        (do ((chk (cdr x) (cdr chk)))
            ((null chk))
          (or (and (not (atom (car chk)))
                   (eq (caaar chk) 'mequal)
                   ($mapatom (cadar chk)))
              (merror "List passed to SEQUENCE_OPTIMIZE of incorrect form.  Bad element is ~%~M" (car chk))))
        (setq optim-vars (append (list '(mlist)) nil)))
    (setq x (collapse (fix-unary-minus (optim-format ($gather_exponents (copy-tree x))))))
    (if (atom x) (return x))
    (comexp x)
    (setq x (optim x))
    (and $save_optim_info 
         (setq $optim_equivs (append $optim_equivs (copy-tree setqs))))
    (return (prog1 (cond ((null setqs) x)
                         (($listp x)
                          (let ((scan x))
                            (do ((opt-con setqs (cdr opt-con)))
                                ((null opt-con) x)
                              (let ((rhs-eqn (caddar opt-con)))
                                (do ((equivs scan (cdr equivs)))
                                    ((null (cdr equivs))
                                     (rplacd scan (append (ncons (car opt-con)) (cdr scan)))
                                     (setq scan (cdr scan)))
                                  (or (freeof (cadadr equivs) rhs-eqn)
                                      (setq scan (cdr equivs))))))))
                         ((or (not (eq 'mprog (caar x)))
                              (and ($listp (cadr x)) (cdadr x)))
                          `((mprog) ,optim-vars ,.setqs ,x))
                         (t `((mprog) ,optim-vars ,.(nconc setqs (cddr x)))))
                   (setq optim-vars nil)
                   (fillarray 'subexp (list nil))))))

(defun copy-to-pntr (x y)
  (do ((redo x (cdr redo))
       (new nil `(,.new ,(car redo))))
      ((eq redo y) new)))

(defun recip-1 (expon)
  (or (and (numberp expon) (minusp expon))
      (and (not (atom expon))
           (let ((op (caar expon)))
             (or (and (eq op 'mtimes) (equal (cadr expon) -1))
                 (and (eq op 'rat) (minusp (cadr expon)))
                 (eq op 'mminus))))))

(defun reciprocalp (x)
  (and (mexptp x)
       (let ((expon (exponent x)))
         (cond ((mquotientp expon) (recip-1 (cadr expon)))
               (t (recip-1 expon))))))

(defun gen-negative (x)
   (cond ((mmminusp x) (cadr x))
         ((mquotientp x) `((mquotient) ,(mul -1 (cadr x)) ,(caddr x)))
         (t (mul -1 x))))

(defun mul-list (x)
   (cond ((cdr x) `((mtimes) ,@x))
         (t (car x))))

(defun optim-format (x)
  (cond ((atom x) x)
        ((and (eq 'rat (caar x)) (minusp (cadr x)))
         `((mminus) ((rat) ,(minus (cadr x)) ,(caddr x))))
        ((and (eq 'mquotient (caar x)) (not (equal 1 (cadr x))))
         (let ((nmr (cadr x)))
           (optim-format `((mtimes simp) ,@(cond ((mtimesp nmr) (cdr nmr))
                                                 (t (list nmr)))
                                         ((mquotient) 1 ,(caddr x))))))
        ((eq 'mexpt (caar x)) (opt-expt x))
        ((eq 'mtimes (caar x))
         (do ((next (cdr x) (cdr next))
              (denominator)
              (numerator))
             ((null next)
              (cond (denominator
                     (let ((recip `((mquotient) 1 ,(mul-list denominator))))
                       (cond (numerator
                              (let ((prod? (mul-list numerator)))
                                (cond ((mtimesp prod?)
                                       (nconc prod? (ncons recip)))
                                      (t `((mtimes) ,prod? ,recip)))))
                             (t recip))))
                    (numerator (mul-list numerator))
                    (t x)))
           (let ((obj (car next)))
             (cond ((reciprocalp obj)
                    (let* ((expon (exponent obj))
                           (optim-expt (let ((mbase (base obj)))
                                         (cond ((equal expon -1)
                                                (optim-format mbase))
                                               (t
                                                (opt-expt (make-expt mbase (gen-negative expon))))))))
                      (setq denominator
                            (nconc denominator
                                   (cond ((mtimesp optim-expt) (cdr optim-expt))
                                         (t (ncons optim-expt)))))
                      (or numerator
                          (do ((seplist (cdr x) (cdr seplist)))
                              ((eq seplist next))
                            (let ((element (car seplist)))
                              (or (reciprocalp element)
                                  (setq numerator `(,.numerator ,element))))))))
                   (t
                    (let ((result (optim-format obj)))
                      (or (eq obj result)
                          numerator
                          (do ((seplist (cdr x) (cdr seplist)))
                              ((eq seplist next))
                            (let ((element (car seplist)))
                              (or (reciprocalp element)
                                  (setq numerator `(,.numerator ,element))))))
                      (and (or numerator denominator (not (eq obj result)))
                           (setq numerator (nconc numerator
                                                  (cond ((and (mexptp obj) (mtimesp result))
                                                         (copy-tree (cdr result)))
                                                        (t (ncons result))))))))))))
        (t
         (do ((next (cdr x) (cdr next))
              (new))
             ((null next)
              (cond (new new)
                    (t x)))
           (let* ((obj (car next))
                  (result (optim-format obj)))
             (or (eq obj result)
                 new
                 (setq new (copy-to-pntr x next)))
             (and (or new (not (eq obj result)))
                  (setq new `(,.new ,result))))))))

(defun opt-expt (x)
  (let ((osym-base (base x)) (oexp (exponent x)))
    (let ((sym-base (optim-format osym-base)) (exp (optim-format oexp)))
      (cond ((reciprocalp x)
             `((mquotient) 1 ,(cond ((equal -1 exp) sym-base)
                                    (t (opt-expt (make-expt sym-base (gen-negative exp)))))))
            ((and (ratnump exp) (equal 2 (caddr exp))) 
             (setq exp (cadr exp))
             (cond ((equal 1 exp) `((%sqrt) ,sym-base))
                   (t (let ((int-exp (quotient exp 2)))
                        `((mtimes) ((%sqrt) ,sym-base)
                                   ,(cond ((equal int-exp 1) sym-base)
                                          (t (make-expt sym-base int-exp))))))))
            (t
             (cond ((and (eq osym-base sym-base) (eq oexp exp)) x)
                   (t (make-expt sym-base exp))))))))

;; the following two functions were motivated by an inability of the
;; cray merge functions to cope with a unary minus.

(defun disp-negate (x)
       (cond ((mtimesp x)
              (let ((coeff (cadr x)))
                (cond ((and (fixp coeff) (minusp coeff))
                       (append `((mtimes) ,(minus coeff)) (cddr x)))
                      (t `((mminus) ,x)))))
             ((mnump x) (mul -1 x))
             ((or (atom x) (not (eq (caar x) 'mminus))) `((mminus) ,x))
             (t (cadr x))))

(defun fix-unary-minus (x)
   (cond (($mapatom x) x)
         ((eq (caar x) 'mtimes)
          (mapc 'fix-unary-minus (cdr x))
          (let ((sign (cadr x)))
            (cond ((and (fixp sign) (minusp sign))
                   (cond ((equal sign -1)
                          (let ((chk-merge (caddr x)))
                            (cond ((and (not (atom chk-merge))
                                        (memq (caar chk-merge) $merge_ops))
                                   (rplacd (cdr x) (append `(((,(caar chk-merge)) ,(disp-negate (cadr chk-merge))
                                                                                  ,(disp-negate (caddr chk-merge))
                                                                                  ,(cadddr chk-merge)))
                                                           (cdddr x)))
                                   (cond ((cdddr x) (rplacd x (cddr x)) x)
                                         (t (caddr x))))
                                  (t `((mminus) ,(cond ((cdddr x)
                                                        (rplacd x (cddr x)) x)
                                                       (t (caddr x))))))))
                         (t `((mminus) ,(append `((mtimes) ,(minus sign)) (cddr x))))))
                  (t x))))
         (t (do ((search (cdr x) (cdr search)))
                ((null search) x)
              (let* ((obj (car search)) (new (fix-unary-minus obj)))
                (or (eq new obj) (rplaca search new)))))))

(defun collapse (x)
  (if (atom x)
      x
      (let ((n (logand 63. (alike1-hash x))))
             (do ((l (cdr x) (cdr l)))
                 ((null l))
               (let* ((carl (car l)) (res (collapse carl)))
                 (or (eq carl res) (rplaca l res))))
             (do ((l (subexp n) (cdr l)))
                 ((null l) (store (subexp n) (cons (list x) (subexp n))) x)
               (if (alike1 x (caar l)) (return (caar l)))))))

(defun comexp (x)
  (cond ((atom x))
        ((eq 'rat (caar x)))
        (t
         (setq x (assq x (subexp (logand 63. (alike1-hash x)))))
         (cond ((null (cdr x))
                (rplacd x 1)
                (mapc 'comexp (cdar x)))
               (t (rplacd x (1+ (cdr x))))))))

(defun optim (x)
  (cond ((atom x) x)
        ((and (memq 'array (cdar x)) (not (mget (caar x) 'arrayfun-mode))) x)
        ((eq 'rat (caar x)) x)
        (t 
         (let ((xpair (assq x (subexp (logand 63. (alike1-hash x)))))
               (nx (do ((l (cdr x) (cdr l))
                        (c (list (car x)) (cons (optim (car l)) c)))
                       ((null l) (nreverse c)))))
           (let ((tmp (cdr xpair))
                 (sym (do ((lk (cdr $optim_equivs) (cdr lk)))
                          ((null lk))
                        (and (alike1 nx (caddar lk))
                             (return (cadar lk))))))
             (cond ((fixp tmp)
                    (cond (sym
                           (rplacd xpair sym)
                           (mformat nil "c - earlier opt-vect, ~M, occurs ~M time(s)" sym tmp)
                           sym)
                          ((= tmp 1) nx)
                          (t 
                           (let ((sym (getvar)))
                             (rplacd xpair sym)
                             (setq setqs `(,.setqs ,(list (cond (optim-vars (list 'msetq))
                                                                (t (list 'mequal)))
                                                          sym nx)))
                             (mformat nil "c - there are ~M occurrences of ~M" tmp sym)
                             sym))))
                   (t tmp))))))) ;;; Should this be an error?

(defun getvar ()
   (let ((newvar (implode (nconc (exploden $sequence_optim_prefix)
                                 (exploden $sequence_optim_counter)
                                 (exploden $sequence_optim_suffix)))))
     (increment $sequence_optim_counter)
     (if optim-vars (setq optim-vars `(,.optim-vars ,newvar)))
     newvar))

;;; The following will not PRE_OPTIMIZE top-level forms.

(defun $pre_optimize (x)
  (cond ((atom x))
        ((eq (caar x) '$cvmgp)
         (let ((term3 (cadddr x))
               (opt-list (append $optim_equivs (cdr $optim_additions))))
           (or ($mapatom term3)
               (and (eq (caar term3) 'mtimes)
                    (equal (cadr term3) -1)
                    (let ((obj (caddr term3))
                          (two-term (= (length term3) 3)))
                      (or (and two-term ($mapatom obj))
                          (do ((l (cdr opt-list) (cdr l)))
                              ((null l))
                            (let ((rhs (caddar l)))
                              (cond ((and two-term (alike1 rhs obj))
                                     (rplaca (cdddr x) (mul -1 (cadar l)))
                                     (return t))
                                    ((alike1 rhs term3)
                                     (rplaca (cdddr x) (cadar l))
                                     (return t))))))))
               (do ((l (cdr opt-list) (cdr l)))
                   ((null l))
                 (let ((rhs (caddar l)))
                   (cond ((alike1 rhs term3)
                          (rplaca (cdddr x) (cadar l))
                          (return t))
                         ((and (eq (caar rhs) 'mtimes)
                               (equal (cadr rhs) -1)
                               (null (cdddr rhs))
                               (alike1 (caddr rhs) term3))
                          (rplaca (cdddr x) (mul (cadar l) -1))
                          (return t)))))
               (let ((name (getvar)))
                 (setq $optim_additions
                       `(,@$optim_additions ((mequal simp) ,name ,term3)))
                 (rplaca (cdddr x) name)))))
        (t (do ((terms (cdr x) (cdr terms)))
               ((null terms))
             (let ((obj (car terms)))
               ($pre_optimize obj)
               (do ((lk (cdr $optim_equivs) (cdr lk)))
                   ((null lk))
                 (and (alike1 obj (caddar lk))
                      (rplaca terms (cadar lk))
                      (return t))))))))

(defun $collapse_pre_optims (x)
  (cond ((atom x) x)
        ((do ((lk (cdr $optim_equivs) (cdr lk)))
             ((null lk))
           (and (alike1 x (caddar lk))
                (return (cadar lk)))))
        (t (do ((terms (cdr x) (cdr terms))
                (success))
               ((null terms)
                (cond ((or success (not (eq (caar x) 'mtimes))) x)
                      (t (do ((l (cdr x) (cdr l))
                              (follow x l))
                             ((null l) x)
                           (let ((saved (car l)))
                             (cond ((atom saved))
                                   ((eq (caar saved) '$cvmgp)
                                    (rplacd follow (cdr l))
                                    (let* ((pminus (equal (cadr x) -1))
                                           (new (do ((lk (cdr $optim_equivs) (cdr lk)))
                                                    ((null lk) x)
                                                  (let ((rhs (caddar lk)))
                                                    (cond ((alike1 x rhs)
                                                           (return (cadar lk)))
                                                          (t (and (eq (caar rhs) 'mtimes)
                                                                  (cond (pminus
                                                                         (alike1 (cddr x) (cdr rhs)))
                                                                        ((equal (cadr rhs) -1)
                                                                         (alike1 (cdr x) (cddr rhs))))
                                                                  (return (mul -1 (cadar lk))))))))))
                                      (return (cond ((eq new x)
                                                     (rplacd follow `(,saved ,@(cdr follow)))
                                                     x)
                                                    (t (mul new saved))))))))))))
             (let* ((obj (car terms)) (new-obj ($collapse_pre_optims obj)))
               (or (eq obj new-obj)
                   (and (setq success t)
                        (rplaca terms new-obj))))))))

(defun product-base (x y)
  (muln (append (cond ((mtimesp x) (cdr x))
                      (t (ncons x)))
                (cond ((mtimesp y) (cdr y))
                      (t (ncons y))))
        nil))

(defun floating-exponent-gather (x)
  (cond ((atom x) x)
        ((mtimesp x)
         (do ((next (cdr x) (cdr next))
              (xfol (cdr x) (cdr xfol))
              (modified)
              (new))
             ((null next)
              (cond ((null new) x)
                    (t (muln new nil))))
           (let* ((obj (car next)) (result obj))
             (and (mexptp result)
                  (let ((expon (exponent result)))
                    (and (not (fixp expon))
                         (do ((remain (cdr next) (cdr remain)))
                             ((null remain))
                           (let ((powered? (car remain)))
                             (and (mexptp powered?)
                                  (let ((expon-2 (exponent powered?)))
                                    (and (not (fixp expon-2))
                                         (let ((intdif (sub expon expon-2)))
                                           (and (fixp intdif)
                                                (let ((pf (> intdif 0))
                                                      (ab intdif))
                                                  (declare (fixnum ab))
                                                  (cond ((or (zerop ab)
                                                             (> (+ $cost_float_power
                                                                   $cost_float_power
                                                                   (cond (pf ($expense expon-2))
                                                                         (t ($expense expon))))
                                                                (+ (cond (pf
                                                                          (let ((mbase (base result)))
                                                                            (cond ((mtimesp mbase) ($expense mbase))
                                                                                  (t 0))))
                                                                         (t
                                                                          (let ((mbase (base powered?)))
                                                                            (cond ((mtimesp mbase) ($expense mbase))
                                                                                  (t 0)))))
                                                                   (multiplies-in-nth-power (abs ab)))))
                                                         (cond ((not modified)
                                                                (setq modified t
                                                                      next (append next nil))
                                                                (setq remain (memq powered? next))
                                                                (setq powered? (car remain))))
                                                         (cond (pf
                                                                (let ((mbase (base result)))
                                                                  (setq result
                                                                        (cond ((equal ab 1) mbase)
                                                                              (t (make-expt mbase ab))))
                                                                  (rplaca remain (make-expt (product-base mbase (base powered?)) (exponent powered?))))
                                                                (return t))
                                                               (t
                                                                (setq result (make-expt (product-base (base result) (base powered?)) (exponent result)))
                                                                (cond ((zerop ab)
                                                                       (setq next (delq powered? next)))
                                                                      (t
                                                                       (let ((pabs (minus ab))
                                                                             (mbase (base powered?)))
                                                                         (cond ((equal pabs 1)
                                                                                (cond ((mtimesp mbase)
                                                                                       (setq next (nconc next (cdr mbase)))
                                                                                       (setq remain (memq powered? next))
                                                                                       (setq next (delq powered? next)))
                                                                                      (t (rplaca remain mbase))))
                                                                               (t (rplaca remain (make-expt mbase pabs))))))))))))))))))))))
             (setq result (floating-exponent-gather result))
             (or (eq obj result)
                 new
                 (setq new (copy-to-pntr (cdr x) xfol)))
             (and (or new (not (eq obj result)))
                  (setq new (nconc new
                                   (cond ((mtimesp result)
                                          (copy-tree (cdr result)))
                                         (t (ncons result)))))))))
        (t
         (do ((next (cdr x) (cdr next))
              (new))
             ((null next)
              (cond (new new)
                    (t x)))
           (let* ((obj (car next))
                  (result (floating-exponent-gather obj)))
             (or (eq obj result)
                 new
                 (setq new (copy-to-pntr x next)))
             (and (or new (not (eq obj result)))
                  (setq new `(,.new ,result))))))))

(defmacro div-q (x y) `(div (simplify ,x) (simplify ,y)))

(defun fgcd-exponent-gather (x)
  (cond ((atom x) x)
        ((mtimesp x)
         (do ((next (cdr x) (cdr next))
              (xfol (cdr x) (cdr xfol))
              (modified)
              (new))
             ((null next)
              (cond ((null new) x)
                    (t (muln new nil))))
           (let* ((obj (car next))
                  (result (fgcd-exponent-gather obj)))
             (and (mexptp result)
                  (let ((expon (exponent result)))
                    (and (not (fixp expon))
                         (do ((allow-fix t nil)
                              (repeat t))
                             ((null repeat))
                           (do ((remain (cdr next) (cdr remain))
                                (current-gcd expon)
                                (pntrs))
                               ((null remain)
                                (or allow-fix (setq repeat nil))
                                (and pntrs
                                     (if (fixp current-gcd)
                                         (<= (multiplies-in-nth-power current-gcd)
                                             (1+ (length pntrs)))
                                         t)
                                     (let* ((leadiv (gen-quotients (div-q expon current-gcd)))
                                            (a-single (equal leadiv 1))
                                            (ints (and (not a-single) (fixp leadiv))))
                                       (do ((scan pntrs (cdr scan))
                                            (save (cond (ints
                                                         (- $cost_float_power
                                                            (multiplies-in-nth-power leadiv)))
                                                        (t 0)))
                                            (interms (cond (ints (ncons (make-expt (base result) leadiv)))
                                                           (t ())))
                                            (others (cond (ints ())
                                                          (t
                                                           (let ((mbase (base result)))
                                                             (cond (a-single
                                                                    (cond ((mtimesp mbase) (cdr mbase))
                                                                          (t (ncons mbase))))
                                                                   (t (ncons (make-expt mbase leadiv)))))))))
                                           ((null scan)
                                            (cond (interms
                                                   (let* ((prod-ints (muln interms nil))
                                                          (try-ints-gather (integer-gathering prod-ints))
                                                          (savings (- (+ (1+ (length pntrs))
                                                                         (- ($expense prod-ints)
                                                                            ($expense try-ints-gather))
                                                                         save)
                                                                      $cost_float_power)))
                                                     (declare (fixnum savings))
                                                     (if (< savings 0) (return nil))
                                                     (setq result (make-expt (muln (nconc others
                                                                                          (cond ((mtimesp try-ints-gather)
                                                                                                  (cdr try-ints-gather))
                                                                                                (t (ncons try-ints-gather))))
                                                                                   nil)
                                                                             current-gcd))))
                                                  (t
                                                   (if (not a-single) (return (setq repeat nil)))
                                                   (setq result (make-expt (muln others nil) current-gcd))))
                                            (do ((rescan pntrs (cdr rescan)))
                                                ((null rescan) (setq repeat nil))
                                              (setq next (delq (car rescan) next))))
                                         (declare (fixnum save))
                                         (let* ((expt (car scan))
                                                (expon-2 (exponent expt))
                                                (nxdiv (gen-quotients (div-q expon-2 current-gcd))))
                                           (cond ((equal nxdiv 1)
                                                  (setq a-single t
                                                        save (+ save $cost_float_power)
                                                        others (nconc others (let ((mbase (base expt)))
                                                                               (cond ((mtimesp mbase) (cdr mbase))
                                                                                     (t (ncons mbase)))))))
                                                 ((fixp nxdiv)
                                                  (setq save (+ save (- $cost_float_power
                                                                        (multiplies-in-nth-power nxdiv)))
                                                        interms `(,.interms ,(make-expt (base expt) nxdiv))))
                                                 (t
                                                  (setq others `(,.others ,(make-expt (base expt) nxdiv))))))))))
                             (let ((powered? (car remain)))
                               (and (mexptp powered?)
                                    (let ((expon-2 (exponent powered?)))
                                      (and (not (fixp expon-2))
                                           (let ((fgcd (gen-quotients ($gcd current-gcd expon-2))))
                                             (cond ((equal fgcd 1))
                                                   ((or pntrs
                                                        (alike1 fgcd expon)
                                                        (alike1 fgcd expon-2)
                                                        (and allow-fix
                                                             (or (fixp (div-q expon fgcd))
                                                                 (fixp (div-q expon-2 fgcd)))))
                                                    (cond ((not modified)
                                                           (setq modified t
                                                                 next (append next nil))
                                                           (setq remain (memq powered? next))
                                                           (setq powered? (car remain))))
                                                    (setq current-gcd fgcd
                                                          pntrs `(,.pntrs ,powered?))))))))))))))
             (or (eq obj result)
                 new
                 (setq new (copy-to-pntr (cdr x) xfol)))
             (and (or new (not (eq obj result)))
                  (setq new `(,.new ,result))))))
        (t
         (do ((next (cdr x) (cdr next))
              (new))
             ((null next)
              (cond (new new)
                    (t x)))
           (let* ((obj (car next))
                  (result (fgcd-exponent-gather obj)))
             (or (eq obj result)
                 new
                 (setq new (copy-to-pntr x next)))
             (and (or new (not (eq obj result)))
                  (setq new `(,.new ,result))))))))

(defun integer-exponent-gather (x)
  (cond ((atom x) x)
        ((mtimesp x)
         (do ((top x (or new top))
              (new 0 new))
             ((null new) top)
           (setq new nil)
           (do ((next (cdr top) (cdr next))
                (xfol (cdr top) (cdr xfol))
                (modified))
               ((null next)
                (and new (setq new (muln new nil))))
             (let* ((obj (car next)) (result obj))
               (and (mexptp result)
                    (let ((expon (exponent result)))
                      (and (fixp expon)
                           (do ((remain (cdr next) (cdr remain)))
                               ((null remain))
                             (let ((powered? (car remain)))
                               (and (mexptp powered?)
                                    (let ((expon-2 (exponent powered?)))
                                      (and (fixp expon-2)
                                           (let* ((intdif (difference expon expon-2))
                                                  (pf (plusp intdif)))
                                             (declare (fixnum intdif))
                                             (cond ((or (zerop intdif)
                                                        (< (+ (cond (pf
                                                                     (let ((mbase (base result)))
                                                                       (cond ((mtimesp mbase) ($expense mbase))
                                                                             (t 0))))
                                                                    (t
                                                                     (let ((mbase (base powered?)))
                                                                       (cond ((mtimesp mbase) ($expense mbase))
                                                                             (t 0)))))
                                                              1
                                                              (multiplies-in-nth-power (abs intdif)))
                                                           (multiplies-in-nth-power (max expon expon-2))))
                                                    (cond ((not modified)
                                                           (setq modified t
                                                                 next (append next nil))
                                                           (setq remain (memq powered? next))
                                                           (setq powered? (car remain))))
                                                    (cond (pf
                                                           (let ((mbase (base result)))
                                                             (setq result
                                                                   (cond ((equal intdif 1) mbase)
                                                                         (t (make-expt mbase intdif))))
                                                             (rplaca remain (make-expt (product-base mbase (base powered?)) (exponent powered?))))
                                                           (return t))
                                                          (t
                                                           (setq result (make-expt (product-base (base result) (base powered?)) (exponent result)))
                                                           (cond ((zerop intdif)
                                                                  (setq next (delq powered? next)))
                                                                 (t
                                                                  (let ((pabs (minus intdif))
                                                                        (mbase (base powered?)))
                                                                    (cond ((equal pabs 1)
                                                                           (cond ((mtimesp mbase)
                                                                                  (setq next (nconc next (cdr mbase)))
                                                                                  (setq remain (memq powered? next))
                                                                                  (setq next (delq powered? next)))
                                                                                 (t (rplaca remain mbase))))
                                                                          (t (rplaca remain (make-expt mbase pabs))))))))))))))))))))
               (setq result (integer-exponent-gather result))
               (or (eq obj result)
                   new
                   (setq new (copy-to-pntr (cdr top) xfol)))
               (and (or new (not (eq obj result)))
                    (setq new (nconc new
                                     (cond ((mtimesp result)
                                            (copy-tree (cdr result)))
                                           (t (ncons result))))))))))
        (t
         (do ((next (cdr x) (cdr next))
              (new))
             ((null next)
              (cond (new new)
                    (t x)))
           (let* ((obj (car next))
                  (result (integer-exponent-gather obj)))
             (or (eq obj result)
                 new
                 (setq new (copy-to-pntr x next)))
             (and (or new (not (eq obj result)))
                  (setq new `(,.new ,result))))))))

(defun igcd-exponent-gather (x)
  (cond ((atom x) x)
        ((mtimesp x)
         (do ((next (cdr x) (cdr next))
              (xfol (cdr x) (cdr xfol))
              (modified)
              (new))
             ((null next)
              (cond ((null new) x)
                    (t (muln new nil))))
           (let* ((obj (car next))
                  (result (igcd-exponent-gather obj)))
             (and (mexptp result)
                  (let ((expon (exponent result)))
                    (and (fixp expon)
                         (do ((remain (cdr next) (cdr remain))
                              (current-gcd expon)
                              (pntrs))
                             ((null remain)
                              (and pntrs
                                   (do ((scan pntrs (cdr scan))
                                        (newbase (let ((mbase (base result)))
                                                   (cond ((equal expon current-gcd)
                                                          (cond ((mtimesp mbase) (cdr mbase))
                                                                (t (ncons mbase))))
                                                         (t (ncons (make-expt mbase (quotient expon current-gcd))))))))
                                       ((null scan)
                                        (setq result (make-expt (muln newbase nil) current-gcd)))
                                     (let* ((expt (car scan))
                                            (expon-2 (exponent expt)))
                                       (setq newbase (nconc newbase (let ((mbase (base expt)))
                                                                      (cond ((equal expon-2 current-gcd)
                                                                             (cond ((mtimesp mbase) (cdr mbase))
                                                                                   (t (ncons mbase))))
                                                                            (t (ncons (make-expt mbase (quotient expon-2 current-gcd)))))))
                                             next (delq expt next))))))
                           (declare (fixnum current-gcd))
                           (let ((powered? (car remain)))
                             (and (mexptp powered?)
                                  (let ((expon-2 (exponent powered?)))
                                    (and (fixp expon-2)
                                         (let ((intgcd (gcd current-gcd expon-2)))
                                           (cond ((not (equal intgcd 1))
                                                  (cond ((not modified)
                                                         (setq modified t
                                                               next (append next nil))
                                                         (setq remain (memq powered? next))
                                                         (setq powered? (car remain))))
                                                  (setq current-gcd intgcd
                                                        pntrs `(,.pntrs ,powered?)))))))))))))
             (or (eq obj result)
                 new
                 (setq new (copy-to-pntr (cdr x) xfol)))
             (and (or new (not (eq obj result)))
                  (setq new `(,.new ,result))))))
        (t
         (do ((next (cdr x) (cdr next))
              (new))
             ((null next)
              (cond (new new)
                    (t x)))
           (let* ((obj (car next))
                  (result (igcd-exponent-gather obj)))
             (or (eq obj result)
                 new
                 (setq new (copy-to-pntr x next)))
             (and (or new (not (eq obj result)))
                  (setq new `(,.new ,result))))))))

(defun gen-quotients (x)
  (cond (($mapatom x) x)
        ((specrepp x) (gen-quotients (specdisrep x)))
        ((eq 'mtimes (caar x))
         (do ((next (cdr x) (cdr next))
              (denominator)
              (numerator))
             ((null next)
              (cond (denominator
                     (let ((den (mul-list denominator)))
                       (cond (numerator
                              `((mquotient) ,(mul-list numerator) ,den))
                             (t `((mquotient) 1 ,den)))))
                    (numerator (mul-list numerator))
                    (t x)))
           (let ((obj (car next)))
             (cond ((reciprocalp obj)
                    (let ((expon (gen-quotients (exponent obj)))
                          (mbase (gen-quotients (base obj))))
                      (setq denominator
                            (nconc denominator
                                   (cond ((equal expon -1)
                                          (cond ((mtimesp mbase) (cdr mbase))
                                                (t (ncons mbase))))
                                         (t (ncons (make-expt mbase (gen-negative expon)))))))
                      (or numerator
                          (do ((seplist (cdr x) (cdr seplist)))
                              ((eq seplist next))
                            (let ((element (car seplist)))
                              (or (reciprocalp element)
                                  (setq numerator `(,.numerator ,element))))))))
                   (t
                    (let ((result (gen-quotients obj)))
                      (or (eq obj result)
                          numerator
                          (do ((seplist (cdr x) (cdr seplist)))
                              ((eq seplist next))
                            (let ((element (car seplist)))
                              (or (reciprocalp element)
                                  (setq numerator `(,.numerator ,element))))))
                      (and (or numerator denominator (not (eq obj result)))
                           (setq numerator `(,.numerator ,result)))))))))
        ((reciprocalp x)
         `((mquotient) 1 ,(gen-quotients (let ((exp (exponent x))
                                               (mbase (base x)))
                                           (cond ((equal -1 exp) mbase)
                                                 (t (make-expt mbase (gen-negative exp))))))))
        (t
         (do ((next (cdr x) (cdr next))
              (new))
             ((null next)
              (cond (new new)
                    (t x)))
           (let* ((obj (car next))
                  (result (gen-quotients obj)))
             (or (eq obj result)
                 new
                 (setq new (copy-to-pntr x next)))
             (and (or new (not (eq obj result)))
                  (setq new `(,.new ,result))))))))

(defun integer-gathering (x)
   (do ((new x (igcd-exponent-gather (integer-exponent-gather new)))
        (onew 0 new))
       ((eq new onew) new)))

(defun $gather_exponents (x)
   (do ((new (gen-quotients x)
             (fgcd-exponent-gather (floating-exponent-gather new)))
        (onew 0 new))
       ((eq new onew) (integer-gathering new))))