//public static int djacobi(
        //    Func<int, int, double[], double[]> fcn,
        //    int xLength,
        //    int fLength,
        //    IntPtr fJacMatrixPtr,
        //    IntPtr xPtr,
        //    ref double eps)
        //{
        //    unsafe
        //    {
        //        return JacobianMatrixCalcNative.djacobi(fcn, ref xLength, ref fLength, fJacMatrixPtr,
        //            xPtr, ref eps);
        //    }
        //}



        public double[,] SolveJacobianMatrix(double prec)
        {
            // int m = mSpaceVector.Count();
            // int m = mFittingValuesVector.Count();
            int    m           = mEventsPointsSetLength;
            int    n           = nParametersSpacePoint.Count();
            double eps         = prec;
            int    RCI_Request = 0;
            double rs          = 0.0d;
            bool   successful  = false;

            double[] theta = nParametersSpacePoint.ToArray();
            double[] f1Vec = objectiveFunction(theta).ToArray();
            double[] f2Vec = objectiveFunction(theta).ToArray();
            double[,] fJacobi = new double[m, n];
            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    fJacobi[i, j] = 0.0d;
                }
            }

            unsafe
            {
                fixed(double *xPtr = &theta[0], f1VecPtr = &f1Vec[0], f2VecPtr = &f2Vec[0], fJacPtr = &fJacobi[0, 0])
                {
                    int init_res = JacobianMatrixCalcNative.djacobi_init(ref jacSolverHandle, ref n, ref m,
                                                                         (IntPtr)xPtr, (IntPtr)fJacPtr, ref eps);

                    if (init_res != MKLwrapper.DEFINE.TR_SUCCESS)
                    {
                        Delete();
                        return(fJacobi);
                    }


                    while (!successful)
                    {
                        int solve_res = JacobianMatrixCalcNative.djacobi_solve(ref jacSolverHandle,
                                                                               (IntPtr)f1VecPtr, (IntPtr)f2VecPtr, ref RCI_Request);
                        if (solve_res != MKLwrapper.DEFINE.TR_SUCCESS)
                        {
                            resultStatus = "error in djacobi_solve";
                            Delete();
                            return(fJacobi);
                        }

                        if (RCI_Request == 0)
                        {
                            successful = true;
                        }

                        if (RCI_Request == 1)
                        {
                            double[] newFVecValues = objectiveFunction(theta).ToArray();
                            for (int i = 0; i < m; i++)
                            {
                                f1Vec[i] = newFVecValues[i];
                            }
                            resultStatus = "";
                        }

                        if (RCI_Request == 2)
                        {
                            double[] newFVecValues = objectiveFunction(theta).ToArray();
                            for (int i = 0; i < m; i++)
                            {
                                f2Vec[i] = newFVecValues[i];
                            }
                            resultStatus = "";
                        }
                    }


                    if (JacobianMatrixCalcNative.djacobi_delete(ref jacSolverHandle) != MKLwrapper.DEFINE.TR_SUCCESS)
                    {
                        resultStatus = "error in djacobi_delete";
                        return(fJacobi);
                    }
                }
            }

            return(fJacobi);
        }
 private int Delete()
 {
     return(JacobianMatrixCalcNative.djacobi_delete(ref jacSolverHandle));
 }