/// <summary> /// Run example /// </summary> /// <seealso cref="http://en.wikipedia.org/wiki/Matrix_norm">Matrix norm</seealso> public void Run() { // Format matrix output to console var formatProvider = (CultureInfo)CultureInfo.InvariantCulture.Clone(); formatProvider.TextInfo.ListSeparator = " "; // Create square matrix var matrix = new DenseMatrix(new[,] { { 1.0, 2.0, 3.0 }, { 6.0, 5.0, 4.0 }, { 8.0, 9.0, 7.0 } }); Console.WriteLine(@"Initial square matrix"); Console.WriteLine(matrix.ToString("#0.00\t", formatProvider)); Console.WriteLine(); // 1. 1-norm of the matrix Console.WriteLine(@"1. 1-norm of the matrix"); Console.WriteLine(matrix.L1Norm()); Console.WriteLine(); // 2. 2-norm of the matrix Console.WriteLine(@"2. 2-norm of the matrix"); Console.WriteLine(matrix.L2Norm()); Console.WriteLine(); // 3. Frobenius norm of the matrix Console.WriteLine(@"3. Frobenius norm of the matrix"); Console.WriteLine(matrix.FrobeniusNorm()); Console.WriteLine(); // 4. Infinity norm of the matrix Console.WriteLine(@"4. Infinity norm of the matrix"); Console.WriteLine(matrix.InfinityNorm()); Console.WriteLine(); // 5. Normalize matrix columns Console.WriteLine(@"5. Normalize matrix columns: before normalize"); foreach (var keyValuePair in matrix.ColumnEnumerator()) { Console.WriteLine(@"Column {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2)); } Console.WriteLine(); var normalized = matrix.NormalizeColumns(2); Console.WriteLine(@"5. Normalize matrix columns: after normalize"); foreach (var keyValuePair in normalized.ColumnEnumerator()) { Console.WriteLine(@"Column {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2)); } Console.WriteLine(); // 6. Normalize matrix columns Console.WriteLine(@"6. Normalize matrix rows: before normalize"); foreach (var keyValuePair in matrix.RowEnumerator()) { Console.WriteLine(@"Row {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2)); } Console.WriteLine(); normalized = matrix.NormalizeRows(2); Console.WriteLine(@"6. Normalize matrix rows: after normalize"); foreach (var keyValuePair in normalized.RowEnumerator()) { Console.WriteLine(@"Row {0} 2-nd norm is: {1}", keyValuePair.Item1, keyValuePair.Item2.Norm(2)); } }
/// <summary> /// Adaptive Cross Approximation (ACA) matrix compression /// the result is stored in U and V matrices like U*V /// </summary> /// <param name="acaThres">Relative error threshold to stop adding rows and columns in ACA iteration</param> /// <param name="m">Row indices of Z submatrix to compress</param> /// <param name="n">Column indices of Z submatrix to compress</param> /// <param name="U">to store result</param> /// <param name="V">to store result</param> /// <returns>pair with matrix U and V</returns> public static Tuple<Matrix, Matrix> Aca(double acaThres, List<int> m, List<int> n, Matrix U, Matrix V) { int M = m.Count; int N = n.Count; int Min = Math.Min(M, N); U = new DenseMatrix(Min, Min); V = new DenseMatrix(Min, Min); //if Z is a vector, there is nothing to compress if (M == 1 || N == 1) { U = UserImpedance(m, n); V = new DenseMatrix(1, 1); V[0, 0] = 1.0; return new Tuple<Matrix,Matrix>(U,V); } //Indices of columns picked up from Z //Vector J = new DenseVector(N); //List<int> J = new List<int>(N); List<int> J = new List<int>(new int [N]); //int[] J = new int[N]; //Indices of rows picked up from Z //Vector I = new DenseVector(M); List<int> I = new List<int>(new int [M]); //int[] I = new int[M]; //Row indices to search for maximum in R //Vector i = new DenseVector(M); List<int> i = new List<int>(); //int[] i = new int[M]; //Column indices to search for maximum in R //Vector j = new DenseVector(N); List<int> j = new List<int>(); //int[] j = new int[N]; for (int k = 1; k < M; k++) { i.Add(k); } for (int k = 0; k < N; k++) { j.Add(k); } //Initialization //Initialize the 1st row index I(1) = 1 I[0] = 0; //Initialize the 1st row of the approximate error matrix List<int> m0 = new List<int>(); m0.Add(m[I[0]]); Matrix Rik = UserImpedance(m0, n); //Find the 1st column index J(0) double max = -1.0; int col = 0; foreach (int ind in j) { if (Math.Abs(Rik[0, ind]) > max) { max = Math.Abs(Rik[0, ind]); col = ind; } } //J[0] = j[col]; J[0] = col; j.Remove(J[0]); //First row of V V = new DenseMatrix(1, Rik.ColumnCount); V.SetRow(0, Rik.Row(0).Divide(Rik[0, J[0]])); //Initialize the 1st column of the approximate error matrix List<int> n0 = new List<int>(); n0.Add(n[J[0]]); Matrix Rjk = UserImpedance(m, n0); //First column of U U = new DenseMatrix(Rjk.RowCount, 1); U.SetColumn(0, Rjk.Column(0)); // Norm of (approximate) Z, to test error double d1 = U.L2Norm(); double d2 = V.L2Norm(); double normZ = d1 * d1 * d2 * d2; //Find 2nd row index I(2) int row = 0; max = -1.0; foreach (int ind in i) { if (Math.Abs(Rjk[ind, 0]) > max) { max = Math.Abs(Rjk[ind, 0]); row = ind; } } //I[1] = i[row]; I[1] = row; i.Remove(I[1]); //Iteration for (int k = 1; k < Math.Min(M, N); k++) { //Update (Ik)th row of the approximate error matrix: List<int> t1 = new List<int>(); t1.Add(m[I[k]]); Rik = (Matrix)(UserImpedance(t1, n) - U.SubMatrix(I[k], 1, 0, U.ColumnCount).Multiply(V)); //CHECKED OK all code before works fine //Find kth column index Jk max = -1.0; col = 0; foreach (int ind in j) { if (Math.Abs(Rik[0, ind]) > max) { max = Math.Abs(Rik[0, ind]); col = ind; } } J[k] = col; j.Remove(J[k]); //Terminate if R(I(k),J(k)) == 0 if (Rik[0, J[k]] == 0) { break; } //Set k-th row of V equal to normalized error Matrix Vk = (Matrix)Rik.Divide(Rik[0, J[k]]); //Update (Jk)th column of the approximate error matrix List<int> n1 = new List<int>(); n1.Add(n[J[k]]); Rjk = (Matrix)(UserImpedance(m, n1) - U.Multiply(V.SubMatrix(0, V.RowCount, J[k], 1))); // Set k-th column of U equal to updated error Matrix Uk = Rjk; //Norm of approximate Z //Matrix s = (Matrix)(U.Transpose().Multiply(Uk)).Multiply((Vk.Multiply(V.Transpose())).Transpose()); //Matrix s = (Matrix)((U.Transpose()).Multiply(Uk)).Multiply(((Vk.Multiply(V.Transpose())).Transpose())); Matrix a = (Matrix)U.Transpose().Multiply(Uk); Matrix b = (Matrix)Vk.Multiply(V.Transpose()).Transpose(); Matrix s = (Matrix)a.PointwiseMultiply(b); double sum = 0; for (int i1 = 0; i1 < s.RowCount; i1++) { for (int j1 = 0; j1 < s.ColumnCount; j1++) { sum += s[i1, j1]; } } d1 = Uk.L2Norm(); d2 = Vk.L2Norm(); normZ += 2 * sum + d1 * d1 * d2 * d2; //Update U and V //U.SetColumn(U.ColumnCount - 1, Uk.Column(0)); //V.SetRow(V.RowCount - 1, Vk.Row(0)); U = AddColumn(U, (Vector)Uk.Column(0)); //U.SetColumn(k, Uk.Column(0)); V = AddRow(V, (Vector)Vk.Row(0)); //V.SetRow(k, Vk.Row(0)); if (d1 * d2 <= acaThres * Math.Sqrt(normZ)) { break; } if (k == Math.Min(N, M) - 1) { break; } max = -1; row = 0; foreach (int ind in i) { if (Math.Abs(Rjk[ind, 0]) > max) { max = Math.Abs(Rjk[ind, 0]); row = ind; } } I[k + 1] = row; //i = removeIndex(i,I[k+1]); i.Remove(I[k + 1]); } return new Tuple<Matrix, Matrix>(U, V); }