linear-algebra
Version:
Efficient, high-performance linear algebra library
103 lines (72 loc) • 1.99 kB
JavaScript
/**
* @fileOverview Basic arithmetic operations
*/
/**
* Transpose this matrix.
* @return {Matrix}
*/
Matrix.prototype.trans = function() {
var thisData = this.data,
rows = this.rows,
cols = this.cols;
var row, col;
var result = new Array(cols);
for (col=0; col<cols; ++col) {
result[col] = new Array(rows);
for (row=0; row<rows; ++row) {
result[col][row] = thisData[row][col];
}
}
return new Matrix(result);
};
/**
* In-place version of trans().
* @return this
*/
Matrix.prototype.trans_ = function() {
var thisData = this.data,
rows = this.rows,
cols = this.cols;
var row, col, t;
// first we transpose the matrix upto length of shortest side
var isSquare = (cols === rows);
var shortestSide = (cols > rows) ? rows : cols;
for (row=0; row<shortestSide; ++row) {
for (col=row + 1; col<shortestSide; ++col) {
t = thisData[col][row];
thisData[col][row] = thisData[row][col];
thisData[row][col] = t;
}
}
// now we transpose the rest of the matrix
if (!isSquare) {
if (cols > rows) {
// do a column at a time
for (col=rows; cols > col; ++col) {
if (!Array.isArray(thisData[col])) {
thisData[col] = new Array(rows);
}
for (row=0; row<rows; ++row) {
thisData[col][row] = thisData[row][col];
}
}
}
else {
// do a row at a time
for (row=cols; rows > row; ++row) {
for (col=0; cols > col; ++col) {
thisData[col][row] = thisData[row][col];
}
}
}
// finally, we update the "official" dimensions
t = rows;
this.rows = cols;
this.cols = t;
}
return this;
};
BUILD(ALGEBRA_OP, div, thisData[row][col] / op2Data[row][col])
BUILD(ALGEBRA_OP, mul, thisData[row][col] * op2Data[row][col])
BUILD(ALGEBRA_OP, plus, thisData[row][col] + op2Data[row][col])
BUILD(ALGEBRA_OP, minus, thisData[row][col] - op2Data[row][col])