full-pivot.scm 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. ;;;; Linear System Solver -- Full-Pivot
  21. ;;; 13 August 1990 -- Full pivoting linear-equation solver -- GJS & Jacob
  22. (declare (usual-integrations))
  23. ;;; This file contains the following definitions:
  24. ;;;
  25. ;;; (full-pivot-solve-linear-system a-matrix b-vector) => x-vector
  26. ;;;
  27. ;;; (full-pivot-solve a-matrix b-vector succeed fail)
  28. ;;; succeed = (lambda (x) (assert A*x=b) ...)
  29. ;;; fail = (lambda (dismiss) ...)
  30. ;;; There is also a version that works on arrays, rather than matrices
  31. ;;; (full-pivot-solve-internal A b succeed fail)
  32. (define (full-pivot-solve-linear-system A b)
  33. (full-pivot-solve-internal (matrix->array A) b (lambda (x) x) barf-on-zero-pivot))
  34. (define (full-pivot-solve A b succeed fail)
  35. (full-pivot-solve-internal (matrix->array A) b succeed fail))
  36. (define (full-pivot-solve-internal A b succeed fail)
  37. ;; succeed = (lambda (x) ... (assert A*x=b))
  38. ;; fail = (lambda (dismiss) ... )
  39. (let ((n (num-rows A)))
  40. (if (fix:< n 2)
  41. (let ((a00 (array-ref A 0 0)))
  42. (if (< (magnitude a00) *minimum-allowable-full-pivot*)
  43. (fail (singular-matrix-error))
  44. (succeed (vector (/ (vector-ref b 0) a00)))))
  45. (full-pivot-find A n
  46. (lambda (p ip jp) ; pivot p is in row ip, column jp
  47. (let ((nm1 (fix:- n 1))
  48. (pivot-row (nth-row A ip)))
  49. (let ((scaled-pivot-row
  50. (make-initialized-vector nm1
  51. (lambda (k)
  52. (if (fix:< k jp)
  53. (/ (vector-ref pivot-row k) p)
  54. (/ (vector-ref pivot-row (fix:+ k 1)) p)))))
  55. (pivot-column
  56. (make-initialized-vector nm1
  57. (lambda (k)
  58. (if (fix:< k ip)
  59. (array-ref A k jp)
  60. (array-ref A (fix:+ k 1) jp)))))
  61. (bp (/ (vector-ref b ip) p)))
  62. (full-pivot-solve-internal
  63. (generate-array nm1 nm1
  64. (lambda (i j)
  65. (let ((c (* (vector-ref pivot-column i)
  66. (vector-ref scaled-pivot-row j))))
  67. (if (fix:< i ip)
  68. (if (fix:< j jp)
  69. (- (array-ref A i j) c)
  70. (- (array-ref A i (fix:+ j 1)) c))
  71. (if (fix:< j jp)
  72. (- (array-ref A (fix:+ i 1) j) c)
  73. (- (array-ref A (fix:+ i 1) (fix:+ j 1)) c))))))
  74. (make-initialized-vector nm1
  75. (lambda (i)
  76. (let ((c (* bp (vector-ref pivot-column i))))
  77. (if (fix:< i ip)
  78. (- (vector-ref b i) c)
  79. (- (vector-ref b (fix:+ i 1)) c)))))
  80. ;;Continuation of full-pivot-solve-internal
  81. (lambda (x)
  82. (let ((xip
  83. (let lp ((k 0) (sum 0))
  84. (if (fix:= k nm1)
  85. (- bp sum)
  86. (lp (fix:+ k 1)
  87. (+ sum
  88. (* (vector-ref x k)
  89. (vector-ref scaled-pivot-row k))))))))
  90. (succeed
  91. (make-initialized-vector n
  92. (lambda (i)
  93. (cond ((fix:< i jp) (vector-ref x i))
  94. ((fix:= i jp) xip)
  95. ((fix:> i jp) (vector-ref x (fix:- i 1)))))))))
  96. fail))))
  97. fail))))
  98. (define (full-pivot-find A n found-pivot fail)
  99. ;; found = (lambda (pivot ip jp) ... )
  100. ;; fail = (lambda (dismiss) ... )
  101. (let row-loop ((i 0) (maxabs -1) (maxpiv -1) (imax -1) (jmax -1))
  102. (if (fix:= i n)
  103. (if (< maxabs *minimum-allowable-full-pivot*)
  104. (fail (singular-matrix-error))
  105. (found-pivot maxpiv imax jmax))
  106. (let col-loop ((j 0) (maxabs maxabs) (maxpiv maxpiv) (imax imax) (jmax jmax))
  107. (if (fix:= j n)
  108. (row-loop (fix:+ i 1) maxabs maxpiv imax jmax)
  109. (let* ((newel (array-ref A i j))
  110. (newabs (magnitude newel)))
  111. (if (> newabs maxabs)
  112. (col-loop (fix:+ j 1) newabs newel i j)
  113. (col-loop (fix:+ j 1) maxabs maxpiv imax jmax))))))))
  114. (define *minimum-allowable-full-pivot* 1.0e-30)