/// <summary>QR Decomposition, computed by Householder reflections.</summary> /// <param name="A"> Rectangular matrix /// </param> /// <returns> Structure to access R and the Householder vectors and compute Q. /// </returns> public QRDecomposition(GeneralMatrix A) { // Initialize. QR = A.ArrayCopy; m = A.RowDimension; n = A.ColumnDimension; Rdiag = new double[n]; // Main loop. for (int k = 0; k < n; k++) { // Compute 2-norm of k-th column without under/overflow. double nrm = 0; for (int i = k; i < m; i++) { nrm = Maths.Hypot(nrm, QR[i][k]); } if (nrm != 0.0) { // Form k-th Householder vector. if (QR[k][k] < 0) { nrm = - nrm; } for (int i = k; i < m; i++) { QR[i][k] /= nrm; } QR[k][k] += 1.0; // Apply transformation to remaining columns. for (int j = k + 1; j < n; j++) { double s = 0.0; for (int i = k; i < m; i++) { s += QR[i][k] * QR[i][j]; } s = (- s) / QR[k][k]; for (int i = k; i < m; i++) { QR[i][j] += s * QR[i][k]; } } } Rdiag[k] = - nrm; } }
/// <summary>A = A - B</summary> /// <param name="B"> another matrix /// </param> /// <returns> A - B /// </returns> public virtual GeneralMatrix SubtractEquals(GeneralMatrix B) { CheckMatrixDimensions(B); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { A[i][j] = A[i][j] - B.A[i][j]; } } return this; }
/// <summary>Linear algebraic matrix multiplication, A * B</summary> /// <param name="B"> another matrix /// </param> /// <returns> Matrix product, A * B /// </returns> /// <exception cref="System.ArgumentException"> Matrix inner dimensions must agree. /// </exception> public virtual GeneralMatrix Multiply(GeneralMatrix B) { if (B.m != n) { throw new System.ArgumentException("GeneralMatrix inner dimensions must agree."); } GeneralMatrix X = new GeneralMatrix(m, B.n); double[][] C = X.Array; double[] Bcolj = new double[n]; for (int j = 0; j < B.n; j++) { for (int k = 0; k < n; k++) { Bcolj[k] = B.A[k][j]; } for (int i = 0; i < m; i++) { double[] Arowi = A[i]; double s = 0; for (int k = 0; k < n; k++) { s += Arowi[k] * Bcolj[k]; } C[i][j] = s; } } return X; }
/// <summary>Least squares solution of A*X = B</summary> /// <param name="B"> A Matrix with as many rows as A and any number of columns. /// </param> /// <returns> X that minimizes the two norm of Q*R*X-B. /// </returns> /// <exception cref="System.ArgumentException"> Matrix row dimensions must agree. /// </exception> /// <exception cref="System.SystemException"> Matrix is rank deficient. /// </exception> public virtual GeneralMatrix Solve(GeneralMatrix B) { if (B.RowDimension != m) { throw new System.ArgumentException("GeneralMatrix row dimensions must agree."); } if (!this.FullRank) { throw new System.SystemException("Matrix is rank deficient."); } // Copy right hand side int nx = B.ColumnDimension; double[][] X = B.ArrayCopy; // Compute Y = transpose(Q)*B for (int k = 0; k < n; k++) { for (int j = 0; j < nx; j++) { double s = 0.0; for (int i = k; i < m; i++) { s += QR[i][k] * X[i][j]; } s = (- s) / QR[k][k]; for (int i = k; i < m; i++) { X[i][j] += s * QR[i][k]; } } } // Solve R*X = Y; for (int k = n - 1; k >= 0; k--) { for (int j = 0; j < nx; j++) { X[k][j] /= Rdiag[k]; } for (int i = 0; i < k; i++) { for (int j = 0; j < nx; j++) { X[i][j] -= X[k][j] * QR[i][k]; } } } return (new GeneralMatrix(X, n, nx).GetMatrix(0, n - 1, 0, nx - 1)); }
/// <summary>LU Decomposition</summary> /// <param name="A"> Rectangular matrix /// </param> /// <returns> Structure to access L, U and piv. /// </returns> public LUDecomposition(GeneralMatrix A) { // Use a "left-looking", dot-product, Crout/Doolittle algorithm. LU = A.ArrayCopy; m = A.RowDimension; n = A.ColumnDimension; piv = new int[m]; for (int i = 0; i < m; i++) { piv[i] = i; } pivsign = 1; double[] LUrowi; double[] LUcolj = new double[m]; // Outer loop. for (int j = 0; j < n; j++) { // Make a copy of the j-th column to localize references. for (int i = 0; i < m; i++) { LUcolj[i] = LU[i][j]; } // Apply previous transformations. for (int i = 0; i < m; i++) { LUrowi = LU[i]; // Most of the time is spent in the following dot product. int kmax = System.Math.Min(i, j); double s = 0.0; for (int k = 0; k < kmax; k++) { s += LUrowi[k] * LUcolj[k]; } LUrowi[j] = LUcolj[i] -= s; } // Find pivot and exchange if necessary. int p = j; for (int i = j + 1; i < m; i++) { if (System.Math.Abs(LUcolj[i]) > System.Math.Abs(LUcolj[p])) { p = i; } } if (p != j) { for (int k = 0; k < n; k++) { double t = LU[p][k]; LU[p][k] = LU[j][k]; LU[j][k] = t; } int k2 = piv[p]; piv[p] = piv[j]; piv[j] = k2; pivsign = - pivsign; } // Compute multipliers. if (j < m & LU[j][j] != 0.0) { for (int i = j + 1; i < m; i++) { LU[i][j] /= LU[j][j]; } } } }
/// <summary>Check for symmetry, then construct the eigenvalue decomposition</summary> /// <param name="Arg"> Square matrix /// </param> /// <returns> Structure to access D and V. /// </returns> public void EigenvalueDecomposition(GeneralMatrix Arg) { double[][] A = Arg.Array; n = Arg.ColumnDimension; V = new double[n][]; for (int i = 0; i < n; i++) { V[i] = new double[n]; } d = new double[n]; e = new double[n]; issymmetric = true; for (int j = 0; (j < n) & issymmetric; j++) { for (int i = 0; (i < n) & issymmetric; i++) { issymmetric = (A[i][j] == A[j][i]); } } if (issymmetric) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { V[i][j] = A[i][j]; } } // Tridiagonalize. tred2(); // Diagonalize. tql2(); } else { H = new double[n][]; for (int i2 = 0; i2 < n; i2++) { H[i2] = new double[n]; } ort = new double[n]; for (int j = 0; j < n; j++) { for (int i = 0; i < n; i++) { H[i][j] = A[i][j]; } } // Reduce to Hessenberg form. orthes(); // Reduce Hessenberg to real Schur form. hqr2(); } }
/// <summary>Solve A*X = B</summary> /// <param name="B"> right hand side /// </param> /// <returns> solution if A is square, least squares solution otherwise /// </returns> public virtual GeneralMatrix Solve(GeneralMatrix B) { return(m == n ? (new LUDecomposition(this)).Solve(B):(new QRDecomposition(this)).Solve(B)); }
/// <summary>Element-by-element left division in place, A = A.\B</summary> /// <param name="B"> another matrix /// </param> /// <returns> A.\B /// </returns> public virtual GeneralMatrix ArrayLeftDivideEquals(GeneralMatrix B) { CheckMatrixDimensions(B); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { A[i][j] = B.A[i][j] / A[i][j]; } } return this; }
/// <summary>Construct a matrix from a copy of a 2-D array.</summary> /// <param name="A"> Two-dimensional array of doubles. /// </param> /// <exception cref="System.ArgumentException"> All rows must have the same length /// </exception> public static GeneralMatrix Create(double[][] A) { int m = A.Length; int n = A[0].Length; GeneralMatrix X = new GeneralMatrix(m, n); double[][] C = X.Array; for (int i = 0; i < m; i++) { if (A[i].Length != n) { throw new System.ArgumentException("All rows must have the same length."); } for (int j = 0; j < n; j++) { C[i][j] = A[i][j]; } } return X; }
/// <summary>Check if size(A) == size(B) *</summary> private void CheckMatrixDimensions(GeneralMatrix B) { if (B.m != m || B.n != n) { throw new System.ArgumentException("GeneralMatrix dimensions must agree."); } }
/// <summary>Generate identity matrix</summary> /// <param name="m"> Number of rows. /// </param> /// <param name="n"> Number of colums. /// </param> /// <returns> An m-by-n matrix with ones on the diagonal and zeros elsewhere. /// </returns> public static GeneralMatrix Identity(int m, int n) { GeneralMatrix A = new GeneralMatrix(m, n); double[][] X = A.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { X[i][j] = (i == j ? 1.0 : 0.0); } } return A; }
/// <summary>Generate matrix with random elements</summary> /// <param name="m"> Number of rows. /// </param> /// <param name="n"> Number of colums. /// </param> /// <returns> An m-by-n matrix with uniformly distributed random elements. /// </returns> public static GeneralMatrix Random(int m, int n) { System.Random random = new System.Random(); GeneralMatrix A = new GeneralMatrix(m, n); double[][] X = A.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { X[i][j] = random.NextDouble(); } } return A; }
/// <summary>Solve X*A = B, which is also A'*X' = B'</summary> /// <param name="B"> right hand side /// </param> /// <returns> solution if A is square, least squares solution otherwise. /// </returns> public virtual GeneralMatrix SolveTranspose(GeneralMatrix B) { return Transpose().Solve(B.Transpose()); }
/// <summary>Solve A*X = B</summary> /// <param name="B"> right hand side /// </param> /// <returns> solution if A is square, least squares solution otherwise /// </returns> public virtual GeneralMatrix Solve(GeneralMatrix B) { return (m == n ? (new LUDecomposition(this)).Solve(B):(new QRDecomposition(this)).Solve(B)); }
/// <summary>Element-by-element multiplication in place, A = A.*B</summary> /// <param name="B"> another matrix /// </param> /// <returns> A.*B /// </returns> public virtual GeneralMatrix ArrayMultiplyEquals(GeneralMatrix B) { CheckMatrixDimensions(B); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { A[i][j] = A[i][j] * B.A[i][j]; } } return this; }
/// <summary>Element-by-element left division, C = A.\B</summary> /// <param name="B"> another matrix /// </param> /// <returns> A.\B /// </returns> public virtual GeneralMatrix ArrayLeftDivide(GeneralMatrix B) { CheckMatrixDimensions(B); GeneralMatrix X = new GeneralMatrix(m, n); double[][] C = X.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { C[i][j] = B.A[i][j] / A[i][j]; } } return X; }
/// <summary>Get a submatrix.</summary> /// <param name="i0"> Initial row index /// </param> /// <param name="i1"> Final row index /// </param> /// <param name="c"> Array of column indices. /// </param> /// <returns> A(i0:i1,c(:)) /// </returns> /// <exception cref="System.IndexOutOfRangeException"> Submatrix indices /// </exception> public virtual GeneralMatrix GetMatrix(int i0, int i1, int[] c) { GeneralMatrix X = new GeneralMatrix(i1 - i0 + 1, c.Length); double[][] B = X.Array; try { for (int i = i0; i <= i1; i++) { for (int j = 0; j < c.Length; j++) { B[i - i0][j] = A[i][c[j]]; } } } catch (System.IndexOutOfRangeException e) { throw new System.IndexOutOfRangeException("Submatrix indices", e); } return X; }
/// <summary>Multiply a matrix by a scalar, C = s*A</summary> /// <param name="s"> scalar /// </param> /// <returns> s*A /// </returns> public virtual GeneralMatrix Multiply(double s) { GeneralMatrix X = new GeneralMatrix(m, n); double[][] C = X.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { C[i][j] = s * A[i][j]; } } return X; }
/// <summary>Get a submatrix.</summary> /// <param name="r"> Array of row indices. /// </param> /// <param name="j0"> Initial column index /// </param> /// <param name="j1"> Final column index /// </param> /// <returns> A(r(:),j0:j1) /// </returns> /// <exception cref="System.IndexOutOfRangeException"> Submatrix indices /// </exception> public virtual GeneralMatrix GetMatrix(int[] r, int j0, int j1) { GeneralMatrix X = new GeneralMatrix(r.Length, j1 - j0 + 1); double[][] B = X.Array; try { for (int i = 0; i < r.Length; i++) { for (int j = j0; j <= j1; j++) { B[i][j - j0] = A[r[i]][j]; } } } catch (System.IndexOutOfRangeException e) { throw new System.IndexOutOfRangeException("Submatrix indices", e); } return X; }
/// <summary>Solve X*A = B, which is also A'*X' = B'</summary> /// <param name="B"> right hand side /// </param> /// <returns> solution if A is square, least squares solution otherwise. /// </returns> public virtual GeneralMatrix SolveTranspose(GeneralMatrix B) { return(Transpose().Solve(B.Transpose())); }
/// <summary>Set a submatrix.</summary> /// <param name="r"> Array of row indices. /// </param> /// <param name="j0"> Initial column index /// </param> /// <param name="j1"> Final column index /// </param> /// <param name="X"> A(r(:),j0:j1) /// </param> /// <exception cref="System.IndexOutOfRangeException"> Submatrix indices /// </exception> public virtual void SetMatrix(int[] r, int j0, int j1, GeneralMatrix X) { try { for (int i = 0; i < r.Length; i++) { for (int j = j0; j <= j1; j++) { A[r[i]][j] = X.GetElement(i, j - j0); } } } catch (System.IndexOutOfRangeException e) { throw new System.IndexOutOfRangeException("Submatrix indices", e); } }
/// <summary>Solve A*X = B</summary> /// <param name="B"> A Matrix with as many rows as A and any number of columns. /// </param> /// <returns> X so that L*U*X = B(piv,:) /// </returns> /// <exception cref="System.ArgumentException"> Matrix row dimensions must agree. /// </exception> /// <exception cref="System.SystemException"> Matrix is singular. /// </exception> public virtual GeneralMatrix Solve(GeneralMatrix B) { if (B.RowDimension != m) { throw new System.ArgumentException("Matrix row dimensions must agree."); } if (!this.IsNonSingular) { throw new System.SystemException("Matrix is singular."); } // Copy right hand side with pivoting int nx = B.ColumnDimension; GeneralMatrix Xmat = B.GetMatrix(piv, 0, nx - 1); double[][] X = Xmat.Array; // Solve L*Y = B(piv,:) for (int k = 0; k < n; k++) { for (int i = k + 1; i < n; i++) { for (int j = 0; j < nx; j++) { X[i][j] -= X[k][j] * LU[i][k]; } } } // Solve U*X = Y; for (int k = n - 1; k >= 0; k--) { for (int j = 0; j < nx; j++) { X[k][j] /= LU[k][k]; } for (int i = 0; i < k; i++) { for (int j = 0; j < nx; j++) { X[i][j] -= X[k][j] * LU[i][k]; } } } return Xmat; }
/// <summary>Set a submatrix.</summary> /// <param name="i0"> Initial row index /// </param> /// <param name="i1"> Final row index /// </param> /// <param name="c"> Array of column indices. /// </param> /// <param name="X"> A(i0:i1,c(:)) /// </param> /// <exception cref="System.IndexOutOfRangeException"> Submatrix indices /// </exception> public virtual void SetMatrix(int i0, int i1, int[] c, GeneralMatrix X) { try { for (int i = i0; i <= i1; i++) { for (int j = 0; j < c.Length; j++) { A[i][c[j]] = X.GetElement(i - i0, j); } } } catch (System.IndexOutOfRangeException e) { throw new System.IndexOutOfRangeException("Submatrix indices", e); } }
public void covariance() { covMatrix = new double[2][]; for (int i = 0; i < 2; i++) covMatrix[i] = new double[2]; V = new double[2][]; for (int i = 0; i < 2; i++) V[i] = new double[2]; d = new double[2]; // //covMatrix[0][0] = 0; // //covMatrix[0][1] = 0; // //covMatrix[1][0] = 0; // //covMatrix[1][1] = 0; for (int i = 0; i < totalCount; i++) { covMatrix[0][0] += (intermediatePoints[i].X - meanX) * (intermediatePoints[i].X - meanX); covMatrix[0][1] += (intermediatePoints[i].X - meanX) * (intermediatePoints[i].Y - meanY); covMatrix[1][0] = covMatrix[0][1]; covMatrix[1][1] += (intermediatePoints[i].Y - meanY) * (intermediatePoints[i].Y - meanY); } covMatrix[0][0] = covMatrix[0][0] / (totalCount - 1); covMatrix[0][1] = covMatrix[0][1] / (totalCount - 1); covMatrix[1][0] = covMatrix[1][0] / (totalCount - 1); covMatrix[1][1] = covMatrix[1][1] / (totalCount - 1); // Console.WriteLine(" testing covariance matrix: {0} {1} {2} {3}", covMatrix[0][0], covMatrix[0][1], covMatrix[1][0], covMatrix[1][1]); // Console.WriteLine(" matrix testing : {0}", covMatrix); //yeta baata eigenvalue ra eigenvector niskincha GeneralMatrix generalMatrix = new GeneralMatrix(covMatrix); EigenvalueDecomposition eigenvalueDecomposition = new EigenvalueDecomposition(generalMatrix); d = eigenvalueDecomposition.getD(); // get eigen values V = eigenvalueDecomposition.getV(); // get eigen vectors //for (int i = 0; i < 2; i++) //Console.WriteLine("eigenvalue: " + d[i]); //for (int i = 0; i < 2; i++) //{ // for (int j = 0; j < 2; j++) // Console.WriteLine("eigenvector " + V[i][j]); //} }
/// <summary>Matrix transpose.</summary> /// <returns> A' /// </returns> public virtual GeneralMatrix Transpose() { GeneralMatrix X = new GeneralMatrix(n, m); double[][] C = X.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { C[j][i] = A[i][j]; } } return X; }
/// <summary>Unary minus</summary> /// <returns> -A /// </returns> public virtual GeneralMatrix UnaryMinus() { GeneralMatrix X = new GeneralMatrix(m, n); double[][] C = X.Array; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { C[i][j] = -A[i][j]; } } return X; }
/// <summary>LU Decomposition</summary> /// <param name="A"> Rectangular matrix /// </param> /// <returns> Structure to access L, U and piv. /// </returns> public LUDecomposition(GeneralMatrix A) { // Use a "left-looking", dot-product, Crout/Doolittle algorithm. LU = A.ArrayCopy; m = A.RowDimension; n = A.ColumnDimension; piv = new int[m]; for (int i = 0; i < m; i++) { piv[i] = i; } pivsign = 1; double[] LUrowi; double[] LUcolj = new double[m]; // Outer loop. for (int j = 0; j < n; j++) { // Make a copy of the j-th column to localize references. for (int i = 0; i < m; i++) { LUcolj[i] = LU[i][j]; } // Apply previous transformations. for (int i = 0; i < m; i++) { LUrowi = LU[i]; // Most of the time is spent in the following dot product. int kmax = System.Math.Min(i, j); double s = 0.0; for (int k = 0; k < kmax; k++) { s += LUrowi[k] * LUcolj[k]; } LUrowi[j] = LUcolj[i] -= s; } // Find pivot and exchange if necessary. int p = j; for (int i = j + 1; i < m; i++) { if (System.Math.Abs(LUcolj[i]) > System.Math.Abs(LUcolj[p])) { p = i; } } if (p != j) { for (int k = 0; k < n; k++) { double t = LU[p][k]; LU[p][k] = LU[j][k]; LU[j][k] = t; } int k2 = piv[p]; piv[p] = piv[j]; piv[j] = k2; pivsign = -pivsign; } // Compute multipliers. if (j < m & LU[j][j] != 0.0) { for (int i = j + 1; i < m; i++) { LU[i][j] /= LU[j][j]; } } } }