#include <oxstd.h>
#import <maximize>
//#import <solvenle>
#include <../src/solvenle.ox>

test813(const avF, const vX)
{
	avF[0] = vX[0] + vX[1] - 3 | sqr(vX[0]) + sqr(vX[1]) - 9;
	return 1;
}
test813_jac(const amJac, const vX)
{
	amJac[0] = (1 ~ 1) | (2 * vX[0] ~ 2 * vX[1]);
	return 1;
}
Powell(const avF, const vX)
{
	avF[0] = vX[0] |
	    10*vX[0]/(vX[0]+0.1) + 2*sqr(vX[1]);
	return 1;
}
Powell_jac(const amJac, const vX)
{
	amJac[0] = (1 ~ 0) | (1/sqr(vX[0]+0.1) ~ 4 * vX[1]);
	return 1;
}

main()
{
	decl x;
	MaxControl(-1, 1, 1);

	x = <1;5>;
	println("==== Using numerical Jacobian:");
	SolveNLE(test813, &x);
	println("x=", x);

	x = <1;5>;
	println("\n==== Using analytical Jacobian:");
	SolveNLE(test813, &x, 0, test813_jac);
	println("x=", x);

	x = <1;0>;
//	x = <3;1>;
	println("\n==== Powell's system, using numerical Jacobian:");
	SolveNLE(Powell, &x);
	println("x=", x);

	x = <1;0>;
//	x = <3;1>;
	println("\n==== Powell's system, using Broyden's Jacobian:");
	SolveNLE(Powell, &x, 2);
	println("x=", x);
}