#!/usr/bin/python3
import numpy as np
import matplotlib.pyplot as plt

def minbracket2D(f,bregion,N): #returns rectangle brackets on the rectable `bregion`
    brac=[] #contains the rectangular brackets
    dx = (bregion[0][1]-bregion[0][0])/N #horizontal increment
    dy = (bregion[1][1]-bregion[1][0])/N #vertical increment
    for i in range(1,N):
        y = bregion[1][0]+dy*i
        for j in range(1,N):
            x = bregion[0][0]+dx*j
            x_low,x_high = bregion[0][0]+dx*(j-1),bregion[0][0]+dx*(j+1) #it is necessary to write x_low as 'ax+dx*(j-1)' rather than 'x-dx' etc. due to the way python handles arithmetic
            y_low,y_high = bregion[1][0]+dy*(i-1),bregion[1][0]+dy*(i+1)
            if f(x,y) < min([f(x-dx,y),f(x+dx,y),f(x,y-dy),f(x,y+dy)]): #we consider a a rectangle bracket if the function evaluated at its midpoint is less than the function at its endpoints
                brac.append([[x_low,x_high],[y_low,y_high]])
    return brac
