


def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase							)		->str:


 '''simple docstring'''
 # "Simpson Rule"
 # int(f) = delta_x/2 * (b-a)/3*(f1 + 4f2 + 2f_3 + ... + fn)
 snake_case :			Tuple							=   (boundary[1] - boundary[0]) / steps
 snake_case :			Tuple							=   boundary[0]
 snake_case :			List[str]							=   boundary[1]
 snake_case :			List[Any]							=   make_points(__lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase							)
 snake_case :			List[str]							=   0.0
 y += (h / 3.0) * f(__lowerCamelCase							)
 snake_case :			int							=   2
 for i in x_i:
  y += (h / 3) * (4 - 2 * (cnt % 2)) * f(__lowerCamelCase							)
  cnt += 1
 y += (h / 3.0) * f(__lowerCamelCase							)
 return y






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase							)		->Optional[Any]:


 '''simple docstring'''
 snake_case :			Tuple							=   a + h
 while x < (b - h):
  yield x
  snake_case :			str							=   x + h






def  _UpperCAmelCase						( __lowerCamelCase							)		->int:  # enter your function here


 '''simple docstring'''
 snake_case :			Tuple							=   (x - 0) * (x - 0)
 return y






def  _UpperCAmelCase						( )		->List[str]:


 '''simple docstring'''
 snake_case :			List[Any]							=   0.0  # Lower bound of integration
 snake_case :			Optional[int]							=   1.0  # Upper bound of integration
 snake_case :			Union[str, Any]							=   10.0  # define number of steps or resolution
 snake_case :			List[Any]							=   [a, b]  # define boundary of integration
 snake_case :			Union[str, Any]							=   method_a(__lowerCamelCase			,  __lowerCamelCase							)
 print(f'''y = {y}'''							)


if __name__ == "__main__":
  main()










from typing import Any

import numpy as np






def  _UpperCAmelCase						( __lowerCamelCase							)		->bool:


 '''simple docstring'''
 assert np.shape(__lowerCamelCase							)[0] == np.shape(__lowerCamelCase							)[1]

 # If matrix not symmetric, exit right away.
 if np.allclose(__lowerCamelCase			,  matrix.T							) is False:
  return False

 # Get eigenvalues and eignevectors for a symmetric matrix.
 snake_case	,       snake_case :			int							=   np.linalg.eigh(__lowerCamelCase							)

 # Check sign of all eigenvalues.
 # np.all returns a value of type np.bool_
 return bool(np.all(eigen_values > 0							)							)






def  _UpperCAmelCase						( __lowerCamelCase							)		->Any:


 '''simple docstring'''
 snake_case :			Dict							=   np.random.randn(__lowerCamelCase			,  __lowerCamelCase							)
 snake_case :			List[Any]							=   np.dot(__lowerCamelCase			,  random_matrix.T							)
 assert _is_matrix_spd(__lowerCamelCase							)
 return spd_matrix






def  _UpperCAmelCase						( __lowerCamelCase			,  __lowerCamelCase			,  __lowerCamelCase = 1000			,  __lowerCamelCase = 1E-8			,  )		->Any:


 '''simple docstring'''
 assert np.shape(__lowerCamelCase							)[0] == np.shape(__lowerCamelCase							)[1]
 assert np.shape(__lowerCamelCase							)[0] == np.shape(__lowerCamelCase							)[0]
 assert _is_matrix_spd(__lowerCamelCase							)

 # Initialize solution guess, residual, search direction.
 snake_case :			Any							=   np.zeros((np.shape(__lowerCamelCase							)[0], 1)							)
 snake_case :			Optional[int]							=   np.copy(__lowerCamelCase							)
 snake_case :			Optional[Any]							=   np.copy(__lowerCamelCase							)

 # Set initial errors in solution guess and residual.
 snake_case :			Optional[Any]							=   1E9
 snake_case :			List[Any]							=   1E9
 snake_case :			List[str]							=   1E9

 # Set iteration counter to threshold number of iterations.
 snake_case :			List[str]							=   0

 while error > tol:
  # Save this value so we only calculate the matrix-vector product once.
  snake_case :			str							=   np.dot(__lowerCamelCase			,  __lowerCamelCase							)

  # The main algorithm.

  # Update search direction magnitude.
  snake_case :			List[str]							=   np.dot(ra.T			,  __lowerCamelCase							) / np.dot(pa.T			,  __lowerCamelCase							)
  # Update solution guess.
  snake_case :			Optional[int]							=   xa + alpha * pa
  # Calculate new residual.
  snake_case :			str							=   ra - alpha * w
  # Calculate new Krylov subspace scale.
  snake_case :			List[str]							=   np.dot(r.T			,  __lowerCamelCase							) / np.dot(ra.T			,  __lowerCamelCase							)
  # Calculate new A conjuage search direction.
  snake_case :			List[Any]							=   r + beta * pa

  # Calculate errors.
  snake_case :			List[Any]							=   np.linalg.norm(r - ra							)
  snake_case :			Any							=   np.linalg.norm(x - xa							)
  snake_case :			str							=   np.maximum(__lowerCamelCase			,  __lowerCamelCase							)

  # Update variables.
  snake_case :			Tuple							=   np.copy(__lowerCamelCase							)
  snake_case :			Tuple							=   np.copy(__lowerCamelCase							)
  snake_case :			List[str]							=   np.copy(__lowerCamelCase							)

  # Update number of iterations.
  iterations += 1
  if iterations > max_iterations:
   break

 return x






def  _UpperCAmelCase						( )		->None:


 '''simple docstring'''
 snake_case :			str							=   3
 snake_case :			Dict							=   _create_spd_matrix(__lowerCamelCase							)
 snake_case :			str							=   np.random.randn(__lowerCamelCase			,  1							)
 snake_case :			str							=   np.dot(__lowerCamelCase			,  __lowerCamelCase							)

 # Numpy solution.
 snake_case :			Any							=   np.linalg.solve(__lowerCamelCase			,  __lowerCamelCase							)

 # Our implementation.
 snake_case :			List[str]							=   conjugate_gradient(__lowerCamelCase			,  __lowerCamelCase							)

 # Ensure both solutions are close to x_true (and therefore one another).
 assert np.linalg.norm(x_numpy - x_true							) <= 1E-6
 assert np.linalg.norm(x_conjugate_gradient - x_true							) <= 1E-6


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  test_conjugate_gradient()






