na-gaussian-elimination

Solves systems of linear equations using Gaussian Elimination

Usage no npm install needed!

<script type="module">
  import naGaussianElimination from 'https://cdn.skypack.dev/na-gaussian-elimination';
</script>

README

na-gaussian-elimination

npm version bower version build status

Solves a system of linear equations (matrix), using the Gaussian elimination algorithm.

Works in Node.js and the browser.

Example

Live Demo

// If not executing on node.js and want to listen to events,
// use an EventEmitter library, e.g., https://github.com/asyncly/EventEmitter2
GaussianElimination.setEventEmitter(EventEmitter2);

// Use bignumber.js to create numbers: https://github.com/MikeMcl/bignumber.js/
var zero = new BigNumber(0);
GaussianElimination.defaultOptions.zero = zero;

var gaussianElimination = new GaussianElimination();

var matrix = [
  [new BigNumber(1) , new BigNumber(2), new BigNumber(3)],
  [new BigNumber(4), new BigNumber(5), new BigNumber(6)],
  [new BigNumber(7), new BigNumber(8), new BigNumber(12)]
];
var result = [
  new BigNumber(4), new BigNumber(7), new BigNumber(10)
];

gaussianElimination.on('swapRows', function(ev) {
  console.log('swap rows ' + ev.i + ' and ' + ev.j);
});

var system = gaussianElimination.solve(matrix, result);

console.log('solution', system.solution); // [-2, 3, 0]

Functions and properties of GaussianElimination

.setEventEmitter(EventEmitter)

If you want to use the events in the browser, you must set an EventEmitter library using the GaussianElimination.setEventEmitter(EventEmitter) method.

.defaultOptions

Used when no other option is specified in the constructor. See Options section.

.SolutionError

Error emitted (or thrown) when there is an error while solving the system.

.OptionsError

Error thrown in the constructor when an option has an invalid value.

#solve(matrix, result)

Solves the system and returns an object with the solution property.

  • matrix can be rectangular.
  • The values of matrix and result must be objects with the following methods: minus, times, div, isZero, abs, comparedTo. This methods are all present in the bignumber.js library.
  • matrix and result are modified when solving the system.
  • It doesn't check the dimensions of matrix and result.

#forwardElimination(matrix, result)

Reduces the given system to row echelon form (without the condition that the leading coefficient must be 1).

#backSubstitution(matrix, result)

Finds a solution for the system.

Options

  • pivoting: one of none, avoid zero, partial, scaled and complete. Default: 'partial'.
  • lu: if it is truthy, then the matrix is transformed into an LU matrix. If not, then the lower triangle of the matrix is filled with zeroes. Default: false.
  • zero: the value used to represent a zero. Only used when lu is falsy. Default: 0.