interp.scm 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. ;;; Given a list of distinct abscissas xs = (x1 x2 ... xn) and a list
  21. ;;; of ordinates ys = (y1 y2 ... yn), return the Lagrange interpolation
  22. ;;; polynomial through the points (x1, y1), (x2, y2), ... (xn, yn).
  23. ;;; Edited by GJS 10Jan09
  24. ;;; This version, in the file interp.scm, is numerical.
  25. ;;; It is loaded into scmutils-base-environment.
  26. ;;; There is also a generic version in the file interp-generic.scm.
  27. (declare (usual-integrations))
  28. (define (lagrange-interpolation-function ys xs)
  29. (let ((n (length ys)))
  30. ;;; FBE move after define
  31. ;;(assert (fix:= (length xs) n))
  32. (define (poly x)
  33. (reduce + :zero
  34. (generate-list n
  35. (lambda (i)
  36. (/ (reduce * :one
  37. (generate-list n
  38. (lambda (j)
  39. (if (fix:= j i)
  40. (list-ref ys i)
  41. (- x (list-ref xs j))))))
  42. (let ((xi (list-ref xs i)))
  43. (reduce * :one
  44. (generate-list n
  45. (lambda (j)
  46. (cond ((fix:< j i) (- (list-ref xs j) xi))
  47. ((fix:= j i) (expt :-one i))
  48. (else (- xi (list-ref xs j)))))))))))))
  49. (assert (fix:= (length xs) n))
  50. poly))
  51. #|
  52. ;;; If run in generic environment we can look at the kind of thing that
  53. ;;; this code does, by partial evaluation... an excellent aid to debugging.
  54. (print-expression
  55. ((lagrange-interpolation-function '(y1 y2 y3 y4) '(x1 x2 x3 x4)) 'x1))
  56. y1
  57. (print-expression
  58. ((lagrange-interpolation-function '(y1 y2 y3 y4) '(x1 x2 x3 x4)) 'x2))
  59. y2
  60. (print-expression
  61. ((lagrange-interpolation-function '(y1 y2 y3 y4) '(x1 x2 x3 x4)) 'x3))
  62. y3
  63. (print-expression
  64. ((lagrange-interpolation-function '(y1 y2 y3 y4) '(x1 x2 x3 x4)) 'x4))
  65. y4
  66. |#
  67. #|
  68. (pp (text/cselim
  69. (expression
  70. ((lagrange-interpolation-function '(y1 y2 y3 y4) '(x1 x2 x3 x4))
  71. 'x))))
  72. (let ((V-609 (- x3 x4)) (V-607 (- x2 x3)) (V-606 (* (- x2 x4) -1))
  73. (V-605 (- x x1)) (V-604 (- x1 x4)) (V-603 (- x1 x3))
  74. (V-602 (- x1 x2)) (V-601 (- x x2)) (V-599 (- x x3))
  75. (V-598 (- x x4)))
  76. (let ((V-608 (* V-601 V-605)) (V-600 (* V-598 V-599)))
  77. (+ (/ (* V-600 V-601 y1) (* V-602 V-603 V-604))
  78. (/ (* V-600 y2 V-605) (* V-606 V-607 V-602))
  79. (/ (* V-608 V-598 y3) (* V-603 V-607 V-609))
  80. (/ (* V-608 y4 V-599) (* V-606 V-609 V-604)))))
  81. |#