#include <oxstd.h>
#include <oxfloat.h>
#import <maximize>


fRosenbrock(const vP, const adFunc, const avScore, const amHessian)
{
    adFunc[0] =
        -100 * (vP[1][0] - vP[0][0] ^ 2) ^ 2 - (1 - vP[0][0]) ^ 2;

return 1;
}
fPowell(const vP, const adFunc, const avScore, const amHessian)
{
    adFunc[0] =
    -((vP[0][0] + 10*vP[1][0]) ^ 2 + 5 * (vP[2][0] - vP[3][0]) ^ 2
     +(vP[1][0] - 2*vP[2][0]) ^ 4 + 10 * (vP[0][0] + vP[3][0]) ^ 4);
     
return 1;
}
fQuad(const vP, const adFunc, const avScore, const amHessian)
{
    adFunc[0] = -double(sumc(vP .^ 4));

return 1;
}

main()
{
    decl vp, vf, mh;

    MaxControl(-1,1000);

    vp = <-1.2;1>;  mh = unit(2);
    MaxBFGS(fRosenbrock, &vp, &vf, &mh, TRUE);
    vp = <-1.2;1>;  mh = unit(2);
    MaxSimplex(fRosenbrock, &vp, &vf, 0 /*<1;1>*/);

    vp = <3;-1;0;1>;  mh = unit(4);
    MaxBFGS(fPowell, &vp, &vf, &mh, TRUE);
    vp = <3;-1;0;1>;  mh = unit(4);
    MaxSimplex(fPowell, &vp, &vf, 0 /*<1;1;1;1>*/);

    vp = ones(10,1); mh = unit(10);
    MaxBFGS(fQuad, &vp, &vf, &mh, TRUE);
    vp = ones(10,1); mh = unit(10);
    MaxSimplex(fQuad, &vp, &vf, 0 /*vp*/);
}

