public static MatrixByArr MD(MatrixByArr M, Vector D) { // M * Diag(D) if (MD_SelfTest) { MD_SelfTest = false; MatrixByArr tM = new double[3, 3] { { 1, 2, 3 } , { 4, 5, 6 } , { 7, 8, 9 } }; Vector tD = new double[3] { 1, 2, 3 }; MatrixByArr tMD0 = new double[3, 3] { { 1, 4, 9 } , { 4, 10, 18 } , { 7, 16, 27 } }; MatrixByArr tMD1 = MD(tM, tD); MatrixByArr dtMD = tMD0 - tMD1; double maxAbsDtMD = dtMD.ToArray().HAbs().HMax(); Debug.Assert(maxAbsDtMD == 0); } HDebug.Assert(M.RowSize == D.Size); MatrixByArr lMD = new double[M.ColSize, M.RowSize]; for (int c = 0; c < lMD.ColSize; c++) { for (int r = 0; r < lMD.RowSize; r++) { lMD[c, r] = M[c, r] * D[r]; } } return(lMD); }
public static double Det(MatrixByArr mat) { PutMatrix("htlib2_matlab_det", mat.ToArray()); double det = GetValue("det(htlib2_matlab_det)"); Execute("clear htlib2_matlab_det;"); return(det); }
protected static string _ToString(string format, object obj) { try{ if (obj == null) { return(""); } string name = obj.GetType().FullName; if (obj is System.Runtime.CompilerServices.ITuple) { return(_ToString(format, obj as System.Runtime.CompilerServices.ITuple)); } if (obj is System.Collections.IDictionary) { return(_ToString(format, obj as System.Collections.IDictionary)); } //if(name==typeof(Tuple<DoubleVector3,Tuple<double,DoubleVector3>[]> ).FullName) return _ToString (format, (Tuple<DoubleVector3,Tuple<double,DoubleVector3>[]> )obj); //if(name==typeof(Tuple<double,DoubleVector3>[] ).FullName) return _ToString (format, (Tuple<double,DoubleVector3>[] )obj); //if(name==typeof(Tuple<double,DoubleVector3> ).FullName) return _ToString (format, (Tuple<double,DoubleVector3> )obj); //if(name==typeof(Tuple<DoubleVector3,Tuple<DoubleVector3,DoubleVector3>>).FullName) return _ToString (format, (Tuple<DoubleVector3,Tuple<DoubleVector3,DoubleVector3>>)obj); //if(name==typeof(Tuple<DoubleVector3,DoubleVector3> ).FullName) return _ToString (format, (Tuple<DoubleVector3,DoubleVector3> )obj); //if(name==typeof(Tuple<double[],double[]> ).FullName) return _ToString (format, (Tuple<double[],double[]> )obj); //if(name==typeof(Tuple<double,double> ).FullName) return _ToString (format, (Tuple<double,double> )obj); //if(name==typeof(Tuple<double,int> ).FullName) return _ToString (format, (Tuple<double,int> )obj); //if(name==typeof(Tuple<int,double> ).FullName) return _ToString (format, (Tuple<int,double> )obj); //if(name==typeof(Tuple<double,Vector> ).FullName) return _ToString (format, (Tuple<double,Vector> )obj); //if(name==typeof(Tuple<Vector,Vector> ).FullName) return _ToString (format, (Tuple<Vector,Vector> )obj); if (name == typeof(List <Tuple <double, int> >).FullName) { return(_ToString(format, (List <Tuple <double, int> >)obj)); } if (name == typeof(List <Tuple <int, double> >).FullName) { return(_ToString(format, (List <Tuple <int, double> >)obj)); } if (name == typeof(List <Tuple <double, double> >).FullName) { return(_ToString(format, (List <Tuple <double, double> >)obj)); } if (name == typeof(List <double>).FullName) { return(_ToString(format, (List <double>)obj)); } if (name == typeof(List <Vector>).FullName) { return(_ToString(format, (List <Vector>)obj)); } //if(name==typeof(List<DoubleVector3> ).FullName) return _ToString (format, (List<DoubleVector3> )obj); if (name == typeof(List <double[]>).FullName) { return(_ToString(format, (List <double[]>)obj)); } if (name == typeof(List <double[, ]>).FullName) { return(_ToString(format, (List <double[, ]>)obj)); } if (name == typeof(MatrixByArr[]).FullName) { return(_ToString <MatrixByArr> (format, (MatrixByArr[] )obj)); } if (name == typeof(Vector[]).FullName) { return(_ToString <Vector> (format, (Vector[] )obj)); } if (name == typeof(string[]).FullName) { return(_ToString <string> (format, (string[] )obj)); } if (name == typeof(string[, ]).FullName) { return(_ToString(format, (string[, ])obj)); } if (name == typeof(double[]).FullName) { return(_ToString(format, (double[] )obj)); } if (name == typeof(int[]).FullName) { return(_ToString(format, (int[] )obj)); } if (name == typeof(double[, ]).FullName) { return(_ToString(format, (double[, ])obj)); } if (name == typeof(double[, , ]).FullName) { return(_ToString(format, (double[, , ])obj)); } //if(name==typeof(DoubleVector3[] ).FullName) return _ToString (format, (DoubleVector3[] )obj); if (name == typeof(double[][]).FullName) { return(_ToString <double[]> (format, (double[][] )obj)); } if (name == typeof(double[][, ]).FullName) { return(_ToString(format, new List <double[, ]>((double[][, ])obj))); } if (name == typeof(double[, ][]).FullName) { return(_ToString(format, (double[, ][])obj)); } if (name == typeof(List <int>).FullName) { return(_ToString <int> (format, (IEnumerable <int>)obj)); } if (name == typeof(List <string>).FullName) { return(_ToString <string> (format, (IEnumerable <string>)obj)); } if (name == typeof(List <string[]>).FullName) { return(_ToString <string[]> (format, (IEnumerable <string[]>)obj)); } if (name == typeof(List <List <int> >).FullName) { return(_ToString <List <int> > (format, (IEnumerable <List <int> >)obj)); } if (name == typeof(List <List <double> >).FullName) { return(_ToString <List <double> > (format, (IEnumerable <List <double> >)obj)); } if (name == typeof(List <List <string> >).FullName) { return(_ToString <List <string> > (format, (IEnumerable <List <string> >)obj)); } if (name == typeof(List <List <Vector> >).FullName) { return(_ToString <List <Vector> > (format, (IEnumerable <List <Vector> >)obj)); } //if(name==typeof(Dictionary<double,double> ).FullName) return _ToString<double,double> (format, (Dictionary<double,double> )obj); //if(name==typeof(KeyValuePair<double,Vector[]> ).FullName) return _ToString<double,Vector[]> (format, (KeyValuePair<double,Vector[]> )obj); //if(name==typeof(KeyValuePair<double,double[]> ).FullName) return _ToString<double,double[]> (format, (KeyValuePair<double,double[]> )obj); //if(name==typeof(KeyValuePair<double,string> ).FullName) return _ToString<double,string> (format, (KeyValuePair<double,string> )obj); //if(name==typeof(KeyValuePair<int, Tuple<Vector, Vector>> ).FullName) return _ToString<int, Tuple<Vector,Vector>>(format, (KeyValuePair<int, Tuple<Vector, Vector>> )obj); //if(name==typeof(Dictionary<double,double[]> ).FullName) return _ToString<double,double[]> (format, (Dictionary<double,double[]> )obj); //if(name==typeof(Tuple<int , int >).FullName) { var val=(Tuple<int , int >)obj; return _ToString(format, val.Item1, val.Item2); } //if(name==typeof(Tuple<int , double >).FullName) { var val=(Tuple<int , double >)obj; return _ToString(format, val.Item1, val.Item2); } //if(name==typeof(Tuple<int , double, double>).FullName) { var val=(Tuple<int , double, double>)obj; return _ToString(format, val.Item1, val.Item2, val.Item3); } //if(name==typeof(Tuple<double, double >).FullName) { var val=(Tuple<double, double >)obj; return _ToString(format, val.Item1, val.Item2); } //if(name==typeof(Tuple<double, double, double>).FullName) { var val=(Tuple<double, double, double>)obj; return _ToString(format, val.Item1, val.Item2, val.Item3); } //if(name==typeof(Tuple<double, int, int, int >).FullName) { var val=(Tuple<double, int, int, int >)obj; return _ToString(format, val.Item1, val.Item2, val.Item3, val.Item4); } //if(name==typeof(Tuple<int ,Vector >).FullName) { var val=(Tuple<int ,Vector >)obj; return _ToString(format, val.Item1, val.Item2); } //if(name==typeof(Tuple<string,string >).FullName) { var val=(Tuple<string,string >)obj; return _ToString(format, val.Item1, val.Item2); } //if(name==typeof(Tuple<string,int >).FullName) { var val=(Tuple<string,int >)obj; return _ToString(format, val.Item1, val.Item2); } if (name == typeof(KeyValuePair <double, double>).FullName) { KeyValuePair <double, double> kvp = (KeyValuePair <double, double>)obj; return(_ToString(format, kvp.Key, kvp.Value)); } //if(name == typeof(MatrixDouble).FullName) //{ // MatrixDouble data = (MatrixDouble)obj; // return _ToString(data.ToArray()); //} if (name == typeof(MatrixByArr).FullName) { MatrixByArr data = (MatrixByArr)obj; return(_ToString(format, data.ToArray())); } if (name == typeof(Vector).FullName) { Vector data = (Vector)obj; return(_ToString(format, data._data)); } //if(name == typeof(DoubleVector3).FullName) //{ // DoubleVector3 data = (DoubleVector3)obj; // return _ToString(data.v0, data.v1, data.v2); //} //if(name == typeof(DoubleMatrix3).FullName) //{ // DoubleMatrix3 data = (DoubleMatrix3)obj; // return _ToString(data.ToArray()); //} if (name == typeof(int).FullName) { if (format == null) { return(obj.ToString()); } else { return(string.Format(format, (int)obj)); } } if (name == typeof(double).FullName) { string text; if (format == null) { text = ((double)obj).ToString(); } else { text = string.Format(format, (double)obj); } if (text.Contains("E") || text.Contains("e")) { text = text.Replace("e", "*10^"); text = text.Replace("E", "*10^"); } return(text); } if (name == typeof(string).FullName) { string str = obj.ToString(); str = str.Replace("\\", "\\\\"); str = "\"" + str + "\""; return(str); } if (name == typeof(object[]).FullName) { return(_ToString(format, (object[])obj)); } HDebug.Assert(false); HDebug.Break(); return(null); } catch (Exception) { HDebug.Break(); return(null); } }