dgutils.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. ;;; For symbolic expressions in operators.
  21. (define (diffop-name form)
  22. (cond ((operator? form) (operator-name form))
  23. ((literal-function? form) (f:expression form))
  24. (else (expression form))))
  25. ;;; The following mappers only make sense if, when there is more than
  26. ;;; one structure they are all isomorphic.
  27. (define (s:sigma/r proc . structures)
  28. (s:sigma/r/l proc structures))
  29. (define (s:sigma/r/l proc structures)
  30. (s:sigma/l (lambda elements
  31. (if (structure? (car elements))
  32. (s:sigma/r/l proc elements)
  33. (apply proc elements)))
  34. structures))
  35. (define (s:sigma proc . structures)
  36. (s:sigma/l proc structures))
  37. (define (s:sigma/l proc structures)
  38. (sigma (lambda (i)
  39. (apply proc
  40. (map (lambda (s) (s:ref s i))
  41. structures)))
  42. 0
  43. (- (s:length (car structures)) 1)))
  44. #|
  45. (define R2 (rectangular 2))
  46. (instantiate-coordinates R2 '(x y))
  47. (define chi-R2 (R2 '->coords))
  48. (define chi-inverse-R2 (R2 '->point))
  49. (define R2-basis (coordinate-system->basis R2))
  50. (pec (s:sigma/r (lambda (e)
  51. ((e (compose (literal-function 'f (-> (UP Real Real) Real))
  52. chi-R2))
  53. (chi-inverse-R2 (up 'x0 'y0))))
  54. (basis->vector-basis R2-basis)))
  55. #| Result:
  56. (+ (((partial 1) f) (up x0 y0)) (((partial 0) f) (up x0 y0)))
  57. |#
  58. |#
  59. ;;; Sometimes we need to simplify an internal result.
  60. (define memoized-simplify
  61. (hash-memoize-1arg (compose canonical-copy g:simplify)))
  62. (define (simplify-numerical-expression expr)
  63. (cond ((and (pair? expr) (eq? (car expr) '*number*))
  64. (let ((result
  65. (make-numerical-literal
  66. (memoized-simplify expr))))
  67. ;; copy extra properties, if any
  68. (set-cdr! (cdr result) (cddr expr))
  69. result))
  70. (else expr)))
  71. (define (with-incremental-simplifier thunk)
  72. (parameterize ((incremental-simplifier g:simplify)
  73. (enable-constructor-simplifications? #t))
  74. (clear-memoizer-tables)
  75. (thunk)))
  76. #|
  77. (pp (simplify-numerical-expression
  78. (/ 1 (+ (/ 1 'r1) (/ 1 'r2)))))
  79. (*number* (expression (/ (* r1 r2) (+ r1 r2))))
  80. |#