forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusolveAll.js
More file actions
199 lines (164 loc) · 5.18 KB
/
Copy pathusolveAll.js
File metadata and controls
199 lines (164 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { factory } from '../../../utils/factory.js'
import { createSolveValidation } from './utils/solveValidation.js'
const name = 'usolveAll'
const dependencies = [
'typed',
'matrix',
'divideScalar',
'multiplyScalar',
'subtract',
'equalScalar',
'DenseMatrix'
]
export const createUsolveAll = /* #__PURE__ */ factory(name, dependencies, ({ typed, matrix, divideScalar, multiplyScalar, subtract, equalScalar, DenseMatrix }) => {
const solveValidation = createSolveValidation({ DenseMatrix })
/**
* Finds all solutions of a linear equation system by backward substitution. Matrix must be an upper triangular matrix.
*
* `U * x = b`
*
* Syntax:
*
* math.usolveAll(U, b)
*
* Examples:
*
* const a = [[-2, 3], [2, 1]]
* const b = [11, 9]
* const x = usolveAll(a, b) // [ [[8], [9]] ]
*
* See also:
*
* usolve, 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[]} An array of affine-independent column vectors (x) that solve the linear system
*/
return typed(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) {
const m = matrix(a)
const R = _denseBackwardSubstitution(m, b)
return R.map(r => r.valueOf())
}
})
function _denseBackwardSubstitution (m, b_) {
// the algorithm is derived from
// https://www.overleaf.com/read/csvgqdxggyjv
// array of right-hand sides
const B = [solveValidation(m, b_, true)._data.map(e => e[0])]
const M = m._data
const rows = m._size[0]
const columns = m._size[1]
// loop columns backwards
for (let i = columns - 1; i >= 0; i--) {
let L = B.length
// loop right-hand sides
for (let k = 0; k < L; k++) {
const b = B[k]
if (!equalScalar(M[i][i], 0)) {
// non-singular row
b[i] = divideScalar(b[i], M[i][i])
for (let j = i - 1; j >= 0; j--) {
// b[j] -= b[i] * M[j,i]
b[j] = subtract(b[j], multiplyScalar(b[i], M[j][i]))
}
} else if (!equalScalar(b[i], 0)) {
// singular row, nonzero RHS
if (k === 0) {
// There is no valid solution
return []
} else {
// This RHS is invalid but other solutions may still exist
B.splice(k, 1)
k -= 1
L -= 1
}
} else if (k === 0) {
// singular row, RHS is zero
const bNew = [...b]
bNew[i] = 1
for (let j = i - 1; j >= 0; j--) {
bNew[j] = subtract(bNew[j], M[j][i])
}
B.push(bNew)
}
}
}
return B.map(x => new DenseMatrix({ data: x.map(e => [e]), size: [rows, 1] }))
}
function _sparseBackwardSubstitution (m, b_) {
// array of right-hand sides
const B = [solveValidation(m, b_, true)._data.map(e => e[0])]
const rows = m._size[0]
const columns = m._size[1]
const values = m._values
const index = m._index
const ptr = m._ptr
// loop columns backwards
for (let i = columns - 1; i >= 0; i--) {
let L = B.length
// loop right-hand sides
for (let k = 0; k < L; k++) {
const b = B[k]
// values & indices (column i)
const iValues = []
const iIndices = []
// first & last indeces in column
const firstIndex = ptr[i]
const lastIndex = ptr[i + 1]
// find the value at [i, i]
let Mii = 0
for (let j = lastIndex - 1; j >= firstIndex; j--) {
const J = index[j]
// check row
if (J === i) {
Mii = values[j]
} else if (J < i) {
// store upper triangular
iValues.push(values[j])
iIndices.push(J)
}
}
if (!equalScalar(Mii, 0)) {
// non-singular row
b[i] = divideScalar(b[i], Mii)
// loop upper triangular
for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
const J = iIndices[j]
b[J] = subtract(b[J], multiplyScalar(b[i], iValues[j]))
}
} else if (!equalScalar(b[i], 0)) {
// singular row, nonzero RHS
if (k === 0) {
// There is no valid solution
return []
} else {
// This RHS is invalid but other solutions may still exist
B.splice(k, 1)
k -= 1
L -= 1
}
} else if (k === 0) {
// singular row, RHS is zero
const bNew = [...b]
bNew[i] = 1
// loop upper triangular
for (let j = 0, lastIndex = iIndices.length; j < lastIndex; j++) {
const J = iIndices[j]
bNew[J] = subtract(bNew[J], iValues[j])
}
B.push(bNew)
}
}
}
return B.map(x => new DenseMatrix({ data: x.map(e => [e]), size: [rows, 1] }))
}
})