Solved a function scope issue

This commit is contained in:
2022-09-05 16:39:09 +10:00
parent 33302ef318
commit f80709ae41
4 changed files with 36 additions and 69 deletions

View File

@@ -1,52 +1,8 @@
//assume the args are matrices
fn matrix(first, second) {
//get the matrix size
var l1 = _length(first); //rows
var l2 = _length(_get(first, 0)); //cols
var l3 = _length(second); //rows
var l4 = _length(_get(second, 0)); //cols
//pre-allocate the matrix
var row = [];
for (var j = 0; j < l4; j++) {
_push(row, 0);
{
fn f() {
return i;
}
var result = [];
for (var i = 0; i < l1; i++) {
_push(result, row);
}
//assign the values
for (var i = 0; i < _length(first); i++) {
//select each element of "first"
var firstElement = _get(first, i);
//for each element of second
for (var i2 = 0; i2 < _length(second); i2++) {
for (var j2 = 0; j2 < _length(_get(second, 0)); j2++) {
var val = _get(_get(first, i), i2) * _get(_get(second, i2), j2);
//TODO: needs better notation than this tmpRow variable
var tmpRow = _get(result, i);
_set(tmpRow, j2, val);
_set(result, i, tmpRow);
//result[ i ][ j2 ] += first[i][i2] * second[i2][j2]
}
}
}
return result;
}
//matrix multiply
var c = [[4], [5], [6]]; //this is a 3x1
var d = [[1, 2, 3]]; //this is a 1x3
print matrix(c, d);