print.scm 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. #| -*-Scheme-*-
  2. Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
  3. 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  4. 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Massachusetts
  5. Institute of Technology
  6. This file is part of MIT/GNU Scheme.
  7. MIT/GNU Scheme is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or (at
  10. your option) any later version.
  11. MIT/GNU Scheme is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with MIT/GNU Scheme; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
  18. USA.
  19. |#
  20. ;;; Hamiltonians look better if we divide them out.
  21. ;;; FBE: make a parameter
  22. (define *divide-out-terms* (make-parameter #f))
  23. (define *heuristic-numbers* #f)
  24. (define (canonicalize-numbers expr)
  25. (cond ((with-units? expr)
  26. (with-si-units->expression expr))
  27. ((list? expr)
  28. (cons (canonicalize-numbers (operator expr))
  29. (map canonicalize-numbers (operands expr))))
  30. ((and (number? expr) *heuristic-numbers*)
  31. (heuristic-canonicalize-complex expr))
  32. (else
  33. expr)))
  34. (define (ham:simplify hexp)
  35. (cond ((and (quotient? hexp) (*divide-out-terms*))
  36. (cond ((sum? (symb:numerator hexp))
  37. (let ((d (symb:denominator hexp)))
  38. (a-reduce symb:+
  39. (map (lambda (n)
  40. (g:simplify (symb:/ n d)))
  41. (operands (symb:numerator hexp))))))
  42. (else hexp)))
  43. ((compound-data-constructor? hexp)
  44. (cons (operator hexp) (map ham:simplify (operands hexp))))
  45. (else hexp)))
  46. (define (divide-out-terms-simplify doit?)
  47. (assert (boolean? doit?) "argument must be a boolean.")
  48. (clear-memoizer-tables)
  49. (*divide-out-terms* doit?))
  50. ;;; Equations are often prettier if we get rid of the denominators,
  51. ;;; but watch out for singularities.
  52. (define (eqn:simplify hexp)
  53. (cond ((quotient? hexp)
  54. (symb:numerator hexp))
  55. ((matrix? hexp)
  56. ((m:elementwise eqn:simplify) hexp))
  57. ((vector? hexp)
  58. ((v:elementwise eqn:simplify) hexp))
  59. (else hexp)))
  60. (define (flush-derivative expr)
  61. (substitute derivative-symbol
  62. 'derivative
  63. expr))
  64. (define (flush-literal-function-constructors expr)
  65. (if (pair? expr)
  66. (if (eq? (car expr) 'literal-function)
  67. (if (and (pair? (cadr expr)) (eq? (caadr expr) 'quote))
  68. (flush-literal-function-constructors (cadadr expr))
  69. (cadr expr))
  70. (cons (flush-literal-function-constructors (car expr))
  71. (flush-literal-function-constructors (cdr expr))))
  72. expr))
  73. (define *factoring* #f)
  74. (define (simplify exp)
  75. (flush-derivative
  76. (flush-literal-function-constructors
  77. (ham:simplify
  78. ((if *factoring* poly:factor (lambda (expr) expr))
  79. (g:simplify exp))))))
  80. ;;; Is this enough?
  81. (define (careful-simplify e)
  82. (simplify e))
  83. (define *only-printing* #f)
  84. ;;; FBE: make a parameter
  85. (define *last-expression-printed* (make-parameter (lambda () 'none-yet)))
  86. (define (system-environments)
  87. (list generic-environment ;rule-environment ; FBE: now alias for 'scmutils-base...'
  88. numerical-environment scmutils-base-environment))
  89. (define (prepare-for-printing expr simplifier)
  90. (*last-expression-printed*
  91. (cond ((unsimplifiable? expr)
  92. (lambda () expr))
  93. ((and (not (with-units? expr))
  94. (apply object-name expr (system-environments)))
  95. => (lambda (name) (lambda () name)))
  96. (else
  97. (let ((rexpr (simplifier expr)))
  98. (lambda () (arg-suppressor rexpr))))))
  99. (*last-expression-printed*))
  100. (define (unsimplifiable? expr)
  101. (or (memq expr '(#t #f))
  102. (null? expr)
  103. (number? expr)
  104. ;;(pathname? expr) ; FBE
  105. (undefined-value? expr)
  106. (and (procedure? expr)
  107. (object-name expr system-global-environment))
  108. ;What is this?
  109. (and (pair? expr)
  110. (memq (car expr) '(*operator* *solution*)))))
  111. (define* (show-expression expr #:optional simplifier)
  112. (if (default-object? simplifier) (set! simplifier simplify))
  113. (prepare-for-printing expr simplifier)
  114. ;; (display "#;\n")
  115. (pp ((*last-expression-printed*)))
  116. (cond ((not *only-printing*)
  117. (internal-show-expression
  118. ((*last-expression-printed*))))))
  119. (define* (print-expression expr #:optional simplifier)
  120. (if (default-object? simplifier)
  121. (set! simplifier simplify))
  122. (prepare-for-printing expr simplifier)
  123. ;; (display "#;\n")
  124. (pp ((*last-expression-printed*))))
  125. (define pe print-expression)
  126. (define se show-expression)
  127. ;;; FBE start: comment out
  128. ;; (define* (print-expression-prefix expr #:optional simplifier)
  129. ;; (if (default-object? simplifier)
  130. ;; (set! simplifier simplify))
  131. ;; (prepare-for-printing expr simplifier)
  132. ;; ((pp-line-prefix "; ") (*last-expression-printed*)))
  133. ;; (define pep print-expression-prefix)
  134. ;;; FBE end.
  135. (define* (print-expression-comment expr #:optional simplifier)
  136. (if (default-object? simplifier)
  137. (set! simplifier simplify))
  138. (prepare-for-printing expr simplifier)
  139. (newline)
  140. (display "#| Result:")
  141. (newline)
  142. (pp ((*last-expression-printed*)))
  143. (display "|#"))
  144. (define pec print-expression-comment)