public static Matrix Tr(this Matrix M) { if (Tr_SelfTest) { Tr_SelfTest = false; Matrix tM0 = new double[2, 3] { { 1, 2, 3 }, { 4, 5, 6 } }; Matrix tT0 = new double[3, 2] { { 1, 4 }, { 2, 5 }, { 3, 6 } }; Matrix tT1 = Tr(tM0); HDebug.AssertToleranceMatrix(double.Epsilon, tT0 - tT1); } Matrix tr = Matrix.Zeros(M.RowSize, M.ColSize); for (int c = 0; c < tr.ColSize; c++) { for (int r = 0; r < tr.RowSize; r++) { tr[c, r] = M[r, c]; } } return(tr); }
public static double V1tD2V3(Vector V1, Matrix D2, Vector V3, bool assertDiag = true) { if (V1tD2V3_SelfTest) { V1tD2V3_SelfTest = false; Vector tV1 = new double[3] { 1, 2, 3 }; MatrixByArr tD2 = new double[3, 3] { { 2, 0, 0 }, { 0, 3, 0 }, { 0, 0, 4 } }; Vector tV3 = new double[3] { 3, 4, 5 }; // [2 ] [3] [ 6] // [1 2 3] * [ 3 ] * [4] = [1 2 3] * [12] = 6+24+60 = 90 // [ 4] [5] [20] double tV1tD2V3 = 90; HDebug.AssertTolerance(double.Epsilon, tV1tD2V3 - V1tD2V3(tV1, tD2, tV3)); } if (assertDiag) // check diagonal matrix { HDebug.AssertToleranceMatrix(double.Epsilon, D2 - Diag(Diag(D2))); } HDebug.Assert(V1.Size == D2.ColSize); HDebug.Assert(D2.RowSize == V3.Size); Vector lD2V3 = DV(D2, V3, assertDiag); double lV1tD2V3 = VtV(V1, lD2V3); return(lV1tD2V3); }
public Matrix FnMul(Matrix A, Matrix B) { if (HDebug.Selftest()) { Matrix tA = new double[, ] { { 1, 2 } }; Matrix tB = new double[, ] { { 2, 3 }, { 4, 5 } }; Matrix tAB0 = new double[, ] { { 10, 13 } }; Matrix tAB1 = FnMul(tA, tB); HDebug.AssertToleranceMatrix(0, tAB0 - tAB1); } var AA = ToILMat(A); var BB = ToILMat(B); var AABB = AA * BB; AA.Dispose(); BB.Dispose(); Matrix AB = AABB.ToArray(); AABB.Dispose(); return(AB); }
public static Vector DV(Matrix D, Vector V, bool assertDiag = true) { if (DV_SelfTest1) { DV_SelfTest1 = false; MatrixByArr tD = new double[3, 3] { { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 } }; Vector tV = new double[3] { 1, 2, 3 }; Vector tDV = new double[3] { 1, 4, 9 }; // [1 0 0] [1] [1] // [0 2 0] * [2] = [4] // [0 0 3] [3] [9] HDebug.AssertTolerance(double.Epsilon, DV(tD, tV) - tDV); } // D is the diagonal matrix HDebug.Assert(D.ColSize == D.RowSize); if (assertDiag) // check diagonal matrix { HDebug.AssertToleranceMatrix(double.Epsilon, D - Diag(Diag(D))); } HDebug.Assert(D.ColSize == V.Size); Vector diagD = Diag(D); Vector diagDV = DV(diagD, V); return(diagDV); }
public static void SelfTest() { if (_SelfTest == false) { return; } _SelfTest = false; MatrixBySparseMatrix mat = new MatrixBySparseMatrix(4, 4, 2); HDebug.AssertToleranceMatrix(0, mat - MatrixByArr.Zeros(4, 4)); mat[0, 0] = mat[1, 1] = mat[2, 2] = mat[3, 3] = 1; HDebug.AssertToleranceMatrix(0, mat - LinAlg.Eye(4, 1)); mat[0, 0] = mat[1, 1] = mat[2, 2] = mat[3, 3] = 0; HDebug.AssertToleranceMatrix(0, mat - MatrixByArr.Zeros(4, 4)); }
public Tuple <Matrix, Vector> FnEigSymm(Matrix A) { if (HDebug.Selftest()) { Matrix tA = new double[, ] { { 1, 2 }, { 2, 1 } }; var tVD = FnEigSymm(tA); Matrix tV = new double[, ] { { -0.7071, 0.7071 }, { 0.7071, 0.7071 } }; Vector tD = new double[] { -1, 3 }; HDebug.AssertToleranceMatrix(0.0001, tV - tVD.Item1); HDebug.AssertToleranceVector(0, tD - tVD.Item2); } var AA = ToILMat(A); var VVDD = EigSymm(AA); return(new Tuple <Matrix, Vector>(VVDD.Item1.ToArray(), VVDD.Item2)); }
public static Matrix ToMatrix(this IList <Vector> vecs, bool colvec = true) { if (colvec) { // [v1, v2, ... ] Matrix mat = Matrix.Zeros(vecs[0].Size, vecs.Count); for (int r = 0; r < mat.RowSize; r++) { HDebug.Assert(mat.ColSize == vecs[r].Size); for (int c = 0; c < mat.ColSize; c++) { mat[c, r] = vecs[r][c]; } } return(mat); } else { // [v1 ] // [v2 ] // [...] Matrix mat = Matrix.Zeros(vecs.Count, vecs[0].Size); for (int c = 0; c < mat.ColSize; c++) { HDebug.Assert(mat.RowSize == vecs[c].Size); for (int r = 0; r < mat.RowSize; r++) { mat[c, r] = vecs[c][r]; } } if (HDebug.IsDebuggerAttachedWithProb(0.1)) #region verify result { Matrix matc = vecs.ToMatrix(true).Tr(); HDebug.AssertToleranceMatrix(0, matc - mat); } #endregion return(mat); } }
public Matrix FnMul(Matrix A, Matrix B, Matrix C) { if (HDebug.Selftest()) { Matrix tA = new double[, ] { { 1, 2 } }; Matrix tB = new double[, ] { { 2, 3 }, { 4, 5 } }; Matrix tC = new double[, ] { { 6 }, { 7 } }; Matrix tABC0 = new double[, ] { { 151 } }; Matrix tABC1 = FnMul(tA, tB, tC); HDebug.AssertToleranceMatrix(0, tABC0 - tABC1); } if (C == null) { return(FnMul(A, B)); } var AA = ToILMat(A); var BB = ToILMat(B); var CC = ToILMat(C); var AABBCC = AA * BB * CC; AA.Dispose(); BB.Dispose(); CC.Dispose(); Matrix ABC = AABBCC.ToArray(); AABBCC.Dispose(); return(ABC); }
public static Matrix FromMatrixArray(Matrix[,] blockmatrix) { if (FromMatrixArray_selftest == true) #region selftest { { FromMatrixArray_selftest = false; Matrix[,] bmat = new Matrix[3, 2]; bmat[0, 0] = new double[1, 2] { { 0, 1 } }; bmat[0, 1] = new double[1, 3] { { 2, 3, 4 } }; bmat[1, 0] = new double[2, 2] { { 0, 1 }, { 2, 3 } }; bmat[1, 1] = new double[2, 3] { { 4, 5, 6 }, { 7, 8, 9 } }; bmat[2, 0] = new double[1, 2] { { 9, 8 } }; bmat[2, 1] = new double[1, 3] { { 7, 6, 5 } }; Matrix smat1 = FromMatrixArray(bmat); Matrix smat0 = new double[, ] { { 0, 1, 2, 3, 4 }, { 0, 1, 4, 5, 6 }, { 2, 3, 7, 8, 9 }, { 9, 8, 7, 6, 5 } }; HDebug.AssertToleranceMatrix(0, smat1 - smat0); } { FromMatrixArray_selftest = false; Matrix[,] bmat = new Matrix[3, 2]; bmat[0, 0] = new double[1, 2] { { 0, 1 } }; bmat[0, 1] = new double[1, 3] { { 2, 3, 4 } }; bmat[1, 0] = new double[2, 2] { { 0, 1 }, { 2, 3 } }; bmat[1, 1] = new double[2, 3] { { 4, 5, 6 }, { 7, 8, 9 } }; bmat[2, 0] = new double[1, 2] { { 9, 8 } }; bmat[2, 1] = null; Matrix smat1 = FromMatrixArray(bmat); HDebug.Assert(smat1 == null); } { FromMatrixArray_selftest = false; Matrix[,] bmat = new Matrix[3, 2]; bmat[0, 0] = new double[1, 2] { { 0, 1 } }; bmat[0, 1] = new double[1, 3] { { 2, 3, 4 } }; bmat[1, 0] = new double[2, 2] { { 0, 1 }, { 2, 3 } }; bmat[1, 1] = new double[1, 3] { { 4, 5, 6 } }; bmat[2, 0] = new double[1, 2] { { 9, 8 } }; bmat[2, 1] = new double[1, 3] { { 7, 6, 5 } }; Matrix smat1 = FromMatrixArray(bmat); HDebug.Assert(smat1 == null); } } #endregion for (int bc = 0; bc < blockmatrix.GetLength(0); bc++) { for (int br = 0; br < blockmatrix.GetLength(1); br++) { if (blockmatrix[bc, br] == null) { return(null); } if (blockmatrix[bc, 0] == null) { return(null); } if (blockmatrix[0, br] == null) { return(null); } if (blockmatrix[bc, br].ColSize != blockmatrix[bc, 0].ColSize) { return(null); } if (blockmatrix[bc, br].RowSize != blockmatrix[0, br].RowSize) { return(null); } } } int[] colsize_acc = new int[blockmatrix.GetLength(0) + 1]; for (int bc = 0; bc < blockmatrix.GetLength(0); bc++) { colsize_acc[bc + 1] = colsize_acc[bc] + blockmatrix[bc, 0].ColSize; } int[] rowsize_acc = new int[blockmatrix.GetLength(1) + 1]; for (int br = 0; br < blockmatrix.GetLength(1); br++) { rowsize_acc[br + 1] = rowsize_acc[br] + blockmatrix[0, br].RowSize; } Matrix mat = Matrix.Zeros(colsize_acc.Last(), rowsize_acc.Last()); for (int bc = 0; bc < blockmatrix.GetLength(0); bc++) { for (int br = 0; br < blockmatrix.GetLength(1); br++) { for (int ic = 0; ic < blockmatrix[bc, br].ColSize; ic++) { for (int ir = 0; ir < blockmatrix[bc, br].RowSize; ir++) { int c = ic + colsize_acc[bc]; int r = ir + rowsize_acc[br]; mat[c, r] = blockmatrix[bc, br][ic, ir]; } } } } return(mat); }