From 16e60c8b10a0530174cda5e99b18dd1edf767f8a Mon Sep 17 00:00:00 2001 From: Paul Bunyan Date: Wed, 6 Sep 2017 15:14:39 -0400 Subject: [PATCH] Use multiplication/division intelligently when calculating RREF. Matrix.rref now chooses scale or divide based on the magnitude of the unscaled leading value. Hopefully should cut down on floating point error (issue #1), but tests don't seem to be indicating that... --- js/matrix.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/js/matrix.js b/js/matrix.js index 53591ae..b4fb89f 100644 --- a/js/matrix.js +++ b/js/matrix.js @@ -127,8 +127,17 @@ function Matrix (h, w, zero) { //Scale row to get the leading one var scaleFactor = result.rref.get(i,i); - result.rref._divideRow(i, scaleFactor); - if (augment) result.augment._divideRow(i, scaleFactor); + + //Determine whether division or multiplication by inverse + //will preserve the most digits + if (Math.abs(scaleFactor) >= 1) { + result.rref._divideRow(i, scaleFactor); + if (augment) result.augment._divideRow(i, scaleFactor); + } else { + scaleFactor = 1./scaleFactor; + result.rref._scaleRow(i, scaleFactor); + if (augment) result.augment._scaleRow(i, scaleFactor) + } //Zero out rest of column for (var row = 0; row < result.rref.rows; ++row) {