public HessMatrix ReshapeByAtomImpl0(IList <int> idxatms, bool ignNegIdx) { HDebug.Assert(idxatms.Count == idxatms.HUnion().Length); // check no-duplication of indexes HessMatrix reshape = Zeros(idxatms.Count * 3, idxatms.Count * 3); for (int nbc = 0; nbc < idxatms.Count; nbc++) { for (int nbr = 0; nbr < idxatms.Count; nbr++) { int bc = idxatms[nbc]; if (ignNegIdx && bc < 0) { continue; } int br = idxatms[nbr]; if (ignNegIdx && br < 0) { continue; } if (HasBlock(bc, br) == false) { if (HDebug.IsDebuggerAttached) { if (GetBlock(bc, br) != null) { HDebug.AssertTolerance(0, GetBlock(bc, br).ToArray()); } } continue; } MatrixByArr blkrc = GetBlock(bc, br); reshape.SetBlock(nbc, nbr, blkrc.CloneT()); } } return(reshape); }
public static Anisou FromValues(MatrixByArr U, Vector[] eigvecs, double[] eigvals) { Anisou anisou = new Anisou(); anisou.U = U.CloneT(); anisou.eigvecs = eigvecs.HClone <Vector>(); HDebug.Assert(eigvecs.Length == 3, eigvecs[0].Size == 3, eigvecs[1].Size == 3, eigvecs[2].Size == 3); anisou.eigvals = eigvals.HClone <double>(); HDebug.Assert(eigvals.Length == 3); return(anisou); }
public static Anisou FromMatrix(MatrixByArr U, Func <MatrixByArr, Tuple <Vector[], double[]> > Eig, double eigvalthres = double.NegativeInfinity) { Anisou anisou = new Anisou(); anisou.U = U.CloneT(); anisou.eigvecs = new Vector[3]; anisou.eigvals = new double[3]; { var eigvecval = Eig(anisou.U); Vector[] eigvec = eigvecval.Item1; double[] eigval = eigvecval.Item2; if (eigval[0] < eigvalthres) { eigval[0] = 0; } if (eigval[1] < eigvalthres) { eigval[1] = 0; } if (eigval[2] < eigvalthres) { eigval[2] = 0; } HDebug.Assert((eigval[0] == 0 && eigval[1] == 0 && eigval[2] == 0) == false); { // normalize eigval and eigvec double l; l = eigvec[0].Dist; anisou.eigvecs[0] = eigvec[0] / l; anisou.eigvals[0] = eigval[0] * (l * l); l = eigvec[1].Dist; anisou.eigvecs[1] = eigvec[1] / l; anisou.eigvals[1] = eigval[1] * (l * l); l = eigvec[2].Dist; anisou.eigvecs[2] = eigvec[2] / l; anisou.eigvals[2] = eigval[2] * (l * l); } } return(anisou); }
public int UpdateAdd(HessMatrix other, double mul_other, IList <int> idxOther, double thres_NearZeroBlock, bool parallel = false) { Matrix debug_updateadd = null; if (UpdateAdd_SelfTest && idxOther == null && thres_NearZeroBlock == 0) { if ((100 < ColBlockSize) && (ColBlockSize < 1000) && (100 < RowBlockSize) && (RowBlockSize < 1000) && (other.NumUsedBlocks > 20)) { UpdateAdd_SelfTest = false; debug_updateadd = this.ToArray(); debug_updateadd.UpdateAdd(other, mul_other); } } int[] idx_other; if (idxOther == null) { //HDebug.Exception(ColSize == other.ColSize); //HDebug.Exception(RowSize == other.RowSize); idx_other = HEnum.HEnumCount(other.ColSize).ToArray(); } else { idx_other = idxOther.ToArray(); } int count = 0; int count_ignored = 0; object _lock = new object(); object _lock_ignored = new object(); Action <ValueTuple <int, int, MatrixByArr> > func = delegate(ValueTuple <int, int, MatrixByArr> bc_br_bval) { count++; int other_bc = bc_br_bval.Item1; int other_br = bc_br_bval.Item2; MatrixByArr other_bmat = bc_br_bval.Item3; if (other_bmat.HAbsMax() <= thres_NearZeroBlock) { lock (_lock_ignored) count_ignored++; //continue; return; } int bc = idx_other[other_bc]; int br = idx_other[other_br]; lock (_lock) { MatrixByArr this_bmat = GetBlock(bc, br); MatrixByArr new_bmat; if (this_bmat == null) { if (other_bmat == null) { new_bmat = null; } else { new_bmat = mul_other * other_bmat; } } else { if (other_bmat == null) { new_bmat = this_bmat.CloneT(); } else { new_bmat = this_bmat + mul_other * other_bmat; } } SetBlock(bc, br, new_bmat); } }; if (parallel) { Parallel.ForEach(other.EnumBlocks(), func); } else { foreach (var bc_br_bval in other.EnumBlocks()) { func(bc_br_bval); } } if (debug_updateadd != null) { Matrix debug_diff = debug_updateadd - this; double debug_absmax = debug_diff.HAbsMax(); HDebug.AssertToleranceMatrix(0, debug_diff); } return(count_ignored); }