#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 18 18:03:16 2024

@author: sam
"""

minimum = []
# creates an empty list to store bracketed minimums



# code from Lab3_Q2 that was adapted to create minbracket2D
def minbracket(f,a,b,N):
    h = (b-a)/N
    for i in range(1,int(N)):
        x = a+h*i
        if f(x)<f(x-h) and f(x)<f(x+h):
            minimum.append([x-h,x+h])
    return minimum



def minbracket2D(f,bregion,N):
# defines function minbracket2D with arguments, 2 variable function f,
# bregion which contains the x and y endpoints for search, and N number of intervals
    
    hx = (bregion[0][1]-bregion[0][0])/N
    # change in x, defined as the difference in x variable endpoints (bx-ax) divided by the number of intervals
    # where ax = bregion[0][0], bx = bregion[0][1]
    
    hy = (bregion[1][1]-bregion[1][0])/N
    # change in y, defined as the difference in y variable endpoints (by-ay) divided by the number of intervals
    # where ay = bregion[1][0], by = bregion[1][1]
    
    for i in range(1,int(N)):
    # variable i iterates through range of N intervals
        
        y = bregion[1][0]+hy*i
        # defines each y point that divides the y range into N intervals
        # defined as ay + hy * i, y range start point + change in y * iteration in y range
        
        for j in range(1,int(N)):
        # variable j iterates through range of N intervals
        # nested for loop allows y and x to iterate independently of each other
            
            x = bregion[0][0]+hx*j
            # defines each x point that divides the x range into N intervals
            # defined as ax + hx * j, y range start point + change in x * iteration in x range
            
            if f(x,y)<f(x-hx,y) and f(x,y)<f(x+hx,y) and f(x,y)<f(x,y-hy) and f(x,y)<f(x,y+hy):
            # tests that changes in x and y provide a minimum in x and y directions independently
                
                minimum.append([[x-hx,x+hx],[y-hy,y+hy]])
                # adds bracketed minimum to list of minimums
                
    return minimum
    # outputs the list of bracketed minimums