public static int ContractByBest(nmsimplex_state_t state, int best, PZMath_vector xc, 
            PZMath_multimin_function f)
        {
            /* Function contracts the simplex in respect to
               best valued corner. That is, all corners besides the
               best corner are moved. */

            /* the xc vector is simply work space here */

            PZMath_matrix x1 = state.x1;
            PZMath_vector y1 = state.y1;

            int  i, j;
            double newval;

            for (i = 0; i < x1.RowCount; i++)
            {
                if (i != best)
                {
                    for (j = 0; j < x1.ColumnCount; j++)
                    {
                        newval = 0.5 * (x1[i, j] + x1[best, j]);
                        x1[i, j] = newval;
                    }

                    /* evaluate function in the new point */

                    x1.CopyRow2(i, xc);
                    newval = f.FN_EVAL(xc);
                    y1[i] = newval;
                }
            }

            return PZMath_errno.PZMath_SUCCESS;
        }
        public static int Init(object vstate, PZMath_multimin_function f, PZMath_vector x, 
            out double size, PZMath_vector step_size)
        {
            int i;
            double val;
            PZMath_vector xtemp = (vstate as nmsimplex_state_t).ws1;

            /* first point is the original x0 */
            val = f.FN_EVAL(x);

            (vstate as nmsimplex_state_t).x1.SetRow(0, x);
            (vstate as nmsimplex_state_t).y1[0] = val;

            /* following points are initialized to x0 + step_size */

            for (i = 0; i < x.size; i++)
            {
                xtemp.MemCopyFrom(x);
                val = xtemp[i] + step_size[i];
                xtemp[i] = val;
                val = f.FN_EVAL(xtemp);
                (vstate as nmsimplex_state_t).x1.SetRow(i + 1, xtemp);
                (vstate as nmsimplex_state_t).y1[i + 1] = val;
            }

            /* Initialize simplex size */
            size = Size (vstate as nmsimplex_state_t);

            return PZMath_errno.PZMath_SUCCESS;
        }
        public static double MoveCorner(double coeff, nmsimplex_state_t state, int corner, 
            PZMath_vector xc, PZMath_multimin_function f)
        {
            /* moves a simplex corner scaled by coeff (negative value represents
                mirroring by the middle point of the "other" corner points)
                and gives new corner in xc and function value at xc as a
                return value
            */

            PZMath_matrix x1 = state.x1;

            int i, j;
            double newval, mp;

            if (x1.RowCount < 2)
            {
                PZMath_errno.ERROR ("nmsimplex::MoveCorner(), simplex cannot have less than two corners!", PZMath_errno.PZMath_FAILURE);
            }

            for (j = 0; j < x1.ColumnCount; j++)
            {
                mp = 0.0;
                for (i = 0; i < x1.RowCount; i++)
                {
                    if (i != corner)
                    {
                        mp += (x1[i, j]);
                    }
                }
                mp /= (double) (x1.RowCount - 1);
                newval = mp - coeff * (mp - x1[corner, j]);
                xc[j] = newval;
            }

            newval = f.FN_EVAL(xc);

            return newval;
        }