public static MatrixSparse <double> GetCorrMatrix(this IList <Mode> modes, IEnumerable <Tuple <int, int> > enumCorrCR) { Vector[][] eigvecs = new Vector[modes.Count][]; double[] eigvals = new double[modes.Count]; for (int i = 0; i < modes.Count; i++) { eigvals[i] = modes[i].eigval; eigvecs[i] = modes[i].GetEigvecsOfAtoms(); } int size = modes[0].size; MatrixSparse <double> Dij = new MatrixSparse <double>(size, size); Vector Dii = new double[size]; for (int i = 0; i < eigvals.Length; i++) { Vector[] eigvec = eigvecs[i]; double eigval = eigvals[i]; for (int c = 0; c < size; c++) { Dii[c] += (LinAlg.VtV(eigvec[c], eigvec[c]) / eigval); } foreach (var cr in enumCorrCR) { int c = cr.Item1; int r = cr.Item2; Dij[c, r] += (LinAlg.VtV(eigvec[c], eigvec[r]) / eigval); } } foreach (var cr in enumCorrCR) { Dij[cr] = Dij[cr] / modes.Count; } Dii = Dii / modes.Count; // Cij foreach (var cr in enumCorrCR) { int c = cr.Item1; int r = cr.Item2; Dij[c, r] /= Math.Sqrt(Dii[c] * Dii[r]); } if (HDebug.IsDebuggerAttached) { Matrix tDij = GetCorrMatrix(modes); foreach (var cr in enumCorrCR) { int c = cr.Item1; int r = cr.Item2; double err = Dij[c, r] - tDij[c, r]; HDebug.Exception(Math.Abs(err) < 0.00000001); } } return(Dij); }
public static T reshapeOrDeclare <T>(T target, MatrixSparse reference) where T : MatrixSparse { if (target == null) { return(reference.createLike <T>()); } else { target.reshape(reference.NumRows, reference.NumCols, reference.NonZeroLength); } return(target); }
//public static IEnumerable<Tuple<int, int, double>> EnumHessAnmSpr_obsolete(IList<Vector> coords, double cutoff, double sprcst) //{ // int size = coords.Count; // double cutoff2 = cutoff*cutoff; // //Matrix Kij = Matrix.Zeros(size, size); // int num_springs = 0; // for(int c=0; c<size; c++) // { // if(coords[c] == null) continue; // for(int r=c+1; r<size; r++) // { // if(coords[r] == null) continue; // double dist2 = (coords[c] - coords[r]).Dist2; // if(dist2 <= cutoff2) // { // yield return new Tuple<int, int, double>(c, r, sprcst); //Kij[c, r] = sprcst; // yield return new Tuple<int, int, double>(r, c, sprcst); //Kij[r, c] = sprcst; // num_springs += 2; // } // } // } // double ratio_springs = ((double)num_springs) / (size*size); // //return Kij; //} public static MatrixSparse <double> GetHessAnmBmat(IList <Vector> coords, double cutoff) { if (HDebug.Selftest()) { var _coords = Pdb._smallest_protein_cacoords; Matrix _Bmat = Hess.GetHessAnmBmat(_coords, 13).ToArray(); Matrix _BB = _Bmat * _Bmat.Tr(); Matrix _ANM = Hess.GetHessAnm(_coords, 13); double _err = (_BB - _ANM).HAbsMax(); HDebug.Assert(_err < 0.00000001); } List <Tuple <int, int, double> > sprs = EnumHessAnmSpr(coords, cutoff, 1).ToList(); MatrixSparse <double> Bmat = new MatrixSparse <double>(3 * coords.Count, sprs.Count); int spr_count = 0; for (int ij = 0; ij < sprs.Count; ij++) { var spr = sprs[ij]; int ai = spr.Item1; int aj = spr.Item2; if (ai >= aj) { continue; } double kij = spr.Item3; Vector coordi = coords[ai]; Vector coordj = coords[aj]; double sij = (coordi - coordj).Dist; double xij = (coordj[0] - coordi[0]) / sij; double yij = (coordj[1] - coordi[1]) / sij; double zij = (coordj[2] - coordi[2]) / sij; Bmat[(ai * 3 + 0), spr_count] = xij; Bmat[(ai * 3 + 1), spr_count] = yij; Bmat[(ai * 3 + 2), spr_count] = zij; Bmat[(aj * 3 + 0), spr_count] = -xij; Bmat[(aj * 3 + 1), spr_count] = -yij; Bmat[(aj * 3 + 2), spr_count] = -zij; spr_count++; } return(Bmat.GetSubMatrix(3 * coords.Count, spr_count)); }