c# - Unstable calculation error -
i need calculate matrix: ( x^(t) * x )^(-1). legend code&comments:
x double[,] array; xt - transposed matrix ^(-1) - inverted matrix
every time generate new random matrix work , found out program unstable, because isn't working input data. i'm sure because need identity matrix in end if everything's fine, totally terrible ineverted matrix don't identity matrix. i'm dissappointes because use same type of data , not convert anything. compiler mvs 2010. hope me.
here program.cs:
static void main(string[] args) { matrix x = new matrix(5, 4); //matrix temp = new matrix(x.row, x.col); //double[] y = new double[x.row]; //double[] b = new double[x.row]; //this data isn't calculated correctly. used debugging x.matrixx[0, 0] = 7; x.matrixx[0, 1] = 6; x.matrixx[0, 2] = 5; x.matrixx[0, 3] = 8; x.matrixx[1, 0] = 7; x.matrixx[1, 1] = 5; x.matrixx[1, 2] = 8; x.matrixx[1, 3] = 5; x.matrixx[2, 0] = 6; x.matrixx[2, 1] = 8; x.matrixx[2, 2] = 6; x.matrixx[2, 3] = 8; x.matrixx[3, 0] = 8; x.matrixx[3, 1] = 5; x.matrixx[3, 2] = 8; x.matrixx[3, 3] = 7; x.matrixx[4, 0] = 8; x.matrixx[4, 1] = 5; x.matrixx[4, 2] = 6; x.matrixx[4, 3] = 7; /* 7,00000 6,00000 5,00000 8,00000 7,00000 5,00000 8,00000 5,00000 6,00000 8,00000 6,00000 8,00000 8,00000 5,00000 8,00000 7,00000 8,00000 5,00000 6,00000 7,00000 */ //random matrix generation /* random rnd = new random(); (int = 0; < x.row; i++) (int j = 0; j < x.col; j++) x.matrixx[i, j] = rnd.next(5, 10); */ /*i'm going calculate: ( x^(t) * x )^(-1) * 1. transpose x * 2. multiply x , (1) * 3. invert matrix (2) * +4. wanna check results: multilate of (2) , (3) = identity_matrix. * */ matrix.display(x); //1 matrix xt = matrix.transpose(x); matrix.display(xt); //2 matrix xxt = matrix.multiply(x, xt); matrix.display(xxt); //3 matrix xxtinv = matrix.invert(matrix.multiply(x, xt)); matrix.display(xxtinv); //4 console.writeline("invert(xxt) * xxt. identitymatrix:"); matrix identitymatrix = matrix.multiply(xxtinv, xxt); matrix.display(identitymatrix); console.readkey(); }
and here matrix.cs functions:
public class matrix { private double[,] matrix; private int row; private int col; #region constructors public matrix(int row, int col) { this.row = row; this.col = col; matrix = new double[row, col]; } public matrix() { random rnd = new random(); row = rnd.next(3, 7); col = rnd.next(3, 7); matrix = new double[row, col]; (int = 0; < row; i++) (int j = 0; j < col; j++) matrix[i, j] = rnd.next(5, 10); } public matrix(matrix a) { this.col = a.col; this.row = a.row; this.matrix = a.matrix; } #endregion #region properties public int col { { return col; } set { col = value; } } public int row { { return row; } set { row = value; } } public double[,] matrixx { { return matrix; } set { matrix = value; } } #endregion static public matrix transpose(matrix array) { matrix temp = new matrix(array.col, array.row); (int = 0; < array.row; i++) (int j = 0; j < array.col; j++) temp.matrix[j, i] = array.matrix[i, j]; return temp; } static public void display(matrix array) { (int = 0; < array.row; i++) { (int j = 0; j < array.col; j++) console.write("{0,5:f2}\t", array.matrix[i, j]); console.writeline(); } console.writeline(); } static public matrix multiply(matrix a, matrix b) { if (a.col != b.row) throw new exception("multiplication impossible: a.col != b.row"); matrix r = new matrix(a.row, b.col); (int = 0; < a.row; i++) { (int j = 0; j < b.col; j++) { double sum = 0; (int k = 0; k < b.row; k++) sum += a.matrix[i, k] * b.matrix[k, j]; r.matrix[i, j] = sum; } } return r; } static public matrix invert(matrix a) { matrix e = new matrix(a.row, a.col); double temp = 0; int n = a.row; (int = 0; < n; i++) (int j = 0; j < n; j++) { e.matrix[i, j] = 0.0; if (i == j) e.matrix[i, j] = 1.0; } (int k = 0; k < n; k++) { temp = a.matrix[k, k]; (int j = 0; j < n; j++) { a.matrix[k, j] /= temp; e.matrix[k, j] /= temp; } (int = k + 1; < n; i++) { temp = a.matrix[i, k]; (int j = 0; j < n; j++) { a.matrix[i, j] -= a.matrix[k, j] * temp; e.matrix[i, j] -= e.matrix[k, j] * temp; } } } (int k = n - 1; k > 0; k--) { (int = k - 1; >= 0; i--) { temp = a.matrix[i, k]; (int j = 0; j < n; j++) { a.matrix[i, j] -= a.matrix[k, j] * temp; e.matrix[i, j] -= e.matrix[k, j] * temp; } } } (int = 0; < n; i++) (int j = 0; j < n; j++) { a.matrix[i, j] = e.matrix[i, j]; } return a; } }
in example, determinant of x * transpose(x) zero. result, there no inverse, why you're getting strange results.
i note inverse
function modifies matrix passed it. should modified avoid that.
Comments
Post a Comment