Esempio n. 1
0
        public double DfDx(MatrixFunctionWithMatrixArgument Func, Matrix x, int FuncIndex1, int FuncIndex2, int xIndex1, int xIndex2, double h)
        {
            int    n1 = x.Values.GetLength(0);
            int    n2 = x.Values.GetLength(1);
            Matrix XPrev = new Matrix(n1, n2);
            Matrix XNext = new Matrix(n1, n2);
            double Prev, Next;

            for (int i = 0; i < n1; i++)
            {
                for (int j = 0; j < n2; j++)
                {
                    XPrev.Values[i, j] = x.Values[i, j];
                    XNext.Values[i, j] = x.Values[i, j];
                    if (i == xIndex1)
                    {
                        if (j == xIndex2)
                        {
                            XPrev.Values[i, j] -= h;
                            XNext.Values[i, j] += h;
                        }
                    }
                }
            }
            Prev = Func(XPrev).Values[FuncIndex1, FuncIndex2];
            Next = Func(XNext).Values[FuncIndex1, FuncIndex2];
            return((Next - Prev) / (2.0D * h));
        }
Esempio n. 2
0
        public double[, , ,] DfDx(MatrixFunctionWithMatrixArgument Func, Matrix x)
        {
            ///<sumary>
            /// retruns an array with all derivatives of the function
            /// </summary>
            int n1 = x.Values.GetLength(0);
            int n2 = x.Values.GetLength(1);
            int m1 = Func(x).Values.GetLength(0);
            int m2 = Func(x).Values.GetLength(1);

            double[, , ,] D = new double[m1, m2, n1, n2];
            for (int i = 0; i < m1; i++)
            {
                for (int j = 0; j < m2; j++)
                {
                    for (int k = 0; k < n1; k++)
                    {
                        for (int l = 0; l < n2; l++)
                        {
                            D[i, j, k, l] = DfDx(Func, x, i, j, k, l);
                        }
                    }
                }
            }
            return(D);
        }
Esempio n. 3
0
        public double DfDx(MatrixFunctionWithMatrixArgument Func, Matrix x, int FuncIndex1, int FuncIndex2, int xIndex1, int xIndex2, double InitialStepSize, double Precision)
        {
            double precision = Precision; // precision to evaluate derivative
            double h = InitialStepSize;   // initial step size
            double c = 10.0D;             // factor do divide h (i.e., h_new =h_old/c)
            double eps = 1.0E-100D;       // small number
            int    maxSteps = 100;        // maximum times to divid h
            double devOld, devNew;        // old derivative
            double calcPrecision;         // new derivative

            // calculate initial derivative
            devOld = this.DfDx(Func, x, FuncIndex1, FuncIndex2, xIndex1, xIndex2, h);
            devNew = devOld;

            for (int i = 0; i < maxSteps; i++)
            {
                // reduce h
                h /= c;
                // calculate new derivative
                devNew = this.DfDx(Func, x, FuncIndex1, FuncIndex2, xIndex1, xIndex2, h);
                //calculate precision
                if (Math.Abs(devNew) > eps)
                {
                    calcPrecision = Math.Abs((devNew - devOld) / devNew);
                }
                else
                {
                    // don't divide by devNew if devNew is small
                    calcPrecision = Math.Abs(devNew - devOld);
                }
                // stop if precision is met
                if (calcPrecision < precision)
                {
                    //Console.Write("number of steps = "+i.ToString()+"\n");
                    //Console.Write("Calculated precision = "+calcPrecision.ToString()+"\n");
                    return(devNew);
                }
                devOld = devNew;
            }
            return(devNew);
        }
Esempio n. 4
0
 public double DfDx(MatrixFunctionWithMatrixArgument Func, Matrix x, int FuncIndex1, int FuncIndex2, int xIndex1, int xIndex2)
 {
     return(DfDx(Func, x, FuncIndex1, FuncIndex2, xIndex1, xIndex2, 1.0E-2D, 1.0E-14D));
 }