heuristic.scm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. ;;; Some processes, such as finding the roots of a polynomial, can
  21. ;;; benefit by heuristic rounding of results (to a nearby rational).
  22. (declare (usual-integrations))
  23. ;;; Heuristic rounding will occur to a rational within
  24. (define heuristic-rounding-tolerance 1.0e-13)
  25. ;;; that is expressible with a denominator less than the
  26. (define heuristic-rounding-denominator 20)
  27. ;;; if such a rational exists.
  28. ;;; Complex numbers with one part small enough are forced to be real
  29. ;;; or imaginary.
  30. (define heuristic-one-part-insignificant 1.0e-10)
  31. ;;; If both real part and imaginary part of a complex number are tiny,
  32. ;;; it is also set to zero. This is dangerous.
  33. (define heuristic-rounding-tiny 1.0e-15)
  34. ;;; If the number is a small rational multiple of an important
  35. ;;; constant, we may substitute the symbolic constant:
  36. (define heuristic-symbolize? #t)
  37. #|
  38. ;;; The basic idea
  39. (define (heuristic-round-real x)
  40. (let ((r (rationalize->exact x (* x heuristic-rounding-tolerance))))
  41. (if (< (denominator r) heuristic-rounding-denominator)
  42. r
  43. x)))
  44. |#
  45. (define* (heuristic-canonicalize-real a #:optional symbolize?)
  46. (if (default-object? symbolize?)
  47. (h-c-r a heuristic-symbolize?)
  48. (h-c-r a symbolize?)))
  49. (define (heuristic-round-real x)
  50. (h-c-r x #f))
  51. (define (h-c-r a symbolize?)
  52. (let lp ((ideas *important-numbers*))
  53. (if (null? ideas)
  54. a
  55. (let* ((ag (/ a (caar ideas)))
  56. (af (rationalize->exact ag heuristic-rounding-tolerance)))
  57. (if (and (not (= af 0))
  58. (< (denominator af) heuristic-rounding-denominator))
  59. (if symbolize?
  60. (symb:* af (cadar ideas)) ;symbolic version
  61. (* af (caar ideas)))
  62. (lp (cdr ideas)))))))
  63. (define *important-numbers*
  64. `((1 1)
  65. (,pi :pi)
  66. (,(/ 1 pi) (/ 1 :pi))
  67. (,(exp 1) (exp 1))
  68. (,(exp -1) (exp -1))
  69. (,(sqrt 2) (sqrt 2))
  70. (,(sqrt 3) (sqrt 3))
  71. (,(sqrt 5) (sqrt 5))
  72. (,(sqrt 7) (sqrt 7))
  73. (,:euler :euler)
  74. (,:phi :phi)
  75. ))
  76. (define* (heuristic-canonicalize-complex z #:optional symbolize?)
  77. (if (default-object? symbolize?)
  78. (h-c-c z heuristic-symbolize?)
  79. (h-c-c z symbolize?)))
  80. (define (heuristic-round-complex z) (h-c-c z #f))
  81. (define (h-c-c z symbolize?)
  82. (if (and (real? z) (not (and (inexact? (imag-part z)) (= (imag-part z) 0))))
  83. (h-c-r z symbolize?)
  84. (let ((r (real-part z)) (i (imag-part z)))
  85. (let ((ar (abs r)) (ai (abs i)))
  86. (cond ((and (< ar heuristic-rounding-tiny)
  87. (< ai heuristic-rounding-tiny))
  88. 0)
  89. ((< ai (* heuristic-one-part-insignificant ar))
  90. (h-c-r r symbolize?))
  91. ((< ar (* heuristic-one-part-insignificant ai))
  92. (g:make-rectangular 0 (h-c-r i symbolize?)))
  93. (else
  94. (let* ((a (angle z))
  95. (af (h-c-r a symbolize?)))
  96. (if (or (not (number? af)) (not (= af a)))
  97. (g:make-polar (h-c-r (magnitude z) symbolize?) af)
  98. (g:make-rectangular (h-c-r r symbolize?)
  99. (h-c-r i symbolize?))))))))))
  100. #|
  101. ;;; previous idea
  102. (let* ((ag (/ (angle z) pi))
  103. (af (heuristic-round-real ag)))
  104. (if (= ag af)
  105. (make-rectangular (heuristic-round-real r)
  106. (heuristic-round-real i))
  107. (make-polar (heuristic-round-real (magnitude z))
  108. (* :pi af))))
  109. |#
  110. ;;; (set! heuristic-number-canonicalizer heuristic-canonicalize-complex)
  111. #|
  112. ;;; Watch out--symb:pi is now in numsymb.scm
  113. (define (heuristic-round-angle a)
  114. (let* ((ag (/ a :pi))
  115. (af (heuristic-round-real ag)))
  116. (if (< (abs (- ag af)) heuristic-rounding-tolerance)
  117. (if heuristic-symbolize?
  118. (g:* af symb:pi)
  119. (* af :pi))
  120. a)))
  121. (define symb:pi ':pi)
  122. (define symb:-pi (symb:- 0 symb:pi))
  123. (define symb:pi/6 (symb:/ symb:pi 6))
  124. (define symb:-pi/6 (symb:- 0 symb:pi/6))
  125. (define symb:pi/3 (symb:/ symb:pi 3))
  126. (define symb:-pi/3 (symb:- 0 symb:pi/3))
  127. (define symb:pi/2 (symb:/ symb:pi 2))
  128. (define symb:-pi/2 (symb:- 0 symb:pi/2))
  129. (define symb:pi/4 (symb:/ symb:pi 4))
  130. (define symb:-pi/4 (symb:- 0 symb:pi/4))
  131. (define symb:2pi (symb:* 2 symb:pi))
  132. (define symb:-2pi (symb:- 0 symb:2pi))
  133. |#