#!/usr/bin/env python

import numpy as np

def f(x, y):
    return (x-1)**2 + (x-3)*y +y**2

def f_grad(x, y):
    dfdx = 2*(x-1) + y
    dfdy = 2*y + x -3
    return (dfdx, dfdy)

A = np.linspace(0, 10, 11)
M_X, M_Y = np.meshgrid(A, A)

P, Y = [], []
for i in xrange(M_X.shape[0]):
    for j in xrange(M_X.shape[1]):
        P.append([i,j])
        Y.append(f(i,j))

import professor2 as prof
import sys
order = int(sys.argv[1]) if len(sys.argv) > 1 else 3
ipol = prof.Ipol(P, Y, order)


from iminuit import Minuit

def I(x, y):
    return ipol.value([x,y])
def I_grad(x, y):
    return ipol.grad([x,y])

FARG=dict()
FARG["x"]=100
FARG["y"]=100
FARG["error_x"]=1
FARG["error_y"]=1


print " Minuit"
m=Minuit(f, errordef=1, print_level=0, **FARG)
m.migrad()
print m.values

print " Minuit + gradient"
m = Minuit(f, grad_fcn=f_grad, errordef=1, print_level=0, **FARG)
m.migrad()
print m.values

print " Professor  IPOL"
m=Minuit(I, errordef=1, print_level=0, **FARG)
m.migrad()
print m.values

print " Professor  IPOL + gradient"
m=Minuit(I, grad_fcn=I_grad, errordef=1, print_level=0, **FARG)
m.migrad()
print m.values

