


"""simple docstring"""





from cva import COLOR_BGR2GRAY, cvtColor, imread, imshow, waitKey
from numpy import array, dot, pad, ravel, uinta, zeros


def   _snake_case    (    _snake_case					:      Optional[Any]			, _snake_case					:      List[Any]      ):
      lowerCAmelCase,   lowerCAmelCase       :						List[Any]							= image.shape
      lowerCAmelCase       :						Optional[int]							= cols - block_size[1] + 1
      lowerCAmelCase       :						Optional[int]							= rows - block_size[0] + 1
      lowerCAmelCase       :						List[Any]							= zeros((dst_height * dst_width, block_size[1] * block_size[0])      )
      lowerCAmelCase       :						Dict							= 0
      for i in range(0			, _snake_case      ):
            for j in range(0			, _snake_case      ):
                  lowerCAmelCase       :						List[str]							= ravel(image[i : i + block_size[0], j : j + block_size[1]]      )
                  lowerCAmelCase       :						Dict							= window
                  row += 1

      return image_array


def   _snake_case    (    _snake_case					:      Dict			, _snake_case					:      str      ):
      lowerCAmelCase,   lowerCAmelCase       :						Union[str, Any]							= image.shape[0], image.shape[1]
      lowerCAmelCase       :						Dict							= filter_kernel.shape[0]
      lowerCAmelCase       :						int							= k_size // 2
      # Pads image with the edge values of array.
      lowerCAmelCase       :						List[str]							= pad(_snake_case			, _snake_case			, mode='''edge'''      )

      # im2col, turn the k_size*k_size pixels into a row and np.vstack all rows
      lowerCAmelCase       :						str							= imacol(_snake_case			, (k_size, k_size)      )

      #  turn the kernel into shape(k*k, 1)
      lowerCAmelCase       :						List[str]							= ravel(_snake_case      )
      # reshape and get the dst image
      lowerCAmelCase       :						str							= dot(_snake_case			, _snake_case      ).reshape(_snake_case			, _snake_case      )
      return dst


if __name__ == "__main__":
 # read original image
 snake_case__		:  List[str]     					=					imread(R'''../image_data/lena.jpg''')
 # turn image in gray scale value
 snake_case__		:  Union[str, Any]     					=					cvtColor(img, COLOR_BGR2GRAY)
 # Laplace operator
 snake_case__		:  Optional[int]     					=					array([[0, 1, 0], [1, -4, 1], [0, 1, 0]])
 snake_case__		:  Any     					=					img_convolve(gray, Laplace_kernel).astype(uinta)
 imshow('''Laplacian''', out)
 waitKey(0)






"""simple docstring"""





import math as m


def   _snake_case    (    _snake_case					:      List[Any]			, _snake_case					:      Tuple			, _snake_case					:      Tuple=0.001      ):
      return (f(a + h      ) - f(a - h      )) / (2 * h)


def   _snake_case    (    _snake_case					:      int			, _snake_case					:      List[Any]=0			, _snake_case					:      Any=100			, _snake_case					:      Any=0.0001			, _snake_case					:      Optional[Any]=1E-6			, _snake_case					:      Any=False      ):
      lowerCAmelCase       :						Optional[Any]							= xa  # set the initial guess
      lowerCAmelCase       :						str							= [a]
      lowerCAmelCase       :						List[str]							= abs(f(_snake_case      )      )
      lowerCAmelCase       :						Dict							= lambda _snake_case      : calc_derivative(_snake_case			, _snake_case			, h=_snake_case      )  # noqa: E731  Derivative of f(x)
      for _ in range(_snake_case      ):
            if fa(_snake_case      ) == 0:
                  raise ValueError('''No converging solution found'''      )
            lowerCAmelCase       :						Tuple							= a - f(_snake_case      ) / fa(_snake_case      )  # Calculate the next estimate
            if logsteps:
                  steps.append(_snake_case      )
            if error < maxerror:
                  break
      else:
            raise ValueError('''Iteration limit reached, no converging solution found'''      )
      if logsteps:
            # If logstep is true, then log intermediate steps
            return a, error, steps
      return a, error


if __name__ == "__main__":
 from matplotlib import pyplot as plt

 snake_case__		:  Any     					=					lambda x: m.tanh(x) ** 2 - m.exp(3 * x)  # noqa: E731
 snake_case__					,  snake_case__					,  snake_case__		:  List[str]     					=					newton_raphson(
     f, xa=10, maxiter=1_000, step=1e-6, logsteps=True
 )
 plt.plot([abs(f(x)) for x in steps])
 plt.xlabel('''step''')
 plt.ylabel('''error''')
 plt.show()
 print(f"""solution = {{{solution:f}}}, error = {{{error:f}}}""")


