Example #1
0
        public static int BalanceColumns(PZMath_matrix A, PZMath_vector D)
        {
            int N = A.ColumnCount;
            int j;

            if (D.Size != N)
            {
                PZMath_errno.ERROR ("PZMath_linalg::BalanceColumns(), length of D must match second dimension of A", PZMath_errno.PZMath_EINVAL);
            }

            D.SetAll(1.0);

            for (j = 0; j < N; j++)
            {
                PZMath_vector A_j = A.Column(j);

                double s = PZMath_blas.Dasum (A_j);

                double f = 1.0;

                if (s == 0.0 || !PZMath_sys.Finite(s))
                {
                    D[j] = f;
                    continue;
                }

                while (s > 1.0)
                {
                    s /= 2.0;
                    f *= 2.0;
                }

                while (s < 0.5)
                {
                    s *= 2.0;
                    f /= 2.0;
                }

                D[j] = f;

                if (f != 1.0)
                    PZMath_blas.Dscal(1.0/f, A_j);
            }

            return PZMath_errno.PZMath_SUCCESS;
        }
Example #2
0
        public static void TestMultiminNMSimplex()
        {
            int np = 2;
            TestMultiminNMSimplexParms parms = new TestMultiminNMSimplexParms(1.0, 2.0);

            PZMath_multimin_fminimizer_type T = new PZMath_multimin_fminimizer_type();
            T.Init("PZMath_multimin_fminimizer_nmsimplex");

            PZMath_multimin_fminimizer s = new PZMath_multimin_fminimizer();

            /* Initial vertex size vector */
            PZMath_vector ss = new PZMath_vector(np);
            PZMath_vector x;

            PZMath_multimin_function minex_func = new PZMath_multimin_function();

            int iter = 0, i;
            int status;
            double size;

            /* Set all step sizes to 1 */
            ss.SetAll(1.0);

            /* Starting point */
            x = new PZMath_vector(np);
            x[0] = 5.0;
            x[1] = 7.0;

            /* Initialize method and iterate */
            minex_func.f = new multimin_function(my_f);
            minex_func.n = np;
            minex_func.parms = parms;
            s.Alloc(T, np);
            s.Init(minex_func, x, ss);

            do
            {
                iter++;
                status = s.Iterate();

                if (status > 0)
                    break;

                size = s.Size();
                status = s.TestSize (size, 1e-2);

                if (status == 0)
                {
                    System.Console.WriteLine  ("converged to minimum at");
                }

                System.Console.Write (iter + " ");

                for (i = 0; i < np; i++)
                {
                    System.Console.Write (s.x[i] + " ");
                }
                System.Console.WriteLine ("f() = " + s.fval + "  size = " + size);
            }
            while (status == -2 && iter < 100);
            System.Console.ReadLine();
        }