Ejemplo n.º 1
0
        public static PClosestPointOnSegment ClosestPointOnSegment(IList <Vector> ABs, Vector Pt)
        {
            HDebug.Assert(ABs.Count >= 2);
            Vector closest = ABs[0];
            //Tuple<Vector,Vector> closest_segment = new Tuple<Vector,Vector>(ABs[0], ABs[1]);
            double closest_dist2 = (closest - Pt).Dist2;
            double closest_dist  = Math.Sqrt(closest_dist2);
            int    closest_iAB   = 0;

            for (int i = 1; i < ABs.Count; i++)
            {
                int    iAB     = i - 1;
                Vector A       = ABs[iAB];
                Vector B       = ABs[iAB + 1];
                Vector C       = ClosestPointOnLine(A, B, Pt, true);
                double C_dist2 = (C - Pt).Dist2;
                if (C_dist2 < closest_dist2)
                {
                    closest       = C;
                    closest_dist2 = C_dist2;
                    closest_dist  = Math.Sqrt(C_dist2);
                    closest_iAB   = iAB;
                    //closest_segment = new Tuple<Vector, Vector>(A, B);
                }
            }
            HDebug.AssertTolerance(0.00000001, closest_dist - DistPointSegment(Pt, ABs));
            return(new PClosestPointOnSegment
            {
                point = closest,
                dist = closest_dist,
                iAB = closest_iAB,
                //segment = closest_segment,
            });
        }
Ejemplo n.º 2
0
        public static void HLayeredArray2_selftest()
        {
            if (HLayeredArray2_doselftest == false)
            {
                return;
            }
            HLayeredArray2_doselftest = false;

            HDebug.Assert((new HLayeredArray2 <int>(0, 1, 2)).ArrLength == 1);
            HDebug.Assert((new HLayeredArray2 <int>(0, 2, 2)).ArrLength == 1);
            HDebug.Assert((new HLayeredArray2 <int>(0, 3, 2)).ArrLength == 2);
            HDebug.Assert((new HLayeredArray2 <int>(0, 4, 2)).ArrLength == 2);
            HDebug.Assert((new HLayeredArray2 <int>(0, 5, 2)).ArrLength == 3);
            HDebug.Assert((new HLayeredArray2 <int>(0, 6, 2)).ArrLength == 3);

            HLayeredArray2 <int> arr = new HLayeredArray2 <int>(0, 5, 3);

            HDebug.Assert(arr.Count == 0, arr[0] == 0, arr[1] == 0, arr[2] == 0, arr[3] == 0, arr[4] == 0);

            arr[0] = 1; HDebug.Assert(arr.ArrCount == 1, arr[0] == 1, arr[1] == 0, arr[2] == 0, arr[3] == 0, arr[4] == 0);
            arr[1] = 2; HDebug.Assert(arr.ArrCount == 1, arr[0] == 1, arr[1] == 2, arr[2] == 0, arr[3] == 0, arr[4] == 0);
            arr[2] = 3; HDebug.Assert(arr.ArrCount == 1, arr[0] == 1, arr[1] == 2, arr[2] == 3, arr[3] == 0, arr[4] == 0);
            arr[3] = 4; HDebug.Assert(arr.ArrCount == 2, arr[0] == 1, arr[1] == 2, arr[2] == 3, arr[3] == 4, arr[4] == 0);
            arr[4] = 5; HDebug.Assert(arr.ArrCount == 2, arr[0] == 1, arr[1] == 2, arr[2] == 3, arr[3] == 4, arr[4] == 5);

            arr[0] = 0; HDebug.Assert(arr.ArrCount == 2, arr[0] == 0, arr[1] == 2, arr[2] == 3, arr[3] == 4, arr[4] == 5);
            arr[3] = 0; HDebug.Assert(arr.ArrCount == 2, arr[0] == 0, arr[1] == 2, arr[2] == 3, arr[3] == 0, arr[4] == 5);
            arr[1] = 0; HDebug.Assert(arr.ArrCount == 2, arr[0] == 0, arr[1] == 0, arr[2] == 3, arr[3] == 0, arr[4] == 5);
            arr[2] = 0; HDebug.Assert(arr.ArrCount == 1, arr[0] == 0, arr[1] == 0, arr[2] == 0, arr[3] == 0, arr[4] == 5);
            arr[4] = 0; HDebug.Assert(arr.ArrCount == 0, arr[0] == 0, arr[1] == 0, arr[2] == 0, arr[3] == 0, arr[4] == 0);
        }
Ejemplo n.º 3
0
 public static T Deserialize <T>(string filename, int?ver)
 {
     object[] objs;
     HDebug.Verify(_Deserialize(filename, ver, out objs));
     HDebug.Assert(objs.Length == 1);
     return((T)objs[0]);
 }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
 public static Matrix DMD(Vector diagmat1, Matrix mat, Vector diagmat2)
 {
     if (DMD_selftest)
     #region selftest
     {
         HDebug.ToDo("check");
         DMD_selftest = false;
         Vector td1 = new double[] { 1, 2, 3 };
         Vector td2 = new double[] { 4, 5, 6 };
         Matrix tm  = new double[, ] {
             { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 }
         };
         Matrix dmd0 = LinAlg.Diag(td1) * tm * LinAlg.Diag(td2);
         Matrix dmd1 = LinAlg.DMD(td1, tm, td2);
         double err  = (dmd0 - dmd1).HAbsMax();
         HDebug.Assert(err == 0);
     }
     #endregion
     Matrix DMD = mat.Clone();
     for (int c = 0; c < mat.ColSize; c++)
     {
         for (int r = 0; r < mat.RowSize; r++)
         {
             double v0 = mat[c, r];
             double v1 = diagmat1[c] * v0 * diagmat2[r];
             if (v0 == v1)
             {
                 continue;
             }
             DMD[c, r] = v1;
         }
     }
     return(DMD);
 }
Ejemplo n.º 6
0
            internal Node <AvlNodeInfo> AvlInsert(T value)
            {
                HDebug.Assert(root == null || root.IsRoot());
                AvlNodeInfo avlvalue = new AvlNodeInfo
                {
                    value        = value,
                    left_height  = -1, // height of null node is -1
                    right_height = -1, // height of null node is -1
                };

                if (root == null)
                {
                    Node <AvlNodeInfo> node = BstInsert <AvlNodeInfo>(null, ref root, avlvalue, avlcomp);
                    HDebug.Assert(root == node);
                    HDebug.Assert(root.left == null);
                    HDebug.Assert(root.right == null);
                    HDebug.Assert(root.value.height == 0);
                    return(node);
                }
                else
                {
                    Node <AvlNodeInfo> node = BstInsert <AvlNodeInfo>(null, ref root, avlvalue, avlcomp);
                    HDebug.Assert(node.value.height == 0);
                    UpdateBalance(node, ref root);
                    return(node);
                }
            }
Ejemplo n.º 7
0
        public static Vector DV(Matrix D, Vector V, bool assertDiag = true)
        {
            if (DV_SelfTest1)
            {
                DV_SelfTest1 = false;
                MatrixByArr tD = new double[3, 3] {
                    { 1, 0, 0 }, { 0, 2, 0 }, { 0, 0, 3 }
                };
                Vector tV = new double[3] {
                    1, 2, 3
                };
                Vector tDV = new double[3] {
                    1, 4, 9
                };
                // [1 0 0]   [1]   [1]
                // [0 2 0] * [2] = [4]
                // [0 0 3]   [3]   [9]
                HDebug.AssertTolerance(double.Epsilon, DV(tD, tV) - tDV);
            }
            // D is the diagonal matrix
            HDebug.Assert(D.ColSize == D.RowSize);
            if (assertDiag) // check diagonal matrix
            {
                HDebug.AssertToleranceMatrix(double.Epsilon, D - Diag(Diag(D)));
            }
            HDebug.Assert(D.ColSize == V.Size);
            Vector diagD  = Diag(D);
            Vector diagDV = DV(diagD, V);

            return(diagDV);
        }
Ejemplo n.º 8
0
        public static double TorsionalAngle(Vector p1, Vector p2, Vector p3, Vector p4)
        {
            //function angle = torangle(atom1, atom2, atom3, atom4)
            //    plane_123 = cross(atom2-atom1, atom3-atom2); n1 = plane_123; n1 = n1 / sqrt(dot(n1,n1));
            //    plane_234 = cross(atom3-atom2, atom4-atom3); n2 = plane_234; n2 = n2 / sqrt(dot(n2,n2));
            //    vec_23 = atom3 - atom2;                      b  = vec_23;    b  = b  / sqrt(dot(b,b));
            //    angle = atan2(dot(cross(n1,n2),b), dot(n1,n2));
            //end

            Vector plane_123 = LinAlg.CrossProd(p2 - p1, p3 - p2); Vector n1 = plane_123.UnitVector();
            Vector plane_234 = LinAlg.CrossProd(p3 - p2, p4 - p3); Vector n2 = plane_234.UnitVector();
            Vector vec_23 = p3 - p2; Vector b = vec_23.UnitVector();
            double angle = Math.Atan2(LinAlg.VtV(LinAlg.CrossProd(n1, n2), b), LinAlg.VtV(n1, n2));

            {
                Vector p12 = p2 - p1;
                Vector p23 = p3 - p2;
                Vector p34 = p4 - p3;
                Vector nn1 = LinAlg.CrossProd(p12, p23).UnitVector();
                Vector nn2 = LinAlg.CrossProd(p23, p34).UnitVector();
                double a   = AngleBetween(nn1, nn2);
                HDebug.Assert(Math.Abs(Math.Abs(a) - Math.Abs(angle)) < 0.00000001);
            }
            return(angle);
        }
Ejemplo n.º 9
0
 public static List <List <TYPE> > ReadTable <TYPE>(string filename, Parser <TYPE> parser)
 {
     try
     {
         System.IO.StreamReader reader = new System.IO.StreamReader(filename);
         List <List <TYPE> >    table  = new List <List <TYPE> >();
         while (reader.EndOfStream == false)
         {
             string      line    = reader.ReadLine();
             string[]    values  = line.Split(' ', ',', '\t');
             List <TYPE> values_ = new List <TYPE>();
             foreach (string value in values)
             {
                 TYPE value_ = parser(value);
                 values_.Add(value_);
             }
             table.Add(values_);
         }
         reader.Close();
         reader.Dispose();
         return(table);
     }
     catch
     {
         HDebug.Assert(false);
         throw;
     }
 }
Ejemplo n.º 10
0
        int IList <T> .IndexOf(T item)
        {
            long indexof = IndexOf(item);

            HDebug.Assert(indexof <= int.MaxValue);
            return((int)indexof);
        }
Ejemplo n.º 11
0
        public void SetValue(int c, int r, double value)
        {
            int c0 = c / BlkSize; int c1 = c % BlkSize;
            int r0 = r / BlkSize; int r1 = r % BlkSize;

            if (value != 0)
            {
                // assign value
                MatrixByArr lmat = blkmatrix[c0, r0];
                lmat[c1, r1]      = value;
                blkmatrix[c0, r0] = lmat;
                return;
            }

            HDebug.Assert(value == 0);
            if (blkmatrix.HasElement(c0, r0))
            {
                MatrixByArr lmat = blkmatrix[c0, r0];
                lmat[c1, r1]      = value;
                blkmatrix[c0, r0] = lmat;
                return;
            }
            else
            {
                // (blkmatrix[c0,r0] == null) && (value == 0)
                // do nothing
                return;
            }
        }
Ejemplo n.º 12
0
        public static double AngleBetween(Vector a, Vector b, Vector c)
        {
            if (AngleBetween_selftest2)
            {
                AngleBetween_selftest2 = false;
                Vector ta, tb, tc;
                double tang;
                ta   = new double[] { 1, 0, 0 };
                tb   = new double[] { 0, 0, 0 };
                tc   = new double[] { 0, 1, 0 };
                tang = AngleBetween(ta, tb, tc);
                HDebug.AssertTolerance(0.0001, Math.PI / 2 - tang);
                ta   = new double[] { 0, 1, 0 };
                tb   = new double[] { 0, 0, 0 };
                tc   = new double[] { 0, 1, 1 };
                tang = AngleBetween(ta, tb, tc);
                HDebug.AssertTolerance(0.0001, Math.PI / 4 - tang);
            }
            Vector left  = a - b;
            Vector right = c - b;
            double ang   = AngleBetween(left, right);

            HDebug.Assert(ang >= 0);
            HDebug.Assert(ang <= Math.PI);
            return(ang);
        }
Ejemplo n.º 13
0
 public T this[long i]
 {
     get
     {
         int ii = (int)i;
         HDebug.Assert(0 <= i, i < Size);
         if (data.ContainsKey(ii))
         {
             return(data[ii]);
         }
         if (GetDefault != null)
         {
             return(GetDefault());
         }
         return(default(T));
     }
     set
     {
         int ii = (int)i;
         HDebug.Assert(0 <= i, i < Size);
         if (data.ContainsKey(ii))
         {
             data[ii] = value; return;
         }
         data.Add(ii, value);
     }
 }
Ejemplo n.º 14
0
        public static double?GetRootSecant(Func <double, object, double> func, double p0, double p1, object etc, int maxiter, double tolFunc = 0.00000001, double tolP = 0.00000001)
        {
            double v0 = func(p0, etc);
            double v1 = func(p1, etc);

            for (int iter = 0; iter < maxiter; iter++)
            {
                if (Math.Abs(v1 - v0) < tolFunc)
                {
                    //HDebug.AssertSimilar(v1, 0, 0.00000001);
                    return(p1);
                }
                if (Math.Abs(p0 - p1) < tolP)
                {
                    //HDebug.AssertSimilar(v1, 0, 0.00000001);
                    return(p1);
                }
                double dv = -1 * v1 * (p1 - p0) / (v1 - v0);
                p0 = p1; v0 = v1;
                p1 = p1 + dv;
                v1 = func(p1, etc);
                HDebug.Assert(double.IsNaN(v1) == false);
            }

            if (Math.Abs(v1 - 0) < tolFunc)
            {
                return(p1);
            }

            return(null);
        }
Ejemplo n.º 15
0
                public static Vector GetRotAxis(MatrixByArr rot)
                {
                    HDebug.ToDo("write selftest code");
                    // http://en.wikipedia.org/wiki/Rotation_matrix#Determining_the_axis
                    //
                    // Determining the axis
                    //     Given a rotation matrix R, a vector u parallel to the rotation axis must satisfy
                    //         R u = u
                    //     since the rotation of u around the rotation axis must result in u. The equation
                    //     above may be solved for u which is unique up to a scalar factor.
                    //     Further, the equation may be rewritten
                    //         R u = I u  =>  (R-I) u = 0
                    //     which shows that u is the null space of R-I. Viewed another way, u is an eigenvector
                    //     of R corresponding to the eigenvalue λ=1(every rotation matrix must have this eigenvalue).
                    HDebug.Assert(IsRotMatrix(rot));
                    MatrixByArr RI = rot - LinAlg.Eye(3);

                    Vector[] eigvec;
                    double[] eigval;
                    NumericSolver.Eig(RI, out eigvec, out eigval);
                    int    idx  = eigval.HAbs().HIdxMin();
                    Vector axis = eigvec[idx];

                    return(axis);
                }
Ejemplo n.º 16
0
        public static int[] HIdxSorted <T>(this IList <T> values, Comparison <T> comparison)
        {
            Comparison <Tuple <T, int> > mycomparison = delegate(Tuple <T, int> x, Tuple <T, int> y)
            {
                return(comparison(x.Item1, y.Item1));
            };

            List <Tuple <T, int> > sorteds = new List <Tuple <T, int> >(values.Count);

            for (int i = 0; i < values.Count; i++)
            {
                sorteds.Add(new Tuple <T, int>(values[i], i));
            }
            sorteds.Sort(mycomparison);
            int[] idxs = new int[values.Count];
            for (int i = 0; i < values.Count; i++)
            {
                idxs[i] = sorteds[i].Item2;
            }

            string debug = "false";

            if (debug == "true")
            {
                HashSet <int> hsIdxs = new HashSet <int>(idxs);
                HDebug.Assert(values.Count == hsIdxs.Count);
            }

            return(idxs);
        }
Ejemplo n.º 17
0
        public static void Derivative2 <INFO>(Func <Vector[], INFO, double> func, Vector[] x, double dx, ref MatrixByArr[,] dy2, INFO info)
        {
            // assume that (x[i].Size == 3)
            int size = x.Length;

            HDebug.Assert(dy2.GetLength(0) == size, dy2.GetLength(1) == size);
            double  dx2 = dx * dx;
            Vectors xx  = x;

            for (int i = 0; i < size; i++)
            {
                for (int di = 0; di < 3; di++)
                {
                    for (int j = 0; j < size; j++)
                    {
                        for (int dj = 0; dj < 3; dj++)
                        {
                            Vector[] x0 = xx.Clone(); x0[i][di] += dx; x0[j][dj] += dx; double y0 = func(x0, info);
                            Vector[] x1 = xx.Clone(); x1[i][di] -= dx; x1[j][dj] += dx; double y1 = func(x1, info);
                            Vector[] x2 = xx.Clone(); x2[i][di] += dx; x2[j][dj] -= dx; double y2 = func(x2, info);
                            Vector[] x3 = xx.Clone(); x3[i][di] -= dx; x3[j][dj] -= dx; double y3 = func(x3, info);
                            double   d2f_didj = (y0 - y1 - y2 + y3) / (4 * dx2);
                            dy2[i, j][di, dj] = d2f_didj;
                        }
                    }
                }
            }
        }
Ejemplo n.º 18
0
        public List <Node> FindPathMST(Node from, IEnumerable <Node> tos, Func <List <Node>, List <Node>, int> selector)
        {
            HashSet <Node> toset = new HashSet <Node>(tos);

            toset.Remove(null);
            bool[]      visited = new bool[nodes.Count];
            List <Node> found   = null;
            List <Node> probing = new List <Node>();

            Tree <Node> tree = BuildTreeMST(from, null);

            foreach (Node to in toset)
            {
                Tree.Node        treeto   = tree.FindNode(to);
                List <Tree.Node> treepath = tree.NodesFromRootTo(treeto);
                List <Node>      path     = tree.GetValue(treepath);
                if (path.Count == 0)
                {
                    continue;
                }
                HDebug.Assert(path.First() == from, path.Last() == to);
                if (found == null || selector(found, path) == 1)
                {
                    found = path;
                }
            }

            return(found);
        }
Ejemplo n.º 19
0
            public void ChangeComp(Comparison <T> comp)
            {
                Comparison <T> comp0 = _comp;

                _comp = comp;
                HDebug.Assert(Validate());
            }
Ejemplo n.º 20
0
 public static T2[,] HToType <T1, T2>(this T1[,] values)
 {
     if (values == null)
     {
         return(null);
     }
     T2[,] values2 = new T2[values.GetLength(0), values.GetLength(1)];
     for (int i0 = 0; i0 < values.GetLength(0); i0++)
     {
         for (int i1 = 0; i1 < values.GetLength(1); i1++)
         {
             dynamic value  = values[i0, i1];
             T2      value2 = (T2)value;
             if (value != null)
             {
                 HDebug.Assert(value2 != null);
             }
             if (value == null)
             {
                 HDebug.Assert(value2 == null);
             }
             values2[i0, i1] = value2;
         }
     }
     return(values2);
 }
Ejemplo n.º 21
0
        public static Vector DV(Vector D, Vector V, bool assertDiag = true)
        {
            if (DV_SelfTest2)
            {
                DV_SelfTest2 = false;
                Vector tD = new double[3] {
                    1, 2, 3
                };
                Vector tV = new double[3] {
                    1, 2, 3
                };
                Vector tDV = new double[3] {
                    1, 4, 9
                };
                // [1 0 0]   [1]   [1]
                // [0 2 0] * [2] = [4]
                // [0 0 3]   [3]   [9]
                HDebug.AssertTolerance(double.Epsilon, DV(tD, tV) - tDV);
            }
            // D is the diagonal matrix
            HDebug.Assert(D.Size == V.Size);
            int    size = V.Size;
            Vector dv   = new double[size];

            for (int i = 0; i < size; i++)
            {
                dv[i] = D[i] * V[i];
            }
            return(dv);
        }
Ejemplo n.º 22
0
 public static T HMax <T>(this T[,,] values)
     where T : IComparable <T>
 {
     int[] idxmax = HIdxMax(values);
     HDebug.Assert(idxmax.Length == 3);
     return(values[idxmax[0], idxmax[1], idxmax[2]]);
 }
Ejemplo n.º 23
0
        public static double V1tD2V3(Vector V1, Matrix D2, Vector V3, bool assertDiag = true)
        {
            if (V1tD2V3_SelfTest)
            {
                V1tD2V3_SelfTest = false;
                Vector tV1 = new double[3] {
                    1, 2, 3
                };
                MatrixByArr tD2 = new double[3, 3] {
                    { 2, 0, 0 }, { 0, 3, 0 }, { 0, 0, 4 }
                };
                Vector tV3 = new double[3] {
                    3, 4, 5
                };
                //           [2    ]   [3]             [ 6]
                // [1 2 3] * [  3  ] * [4] = [1 2 3] * [12] = 6+24+60 = 90
                //           [    4]   [5]             [20]
                double tV1tD2V3 = 90;
                HDebug.AssertTolerance(double.Epsilon, tV1tD2V3 - V1tD2V3(tV1, tD2, tV3));
            }
            if (assertDiag) // check diagonal matrix
            {
                HDebug.AssertToleranceMatrix(double.Epsilon, D2 - Diag(Diag(D2)));
            }
            HDebug.Assert(V1.Size == D2.ColSize);
            HDebug.Assert(D2.RowSize == V3.Size);

            Vector lD2V3    = DV(D2, V3, assertDiag);
            double lV1tD2V3 = VtV(V1, lD2V3);

            return(lV1tD2V3);
        }
Ejemplo n.º 24
0
 public static T HMin <T>(this T[,,] values)
     where T : IComparable <T>
 {
     int[] idxmin = HIdxMin(values);
     HDebug.Assert(idxmin.Length == 3);
     return(values[idxmin[0], idxmin[1], idxmin[2]]);
 }
Ejemplo n.º 25
0
 public static double[,] GetMatrix(string name, int colsize, int rowsize)
 {
     double[,] matrix = GetMatrix(name);
     HDebug.Assert(matrix.GetLength(0) == colsize);
     HDebug.Assert(matrix.GetLength(1) == rowsize);
     return(matrix);
 }
Ejemplo n.º 26
0
        ///////////////////////////////////////////////////////////////////////////
        ///////////////////////////////////////////////////////////////////////////
        //public static Complex[] Roots2c(double p2, double p1, double p0)
        //{
        //    Complex d = Complex.Sqrt(new Complex(p1 * p1 - 4 * p2 * p0));
        //    Complex[] roots = new Complex[2] {
        //        (-1*p1 + d)/(2*p2),
        //        (-1*p1 - d)/(2*p2)
        //    };
        //    return roots;
        //}
        public static double[] GetRootsClosedFormDegree2(double p2, double p1, double p0)
        {
            if (HDebug.Selftest())
            {
                double[] tsol;

                tsol = GetRootsClosedFormDegree2(1, 2, -3);
                HDebug.Assert(tsol[0] == 1, tsol[1] == -3);

                tsol = GetRootsClosedFormDegree2(4, 3, 10);
                HDebug.Assert(tsol == null);
            }
            double d = p1 * p1 - 4 * p2 * p0;

            if (d >= 0)
            {
                d = Math.Sqrt(d);
                double[] roots = new double[2] {
                    (-1 * p1 + d) / (2 * p2),
                    (-1 * p1 - d) / (2 * p2)
                };
                return(roots);
            }
            return(null);
        }
Ejemplo n.º 27
0
        public static bool IsZero(this Matrix mat)
        {
            if (IsZero_selftest)
            {
                IsZero_selftest = false;
                HDebug.Assert(((MatrixByArr)(new double[2, 2] {
                    { 0, 0 }, { 0, 0 }
                })).IsZero() == true);
                HDebug.Assert(((MatrixByArr)(new double[2, 2] {
                    { 0, 0 }, { 0, 1 }
                })).IsZero() == false);
            }

            for (int c = 0; c < mat.ColSize; c++)
            {
                for (int r = 0; r < mat.RowSize; r++)
                {
                    if (mat[c, r] != 0)
                    {
                        return(false);
                    }
                }
            }
            return(true);
        }
Ejemplo n.º 28
0
        public static void DSW <T>(ref Node <T> root)
        {
            if (DSW_selftest)
            {
                DSW_selftest = false;
                Comparison <int> _compare = delegate(int a, int b) { return(a - b); };
                Node <int>       _root    = null;
                BstInsertRange(ref _root, new int[] { 43, 10, 12, 1, 49, 27, 40, 39, 30, 29, 18, 15, 2, 9, 44, 24, 3, 5, 37, 38, 34, 0, 35, 16, 21, 36, 23, 31, 19, 20, 42, 17, 11, 25, 47, 41, 48, 26, 14, 46 }, _compare);

                HDebug.Assert(_root.IsBalanced() == false);
                HDebug.Assert(_root.ToString() == "(((0,1,(_,2,((_,3,5),9,_))),10,(11,12,(((14,15,(_,16,17)),18,(((_,19,20),21,23),24,(_,25,26))),27,(((29,30,((31,34,(_,35,36)),37,38)),39,_),40,(41,42,_))))),43,((_,44,(46,47,48)),49,_))");
                DSW(ref _root);
                HDebug.Assert(_root.IsBalanced() == true);
                HDebug.Assert(_root.ToString() == "(((((0,1,2),3,(5,9,10)),11,((12,14,15),16,(17,18,19))),20,(((21,23,_),24,25),26,(27,29,30))),31,(((34,35,36),37,(38,39,40)),41,((42,43,44),46,(47,48,49))))");

                //cout << "    7) BST built with values : 43,10,12,1,49,27,40,39,30,29,18,15,2,9,44,24,3,5,37,38,34,0,35,16,21,36,23,31,19,20,42,17,11,25,47,41,48,26,14,46" << endl;
                //int array[40] = { 43,10,12,1,49,27,40,39,30,29,18,15,2,9,44,24,3,5,37,38,34,0,35,16,21,36,23,31,19,20,42,17,11,25,47,41,48,26,14,46 };
                //string bst_string      = "(((0,1,(_,2,((_,3,5),9,_))),10,(11,12,(((14,15,(_,16,17)),18,(((_,19,20),21,23),24,(_,25,26))),27,(((29,30,((31,34,(_,35,36)),37,38)),39,_),40,(41,42,_))))),43,((_,44,(46,47,48)),49,_))";
                //string backbone_string = "(_,0,(_,1,(_,2,(_,3,(_,5,(_,9,(_,10,(_,11,(_,12,(_,14,(_,15,(_,16,(_,17,(_,18,(_,19,(_,20,(_,21,(_,23,(_,24,(_,25,(_,26,(_,27,(_,29,(_,30,(_,31,(_,34,(_,35,(_,36,(_,37,(_,38,(_,39,(_,40,(_,41,(_,42,(_,43,(_,44,(_,46,(_,47,(_,48,49)))))))))))))))))))))))))))))))))))))))";
                //string dsw_string      = "(((((0,1,2),3,(5,9,10)),11,((12,14,15),16,(17,18,19))),20,(((21,23,_),24,25),26,(27,29,30))),31,(((34,35,36),37,(38,39,40)),41,((42,43,44),46,(47,48,49))))";
            }

            DSW_TreeToBackbone(ref root);
            DSW_BackboneToACBT(ref root);
        }
Ejemplo n.º 29
0
 public int Compare(Elem x, Elem y)
 {
     if (x == y)
     {
         return(0);
     }
     if (x.node == y.node)
     {
         HDebug.Assert(false); return(0);
     }
     if (x.dist < y.dist)
     {
         return(-1);
     }
     if (x.dist > y.dist)
     {
         return(1);
     }
     if (x.node.id < y.node.id)
     {
         return(-1);
     }
     if (x.node.id > y.node.id)
     {
         return(1);
     }
     HDebug.Assert(false);
     return(0);
 }
Ejemplo n.º 30
0
 public T GetAtLock(int c, int r)
 {
     HDebug.Assert(0 <= c, c < ColSize, 0 <= r, r < RowSize);
     if (c == r)
     {
         lock (diagonal)
         {
             if (diagonal[c] != null)
             {
                 return(diagonal[c]);
             }
         }
     }
     else
     {
         var offdiagonal_c = offdiagonal[c];
         lock (offdiagonal_c)
         {
             if (offdiagonal_c.ContainsKey(r))
             {
                 return(offdiagonal_c[r]);
             }
         }
     }
     if (GetDefault != null)
     {
         return(GetDefault());
     }
     return(default(T));
 }