* Finds one solution of a linear equation system by backward substitution. Matrix must be an upper triangular matrix. Throws an error if there's no solution.
*
* `U * x = b`
*
* Syntax:
*
* math.usolve(U, b)
*
* Examples:
*
* const a = [[-2, 3], [2, 1]]
* const b = [11, 9]
* const x = usolve(a, b) // [[8], [9]]
*
* See also:
*
* usolveAll, lup, slu, usolve, lusolve
*
* @param {Matrix, Array} U A N x N matrix or array (U)
* @param {Matrix, Array} b A column vector with the b values
*
* @return {DenseMatrix | Array} A column vector with the linear system solution (x)
*/
returntyped(name,{
'SparseMatrix, Array | Matrix': function(m,b){
return_sparseBackwardSubstitution(m,b)
},
'DenseMatrix, Array | Matrix': function(m,b){
return_denseBackwardSubstitution(m,b)
},
'Array, Array | Matrix': function(a,b){
constm=matrix(a)
constr=_denseBackwardSubstitution(m,b)
returnr.valueOf()
}
})
function_denseBackwardSubstitution(m,b){
// make b into a column vector
b=solveValidation(m,b,true)
constbdata=b._data
constrows=m._size[0]
constcolumns=m._size[1]
// result
constx=[]
constmdata=m._data
// loop columns backwards
for(letj=columns-1;j>=0;j--){
// b[j]
constbj=bdata[j][0]||0
// x[j]
letxj
if(!equalScalar(bj,0)){
// value at [j, j]
constvjj=mdata[j][j]
if(equalScalar(vjj,0)){
// system cannot be solved
thrownewError('Linear system cannot be solved since matrix is singular')