public static HessMatrix GetHessByKijFij(IList <Vector> coords, double[,] K, double[,] F, HessMatrix hess = null)
        {
            int size = coords.Count;

            if (hess == null)
            {
                hess = HessMatrixDense.ZerosDense(size * 3, size * 3);
            }
            if (K == null)
            {
                K = new double[size, size];
            }
            if (F == null)
            {
                F = new double[size, size];
            }
            /// Parallel.For(0, size, delegate(int i)
            for (int i = 0; i < size; i++)
            {
                for (int j = i; j < size; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (K[i, j] == 0 && F[i, j] == 0)
                    {
                        continue;
                    }

                    MatrixByArr hessij = GetHessByKijFij(coords[i], coords[j], K[i, j], F[i, j]);
                    HDebug.AssertTolerance(0.00000001, K[i, j] - K[j, i]);
                    HDebug.AssertTolerance(0.00000001, F[i, j] - F[j, i]);
                    if (HDebug.IsDebuggerAttached)
                    {
                        HDebug.AssertTolerance(0.00000001, hessij - GetHessByKijFij(coords[j], coords[i], K[j, i], F[j, i]));
                    }

                    int n0 = i * 3;
                    int n1 = j * 3;
                    for (int di = 0; di < 3; di++)
                    {
                        for (int dj = 0; dj < 3; dj++)
                        {
                            hess[n0 + di, n0 + dj] -= hessij[di, dj];
                            hess[n0 + di, n1 + dj] += hessij[di, dj];
                            hess[n1 + di, n0 + dj] += hessij[di, dj];
                            hess[n1 + di, n1 + dj] -= hessij[di, dj];
                        }
                    }
                }
            }
            /// );
            return(hess);
        }
Example #2
0
        //public static void GetHessAnm(IList<Vector> coords, double cutoff, MatrixSparse<MatrixByArr> hess)
        //{
        //    int n = coords.Count;
        //
        //    MatrixSparse<MatrixByArr> lhess = null;
        //    if(HDebug.Selftest())
        //        lhess = new MatrixSparse<MatrixByArr>(n, n, hess.GetDefault);
        //
        //    double cutoff2 = cutoff * cutoff;
        //    for(int i=0; i<n; i++)
        //    {
        //        if(coords[i] == null)
        //            continue;
        //        for(int j=0; j<n; j++)
        //        {
        //            if(i == j)
        //                continue;
        //            if(coords[j] == null)
        //                continue;
        //            Vector vec_ij = coords[j] - coords[i];
        //            double dist2 = vec_ij.Dist2;
        //            if(dist2 > cutoff2)
        //                continue;
        //
        //            Vector unit_ij = vec_ij.UnitVector();
        //            double k_ij = 1;
        //            MatrixByArr hessij = new double[3, 3];
        //            hessij[0, 0] = k_ij * unit_ij[0]*unit_ij[0];
        //            hessij[0, 1] = k_ij * unit_ij[0]*unit_ij[1];
        //            hessij[0, 2] = k_ij * unit_ij[0]*unit_ij[2];
        //            hessij[1, 0] = k_ij * unit_ij[1]*unit_ij[0];
        //            hessij[1, 1] = k_ij * unit_ij[1]*unit_ij[1];
        //            hessij[1, 2] = k_ij * unit_ij[1]*unit_ij[2];
        //            hessij[2, 0] = k_ij * unit_ij[2]*unit_ij[0];
        //            hessij[2, 1] = k_ij * unit_ij[2]*unit_ij[1];
        //            hessij[2, 2] = k_ij * unit_ij[2]*unit_ij[2];
        //            hess[i, j] -= hessij;
        //            hess[i, i] += hessij;
        //            if(lhess != null)
        //            {
        //                lhess[i, j] -= hessij;
        //                lhess[i, i] += hessij;
        //            }
        //        }
        //    }
        //
        //    if(lhess != null)
        //    {
        //        MatrixByArr lhess0 = GetHessAnm(coords, cutoff);
        //        MatrixByArr lhess1 = MatrixByArr.FromMatrixArray(lhess.ToArray());
        //        HDebug.AssertTolerance(0.00000000001, lhess0-lhess1);
        //    }
        //}
        static HessMatrix GetHessAnm_debug(IList <Vector> coords, Matrix Kij)
        {
            //Debug.Assert(AnmHessianSelfTest());

            int        n    = coords.Count;
            HessMatrix hess = HessMatrixDense.ZerosDense(n * 3, n * 3);

            for (int i = 0; i < n; i++)
            {
                if (coords[i] == null)
                {
                    continue;
                }
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        HDebug.Assert(Kij[i, j] == 0);
                        continue;
                    }
                    if (coords[j] == null)
                    {
                        continue;
                    }
                    Vector vec_ij  = coords[j] - coords[i];
                    Vector unit_ij = vec_ij.UnitVector();
                    double k_ij    = Kij[i, j];
                    HDebug.Assert(double.IsNaN(k_ij) == false);
                    if (k_ij == 0)
                    {
                        continue;
                    }
                    hess[i * 3 + 0, j *3 + 0] = -k_ij * unit_ij[0] * unit_ij[0];    hess[i * 3 + 0, i *3 + 0] += k_ij * unit_ij[0] * unit_ij[0];
                    hess[i * 3 + 0, j *3 + 1] = -k_ij * unit_ij[0] * unit_ij[1];    hess[i * 3 + 0, i *3 + 1] += k_ij * unit_ij[0] * unit_ij[1];
                    hess[i * 3 + 0, j *3 + 2] = -k_ij * unit_ij[0] * unit_ij[2];    hess[i * 3 + 0, i *3 + 2] += k_ij * unit_ij[0] * unit_ij[2];
                    hess[i * 3 + 1, j *3 + 0] = -k_ij * unit_ij[1] * unit_ij[0];    hess[i * 3 + 1, i *3 + 0] += k_ij * unit_ij[1] * unit_ij[0];
                    hess[i * 3 + 1, j *3 + 1] = -k_ij * unit_ij[1] * unit_ij[1];    hess[i * 3 + 1, i *3 + 1] += k_ij * unit_ij[1] * unit_ij[1];
                    hess[i * 3 + 1, j *3 + 2] = -k_ij * unit_ij[1] * unit_ij[2];    hess[i * 3 + 1, i *3 + 2] += k_ij * unit_ij[1] * unit_ij[2];
                    hess[i * 3 + 2, j *3 + 0] = -k_ij * unit_ij[2] * unit_ij[0];    hess[i * 3 + 2, i *3 + 0] += k_ij * unit_ij[2] * unit_ij[0];
                    hess[i * 3 + 2, j *3 + 1] = -k_ij * unit_ij[2] * unit_ij[1];    hess[i * 3 + 2, i *3 + 1] += k_ij * unit_ij[2] * unit_ij[1];
                    hess[i * 3 + 2, j *3 + 2] = -k_ij * unit_ij[2] * unit_ij[2];    hess[i * 3 + 2, i *3 + 2] += k_ij * unit_ij[2] * unit_ij[2];
                }
            }
            return(hess);
        }
Example #3
0
        public static HessMatrix GetHessAnm(IList <Vector> coords)
        {
            //Debug.Assert(AnmHessianSelfTest());

            int        n    = coords.Count;
            HessMatrix hess = HessMatrixDense.ZerosDense(n * 3, n * 3);

            for (int i = 0; i < n; i++)
            {
                if (coords[i] == null)
                {
                    continue;
                }
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (coords[j] == null)
                    {
                        continue;
                    }
                    Vector vec_ij = coords[j] - coords[i];
                    double dist2  = vec_ij.Dist2;

                    Vector unit_ij = vec_ij.UnitVector();
                    double k_ij    = 1.0 / dist2;
                    hess[i * 3 + 0, j *3 + 0] = double.Epsilon - k_ij * unit_ij[0] * unit_ij[0]; hess[i * 3 + 0, i *3 + 0] += k_ij * unit_ij[0] * unit_ij[0];
                    hess[i * 3 + 0, j *3 + 1] = double.Epsilon - k_ij * unit_ij[0] * unit_ij[1]; hess[i * 3 + 0, i *3 + 1] += k_ij * unit_ij[0] * unit_ij[1];
                    hess[i * 3 + 0, j *3 + 2] = double.Epsilon - k_ij * unit_ij[0] * unit_ij[2]; hess[i * 3 + 0, i *3 + 2] += k_ij * unit_ij[0] * unit_ij[2];
                    hess[i * 3 + 1, j *3 + 0] = double.Epsilon - k_ij * unit_ij[1] * unit_ij[0]; hess[i * 3 + 1, i *3 + 0] += k_ij * unit_ij[1] * unit_ij[0];
                    hess[i * 3 + 1, j *3 + 1] = double.Epsilon - k_ij * unit_ij[1] * unit_ij[1]; hess[i * 3 + 1, i *3 + 1] += k_ij * unit_ij[1] * unit_ij[1];
                    hess[i * 3 + 1, j *3 + 2] = double.Epsilon - k_ij * unit_ij[1] * unit_ij[2]; hess[i * 3 + 1, i *3 + 2] += k_ij * unit_ij[1] * unit_ij[2];
                    hess[i * 3 + 2, j *3 + 0] = double.Epsilon - k_ij * unit_ij[2] * unit_ij[0]; hess[i * 3 + 2, i *3 + 0] += k_ij * unit_ij[2] * unit_ij[0];
                    hess[i * 3 + 2, j *3 + 1] = double.Epsilon - k_ij * unit_ij[2] * unit_ij[1]; hess[i * 3 + 2, i *3 + 1] += k_ij * unit_ij[2] * unit_ij[1];
                    hess[i * 3 + 2, j *3 + 2] = double.Epsilon - k_ij * unit_ij[2] * unit_ij[2]; hess[i * 3 + 2, i *3 + 2] += k_ij * unit_ij[2] * unit_ij[2];
                }
            }
            return(hess);
        }
Example #4
0
            public static ValueTuple <HessMatrix, Vector> Get_BInvDC_BInvDG_Simple
                (HessMatrix CC
                , HessMatrix DD
                , Vector GG
                , bool process_disp_console
                , double?thld_BinvDC = null
                , bool parallel      = false
                )
            {
                HessMatrix BB_invDD_CC;
                Vector     BB_invDD_GG;

                using (new Matlab.NamedLock(""))
                {
                    Matlab.Execute("clear;"); if (process_disp_console)
                    {
                        System.Console.Write("matlab(");
                    }
                    Matlab.PutMatrix("C", CC); if (process_disp_console)
                    {
                        System.Console.Write("C");                                                 //Matlab.PutSparseMatrix("C", C.GetMatrixSparse(), 3, 3);
                    }
                    Matlab.PutMatrix("D", DD); if (process_disp_console)
                    {
                        System.Console.Write("D");
                    }
                    Matlab.PutVector("G", GG); if (process_disp_console)
                    {
                        System.Console.Write("G");
                    }
                    // Matlab.Execute("BinvDC = C' * inv(D) * C;");
                    {
                        Matlab.Execute("BinvDC = C' * inv(D);");
                        Matlab.Execute("BinvD_G = BinvDC * G;");
                        Matlab.Execute("BinvDC  = BinvDC * C;");
                    }

                    BB_invDD_GG = Matlab.GetVector("BinvD_G");

                    //Matrix BBinvDDCC = Matlab.GetMatrix("BinvDC", true);
                    if (thld_BinvDC != null)
                    {
                        Matlab.Execute("BinvDC(find(abs(BinvDC) < " + thld_BinvDC.ToString() + ")) = 0;");
                    }

                    Func <int, int, HessMatrix> Zeros = delegate(int colsize, int rowsize)
                    {
                        return(HessMatrixDense.ZerosDense(colsize, rowsize));
                    };
                    BB_invDD_CC = Matlab.GetMatrix("BinvDC", Zeros, true);
                    if (process_disp_console)
                    {
                        System.Console.Write("Y), ");
                    }

                    Matlab.Execute("clear;");
                }
                //GC.Collect(0);

                return(new ValueTuple <HessMatrix, Vector>
                           (BB_invDD_CC
                           , BB_invDD_GG
                           ));
            }
            private static HessMatrix GetHessCoarseResiIterImpl_Matlab_IterLowerTri_Get_BInvDC
                (HessMatrix A
                , HessMatrix C
                , HessMatrix D
                , bool process_disp_console
                , string[] options
                , double?thld_BinvDC = null
                , bool parallel      = false
                )
            {
                if (options == null)
                {
                    options = new string[0];
                }

                HessMatrix            B_invD_C;
                Dictionary <int, int> Cbr_CCbr = new Dictionary <int, int>();
                List <int>            CCbr_Cbr = new List <int>();

                foreach (ValueTuple <int, int, MatrixByArr> bc_br_bval in C.EnumBlocks())
                {
                    int Cbr = bc_br_bval.Item2;
                    if (Cbr_CCbr.ContainsKey(Cbr) == false)
                    {
                        HDebug.Assert(Cbr_CCbr.Count == CCbr_Cbr.Count);
                        int CCbr = Cbr_CCbr.Count;
                        Cbr_CCbr.Add(Cbr, CCbr);
                        CCbr_Cbr.Add(Cbr);
                        HDebug.Assert(CCbr_Cbr[CCbr] == Cbr);
                    }
                }

                HessMatrix CC = C.Zeros(C.ColSize, Cbr_CCbr.Count * 3);

                {
                    Action <ValueTuple <int, int, MatrixByArr> > func = delegate(ValueTuple <int, int, MatrixByArr> bc_br_bval)
                    {
                        int Cbc = bc_br_bval.Item1; int CCbc = Cbc;
                        int Cbr = bc_br_bval.Item2; int CCbr = Cbr_CCbr[Cbr];
                        var bval = bc_br_bval.Item3;
                        lock (CC)
                            CC.SetBlock(CCbc, CCbr, bval);
                    };

                    if (parallel)
                    {
                        Parallel.ForEach(C.EnumBlocks(), func);
                    }
                    else
                    {
                        foreach (var bc_br_bval in C.EnumBlocks())
                        {
                            func(bc_br_bval);
                        }
                    }
                }
                if (process_disp_console)
                {
                    System.Console.Write("squeezeC({0,6}->{1,6} blk), ", C.RowBlockSize, CC.RowBlockSize);
                }
                {
                    /// If a diagonal element of D is null, that row and column should be empty.
                    /// This assume that the atom is removed. In this case, the removed diagonal block
                    /// is replace as the 3x3 identity matrix.
                    ///
                    ///  [B1  0] [ A 0 ]^-1 [C1 C2 C3] = [B1  0] [ A^-1  0    ] [C1 C2 C3]
                    ///  [B2  0] [ 0 I ]    [ 0  0  0]   [B2  0] [ 0     I^-1 ] [ 0  0  0]
                    ///  [B3  0]                         [B3  0]
                    ///                                = [B1.invA  0] [C1 C2 C3]
                    ///                                  [B2.invA  0] [ 0  0  0]
                    ///                                  [B3.invA  0]
                    ///                                = [B1.invA.C1  B1.invA.C2  B1.invA.C3]
                    ///                                  [B2.invA.C1  B2.invA.C2  B2.invA.C3]
                    ///                                  [B3.invA.C1  B3.invA.C2  B3.invA.C3]
                    ///
                    {
                        //HDebug.Exception(D.ColBlockSize == D.RowBlockSize);
                        for (int bi = 0; bi < D.ColBlockSize; bi++)
                        {
                            if (D.HasBlock(bi, bi) == true)
                            {
                                continue;
                            }
                            //for(int bc=0; bc< D.ColBlockSize; bc++) HDebug.Exception( D.HasBlock(bc, bi) == false);
                            //for(int br=0; br< D.RowBlockSize; br++) HDebug.Exception( D.HasBlock(bi, br) == false);
                            //for(int br=0; br<CC.RowBlockSize; br++) HDebug.Exception(CC.HasBlock(bi, br) == false);
                            D.SetBlock(bi, bi, new double[3, 3] {
                                { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 }
                            });
                        }
                    }

                    HessMatrix BB_invDD_CC;
                    using (new Matlab.NamedLock(""))
                    {
                        Matlab.Execute("clear;");   if (process_disp_console)
                        {
                            System.Console.Write("matlab(");
                        }
                        Matlab.PutMatrix("C", CC);  if (process_disp_console)
                        {
                            System.Console.Write("C");                                                  //Matlab.PutSparseMatrix("C", CC.GetMatrixSparse(), 3, 3);
                        }
                        Matlab.PutMatrix("D", D);   if (process_disp_console)
                        {
                            System.Console.Write("D");
                        }

                        // Matlab.Execute("BinvDC = (C' / D) * C;");
                        {
                            if (options.Contains("pinv(D)"))
                            {
                                Matlab.Execute("BinvDC = C' * pinv(D) * C;");
                            }
                            if (options.Contains("/D -> pinv(D)"))
                            {
                                string msg = Matlab.Execute("BinvDC = (C' / D) * C;", true);
                                if (msg != "")
                                {
                                    Matlab.Execute("BinvDC = C' * pinv(D) * C;");
                                }
                            }
                            else if (options.Contains("/D"))
                            {
                                Matlab.Execute("BinvDC = (C' / D) * C;");
                            }
                            else
                            {
                                Matlab.Execute("BinvDC = (C' / D) * C;");
                            }
                        }
                        if (process_disp_console)
                        {
                            System.Console.Write("X");
                        }

                        //Matrix BBinvDDCC = Matlab.GetMatrix("BinvDC", true);
                        if (thld_BinvDC != null)
                        {
                            Matlab.Execute("BinvDC(find(BinvDC < " + thld_BinvDC.ToString() + ")) = 0;");
                        }
                        if (Matlab.GetValue("nnz(BinvDC)/numel(BinvDC)") > 0.5 || HDebug.True)
                        {
                            Func <int, int, HessMatrix> Zeros = delegate(int colsize, int rowsize)
                            {
                                return(HessMatrixDense.ZerosDense(colsize, rowsize));
                            };
                            BB_invDD_CC = Matlab.GetMatrix("BinvDC", Zeros, true);
                            if (process_disp_console)
                            {
                                System.Console.Write("Y), ");
                            }
                        }
                        else
                        {
                            Matlab.Execute("[i,j,s] = find(sparse(BinvDC));");
                            TVector <int>    listi = Matlab.GetVectorLargeInt("i", true);
                            TVector <int>    listj = Matlab.GetVectorLargeInt("j", true);
                            TVector <double> lists = Matlab.GetVectorLarge("s", true);
                            int colsize            = Matlab.GetValueInt("size(BinvDC,1)");
                            int rowsize            = Matlab.GetValueInt("size(BinvDC,2)");

                            Dictionary <ValueTuple <int, int>, MatrixByArr> lst_bc_br_bval = new Dictionary <ValueTuple <int, int>, MatrixByArr>();
                            for (long i = 0; i < listi.SizeLong; i++)
                            {
                                int    c = listi[i] - 1; int bc = c / 3; int ic = c % 3;
                                int    r = listj[i] - 1; int br = r / 3; int ir = r % 3;
                                double v = lists[i];
                                ValueTuple <int, int> bc_br = new ValueTuple <int, int>(bc, br);
                                if (lst_bc_br_bval.ContainsKey(bc_br) == false)
                                {
                                    lst_bc_br_bval.Add(bc_br, new double[3, 3]);
                                }
                                lst_bc_br_bval[bc_br][ic, ir] = v;
                            }

                            //  Matrix BBinvDDCC = Matrix.Zeros(colsize, rowsize);
                            //  for(int i=0; i<listi.Length; i++)
                            //      BBinvDDCC[listi[i]-1, listj[i]-1] = lists[i];
                            //  //GC.Collect(0);
                            BB_invDD_CC = D.Zeros(colsize, rowsize);
                            foreach (var bc_br_bval in lst_bc_br_bval)
                            {
                                int bc   = bc_br_bval.Key.Item1;
                                int br   = bc_br_bval.Key.Item2;
                                var bval = bc_br_bval.Value;
                                BB_invDD_CC.SetBlock(bc, br, bval);
                            }
                            if (process_disp_console)
                            {
                                System.Console.Write("Z), ");
                            }

                            if (HDebug.IsDebuggerAttached)
                            {
                                for (int i = 0; i < listi.Size; i++)
                                {
                                    int    c = listi[i] - 1;
                                    int    r = listj[i] - 1;
                                    double v = lists[i];
                                    HDebug.Assert(BB_invDD_CC[c, r] == v);
                                }
                            }
                        }
                        Matlab.Execute("clear;");
                    }
                    //GC.Collect(0);

                    B_invD_C = A.Zeros(C.RowSize, C.RowSize);
                    {
                        //  for(int bcc=0; bcc<CCbr_Cbr.Count; bcc++)
                        //  {
                        //      int bc = CCbr_Cbr[bcc];
                        //      for(int brr=0; brr<CCbr_Cbr.Count; brr++)
                        //      {
                        //          int br   = CCbr_Cbr[brr];
                        //          HDebug.Assert(B_invD_C.HasBlock(bc, br) == false);
                        //          if(BB_invDD_CC.HasBlock(bcc, brr) == false)
                        //              continue;
                        //          var bval = BB_invDD_CC.GetBlock(bcc, brr);
                        //          B_invD_C.SetBlock(bc, br, bval);
                        //          HDebug.Exception(A.HasBlock(bc, bc));
                        //          HDebug.Exception(A.HasBlock(br, br));
                        //      }
                        //  }
                        Action <ValueTuple <int, int, MatrixByArr> > func = delegate(ValueTuple <int, int, MatrixByArr> bcc_brr_bval)
                        {
                            int bcc  = bcc_brr_bval.Item1;
                            int brr  = bcc_brr_bval.Item2;
                            var bval = bcc_brr_bval.Item3;

                            int bc = CCbr_Cbr[bcc];
                            int br = CCbr_Cbr[brr];
                            //lock(B_invD_C)
                            B_invD_C.SetBlockLock(bc, br, bval);
                        };

                        if (parallel)
                        {
                            Parallel.ForEach(BB_invDD_CC.EnumBlocks(), func);
                        }
                        else
                        {
                            foreach (var bcc_brr_bval in BB_invDD_CC.EnumBlocks())
                            {
                                func(bcc_brr_bval);
                            }
                        }
                    }
                }
                GC.Collect(0);
                return(B_invD_C);
            }
Example #6
0
            public static CTesthess ReadHess
                (Xyz xyz
                , string outputpath
                , Dictionary <string, string[]> optOutSource  // =null
                , Func <int, int, HessMatrix> HessMatrixZeros // =null
                )
            {
                int size = xyz.atoms.Length;

                #region format: output.txt
                ///      ######################################################################
                ///    ##########################################################################
                ///   ###                                                                      ###
                ///  ###            TINKER  ---  Software Tools for Molecular Design            ###
                ///  ##                                                                          ##
                ///  ##                        Version 6.2  February 2013                        ##
                ///  ##                                                                          ##
                ///  ##               Copyright (c)  Jay William Ponder  1990-2013               ##
                ///  ###                           All Rights Reserved                          ###
                ///   ###                                                                      ###
                ///    ##########################################################################
                ///      ######################################################################
                ///
                ///
                ///  Atoms with an Unusual Number of Attached Atoms :
                ///
                ///  Type           Atom Name      Atom Type       Expected    Found
                ///
                ///  Valence        1496-NR2           69              2         3
                ///  Valence        2438-FE           107              6         5
                ///
                ///  Diagonal Hessian Elements for Each Atom :
                ///
                ///       Atom                 X               Y               Z
                ///
                ///          1            1445.9830       1358.3447       1484.8642
                ///          2             212.9895        294.4584        604.5062
                ///          3             187.1986        751.6727        125.0336
                ///          4             813.4799        132.1743        151.5707
                ///        ...
                ///       2507             178.7277        479.7434        243.4103
                ///       2508             969.8813        977.2507       1845.6726
                ///       2509             390.1648        232.0577       1029.6844
                ///       2510             322.3674        337.8749        996.8230
                ///
                ///  Sum of Diagonal Hessian Elements :          5209042.9060
                ///
                ///  Hessian Matrix written to File :  test.hes
                #endregion
                Tuple <int, double, double, double>[] outDiagHessElems = new Tuple <int, double, double, double> [size];
                double?outSumDiagHessElem = null;
                string outHessMatFile     = null;
                string outHessFormat      = null;
                {
                    string step = "";
                    foreach (string _line in HFile.ReadLines(outputpath))
                    {
                        string line = _line.Trim();
                        if (line.Length == 0)
                        {
                            continue;
                        }
                        if (line.Trim().StartsWith("#"))
                        {
                            continue;
                        }
                        if (line.Contains(" :"))
                        {
                            if (line.Contains("Atoms with an Unusual Number of Attached Atoms"))
                            {
                                step = "";                  continue;
                            }
                            else if (line.Contains("Diagonal Hessian Elements for Each Atom"))
                            {
                                step = "hess diagonal";     continue;
                            }
                            else if (line.Contains("Sum of Diagonal Hessian Elements"))
                            {
                                step = "sum hess diagonal";
                            }
                            else if (line.Contains("Hessian Matrix written to File"))
                            {
                                step = "full hess file";
                            }
                            else if (line.Contains("Hessian Sparse Matrix written to File"))
                            {
                                step = "sparse hess file";
                            }
                            else if (line.Contains("Select Smaller Size for Sparse Hessian Matrix"))
                            {
                                continue;
                            }
                            else
                            {
                                HDebug.Assert(false);
                                continue;
                            }
                        }
                        switch (step)
                        {
                        case "":
                            continue;

                        case "hess diagonal":
                        {
                            string[] tokens = line.Split().HRemoveAll("");
                            if (tokens[0] == "Atom")
                            {
                                continue;
                            }
                            int    Atom = int.Parse(tokens[0]);         // ; if(   int.TryParse(tokens[0], out Atom) == false) continue;
                            double X    = double.Parse(tokens[1]);      // ; if(double.TryParse(tokens[1], out    X) == false) continue;
                            double Y    = double.Parse(tokens[2]);      // ; if(double.TryParse(tokens[2], out    Y) == false) continue;
                            double Z    = double.Parse(tokens[3]);      // ; if(double.TryParse(tokens[3], out    Z) == false) continue;
                            HDebug.Assert(outDiagHessElems[Atom - 1] == null);
                            outDiagHessElems[Atom - 1] = new Tuple <int, double, double, double>(Atom, X, Y, Z);
                        }
                            continue;

                        case "sum hess diagonal":
                        {
                            string[] tokens = line.Split().HRemoveAll("");
                            double   sum    = double.Parse(tokens.Last());    // if(double.TryParse(tokens.Last(), out sum) == false) continue;
                            outSumDiagHessElem = sum;
                        }
                            step = "";
                            continue;

                        case   "full hess file": outHessFormat = "full matrix"; goto case "hess file";

                        case "sparse hess file": outHessFormat = "sparse matrix"; goto case "hess file";

                        case "hess file":
                        {
                            string[] tokens         = line.Split().HRemoveAll("");
                            string   outputpath_dir = HFile.GetFileInfo(outputpath).Directory.FullName;
                            outHessMatFile = outputpath_dir + "\\" + tokens.Last();
                        }
                            step = "";
                            continue;

                        default:
                            HDebug.Assert(false);
                            step = "";
                            continue;
                        }
                    }
                }
                #region Format: test.hes
                /// Diagonal Hessian Elements  (3 per Atom)
                ///
                ///   1445.9830   1358.3447   1484.8642    212.9895    294.4584    604.5062
                ///    187.1986    751.6727    125.0336    813.4799    132.1743    151.5707
                ///   1167.7472   1188.3459   1162.5475    268.8291    498.0188    143.2438
                ///   ...
                /// Off-diagonal Hessian Elements for Atom     1 X
                ///     23.0878     14.6380   -214.0545     96.9308   -186.7740   -205.1460
                ///   -208.7035    -28.9630   -788.9124     39.6748    126.4491   -190.5074
                ///   ...
                /// Off-diagonal Hessian Elements for Atom     1 Y
                ///    -64.1616     97.2697   -292.3813    264.1922   -184.4701   -728.8687
                ///   -109.9103     15.4168   -152.5717    -11.7733     18.8369   -188.4875
                ///   ...
                /// Off-diagonal Hessian Elements for Atom     1 Z
                ///   -155.9409    219.8782   -610.0622    -10.0041    -62.8209   -156.1112
                ///     77.0829    -14.2127   -166.0525     53.1834    -97.4207   -347.9315
                ///   ...
                /// Off-diagonal Hessian Elements for Atom     2 X
                ///   -106.5166    185.8395     19.7546    -10.0094     25.8072     -8.1335
                ///     35.8501    -64.8320      3.8761    -13.8893      9.4266      0.1312
                ///   ...
                ///
                ///   ...
                ///
                /// Off-diagonal Hessian Elements for Atom  2508 X
                ///    488.4590    -69.2721   -338.4824   -155.1656    253.3732   -297.4214
                ///   -164.8301   -191.3769
                /// Off-diagonal Hessian Elements for Atom  2508 Y
                ///     74.0161   -149.1060   -273.4863    176.4975   -170.6527   -341.1639
                ///   -263.6141
                /// Off-diagonal Hessian Elements for Atom  2508 Z
                ///    304.9942    223.7496   -851.3028   -242.9481   -310.7953   -821.2532
                /// Off-diagonal Hessian Elements for Atom  2509 X
                ///    198.4498   -331.6530     78.8172     17.3909    -30.0512
                /// Off-diagonal Hessian Elements for Atom  2509 Y
                ///   -227.5916     25.7235     74.5131    -53.2690
                /// Off-diagonal Hessian Elements for Atom  2509 Z
                ///     46.7873     20.7615   -168.0704
                /// Off-diagonal Hessian Elements for Atom  2510 X
                ///    252.6853    247.2963
                /// Off-diagonal Hessian Elements for Atom  2510 Y
                ///    343.6797
                #endregion
                HessMatrix hess;
                {
                    if (HessMatrixZeros != null)
                    {
                        hess = HessMatrixZeros(size * 3, size * 3);
                    }
                    else
                    {
                        switch (outHessFormat)
                        {
                        case "full matrix": hess = HessMatrixDense.ZerosDense(size * 3, size * 3); break;

                        case "sparse matrix": hess = HessMatrixSparse.ZerosSparse(size * 3, size * 3); break;

                        default:              hess = null;                                             break;
                        }
                    }
                    //HDebug.SetEpsilon(hess);
                    string step    = "";
                    int    diagidx = -1;
                    int    offidx1 = -1;
                    int    offidx2 = -1;

                    /// string[] lines = HFile.ReadAllLines(outHessMatFile);
                    /// GC.WaitForFullGCComplete();
                    /// object[] tokenss = new object[lines.Length];
                    ///
                    /// Parallel.For(0, lines.Length, delegate(int i)
                    /// //for(int i=0; i<lines.Length; i++)
                    /// {
                    ///     string line = lines[i];
                    ///     string[] stokens = line.Trim().Split().RemoveAll("");
                    ///     if(stokens.Length != 0)
                    ///     {
                    ///         double[] dtokens = new double[stokens.Length];
                    ///         for(int j=0; j<stokens.Length; j++)
                    ///             if(double.TryParse(stokens[j], out dtokens[j]) == false)
                    ///             {
                    ///                 tokenss[i] = stokens;
                    ///                 goto endfor;
                    ///             }
                    ///         tokenss[i] = dtokens;
                    ///     }
                    /// endfor: ;
                    /// });

                    /// System.IO.StreamReader matreader = HFile.OpenText(outHessMatFile);
                    /// while(matreader.EndOfStream == false)
                    /// for(int i=0; i<lines.Length; i++)
                    /// foreach(string line in lines)
                    foreach (string line in HFile.HEnumAllLines(outHessMatFile))
                    {
                        ///     string line = lines[i];
                        ///     string line = matreader.ReadLine();
                        string[] tokens = line.Trim().Split().HRemoveAll("");
                        if (tokens.Length == 0)
                        {
                            continue;
                        }
                        ///     if(tokenss[i] == null)
                        ///         continue;
                        ///     string[] stokens = (tokenss[i] as string[]);
                        if (tokens[0] == "Diagonal" && tokens[1] == "Hessian" && tokens[2] == "Elements")
                        {
                            step = "Diagonal"; diagidx = 0; continue;
                        }
                        if (tokens[0] == "Off-diagonal" && tokens[1] == "Hessian" && tokens[2] == "Elements")
                        {
                            step = "Off-diagonal"; offidx1++; offidx2 = offidx1 + 1; continue;
                        }
                        if (tokens[0] == "Off-diagonal" && tokens[1] == "sparse" && tokens[2] == "Hessian" &&
                            tokens[3] == "Indexes/Elements" && tokens[4] == "for" && tokens[5] == "Atom")
                        {
                            step    = "Off-diagonal sparse";
                            offidx1 = (int.Parse(tokens[6]) - 1) * 3;
                            switch (tokens[7])
                            {
                            case "X": break;

                            case "Y": offidx1 += 1; break;

                            case "Z": offidx1 += 2; break;

                            default: throw new HException();
                            }
                            continue;
                        }

                        ///     double[] dtokens = (tokenss[i] as double[]);
                        ///     tokens = new string[20];
                        ///     for(int i=0; i*12<line.Length; i++)
                        ///         tokens[i] = line.Substring(i*12, 12).Trim();
                        ///     tokens = tokens.HRemoveAll("").ToArray();
                        ///     tokens = tokens.HRemoveAll(null).ToArray();

                        switch (step)
                        {
                        case "Diagonal":
                        {
                            ///                 foreach(double val in dtokens)
                            foreach (string token in tokens)
                            {
                                HDebug.Assert(token.Last() != ' ');
                                double val = double.Parse(token);
                                HDebug.Assert(hess[diagidx, diagidx] == 0);
                                hess[diagidx, diagidx] = val;
                                diagidx++;
                            }
                        }
                            continue;

                        case "Off-diagonal":
                        {
                            ///                 foreach(double val in dtokens)
                            foreach (string token in tokens)
                            {
                                HDebug.Assert(token.Last() != ' ');
                                //double val = double.Parse(token);
                                double val;
                                bool   succ = double.TryParse(token, out val);
                                if (succ == false)
                                {
                                    HDebug.Assert(false);
                                    goto case "exception 0";
                                }
                                if (size < 3000)
                                {
                                    HDebug.Assert(hess[offidx1, offidx2] == 0);
                                    HDebug.Assert(hess[offidx2, offidx1] == 0);
                                }
                                if (val != 0)
                                {
                                    hess[offidx1, offidx2] = val;
                                    hess[offidx2, offidx1] = val;
                                }
                                offidx2++;
                            }
                        }
                            continue;

                        case "Off-diagonal sparse":
                        {
                            HDebug.Assert(tokens.Length % 2 == 0);
                            for (int i = 0; i < tokens.Length; i += 2)
                            {
                                HDebug.Assert(tokens[i].Last() != ' ');
                                HDebug.Assert(tokens[i + 1].Last() != ' ');
                                //double val = double.Parse(token);
                                double val;
                                bool   succ = true;
                                succ &= int.TryParse(tokens[i], out offidx2); offidx2--;
                                succ &= double.TryParse(tokens[i + 1], out val);
                                if (succ == false)
                                {
                                    HDebug.Assert(false);
                                    goto case "exception 0";
                                }
                                if (size < 3000)
                                {
                                    HDebug.Assert(hess[offidx1, offidx2] == 0);
                                    HDebug.Assert(hess[offidx2, offidx1] == 0);
                                }
                                if (val != 0)
                                {
                                    hess[offidx1, offidx2] = val;
                                    hess[offidx2, offidx1] = val;
                                }
                            }
                        }
                            continue;

                        default:
                            HDebug.Assert(false);
                            goto case "exception 1";

                        case "exception 0":
                            throw new HException("incomplete hessian file (0)");

                        case "exception 1":
                            throw new HException("incomplete hessian file (1)");
                        }
                        throw new HException("incomplete hessian file (2)");
                    }
                    /// matreader.Close();

                    if (HDebug.IsDebuggerAttached && size < 3000)
                    {
                        //for(int c=0; c<size*3; c++)
                        //    for(int r=0; r<size*3; r++)
                        //        if(hess[c, r] == double.Epsilon)
                        //            throw new Exception("incomplete hessian file (3)");
                        //
                        //for(int c=0; c<size*3; c++)
                        //{
                        //    double sum = 0;
                        //    double max = double.NegativeInfinity;
                        //    for(int r=0; r<size*3; r++)
                        //    {
                        //        max = Math.Max(max, hess[c, r]);
                        //        sum += hess[c, r];
                        //        HDebug.Assert(hess[c, r] != double.Epsilon);
                        //    }
                        //    HDebug.Assert(Math.Abs(sum) < max/1000);
                        //    HDebug.AssertTolerance(0.1, sum);
                        //}

                        Hess.CheckHessDiag(hess, 0.1, null);

                        for (int i = 0; i < size; i++)
                        {
                            HDebug.Assert(outDiagHessElems[i].Item1 == i + 1);
                            HDebug.AssertTolerance(0.000001, outDiagHessElems[i].Item2 - hess[i * 3 + 0, i * 3 + 0]);
                            HDebug.AssertTolerance(0.000001, outDiagHessElems[i].Item3 - hess[i * 3 + 1, i * 3 + 1]);
                            HDebug.AssertTolerance(0.000001, outDiagHessElems[i].Item4 - hess[i * 3 + 2, i * 3 + 2]);
                        }
                    }
                }

                if (optOutSource != null)
                {
                    optOutSource.Add("test.hes", HFile.HEnumAllLines(outHessMatFile).ToArray());
                }

                return(new CTesthess
                {
                    hess = hess,
                    xyz = xyz,
                });
            }
Example #7
0
        public static HessMatrix GetHessAnm(IList <Vector> coords, IEnumerable <Tuple <int, int, double> > enumKij)
        {
            if (GetHessAnm_selftest)
            {
                GetHessAnm_selftest = false;
                Vector[] tcoords = new Vector[]
                {
                    new Vector(0, 0, 0),
                    new Vector(1, 0, 0),
                    new Vector(0, 2, 0),
                    new Vector(0, 0, 3),
                    new Vector(4, 5, 0),
                    new Vector(0, 6, 7),
                    new Vector(8, 0, 9),
                    new Vector(-1, -1, -1),
                };
                Matrix tKij = Matrix.Ones(8, 8);
                for (int i = 0; i < 8; i++)
                {
                    tKij[i, i] = 0;
                }
                HessMatrix hess0 = GetHessAnm_debug(tcoords, tKij);
                HessMatrix hess1 = GetHessAnm(tcoords, tKij.HEnumNonZeros());
                HDebug.Assert((hess0 - hess1).HAbsMax() == 0);
            }
            //Debug.Assert(AnmHessianSelfTest());

            int        n    = coords.Count;
            HessMatrix hess = null;

            if (n < 100)
            {
                hess = HessMatrixDense.ZerosDense(n * 3, n * 3);
            }
            else
            {
                hess = HessMatrixSparse.ZerosSparse(n * 3, n * 3);
            }
            int numset = 0;

            foreach (var kij in enumKij)
            {
                int    i    = kij.Item1;
                int    j    = kij.Item2;
                double k_ij = kij.Item3;
                {
                    if (i == j)
                    {
                        HDebug.Assert(k_ij == 0);
                        continue;
                    }
                    if (coords[j] == null)
                    {
                        continue;
                    }
                    Vector vec_ij  = coords[j] - coords[i];
                    Vector unit_ij = vec_ij.UnitVector();
                    HDebug.Assert(double.IsNaN(k_ij) == false);
                    if (k_ij == 0)
                    {
                        continue;
                    }
                    MatrixByArr hessij = new double[3, 3];
                    hessij[0, 0] = -k_ij * unit_ij[0] * unit_ij[0];
                    hessij[0, 1] = -k_ij * unit_ij[0] * unit_ij[1];
                    hessij[0, 2] = -k_ij * unit_ij[0] * unit_ij[2];
                    hessij[1, 0] = -k_ij * unit_ij[1] * unit_ij[0];
                    hessij[1, 1] = -k_ij * unit_ij[1] * unit_ij[1];
                    hessij[1, 2] = -k_ij * unit_ij[1] * unit_ij[2];
                    hessij[2, 0] = -k_ij * unit_ij[2] * unit_ij[0];
                    hessij[2, 1] = -k_ij * unit_ij[2] * unit_ij[1];
                    hessij[2, 2] = -k_ij * unit_ij[2] * unit_ij[2];
                    hess.SetBlock(i, j, hessij);
                    MatrixByArr hessii = hess.GetBlock(i, i) - hessij;
                    hess.SetBlock(i, i, hessii);
                    numset++;
                }
            }

            return(hess);
        }
Example #8
0
        public static HessMatrix GetHessAnm(IList <Vector> coords, IList <double> cutoffs, string options = "")
        {
            if (coords.Count > 10000)
            {
                HDebug.ToDo("System size is too big. Use EnumHessAnmSpr() and other GetHessAnm()");
            }

            //Debug.Assert(AnmHessianSelfTest());

            HDebug.Assert(coords.Count == cutoffs.Count);

            double[] cutoffs2 = new double[cutoffs.Count];
            for (int i = 0; i < cutoffs.Count; i++)
            {
                cutoffs2[i] = cutoffs[i] * cutoffs[i];
            }

            double Epsilon = 0;// double.Epsilon;

            int        n    = coords.Count;
            HessMatrix hess = null;

            if (n < 100)
            {
                hess = HessMatrixDense.ZerosDense(n * 3, n * 3);
            }
            else
            {
                hess = HessMatrixSparse.ZerosSparse(n * 3, n * 3);
            }
            Action <int> comp_hess_i = delegate(int i)
            {
                if (coords[i] == null)
                {
                    return;
                }
                //continue;
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        continue;
                    }
                    if (coords[j] == null)
                    {
                        continue;
                    }
                    Vector vec_ij  = coords[j] - coords[i];
                    double dist2   = vec_ij.Dist2;
                    double cutoff2 = Math.Max(cutoffs2[i], cutoffs2[j]);
                    if (dist2 > cutoff2)
                    {
                        if (Epsilon == 0)
                        {
                            continue;
                        }
                        HDebug.Assert(hess[i * 3 + 0, j * 3 + 0] == 0); hess[i * 3 + 0, j *3 + 0] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 0, j * 3 + 1] == 0); hess[i * 3 + 0, j *3 + 1] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 0, j * 3 + 2] == 0); hess[i * 3 + 0, j *3 + 2] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 1, j * 3 + 0] == 0); hess[i * 3 + 1, j *3 + 0] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 1, j * 3 + 1] == 0); hess[i * 3 + 1, j *3 + 1] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 1, j * 3 + 2] == 0); hess[i * 3 + 1, j *3 + 2] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 2, j * 3 + 0] == 0); hess[i * 3 + 2, j *3 + 0] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 2, j * 3 + 1] == 0); hess[i * 3 + 2, j *3 + 1] = Epsilon;
                        HDebug.Assert(hess[i * 3 + 2, j * 3 + 2] == 0); hess[i * 3 + 2, j *3 + 2] = Epsilon;
                        continue;
                    }
                    Vector unit_ij = vec_ij.UnitVector();
                    double k_ij = 1; double val;
                    val = k_ij * unit_ij[0] * unit_ij[0]; hess[i * 3 + 0, j *3 + 0] = Epsilon - val; hess[i * 3 + 0, i *3 + 0] += val;  // hess[i*3+0, j*3+0] = Epsilon - k_ij * unit_ij[0]*unit_ij[0]; hess[i*3+0, i*3+0] += k_ij * unit_ij[0]*unit_ij[0];
                    val = k_ij * unit_ij[0] * unit_ij[1]; hess[i * 3 + 0, j *3 + 1] = Epsilon - val; hess[i * 3 + 0, i *3 + 1] += val;  // hess[i*3+0, j*3+1] = Epsilon - k_ij * unit_ij[0]*unit_ij[1]; hess[i*3+0, i*3+1] += k_ij * unit_ij[0]*unit_ij[1];
                    val = k_ij * unit_ij[0] * unit_ij[2]; hess[i * 3 + 0, j *3 + 2] = Epsilon - val; hess[i * 3 + 0, i *3 + 2] += val;  // hess[i*3+0, j*3+2] = Epsilon - k_ij * unit_ij[0]*unit_ij[2]; hess[i*3+0, i*3+2] += k_ij * unit_ij[0]*unit_ij[2];
                    val = k_ij * unit_ij[1] * unit_ij[0]; hess[i * 3 + 1, j *3 + 0] = Epsilon - val; hess[i * 3 + 1, i *3 + 0] += val;  // hess[i*3+1, j*3+0] = Epsilon - k_ij * unit_ij[1]*unit_ij[0]; hess[i*3+1, i*3+0] += k_ij * unit_ij[1]*unit_ij[0];
                    val = k_ij * unit_ij[1] * unit_ij[1]; hess[i * 3 + 1, j *3 + 1] = Epsilon - val; hess[i * 3 + 1, i *3 + 1] += val;  // hess[i*3+1, j*3+1] = Epsilon - k_ij * unit_ij[1]*unit_ij[1]; hess[i*3+1, i*3+1] += k_ij * unit_ij[1]*unit_ij[1];
                    val = k_ij * unit_ij[1] * unit_ij[2]; hess[i * 3 + 1, j *3 + 2] = Epsilon - val; hess[i * 3 + 1, i *3 + 2] += val;  // hess[i*3+1, j*3+2] = Epsilon - k_ij * unit_ij[1]*unit_ij[2]; hess[i*3+1, i*3+2] += k_ij * unit_ij[1]*unit_ij[2];
                    val = k_ij * unit_ij[2] * unit_ij[0]; hess[i * 3 + 2, j *3 + 0] = Epsilon - val; hess[i * 3 + 2, i *3 + 0] += val;  // hess[i*3+2, j*3+0] = Epsilon - k_ij * unit_ij[2]*unit_ij[0]; hess[i*3+2, i*3+0] += k_ij * unit_ij[2]*unit_ij[0];
                    val = k_ij * unit_ij[2] * unit_ij[1]; hess[i * 3 + 2, j *3 + 1] = Epsilon - val; hess[i * 3 + 2, i *3 + 1] += val;  // hess[i*3+2, j*3+1] = Epsilon - k_ij * unit_ij[2]*unit_ij[1]; hess[i*3+2, i*3+1] += k_ij * unit_ij[2]*unit_ij[1];
                    val = k_ij * unit_ij[2] * unit_ij[2]; hess[i * 3 + 2, j *3 + 2] = Epsilon - val; hess[i * 3 + 2, i *3 + 2] += val;  // hess[i*3+2, j*3+2] = Epsilon - k_ij * unit_ij[2]*unit_ij[2]; hess[i*3+2, i*3+2] += k_ij * unit_ij[2]*unit_ij[2];
                }
            };

            if (options.Split(';').Contains("parallel"))
            {
                System.Threading.Tasks.Parallel.For(0, n, comp_hess_i);
            }
            else
            {
                for (int i = 0; i < n; i++)
                {
                    comp_hess_i(i);
                }
            }

            return(hess);
        }
Example #9
0
        static HessMatrix GetMulImpl(HessMatrix left, HessMatrix right, ILinAlg ila, bool warning)
        {
            if (HDebug.Selftest())
            {
                Matrix h1 = new double[, ] {
                    { 0, 1, 2, 3, 4, 5 }
                    , { 1, 2, 3, 4, 5, 6 }
                    , { 2, 3, 4, 5, 6, 7 }
                    , { 3, 4, 5, 6, 7, 8 }
                    , { 4, 5, 6, 7, 8, 9 }
                    , { 5, 6, 7, 8, 9, 0 }
                };
                HessMatrix h2    = HessMatrixSparse.FromMatrix(h1);
                Matrix     h11   = Matrix.GetMul(h1, h1);
                HessMatrix h22   = HessMatrix.GetMulImpl(h2, h2, null, false);
                Matrix     hdiff = h11 - h22;
                HDebug.AssertToleranceMatrix(0, hdiff);
            }
            if ((left is HessMatrixDense) && (right is HessMatrixDense))
            {
                if (ila != null)
                {
                    return(new HessMatrixDense {
                        hess = ila.Mul(left, right)
                    });
                }
                if (warning)
                {
                    HDebug.ToDo("Check (HessMatrixDense * HessMatrixDense) !!!");
                }
            }

            Dictionary <int, Dictionary <int, Tuple <int, int, MatrixByArr> > > left_ic_rows = new Dictionary <int, Dictionary <int, Tuple <int, int, MatrixByArr> > >();

            foreach (var ic_row in left.EnumRowBlocksAll())
            {
                left_ic_rows.Add(ic_row.Item1, ic_row.Item2.HToDictionaryWithKeyItem2());
            }

            Dictionary <int, Dictionary <int, Tuple <int, int, MatrixByArr> > > right_ir_cols = new Dictionary <int, Dictionary <int, Tuple <int, int, MatrixByArr> > >();

            foreach (var ir_col in right.EnumColBlocksAll())
            {
                right_ir_cols.Add(ir_col.Item1, ir_col.Item2.HToDictionaryWithKeyItem1());
            }

            HessMatrix mul = null;

            if ((left is HessMatrixDense) && (right is HessMatrixDense))
            {
                mul = HessMatrixDense.ZerosDense(left.ColSize, right.RowSize);
            }
            else
            {
                mul = HessMatrixSparse.ZerosSparse(left.ColSize, right.RowSize);
            }
            for (int ic = 0; ic < left.ColBlockSize; ic++)
            {
                var left_row = left_ic_rows[ic];
                if (left_row.Count == 0)
                {
                    continue;
                }
                for (int ir = 0; ir < right.RowBlockSize; ir++)
                {
                    var right_col = right_ir_cols[ir];
                    if (right_col.Count == 0)
                    {
                        continue;
                    }
                    foreach (var left_ck in left_row)
                    {
                        int ik = left_ck.Key;
                        HDebug.Assert(ic == left_ck.Value.Item1);
                        HDebug.Assert(ik == left_ck.Value.Item2);
                        if (right_col.ContainsKey(ik))
                        {
                            var right_kr = right_col[ik];
                            HDebug.Assert(ik == right_kr.Item1);
                            HDebug.Assert(ir == right_kr.Item2);
                            MatrixByArr mul_ckr = mul.GetBlock(ic, ir) + left_ck.Value.Item3 * right_kr.Item3;
                            mul.SetBlock(ic, ir, mul_ckr);
                        }
                    }
                }
            }

            return(mul);
        }