This repository has been archived by the owner on Aug 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev-javascript' into main
- Loading branch information
Showing
3 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |