0
点赞
收藏
分享

微信扫一扫

Matrix Arithmetic, Matrix Inverse


矩阵加法、矩阵乘法

Matrix Arithmetic, Matrix Inverse_javascript

Inverse of a matrix

Matrix Arithmetic, Matrix Inverse_ecmascript_02

* matrix.js

function Matrix(/* Array */a) {
	if (typeof a == "undefined") {
		a = [];
	}
	this._a = a;
	this.row = this._a.length;
	this.col = (typeof this._a[0] == "undefined") ? 0 : this._a[0].length;
}

Matrix.prototype.init = function(/* int */row, /* int */col, val) {
	var i,j;
	
	for (i = 0; i < row; i++) {
		var t = [];
		for (j = 0; j < col; j++) {
			t[j] = val;
		}
		this._a[i] = t;
	}
	this.row = this._a.length;
	this.col = this._a[0].length;
	return this;
};

Matrix.prototype.get = function(/* int */x, /* int */y) {
	return this._a[x][y];
};
Matrix.prototype.set = function(/* int */x, /* int */y, v) {
	this._a[x][y] = v;
	return this;
};


/* m rows x n cols Matrix, exclude row, line */
function _matExclude(mat, /* int */row, /* int */col) {
	var subMat = [];
	var i, j;
	var t = [], k;
	
	for ( i = 0; i < row; i++) {
		t = []; k = 0;
		for (j = 0; j < col; j++) {
			t[k++] = mat[i][j];
		}
		for (j = col+1; j < mat[0].length; j++) {
			t[k++] = mat[i][j];
		}
		subMat.push(t);
	}
	for (i = row + 1; i < mat.length; i++) {
		t = []; k = 0;
		for (j = 0; j < col; j++) {
			t[k++] = mat[i][j];
		}
		for (j = col+1; j < mat[0].length; j++) {
			t[k++] = mat[i][j];
		}
		subMat.push(t);
	}
	return subMat;
}

function determinant(/* Array m * n */ mat) {
	var total = 0, sign;
	/* Base case of recursive function: 1x1 matrix */
	if (mat.length == 1 && mat[0].length == 1) {
		return mat[0][0];
	}
	for (var col = 0; col < mat[0].length; col++) {
		/* Exclude first row and current column. */
		var k = _matExclude(mat, 0, col);
        /* (-1)^(row+col) */
		if (col % 2 == 0) { sign=1; } else { sign = -1;}
		total += sign * mat[0][col] * determinant(k);
	}
	return total;
};

Matrix.prototype.determinant = function() {
	return determinant(this._a);
};

function MatrixInverse(/* Number */det, /* Matrix */nMat) {
	this.deno = det;
	this.mat = nMat;
}
MatrixInverse.prototype.toString = function() {
	return this.mat.toString() + "/" + this.deno;
};

Matrix.prototype.adjoint = function() {
	var i, j;
	var mat = new Matrix();
	var subA = [];
	var sign;
	
	mat.init(this.row, this.col, 0);
	for (i = 0; i < mat.row; i++) {
		for (j = 0; j < mat.col; j++) {
			subA = _matExclude(this._a, j, i); /* not i, j. transpose */
			sign = (i+j)%2 == 0 ? 1 : -1;
			mat.set(i, j, sign * determinant(subA));
		}
	}
	return mat;
};
/* https://www.geeksforgeeks.org/inverse-of-a-matrix/ */
Matrix.prototype.inverse = function() {
	if (this.row - this.col != 0) {
		throw new Exception("Invertible matrix must be n-by-n square matrix.");
	}
	/* A^(-1) = (1/|A|) * Adj(A) */
	return new MatrixInverse(determinant(this._a), this.adjoint());
};

Matrix.prototype.toString = function() {
	var i, j;
	var s = "[";
	
	s += this.get(0, 0);
	for (j = 1; j < this.col; j++) {
		s += ", " + this.get(0, j);
	}
	for (i = 1; i < this.row; i++) {
		s += "\n " + this.get(i, 0);
		for (j = 1; j < this.col; j++) {
			s += ", " + this.get(i, j);
		}
	}
	s += "]";
	return s;
};

Matrix.add = function(/* Matrix */mat1, /* Matrix */mat2) {
	var i, j;
	var mat3 = new Matrix();

	mat3.init(mat1.row, mat2.col, 0);
	for (i = 0; i < mat1.row; i++) {
		for (j = 0; j < mat1.col; j++) {
			mat3.set(i, j, mat1.get(i, j) + mat2.get(i, j));
		}
	}
	return mat3;
};

Matrix.mul = function(/* Matrix */mat1, /* Matrix */mat2) {
	var i, j, k;
	var mat3 = new Matrix();
	var sum;

	mat3.init(mat1.row, mat2.col, 0);
	for (i = 0; i < mat3.row; i++) {
		for (j = 0; j < mat3.col; j++) {
			sum = 0;
			for (k = 0; k < mat1.col; k++) {
				sum += mat1.get(i, k) * mat2.get(k, j);
			}
			mat3.set(i, j, sum);
		}
	}
	return mat3;
}

module.exports = Matrix;

* index.js

var mat1 = new Matrix([[1,-4],[3,2]]),
	mat2 = new Matrix([[2,2],[2,2]]);
	mat3 = Matrix.add(mat1, mat2);
console.log(mat3.toString());

var mat4 = new Matrix([[0,1],[2,3],[4,5]]),
	mat5 = new Matrix([[6,7,8,9],[0,1,2,3]]),
	mat6;
mat6 = Matrix.mul(mat4, mat5);
console.log(mat6.toString());

var mat7 = new Matrix([[1,1,2],[1,3,4],[0,2,5]]);
console.log(mat7.inverse().toString());

Matrix Arithmetic, Matrix Inverse_开发语言_03

Matrix Arithmetic, Matrix Inverse_javascript_04

举报

相关推荐

0 条评论