private void buttonClearAll_Click(object sender, EventArgs e) { MainMatrix = null; dataGridViewMatrix.Columns.Clear(); b = null; dataGridViewVectorb.Columns.Clear(); }
public static Vector operator *(Vector A, double Scalar) { Vector B = new Vector(A.Elements.Length); for (int i = 0; i < A.Elements.Length; ++i) { B[i] = A[i] * Scalar; } return B; }
public static MMatrix CalculateArgumentMatrix(MMatrix CoefficientMatrix, Vector b) { //Form Argument matrix from CoefficientMatrix and b MMatrix ArgumentMatrix = new MMatrix(CoefficientMatrix.row, CoefficientMatrix.col + 1); for (int i = 0; i < CoefficientMatrix.row; ++i) for (int j = 0; j < CoefficientMatrix.col; ++j) ArgumentMatrix[i, j] = CoefficientMatrix[i, j]; for (int i = 0; i < CoefficientMatrix.row; ++i) ArgumentMatrix[i, ArgumentMatrix.col - 1] = b[i]; return ArgumentMatrix; }
public static string PrintSystemOfEquations(MMatrix CoefficientMatrix, Vector b) { StringBuilder sb = new StringBuilder(""); for (int i = 0; i < CoefficientMatrix.row; ++i) { for (int j = 0; j < CoefficientMatrix.col; ++j) { string sign = (CoefficientMatrix[i, j] >= 0) ? "+" : ""; sb.Append(sign); sb.Append(CoefficientMatrix[i, j]); sb.Append(" X"); sb.Append(j + 1); sb.Append("\t\t"); } sb.Append(" = "); sb.Append(b[i]); sb.Append("\r\n"); } return sb.ToString(); }
private void button1_MouseClick(object sender, MouseEventArgs e) { ReadFromFile(); b = new DenseVector(n, n); b = A.Multiply(s); // Punctul 1 Giv_R = new DenseMatrix(n, n); Giv_Q = new DenseMatrix(n, n); //Givens Givens_Decomposition(A,b); // Punctul 2 dnAnalytics.LinearAlgebra.Decomposition.IQR t; t = new Householder(A); textBox1.AppendText("\r\nR\r\n\r\n"); print_matrix(t.R()); textBox1.AppendText("\r\nR\r\n\r\n"); print_matrix(Giv_R); textBox1.AppendText("Q\r\n\r\n"); print_matrix(t.Q()); textBox1.AppendText("Q\r\n\r\n"); print_matrix(Giv_Q); }
public static double DotProduct(Vector A, Vector B) { return A*B; }
public static double L2Norm(Vector A) { return Math.Sqrt(A*A); }
public static double SpectralRadius(MMatrix A) { Vector Eigenvalues = new Vector(); if (!A.IsSquared()) throw new MMatrixException("Matrix must be squared."); Eigenvalues = A.QRIterationBasic(GlobalMath.MAXITER).DiagVector(); Eigenvalues.Sort(true); return Math.Abs(Eigenvalues[0]); }
public Vector DiagVector() { if (!this.IsSquared()) throw new MMatrixException("Cannot get diagonal of a non-square matrix."); Vector v = new Vector(this.col); for (int i = 0; i < this.col; ++i) { v[i] = this[i, i]; } return v; }
public static void Sort(Vector A, bool bDescending) { Array.Sort(A.Elements); if (bDescending) //Sort_Descending(A); Array.Reverse(A.Elements); //else //Sort_Ascending(A); }
public Vector Orthonormalize(Vector OrthonormalVector) { return Orthonormalize(this,OrthonormalVector); }
//Orthonormalize all eigenvectors based on the first one public static MMatrix Orthonormalize(MMatrix Eigenvectors) { MMatrix A = new MMatrix(Eigenvectors); Vector v1 = new Vector(Eigenvectors.row); Vector v = new Vector(Eigenvectors.row); for (int i = 0; i < v1.Elements.Length; ++i) v1[i] = A[i, 0]; v1 = v1.Normalize(); for (int i = 0; i < v1.Elements.Length; ++i) A[i, 0] = v1[i]; for (int i = 1; i < Eigenvectors.col; ++i) { for (int j = 0; j < Eigenvectors.row; ++j) v[j] = Eigenvectors[j,i]; v = v.Orthonormalize(v1); for (int j = 0; j < v.Elements.Length; ++j) A[j, i] = v[j]; } return A; }
public static Vector Orthonormalize(Vector A, Vector OrthonormalVector) { Vector B = new Vector(); B = A - OrthonormalVector * A * OrthonormalVector; B = B.Normalize(); return B; }
//A = U*S*Vt //This method is not right public static void FactorSVD(MMatrix A) { MMatrix B = new MMatrix(); Vector Eigenvalues = new Vector(); MMatrix Eigenvectors = new MMatrix(); int index; //U B = A * A.Transpose(); B.Jacobi_Cyclic_Method(ref Eigenvalues,ref Eigenvectors); SortEigen(ref Eigenvalues,ref Eigenvectors); A.SVD_U = Eigenvectors.MArray; //S A.SVD_S = new double[A.row, A.col]; index = (A.row < A.col) ? (A.row) : (A.col); for (int i = 0; i < index; ++i) A.SVD_S[i, i] = Math.Sqrt(Eigenvalues[i]); //V B = B.Transpose(); B.Jacobi_Cyclic_Method(ref Eigenvalues,ref Eigenvectors); SortEigen(ref Eigenvalues,ref Eigenvectors); A.SVD_Vt = Eigenvectors.Transpose().MArray; }
//A = U*S*Vt //A*At = U*S^2*Ut //At*A = V*S^2*Vt //A*At = (At*A)t //This method is not right public static void FactorSVD_LDLt(MMatrix A) { MMatrix AA = new MMatrix(); MMatrix Eigenvectors = new MMatrix(); Vector Eigenvalues = new Vector(); int index = A.row; if (A.row > A.col) index = A.col; AA = A * A.Transpose(); AA.FactorLDLt(); Eigenvectors = new MMatrix(AA.LDL_L); Eigenvalues = new Vector(AA.row); for (int i = 0; i < index; ++i) Eigenvalues[i] = AA.LDL_D[i, i]; SortEigen(ref Eigenvalues, ref Eigenvectors); A.SVD_U = Eigenvectors.MArray; if (A.SVD_S == null) A.SVD_S = new double[A.row, A.col]; for (int i = 0; i < index; ++i) A.SVD_S[i, i] = Math.Sqrt(Eigenvalues[i]); AA = AA.Transpose(); AA.FactorLDLt(); Eigenvectors = new MMatrix(AA.LDL_L); Eigenvalues = new Vector(AA.row); for (int i = 0; i < index; ++i) Eigenvalues[i] = AA.LDL_D[i, i]; SortEigen(ref Eigenvalues, ref Eigenvectors); A.SVD_Vt = Eigenvectors.Transpose().MArray; }
//Find eigenvalues, eigenvectors (each column is a vector) private static void Jacobi_Cyclic_Method(ref Vector eigenvalues, ref MMatrix eigenvectors, MMatrix A, int n) { int i, j, k, m; MMatrix pAk, pAm, p_r, p_e; double threshold_norm; double threshold; double tan_phi, sin_phi, cos_phi, tan2_phi, sin2_phi, cos2_phi; double sin_2phi, cos_2phi, cot_2phi; double dum1; double dum2; double dum3; double max; eigenvalues = new Vector(n); eigenvectors = DiagonalMatrix(n, 1); pAk = new MMatrix(); pAm = new MMatrix(); p_r = new MMatrix(); p_e = eigenvectors; // Take care of trivial cases if (n < 1) return; if (n == 1) { eigenvalues[0] = A[0, 0]; eigenvectors[0, 0] = 1.0; return; } // Initialize the eigenvalues to the identity matrix. /*for (p_e = eigenvectors, i = 0; i < n; ++i) for (j = 0; j < n; ++j) p_e[i, j] = (i == j) ? 1.0 : 0.0; */ // Calculate the threshold and threshold_norm. for (threshold = 0.0, pAk = A, i = 0; i < (n - 1); ++i) for (j = i + 1; j < n; ++j) threshold += pAk[i, j] * pAk[i, j]; threshold = Math.Sqrt(threshold + threshold); threshold_norm = threshold * GlobalMath.EPSILON; max = threshold + 1.0; while (threshold > threshold_norm) { threshold /= 10.0; if (max < threshold) continue; max = 0.0; pAk = A; pAm = pAk; for (k = 0; k < (n - 1); ++k) { for (m = k + 1; m < n; ++m) { if (Math.Abs(pAk[k, m]) < threshold) continue; // Calculate the sin and cos of the rotation angle which annihilates A[k][m]. cot_2phi = 0.5 * (pAk[k, k] - pAm[m, m]) / pAk[k, m]; dum1 = Math.Sqrt(cot_2phi * cot_2phi + 1.0); if (cot_2phi < 0.0) dum1 = -dum1; tan_phi = -cot_2phi + dum1; tan2_phi = tan_phi * tan_phi; sin2_phi = tan2_phi / (1.0 + tan2_phi); cos2_phi = 1.0 - sin2_phi; sin_phi = Math.Sqrt(sin2_phi); if (tan_phi < 0.0) sin_phi = -sin_phi; cos_phi = Math.Sqrt(cos2_phi); sin_2phi = 2.0 * sin_phi * cos_phi; cos_2phi = cos2_phi - sin2_phi; // Rotate columns k and m for both the matrix A and the matrix of eigenvectors. p_r = A; dum1 = pAk[k, k]; dum2 = pAm[m, m]; dum3 = pAk[k, m]; pAk[k, k] = dum1 * cos2_phi + dum2 * sin2_phi + dum3 * sin_2phi; pAm[m, m] = dum1 * sin2_phi + dum2 * cos2_phi - dum3 * sin_2phi; pAk[k, m] = 0.0; pAm[m, k] = 0.0; for (i = 0; i < n; ++i) { if ((i == k) || (i == m)) continue; if (i < k) dum1 = p_r[i, k]; else dum1 = pAk[k, i]; if (i < m) dum2 = p_r[i, m]; else dum2 = pAm[m, i]; dum3 = dum1 * cos_phi + dum2 * sin_phi; if (i < k) p_r[i, k] = dum3; else pAk[k, i] = dum3; dum3 = -dum1 * sin_phi + dum2 * cos_phi; if (i < m) p_r[i, m] = dum3; else pAm[m, i] = dum3; } for (p_e = eigenvectors, i = 0; i < n; ++i) { dum1 = p_e[i, k]; dum2 = p_e[i, m]; p_e[i, k] = dum1 * cos_phi + dum2 * sin_phi; p_e[i, m] = -dum1 * sin_phi + dum2 * cos_phi; } } for (i = 0; i < n; ++i) if (i == k) continue; else if (max < Math.Abs(pAk[k, i])) max = Math.Abs(pAk[k, i]); } } for (pAk = A, k = 0; k < n; ++k) eigenvalues[k] = pAk[k, k]; }
public void Jacobi_Cyclic_Method(ref Vector eigenvalues, ref MMatrix eigenvectors) { if (!this.IsSquared()) throw new MMatrixException("Matrix must be squared."); Jacobi_Cyclic_Method(ref eigenvalues, ref eigenvectors, this, this.row); }
//Jacobi Cyclic Method is not correct enough public Vector Eigenvalues_JacobiCyclic() { Vector Eigenvalues = new Vector(); MMatrix Eigenvectors = new MMatrix(); MMatrix temp = this.Clone(); if (!this.IsSquared()) throw new MMatrixException("Matrix must be squared."); temp.Jacobi_Cyclic_Method(ref Eigenvalues, ref Eigenvectors); return Eigenvalues; }
public static double LInfinitiveNorm(Vector A) { Vector B = A.Abs(); double Max = B[0]; for (int j = 1; j < B.Elements.Length; ++j) if (Max < B[j]) Max = B[j]; return Max; }
public void EigenvectorTest() { double[,] Arr1 = new double[4, 4] { { 4, 1, -2, 2 }, { 1, 2, 0, 1 }, { -2, 0, 3, -2 }, { 2, 1, -2, -1 } }; double[,] Arr2 = new double[2, 3] { { 3,1,1 }, { -1, 3,1 }}; double[,] Arr3 = new double[5, 5] { { 2, 0, 8, 6, 0 }, { 1, 6, 0, 1, 7 }, { 5, 0, 7, 4, 0 }, { 7, 0, 8, 5, 0 }, { 0, 10, 0, 0, 7 } }; double[,] Arr4 = new double[4, 4] { { 3, 1, 0, 1 }, { 1, 3, 1, 1 }, { 0, 1, 3, 1 }, { 1, 1, 1, 3 } }; MMatrix A = new MMatrix(Arr2); MMatrix B = new MMatrix(); string s = ""; Vector v = new Vector(); MMatrix Eigenvectors = new MMatrix(); try { //A = MMatrix.RandomMatrix(3,0,255); B = A*A.Transpose(); s += "\nAA*=\n" + B.PrintToString(); B.Jacobi_Cyclic_Method(ref v, ref Eigenvectors); s += "\nBefore sort:"; s += "\nEigenvalues of(A*At)=" + v.PrintToString(); s += "\nEigenvectors of A*At:\n" + Eigenvectors.PrintToString(); MMatrix.SortEigen(ref v, ref Eigenvectors); s += "\nAfter sort:"; s += "\nEigenvalues of(A*At)=" + v.PrintToString(); s += "\nEigenvectors of A*At:\n" + Eigenvectors.PrintToString(); //This is not neccesary anymore since the eigenvectors are already orthonomalized s += "\nOrthonormalize eigenvectors(Ax = 0)\n" + MMatrix.Orthonormalize(Eigenvectors).PrintToString(); this.RichText_Display(s); } catch (MMatrixException exp) { MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
public static Vector Normalize(Vector A) { return A / L2Norm(A); }
private void eigenvectorsToolStripMenuItem_Click(object sender, EventArgs e) { try { StringBuilder sb = new StringBuilder(""); Vector eigenvalues = new Vector(); MMatrix eigenvectors = new MMatrix(); if (OutputMatrix != null) { MMatrix temp = OutputMatrix.Clone(); temp.Jacobi_Cyclic_Method(ref eigenvalues, ref eigenvectors); sb.Append("\n\n\nEigenvectors (A) = \n"); for (int i = 0; i < eigenvectors.col; ++i) { sb.Append("V"); sb.Append(i + 1); sb.Append("\t\t"); } sb.Append("\n"); sb.Append(eigenvectors.PrintToString()); RichText_Display(sb.ToString()); } } catch (MMatrixException exp) { MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
public static Vector RandomVector(int NumElements, int minvalue, int maxvalue) { Vector A = new Vector(NumElements); Random ranobj = new Random(); for (int i = 0; i < NumElements; ++i) A[i] = ranobj.Next(minvalue, maxvalue); return A; }
public static double SpectralRadius_Jacobi(MMatrix A) { Vector Eigenvalues = new Vector(); MMatrix Eigenvectors = new MMatrix(); MMatrix temp = A.Clone(); temp.Jacobi_Cyclic_Method(ref Eigenvalues, ref Eigenvectors); Eigenvalues.Sort(true); return Math.Abs(Eigenvalues[0]); }
public static void Sort_Descending(Vector A) { int m = A.Elements.Length; double temp; for (int i = 0; i < m; ++i) for (int j = i + 1; j < m; ++j) { if (A[i] < A[j]) { temp = A[i]; A[i] = A[j]; A[j] = temp; } } }
private void buttonGenerateMatrix_Click(object sender, EventArgs e) { Random rand = new Random(); int rows = (int)numericUpDownRow.Value; int cols = (int)numericUpDownCol.Value; min = (int)numericUpDownMin.Value; max = (int)numericUpDownMax.Value; if (radioButtonRandom.Checked) MainMatrix = MMatrix.RandomMatrix(rows, cols, min, max); else if (radioButtonDiagonal.Checked) { MainMatrix = MMatrix.DiagonalMatrix(rows, rand.Next(max)); cols = rows; } else if (radioButtonIdentity.Checked) { MainMatrix = MMatrix.DiagonalMatrix(rows, 1); cols = rows; } else if (radioButtonSymetric.Checked) { MainMatrix = MMatrix.RandomMatrix(rows, cols, min, (int)Math.Sqrt(max)); MainMatrix = MainMatrix * MainMatrix.Transpose(); cols = rows; } //Matrix A this.dataGridViewMatrix.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing; dataGridViewMatrix.SuspendLayout(); dataGridViewMatrix.Columns.Clear(); DataGridViewColumn[] DCS = new DataGridViewColumn[cols]; for (int i = 0; i < cols; ++i) { DataGridViewColumn column = new DataGridViewTextBoxColumn(); column.HeaderText = string.Concat("A", (i + 1).ToString()); DCS[i] = column; } this.dataGridViewMatrix.Columns.AddRange(DCS); dataGridViewMatrix.Rows.Add(rows); //for DataGrid: [column, row] for (int j = 0; j < MainMatrix.col; ++j) for (int i = 0; i < MainMatrix.row; ++i) dataGridViewMatrix[j,i].Value = MainMatrix[i, j]; //this.dataGridViewMatrix.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells; dataGridViewMatrix.ResumeLayout(); numericUpDownCol.Value = cols; //Vector b b = Vector.RandomVector(rows, min, max); dataGridViewVectorb.SuspendLayout(); dataGridViewVectorb.Columns.Clear(); dataGridViewVectorb.Columns.Add("b","b"); dataGridViewVectorb.Columns[0].Width = ColWidth; dataGridViewVectorb.Rows.Add(rows); for (int i = 0; i < b.Elements.Length; ++i) dataGridViewVectorb[0, i].Value = b[i]; dataGridViewVectorb.ResumeLayout(); }
public Vector(Vector A) { Elements = (double[])A.Elements.Clone(); }
public static Vector Abs(Vector A) { Vector B = new Vector(A.Elements.Length); for (int i = 0; i < A.Elements.Length; ++i) B[i] = Math.Abs(A[i]); return B; }
public static Vector operator +(Vector A, Vector B) { if (A.Elements.Length != B.Elements.Length) throw new MMatrixException("Two vectors are different size."); Vector C = new Vector(A.Elements.Length); for (int i = 0; i < A.Elements.Length; ++i) { C[i] = A[i] + B[i]; } return C; }
public Vector Column(int j) { Vector v = new Vector(this.row); for (int i = 0; i < this.row; ++i) { v[i] = this[i, j]; } return v; }