diff --git a/staging/javascript/src/utils.js b/staging/javascript/src/utils.js new file mode 100644 index 0000000..9bcbfa6 --- /dev/null +++ b/staging/javascript/src/utils.js @@ -0,0 +1,51 @@ +/** + * MIT License + * + * Copyright (c) 2021 Guilherme Tadashi Maeoka + * https://github.com/guimspace/envelope-matrix + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +module.exports = { + initEnvelope, getElement +} + +function getElement (envelope, i, j) { + if (i === j) return envelope.diagg[i] + else if (envelope.enveCol[j] === envelope.enveCol[j + 1]) return 0 + + let p = envelope.enveCol[j] + const l = envelope.enveLin[p] + + if (l > i) return 0 + + p = p + i - l + + return envelope.enve[p] +} + +function initEnvelope () { + return { + diagg: [], + enve: [], + enveCol: [], + enveLin: [] + } +} diff --git a/staging/javascript/src/wrap.js b/staging/javascript/src/wrap.js new file mode 100644 index 0000000..423e658 --- /dev/null +++ b/staging/javascript/src/wrap.js @@ -0,0 +1,93 @@ +/** + * MIT License + * + * Copyright (c) 2021 Guilherme Tadashi Maeoka + * https://github.com/guimspace/envelope-matrix + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +module.exports = { + generateEnvelope, unwrapEnvelope +} + +function unwrapEnvelope (envelope, matrix, isLine = false) { + const n = envelope.diagg.length + + let p = 0 + + for (let i = 0; i < n; i++) { + matrix[i][i] = envelope.diagg[i] + + const d = envelope.enveLin[i] - p + const top = envelope.enveCol[i + 1] + + while (p < top) { + if (isLine) matrix[i][d + p] = envelope.enve[p] + else matrix[d + p][i] = envelope.enve[p] + + p++ + } + } +} + +function generateEnvelope (matrix, isLine = false) { + const diagg = [] + const enve = [] + const enveCol = [] + const enveLin = [] + + const n = matrix.length + + let p = 0 + let j = 0 + + while (j < n) { + diagg[j] = matrix[j][j] + + let i = 0 + if (isLine) { + while (i < j && matrix[j][i] === 0) { i++ } + } else { + while (i < j && matrix[i][j] === 0) { i++ } + } + + enveCol[j] = p + enveLin[j] = i + + p += j - i + + while (i < j) { + isLine ? enve.push(matrix[j][i]) : enve.push(matrix[i][j]) + i++ + } + + j++ + } + + enve.push(null) + enveCol[j] = p + + return { + diagg: diagg, + enve: enve, + enveCol: enveCol, + enveLin: enveLin + } +} diff --git a/staging/javascript/test/test.js b/staging/javascript/test/test.js new file mode 100644 index 0000000..99b7e77 --- /dev/null +++ b/staging/javascript/test/test.js @@ -0,0 +1,22 @@ +const wrap = require('../src/wrap.js') + +const matrix = [ + [11, 12, 0, 14, 0, 0], + [-12, 22, 23, 0, 0, 0], + [0, -23, 33, 0, 0, 0], + [-14, 0, 0, 44, 0, 46], + [0, 0, 0, 0, 55, 0], + [0, 0, 0, -46, 0, 66] +] +const n = matrix.length + +const envelope = wrap.generateEnvelope(matrix) +console.log(envelope) + +const regen = new Array(n) +for (let i = 0; i < n; i++) { + regen[i] = new Array(n).fill(0) +} + +wrap.unwrapEnvelope(envelope, regen) +console.log(regen)