Skip to content
This repository has been archived by the owner on Aug 8, 2024. It is now read-only.

Commit

Permalink
Merge branch 'dev-javascript' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
guimspace committed May 24, 2021
2 parents 624e10f + 2addfa4 commit d521334
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 0 deletions.
51 changes: 51 additions & 0 deletions staging/javascript/src/utils.js
Original file line number Diff line number Diff line change
@@ -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: []
}
}
93 changes: 93 additions & 0 deletions staging/javascript/src/wrap.js
Original file line number Diff line number Diff line change
@@ -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
}
}
22 changes: 22 additions & 0 deletions staging/javascript/test/test.js
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit d521334

Please sign in to comment.