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

def bracketing(f,a,b,n): #bracketing procedure
    brackets = []
    x = np.linspace(a,b,n).tolist() #create n evenly spaced domain elements
    for i in range(len(x)-1): #iterate for each domain element
        if f(x[i])*f(x[i+1])<0: #apply IVT to check for zero
            brackets.append([x[i],x[i+1]])
    return brackets

def secant(f,x,y): #secant method
    return [(y*f(x)-x*f(y))/(f(x)-f(y)),x]

def findallzeros(f,a,b,tol): #zero finding algorithm
    brackets=bracketing(f,a,b,100) #find bracketed roots
    zeroes=[]
    for bracket in brackets: #iterate for each bracket
        [x,y]=bracket[:] #set initial values for secant method as bracket endpoints
        it=0 #iteration counter for applications of secant method
        while abs(x-y) >= tol and it < 100: #apply secant method until tolerance reached or 100 iterations surpassed
            [x,y]=secant(f,x,y)
            it+=1
        zeroes.append(x) #append zero to output
    return zeroes
