/// <summary> /// MATLAB 'backslash' solver for the system /// <paramref name="M"/>*<paramref name="X"/> = <paramref name="RHS"/>. /// </summary> /// <param name="M"> /// Matrix of the linear system. /// </param> /// <param name="RHS"> /// Input, the right hand side of the linear system. /// </param> /// <param name="X"> /// Output, the solution. /// </param> /// <param name="__WorkingPath"></param> public static void SolveMATLAB <T1, T2>(this IMutableMatrixEx M, T1 X, T2 RHS, string __WorkingPath = null) where T1 : IList <double> where T2 : IList <double> // { if (M.RowPartitioning.LocalLength != RHS.Count) { throw new ArgumentException("Mismatch between number of rows and length of right-hand-side."); } if (M.ColPartition.LocalLength != X.Count) { throw new ArgumentException("Mismatch between number of columns and length of unknown vector."); } MultidimensionalArray Xwrapper = MultidimensionalArray.Create(X.Count, 1); using (var connector = new BatchmodeConnector(WorkingPath: __WorkingPath)) { MultidimensionalArray output = MultidimensionalArray.Create(1, 1); connector.PutSparseMatrix(M, "Matrix"); connector.PutVector(RHS, "RHS"); connector.Cmd("X = Matrix \\ RHS ;"); connector.GetMatrix(Xwrapper, "X"); connector.Execute(false); Xwrapper.GetColumn(0, X); } }
/// <summary> /// MATLAB function 'condest' (condition number estimation for sparse /// matrices) /// </summary> public static double condest(this IMutableMatrixEx M, string __WorkingPath = null) { using (var connector = new BatchmodeConnector(WorkingPath: __WorkingPath)) { MultidimensionalArray output = MultidimensionalArray.Create(1, 1); connector.PutSparseMatrix(M, "Matrix"); connector.Cmd("cond = condest(Matrix)"); connector.GetMatrix(output, "cond"); connector.Execute(false); return(output[0, 0]); } }
/// <summary> /// MATLAB function 'eigs' (eigenvalues of a matrix); /// </summary> /// <param name="C"> /// options, ('lm': largest magnitude, etc.) see MATLAB documentation. /// </param> /// <param name="K"> /// Number of eigenvalues. /// </param> /// <param name="M"> /// Matrix. /// </param> /// <param name="__WorkingPath"></param> public static double[] eigs(this IMutableMatrixEx M, int K, string C, string __WorkingPath = null) { using (var connector = new BatchmodeConnector(WorkingPath: __WorkingPath)) { MultidimensionalArray output = MultidimensionalArray.Create(1, 1); connector.PutSparseMatrix(M, "Matrix"); connector.Cmd(string.Format("EV = eigs(Matrix,{0},'{1}')", K, C)); connector.GetMatrix(output, "EV"); connector.Execute(false); return(output.GetColumn(0)); } }
/// <summary> /// Tests, via a Cholesky factorization, if a symmetric matrix is negative definite. /// </summary> public static bool IsNegDef(this IMutableMatrixEx M, string __WorkingPath = null) { using (var connector = new BatchmodeConnector(WorkingPath: __WorkingPath)) { MultidimensionalArray output = MultidimensionalArray.Create(1, 1); connector.PutSparseMatrix(M, "Matrix"); connector.Cmd("[V,r]=chol(-0.5*(Matrix+Matrix'));"); connector.GetMatrix(output, "r"); connector.Execute(false); return(output[0, 0] == 0); } }
/// <summary> /// MATLAB function 'rank' (rank of a matrix); /// </summary> public static double rank(this IMutableMatrixEx M, string __WorkingPath = null) { if (M == null) { throw new ArgumentNullException(); } using (var connector = new BatchmodeConnector(WorkingPath: __WorkingPath)) { MultidimensionalArray output = MultidimensionalArray.Create(1, 1); connector.PutSparseMatrix(M, "Matrix"); connector.Cmd("rank = rank(full(Matrix))"); connector.GetMatrix(output, "rank"); connector.Execute(false); return(output[0, 0]); } }
/// <summary> /// Tests, via a Cholesky factorization, if a symmetric matrix is positive or negative definite. /// </summary> public static bool IsDefinite(this IMutableMatrixEx M, string __WorkingPath = null) { if (M == null) { throw new ArgumentNullException(); } using (var connector = new BatchmodeConnector(WorkingPath: __WorkingPath)) { MultidimensionalArray output = MultidimensionalArray.Create(1, 2); connector.PutSparseMatrix(M, "Matrix"); connector.Cmd("[V,pr]=chol( 0.5*(Matrix+Matrix'));"); connector.Cmd("[V,nr]=chol(-0.5*(Matrix+Matrix'));"); connector.Cmd("ret=[pr,nr]"); connector.GetMatrix(output, "ret"); connector.Execute(false); return(output[0, 0] == 0 || output[0, 1] == 0); } }
/// <summary> /// MATLAB function 'eigs' (eigenvalues of a sparse matrix); /// </summary> /// <param name="C"> /// options, ('lm': largest magnitude, etc.) see MATLAB documentation. /// </param> /// <param name="K"> /// Number of eigenvalues. /// </param> /// <param name="M"> /// A sparse Matrix. /// </param> /// <param name="__WorkingPath"></param> public static double[] eigs(this IMutableMatrixEx M, int K, string C, string __WorkingPath = null) { using (var connector = new BatchmodeConnector(WorkingPath: __WorkingPath)) { if (M == null) { throw new ArgumentNullException(); } if (K < 1) { throw new ArgumentOutOfRangeException(); } MultidimensionalArray output = MultidimensionalArray.Create(K, 1); connector.PutSparseMatrix(M, "Matrix"); connector.Cmd(string.Format("EV = eigs(Matrix,{0},'{1}');", K, C)); connector.GetMatrix(output, "EV"); connector.Execute(false); return(output.GetColumn(0)); } }