protected override bool InvEigImpl(Matrix mat, double?thresEigval, int?numZeroEigval, out Matrix inv, InfoPack extra) { NamedLock.FuncO <Matrix, bool> func = delegate(out Matrix linv) { Matlab.Clear("HTLib2_Matlab_InvEigImpl"); Matlab.PutMatrix("HTLib2_Matlab_InvEigImpl.A", mat.ToArray()); Matlab.PutValue("HTLib2_Matlab_InvEigImpl.ze", numZeroEigval.GetValueOrDefault(0)); Matlab.PutValue("HTLib2_Matlab_InvEigImpl.th", Math.Abs(thresEigval.GetValueOrDefault(0))); Matlab.Execute("[HTLib2_Matlab_InvEigImpl.V, HTLib2_Matlab_InvEigImpl.D] = eig(HTLib2_Matlab_InvEigImpl.A);"); Matlab.Execute("HTLib2_Matlab_InvEigImpl.D = diag(HTLib2_Matlab_InvEigImpl.D);"); Matlab.Execute("HTLib2_Matlab_InvEigImpl.sortAbsD = sort(abs(HTLib2_Matlab_InvEigImpl.D));"); Matlab.Execute("HTLib2_Matlab_InvEigImpl.sortAbsD0 = [0; HTLib2_Matlab_InvEigImpl.sortAbsD];"); // add zero to the first list, for the case ze=0 (null) Matlab.Execute("HTLib2_Matlab_InvEigImpl.ze = HTLib2_Matlab_InvEigImpl.sortAbsD0(HTLib2_Matlab_InvEigImpl.ze+1);"); Matlab.Execute("HTLib2_Matlab_InvEigImpl.th = max(HTLib2_Matlab_InvEigImpl.th, HTLib2_Matlab_InvEigImpl.ze);"); Matlab.Execute("HTLib2_Matlab_InvEigImpl.idx = abs(HTLib2_Matlab_InvEigImpl.D) <= HTLib2_Matlab_InvEigImpl.th;"); Matlab.Execute("HTLib2_Matlab_InvEigImpl.invD = ones(size(HTLib2_Matlab_InvEigImpl.D)) ./ HTLib2_Matlab_InvEigImpl.D;"); Matlab.Execute("HTLib2_Matlab_InvEigImpl.invD(HTLib2_Matlab_InvEigImpl.idx) = 0;"); Matlab.Execute("HTLib2_Matlab_InvEigImpl.invD = diag(HTLib2_Matlab_InvEigImpl.invD);"); Matlab.Execute("HTLib2_Matlab_InvEigImpl.invA = HTLib2_Matlab_InvEigImpl.V * HTLib2_Matlab_InvEigImpl.invD * inv(HTLib2_Matlab_InvEigImpl.V);"); linv = Matlab.GetMatrix("HTLib2_Matlab_InvEigImpl.invA"); if (extra != null) { int num_zero_eigvals = Matlab.GetValueInt("sum(HTLib2_Matlab_InvEigImpl.idx)"); HDebug.AssertIf(numZeroEigval != null, numZeroEigval.GetValueOrDefault() <= num_zero_eigvals); extra["num_zero_eigvals"] = num_zero_eigvals; extra["eigenvalues"] = Matlab.GetVector("HTLib2_Matlab_InvEigImpl.D"); } Matlab.Clear("HTLib2_Matlab_InvEigImpl"); return(true); }; //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.InvEigImpl(Matrix, double?, int?, out Matrix, InfoPack)", func, out inv); return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_InvEigImpl"), func, out inv)); }
public override ILinAlgMat _Inv(ILinAlgMat A) { using (new Matlab.NamedLock("LA")) { Matlab.PutMatrix("LA.A", A.ToArray()); CMatrix inv = Matlab.GetMatrix("inv(LA.A)"); Matlab.Clear(); return(inv); } }
public override ILinAlgMat _Diag(ILinAlgMat mat) { using (new Matlab.NamedLock("LA")) { Matlab.PutMatrix("LA.A", mat.ToArray()); CMatrix diag = Matlab.GetMatrix("diag(LA.A)"); Matlab.Clear(); return(diag); } }
public override ILinAlgMat _LinSolve(ILinAlgMat A, ILinAlgMat B) { using (new Matlab.NamedLock("LA")) { Matlab.PutMatrix("LA.A", A.ToArray()); Matlab.PutMatrix("LA.B", B.ToArray()); CMatrix X = Matlab.GetMatrix(@"LA.A \ LA.B"); /// A X = B => X = A\B Matlab.Clear(); return(X); } }
protected override bool SvdImpl(Matrix X, out Matrix U, out Vector S, out Matrix V) { using (new Matlab.NamedLock("NUMSLV")) { Matlab.Clear("NUMSLV"); Matlab.PutMatrix("NUMSLV.X", X.ToArray()); Matlab.Execute("[NUMSLV.U, NUMSLV.S, NUMSLV.V] = svd(NUMSLV.X);"); Matlab.Execute("NUMSLV.S = diag(NUMSLV.S);"); U = Matlab.GetMatrix("NUMSLV.U"); S = Matlab.GetVector("NUMSLV.S"); V = Matlab.GetMatrix("NUMSLV.V"); Matlab.Clear("NUMSLV"); } return(true); }
public override ILinAlgMat _Mul(params ILinAlgMat[] mats) { using (new Matlab.NamedLock("LA")) { Matlab.PutMatrix("LA.mul", mats[0].ToArray(), true); for (int i = 1; i < mats.Length; i++) { Matlab.PutMatrix("LA.tmp", mats[i].ToArray(), true); Matlab.Execute("LA.mul = LA.mul * LA.tmp;"); } CMatrix mul = Matlab.GetMatrix("LA.mul", true); Matlab.Clear(); return(mul); } }
protected override bool EigImpl(Matrix A, Matrix B, out Matrix eigvec, out Vector eigval) { NamedLock.FuncOO <Matrix, Vector, bool> func = delegate(out Matrix leigvec, out Vector leigval) { Matlab.Clear("HTLib2_Matlab_EigImpl"); Matlab.PutMatrix("HTLib2_Matlab_EigImpl.A", A.ToArray()); Matlab.PutMatrix("HTLib2_Matlab_EigImpl.B", B.ToArray()); Matlab.Execute("[HTLib2_Matlab_EigImpl.V, HTLib2_Matlab_EigImpl.D] = eig(HTLib2_Matlab_EigImpl.A, HTLib2_Matlab_EigImpl.B);"); Matlab.Execute("HTLib2_Matlab_EigImpl.D = diag(HTLib2_Matlab_EigImpl.D);"); leigvec = Matlab.GetMatrix("HTLib2_Matlab_EigImpl.V"); leigval = Matlab.GetVector("HTLib2_Matlab_EigImpl.D"); Matlab.Clear("HTLib2_Matlab_EigImpl"); return(true); }; //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.EigImpl(Matrix, Matrix, out Matrix, out Vector)", func, out eigvec, out eigval); return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_EigImpl"), func, out eigvec, out eigval)); }
protected override bool EigImpl(Matrix mat, out Matrix eigvec, out Vector eigval) { NamedLock.FuncOO <Matrix, Vector, bool> func = delegate(out Matrix leigvec, out Vector leigval) { bool bUseFile = (mat.ColSize * mat.ColSize > 1000 * 1000); Matlab.Clear("HTLib2_Matlab_EigImpl"); Matlab.PutMatrix("HTLib2_Matlab_EigImpl.A", mat.ToArray(), bUseFile); Matlab.Execute("[HTLib2_Matlab_EigImpl.V, HTLib2_Matlab_EigImpl.D] = eig(HTLib2_Matlab_EigImpl.A);"); Matlab.Execute("HTLib2_Matlab_EigImpl.D = diag(HTLib2_Matlab_EigImpl.D);"); leigvec = Matlab.GetMatrix("HTLib2_Matlab_EigImpl.V", bUseFile); leigval = Matlab.GetVector("HTLib2_Matlab_EigImpl.D"); Matlab.Clear("HTLib2_Matlab_EigImpl"); return(true); }; //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.EigImpl(Matrix, out Matrix, out Vector)", func, out eigvec, out eigval); return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_EigImpl"), func, out eigvec, out eigval)); }
protected override bool PinvImpl(Matrix mat, out Matrix pinv, InfoPack extra) { NamedLock.FuncO <Matrix, bool> func = delegate(out Matrix lpinv) { Matlab.Clear("HTLib2_Matlab_PinvImpl"); Matlab.PutMatrix("HTLib2_Matlab_PinvImpl", mat.ToArray()); if (extra != null) { extra.SetValue("rank", Matlab.GetValueInt("rank(HTLib2_Matlab_PinvImpl)")); } Matlab.Execute("HTLib2_Matlab_PinvImpl = pinv(HTLib2_Matlab_PinvImpl);"); lpinv = Matlab.GetMatrix("HTLib2_Matlab_PinvImpl"); Matlab.Clear("HTLib2_Matlab_PinvImpl"); return(true); }; //return NamedLock.LockedCall("bool HTLib2.Matlab.NumericSolver.PinvImpl(Matrix, out Matrix, InfoPack)", func, out pinv); return(NamedLock.LockedCall(Matlab.NamedLock.GetName("HTLib2_Matlab_PinvImpl"), func, out pinv)); }
public override Tuple <ILinAlgMat, double[]> _EigSymm(ILinAlgMat A) { using (new Matlab.NamedLock("LA")) { Matlab.PutMatrix("LA.A", A.ToArray()); Matlab.Execute("LA.A = (LA.A + LA.A)/2;"); Matlab.Execute("[LA.V, LA.D] = eig(LA.A);"); Matlab.Execute("LA.D = diag(LA.D);"); CMatrix V = Matlab.GetMatrix("LA.V"); double[] D = Matlab.GetVector("LA.D"); if (HDebug.IsDebuggerAttached) { Matlab.Execute("LA.ERR = LA.A - (LA.V * diag(LA.D) * LA.V');"); Matlab.Execute("LA.ERR = max(max(abs(LA.ERR)));"); double err = Matlab.GetValue("LA.ERR"); HDebug.AssertTolerance(0.00000001, err); } Matlab.Clear(); return(new Tuple <ILinAlgMat, double[]>(V, D)); } }
public override ILinAlgMat _Ones(int colsize, int rowsize) { CMatrix zeros = Matlab.GetMatrix(string.Format("ones({0},{1})", colsize, rowsize)); return(zeros); }
public override ILinAlgMat _Eye(int size) { CMatrix eye = Matlab.GetMatrix(string.Format("eye({0})", size)); return(eye); }