solve-tridiagonal

2.0.0 • Public • Published

solve-tridiagonal Build Status npm version js-standard-style

Solve a system of linear tridiagonal equations

Introduction

This module accepts javascript Arrays or typed arrays representing the bands of a tridiagonal matrix and computes the solution using the Thomas algorithm. The problem in matrix form is

\left[
\begin{matrix}
   {b_0} & {c_0} & {   } & {   } & { 0 } \\
   {a_1} & {b_1} & {c_1} & {   } & {   } \\
   {   } & {a_2} & {b_2} & \cdot & {   } \\
   {   } & {   } & \cdot & \cdot & {c_{n-2}}\\
   { 0 } & {   } & {   } & {a_{n-1}} & {b_{n-1}}\\
\end{matrix}
\right]
\left[
\begin{matrix}
   {x_0 }  \\
   {x_1 }  \\
   \cdot   \\
   \cdot   \\
   {x_{n-1} }  \\
\end{matrix}
\right]
=
\left[
\begin{matrix}
   {d_0 }  \\
   {d_1 }  \\
   \cdot   \\
   \cdot   \\
   {d_{n-1} }  \\
\end{matrix}
\right].

The solver will fail if the matrix is singular and may not succeed if the matrix is not diagonally dominant. If the solver fails, it will log a console message and return false.

Example

Consider the solution of

\left[
\begin{matrix}
   2 & 1 &  0 \\
  -1 & 7 &  4 \\
   0 & 2 & -3 \\
\end{matrix}
\right]
\left[
\begin{matrix}
   {x_0 }  \\
   {x_1 }  \\
   {x_2 }  \\
\end{matrix}
\right]
=
\left[
\begin{matrix}
   {4}  \\
   {25}  \\
   {-5}  \\
\end{matrix}
\right].

var tdiag = require('solve-tridiagonal')
 
var d = [4, 25, -5]
 
tdiag([0, -1, 2], [2, 7, -3], [1, 4, 0], d)
// => d = [ 1, 2, 3 ]

Installation

$ npm install solve-tridiagonal

API

require('solve-tridiagonal')(n, a, b, c, d)

Arguments:

  • n: an integer representing the number of equations to be solved
  • a: a javascript Array or typed array of length n representing the subdiagonal, indexed according to the equation above. Left unchanged by the solver.
  • b: a javascript Array or typed array of length n representing the diagonal, indexed as above. This vector is modified by the solver.
  • c: a javascript Array or typed array of length n representing the superdiagonal, indexed as above. This vector is modified by the solver.
  • d: a javascript Array or typed array of length n representing the known vector d. On successful completion, this vector will contain the solution.

Returns: True on successful completion, false otherwise.

License

© 2016 Ricky Reusser. MIT License.

Package Sidebar

Install

npm i solve-tridiagonal

Weekly Downloads

5

Version

2.0.0

License

MIT

Last publish

Collaborators

  • rreusser