public static Vector CenterOfMass(IList <Vector> coords, IList <double> masses, params int[] sele) { HDebug.Exception(coords.Count == masses.Count, "coords.Count != masses.Count"); Vector sum = new double[3]; double wei = 0; int leng = coords.Count; if (sele != null) { foreach (int i in sele) { wei += masses[i]; sum += masses[i] * coords[i]; } } else { for (int i = 0; i < leng; i++) { wei += masses[i]; sum += masses[i] * coords[i]; } } Vector cent = sum / wei; return(cent); }
////////////////////////////////////////////////////////////////////////////////////////////////// // others public virtual void UpdateAdd(IMatrix <double> other, double other_mul) { HDebug.Exception(ColSize == other.ColSize); HDebug.Exception(RowSize == other.RowSize); if (other_mul == 1) { for (int c = 0; c < ColSize; c++) { for (int r = 0; r < RowSize; r++) { this[c, r] += other[c, r]; } } } else if (other_mul == -1) { for (int c = 0; c < ColSize; c++) { for (int r = 0; r < RowSize; r++) { this[c, r] -= other[c, r]; } } } else { for (int c = 0; c < ColSize; c++) { for (int r = 0; r < RowSize; r++) { this[c, r] += (other[c, r] * other_mul); } } } }
public static void AddTo(Vector[] target, Vector[] value, bool allowNullVec = false) { HDebug.Exception(target.Length == value.Length); for (int i = 0; i < target.Length; i++) { if (target[i] != null && value[i] != null) { target[i] = target[i] + value[i]; continue; } if (allowNullVec) { if (target[i] == null && value[i] == null) { continue; } if (target[i] == null && value[i] != null) { target[i] = value[i].Clone(); continue; } if (target[i] != null && value[i] == null) { target[i] = value[i].Clone(); continue; } } HDebug.Assert(false); } }
public static Vector[] ExtendSegmentBeginEnd(IList <Vector> segment, double extlength_begin, double extlength_end) { HDebug.Exception(extlength_begin > 0, "must be (extlength_begin > 0)"); HDebug.Exception(extlength_end > 0, "must be (extlength_end > 0)"); Vector[] extsegment = segment.HCloneVectors().ToArray(); extsegment = ExtendSegmentBegin(extsegment, extlength_begin); extsegment = ExtendSegmentEnd(extsegment, extlength_end); return(extsegment); }
public static void VVt(Vector lvec, Vector rvec, MatrixByArr outmat) { HDebug.Exception(outmat.ColSize == lvec.Size); HDebug.Exception(outmat.RowSize == rvec.Size); MatrixByArr mat = new MatrixByArr(lvec.Size, rvec.Size); for (int c = 0; c < lvec.Size; c++) { for (int r = 0; r < rvec.Size; r++) { outmat[c, r] = lvec[c] * rvec[r]; } } }
public static string HToString(this IList <char> chars) { if (HToStringChars_selftest) { HToStringChars_selftest = false; string str0 = ""; foreach (var ch in chars) { str0 += ch; } string str1 = HToString(chars); HDebug.Exception(str0 == str1); } return(new String(chars.ToArray())); }
public static string ToMathematicaStringBase(object obj) { if (HDebug.True) { HDebug.Assert(false); return(null); } HDebug.Exception("should not reach here"); Type objtype = obj.GetType(); //if(objtype.FullName == typeof(Tuple<DoubleVector3, Tuple<double, DoubleVector3>[]>).FullName) //{ // Tuple<DoubleVector3, Tuple<double, DoubleVector3>[]> data = (Tuple<DoubleVector3, Tuple<double, DoubleVector3>[]>)obj; // return ToString(data); //} //if(objtype.FullName == typeof(Tuple<double, DoubleVector3>[]).FullName) //{ //} //if(objtype.FullName == typeof(Tuple<double, DoubleVector3>).FullName) //{ // Tuple<double, DoubleVector3> data = (Tuple<double, DoubleVector3>)obj; // StringBuilder text = new StringBuilder(); // //data.first; //} //if(objtype.FullName == typeof(DoubleVector3).FullName) //{ // StringBuilder text = new StringBuilder(); // DoubleVector3 data = (DoubleVector3)obj; // text.Append("{"); // text.Append(ToString(data.v0).ToString()); // text.Append(","); // text.Append(ToString(data.v1).ToString()); // text.Append(","); // text.Append(ToString(data.v2).ToString()); // text.Append("}"); //} if (objtype.FullName == typeof(double).FullName) { string text = ((double)obj).ToString(); return(text); } HDebug.Assert(false); return(null); }
public static double HSumSub <MAT1, MAT2>(this(MAT1, MAT2) mat12) where MAT1 : IMatrix <double> where MAT2 : IMatrix <double> { IMatrix <double> mat1 = mat12.Item1; IMatrix <double> mat2 = mat12.Item2; HDebug.Exception(mat1.ColSize == mat2.ColSize); HDebug.Exception(mat1.RowSize == mat2.RowSize); double sum = 0; for (int c = 0; c < mat1.ColSize; c++) { for (int r = 0; r < mat1.RowSize; r++) { double v = (mat1[c, r] - mat2[c, r]); sum += v; } } return(sum); }
public static Matrix GetMul(Matrix left, IMatrix <double> right) { int RowSize = left.RowSize; int ColSize = left.ColSize; HDebug.Exception(RowSize == right.ColSize); Matrix mul = Matrix.Zeros(ColSize, right.RowSize); int colsize = mul.ColSize; int midsize = RowSize; int rowsize = mul.RowSize; for (int c = 0; c < colsize; c++) { for (int r = 0; r < rowsize; r++) { for (int i = 0; i < midsize; i++) { mul[c, r] += left[c, i] * right[i, r]; } } } return(mul); }
public static IEnumerable <int> HEnumIntByString(string str) { string[] ranges = str.HSplit(','); foreach (string range in ranges) { if (range.Contains("--")) { string[] from_to = range.Split(new string[] { "--" }, StringSplitOptions.RemoveEmptyEntries); HDebug.Assert(from_to.Length == 2); int from = from_to[0].HParseInt().Value; int to = from_to[1].HParseInt().Value; HDebug.Exception(from <= to); for (int i = from; i <= to; i++) { yield return(i); } } else { yield return(range.HParseInt().Value); } } }
public static int[] HIndexAllContains(this IList <string> values, string contain) { if (HIndexAllContains_selftest) { HIndexAllContains_selftest = false; string[] tvalues = new string[] { "", "abc", " abc ", " ab ", " cba ", }; int[] tidx; tidx = HIndexAllContains(tvalues, "ab"); HDebug.Exception(tidx.Length == 3); HDebug.Exception(tidx[0] == 1, tidx[1] == 2, tidx[2] == 3); tidx = HIndexAllContains(tvalues, "abc"); HDebug.Exception(tidx.Length == 2); HDebug.Exception(tidx[0] == 1, tidx[1] == 2); tidx = HIndexAllContains(tvalues, "abc "); HDebug.Exception(tidx.Length == 1); HDebug.Exception(tidx[0] == 2); tidx = HIndexAllContains(tvalues, "abcd"); HDebug.Exception(tidx.Length == 0); } List <int> idxs = new List <int>(); for (int i = 0; i < values.Count; i++) { if (values[i].Contains(contain)) { idxs.Add(i); } } return(idxs.ToArray()); }
/// Matlab.PutSparseMatrix("H", H.GetMatrixSparse(), 3, 3); //public static void PutSparseMatrix<MATRIX>(string name, MatrixSparse<MATRIX> real, int elemColSize, int elemRowSize) //public static void PutSparseMatrix<MATRIX>(string name, IMatrixSparse<MATRIX> real, int elemColSize, int elemRowSize) // where MATRIX : Matrix public static void PutSparseMatrix(string name, IMatrixSparse <MatrixByArr> real, int elemColSize, int elemRowSize, string opt = null) { /// http://www.mathworks.com/help/matlab/ref/sparse.html /// S = sparse(i,j,s,m,n) /// * create m-by-n sparse matrix /// * where S(i(k),j(k)) = s(k) /// * Vectors i, j, and s are all the same length. /// * Any elements of s that are zero are ignored. /// * Any elementsof s that have duplicate values of i and j are added together. if (opt == null) { int m = real.ColSize * elemColSize; int n = real.RowSize * elemRowSize; //if(opt == null) { List <int> i = new List <int>(); List <int> j = new List <int>(); List <double> s = new List <double>(); foreach (var c_r_val in real.EnumElements()) { int c = c_r_val.Item1; int r = c_r_val.Item2; Matrix hesscr = c_r_val.Item3; HDebug.Assert(hesscr != null); HDebug.Assert(hesscr.ColSize == elemColSize, hesscr.RowSize == elemRowSize); for (int dc = 0; dc < elemColSize; dc++) { for (int dr = 0; dr < elemRowSize; dr++) { i.Add(c * elemColSize + dc); j.Add(r * elemRowSize + dr); s.Add(hesscr[dc, dr]); } } } //for(int ii=0; ii<i.Count; ii++) //{ // if(i[ii] == j[ii]) // HDebug.Assert(s[ii] != 0); //} PutVector("htlib2_matlab_PutSparseMatrix.i", i.ToArray()); PutVector("htlib2_matlab_PutSparseMatrix.j", j.ToArray()); PutVector("htlib2_matlab_PutSparseMatrix.s", s.ToArray()); } //else //{ // Execute("htlib2_matlab_PutSparseMatrix.i = [];"); // Execute("htlib2_matlab_PutSparseMatrix.j = [];"); // Execute("htlib2_matlab_PutSparseMatrix.s = [];"); // // int maxleng = 10_000_000; // Count = 134217728 (maximum) // List<int > i = new List<int >(maxleng); // List<int > j = new List<int >(maxleng); // List<double> s = new List<double>(maxleng); // foreach(var c_r_val in real.EnumElements()) // { // int c = c_r_val.Item1; // int r = c_r_val.Item2; // Matrix hesscr = c_r_val.Item3; // HDebug.Assert(hesscr != null); // HDebug.Assert(hesscr.ColSize == elemColSize, hesscr.RowSize == elemRowSize); // for(int dc=0; dc<elemColSize; dc++) // for(int dr=0; dr<elemRowSize; dr++) // { // if(i.Count == maxleng) // { // PutVector("htlib2_matlab_PutSparseMatrix.ix", i.ToArray()); // PutVector("htlib2_matlab_PutSparseMatrix.jx", j.ToArray()); // PutVector("htlib2_matlab_PutSparseMatrix.sx", s.ToArray()); // Execute("htlib2_matlab_PutSparseMatrix.i = [htlib2_matlab_PutSparseMatrix.i; htlib2_matlab_PutSparseMatrix.ix];"); // Execute("htlib2_matlab_PutSparseMatrix.j = [htlib2_matlab_PutSparseMatrix.j; htlib2_matlab_PutSparseMatrix.jx];"); // Execute("htlib2_matlab_PutSparseMatrix.s = [htlib2_matlab_PutSparseMatrix.s; htlib2_matlab_PutSparseMatrix.sx];"); // Execute("clear htlib2_matlab_PutSparseMatrix.ix;"); // Execute("clear htlib2_matlab_PutSparseMatrix.jx;"); // Execute("clear htlib2_matlab_PutSparseMatrix.sx;"); // i.Clear(); // j.Clear(); // s.Clear(); // } // i.Add(c*elemColSize+dc); // j.Add(r*elemRowSize+dr); // s.Add(hesscr[dc, dr]); // } // } // if(i.Count != 0) // { // PutVector("htlib2_matlab_PutSparseMatrix.ix", i.ToArray()); // PutVector("htlib2_matlab_PutSparseMatrix.jx", j.ToArray()); // PutVector("htlib2_matlab_PutSparseMatrix.sx", s.ToArray()); // Execute("htlib2_matlab_PutSparseMatrix.i = [htlib2_matlab_PutSparseMatrix.i; htlib2_matlab_PutSparseMatrix.ix];"); // Execute("htlib2_matlab_PutSparseMatrix.j = [htlib2_matlab_PutSparseMatrix.j; htlib2_matlab_PutSparseMatrix.jx];"); // Execute("htlib2_matlab_PutSparseMatrix.s = [htlib2_matlab_PutSparseMatrix.s; htlib2_matlab_PutSparseMatrix.sx];"); // Execute("htlib2_matlab_PutSparseMatrix.ix = [];"); // Execute("htlib2_matlab_PutSparseMatrix.jx = [];"); // Execute("htlib2_matlab_PutSparseMatrix.sx = [];"); // i.Clear(); // j.Clear(); // s.Clear(); // } // HDebug.Assert(i.Count == 0); // HDebug.Assert(j.Count == 0); // HDebug.Assert(s.Count == 0); //} PutValue("htlib2_matlab_PutSparseMatrix.m", m); PutValue("htlib2_matlab_PutSparseMatrix.n", n); Execute("htlib2_matlab_PutSparseMatrix = sparse(htlib2_matlab_PutSparseMatrix.i+1, htlib2_matlab_PutSparseMatrix.j+1, htlib2_matlab_PutSparseMatrix.s, htlib2_matlab_PutSparseMatrix.m, htlib2_matlab_PutSparseMatrix.n);"); Execute(name + " = htlib2_matlab_PutSparseMatrix;"); Execute("clear htlib2_matlab_PutSparseMatrix;"); } else if (opt == "use file") { string i_path = HFile.GetTempPath(_path_temporary, ".dat"); string j_path = HFile.GetTempPath(_path_temporary, ".dat"); string s_path = HFile.GetTempPath(_path_temporary, ".dat"); ulong count = 0; { System.IO.BinaryWriter i_writer = new System.IO.BinaryWriter(new System.IO.FileStream(i_path, System.IO.FileMode.CreateNew)); System.IO.BinaryWriter j_writer = new System.IO.BinaryWriter(new System.IO.FileStream(j_path, System.IO.FileMode.CreateNew)); System.IO.BinaryWriter s_writer = new System.IO.BinaryWriter(new System.IO.FileStream(s_path, System.IO.FileMode.CreateNew)); foreach (var c_r_val in real.EnumElements()) { int c = c_r_val.Item1; int r = c_r_val.Item2; Matrix hesscr = c_r_val.Item3; HDebug.Assert(hesscr != null); HDebug.Assert(hesscr.ColSize == elemColSize, hesscr.RowSize == elemRowSize); for (int dc = 0; dc < elemColSize; dc++) { for (int dr = 0; dr < elemRowSize; dr++) { count++; double i = (c * elemColSize + dc); double j = (r * elemRowSize + dr); double s = (hesscr[dc, dr]); i_writer.Write(i); j_writer.Write(j); s_writer.Write(s); HDebug.Exception(count > 0); } } } i_writer.Flush(); i_writer.Close(); j_writer.Flush(); j_writer.Close(); s_writer.Flush(); s_writer.Close(); } { int m = real.ColSize * elemColSize; int n = real.RowSize * elemRowSize; PutValue("htlib2_matlab_PutSparseMatrix.m", m); PutValue("htlib2_matlab_PutSparseMatrix.n", n); Execute("htlib2_matlab_PutSparseMatrix.ifid=fopen('" + i_path + "','r');"); Execute("htlib2_matlab_PutSparseMatrix.jfid=fopen('" + j_path + "','r');"); Execute("htlib2_matlab_PutSparseMatrix.sfid=fopen('" + s_path + "','r');"); // A = fread(fileID) reads all the data in the file into a vector of class double. By default, fread reads a file 1 byte at a time, interprets each byte as an 8-bit unsigned integer (uint8), and returns a double array. Execute("htlib2_matlab_PutSparseMatrix.imat=fread(htlib2_matlab_PutSparseMatrix.ifid, [" + count + "],'*double')';"); Execute("htlib2_matlab_PutSparseMatrix.jmat=fread(htlib2_matlab_PutSparseMatrix.jfid, [" + count + "],'*double')';"); Execute("htlib2_matlab_PutSparseMatrix.smat=fread(htlib2_matlab_PutSparseMatrix.sfid, [" + count + "],'*double')';"); Execute("fclose(htlib2_matlab_PutSparseMatrix.ifid);"); Execute("fclose(htlib2_matlab_PutSparseMatrix.jfid);"); Execute("fclose(htlib2_matlab_PutSparseMatrix.sfid);"); Execute("htlib2_matlab_PutSparseMatrix = sparse(htlib2_matlab_PutSparseMatrix.imat+1, htlib2_matlab_PutSparseMatrix.jmat+1, htlib2_matlab_PutSparseMatrix.smat, htlib2_matlab_PutSparseMatrix.m, htlib2_matlab_PutSparseMatrix.n);"); Execute(name + " = htlib2_matlab_PutSparseMatrix;"); Execute("clear htlib2_matlab_PutSparseMatrix;"); } HFile.Delete(i_path); HFile.Delete(j_path); HFile.Delete(s_path); } }