# Path: optax/_src/linear_algebra.py
# def global_norm(updates: base.Updates) -> base.Updates:
# def power_iteration(
#     matrix,
#     num_iters=100,
#     error_tolerance=1e-6,
#     precision=lax.Precision.HIGHEST):
#   def _iter_condition(state):
#   def _iter_body(state):
# def matrix_inverse_pth_root(
#     matrix,
#     p,
#     num_iters=100,
#     ridge_epsilon=1e-6,
#     error_tolerance=1e-6,
#     precision=lax.Precision.HIGHEST):
#   def _unrolled_mat_pow_1(mat_m):
#   def _unrolled_mat_pow_2(mat_m):
#   def _unrolled_mat_pow_4(mat_m):
#   def _unrolled_mat_pow_8(mat_m):
#   def mat_power(mat_m, p):
#   def _iter_condition(state):
#   def _iter_body(state):


from absl.testing import absltest
from optax._src import linear_algebra
import jax.numpy as jnp
import numpy as np
import scipy



class LinearAlgebraTest(absltest.TestCase):

  def test_global_norm(self):
    flat_updates = jnp.array([2., 4., 3., 5.], dtype=jnp.float32)
    nested_updates = dict(
        a=jnp.array([2., 4.], dtype=jnp.float32),
        b=jnp.array([3., 5.], dtype=jnp.float32))
    np.testing.assert_array_equal(
        jnp.sqrt(jnp.sum(flat_updates**2)),
        linear_algebra.global_norm(nested_updates))

  def test_matrix_inverse_pth_root(self):
    """Test for matrix inverse pth root."""

    def _gen_symmetrix_matrix(dim, condition_number):
      u = scipy.stats.ortho_group.rvs(dim=dim).astype(np.float64)
      v = u.T
      diag = np.diag([condition_number ** (-i/(dim-1)) for i in range(dim)])
      return u @ diag @ v

    # Fails after it reaches a particular condition number.
    for e in range(2, 12):
      condition_number = 10 ** e
      ms = _gen_symmetrix_matrix(16, condition_number)
      self.assertLess(
          np.abs(np.linalg.cond(ms) - condition_number),
          condition_number * 0.01)

# Path: optax/_src/linear_algebra.py
# def global_norm(updates: base.Updates) -> base.Updates:
# def power_iteration(
#     matrix,
#     num_iters=100,
#     error_tolerance=1e-6,
#     precision=lax.Precision.HIGHEST):
#   def _iter_condition(state):
#   def _iter_body(state):
# def matrix_inverse_pth_root(
#     matrix,
#     p,
#     num_iters=100,
#     ridge_epsilon=1e-6,
#     error_tolerance=1e-6,
#     precision=lax.Precision.HIGHEST):
#   def _unrolled_mat_pow_1(mat_m):
#   def _unrolled_mat_pow_2(mat_m):
#   def _unrolled_mat_pow_4(mat_m):
#   def _unrolled_mat_pow_8(mat_m):
#   def mat_power(mat_m, p):
#   def _iter_condition(state):
#   def _iter_body(state):


# Path: optax/_src/linear_algebra_test.py
from absl.testing import absltest
from optax._src import linear_algebra
import jax.numpy as jnp
import numpy as np
import scipy




class LinearAlgebraTest(absltest.TestCase):

  def test_global_norm(self):
    flat_updates = jnp.array([2., 4., 3., 5.], dtype=jnp.float32)
    nested_updates = dict(
        a=jnp.array([2., 4.], dtype=jnp.float32),
        b=jnp.array([3., 5.], dtype=jnp.float32))
    np.testing.assert_array_equal(
        jnp.sqrt(jnp.sum(flat_updates**2)),
        linear_algebra.global_norm(nested_updates))

  def test_matrix_inverse_pth_root(self):
    """Test for matrix inverse pth root."""

    def _gen_symmetrix_matrix(dim, condition_number):
      u = scipy.stats.ortho_group.rvs(dim=dim).astype(np.float64)
      v = u.T
      diag = np.diag([condition_number ** (-i/(dim-1)) for i in range(dim)])
      return u @ diag @ v

    # Fails after it reaches a particular condition number.
    for e in range(2, 12):
      condition_number = 10 ** e
      ms = _gen_symmetrix_matrix(16, condition_number)
      self.assertLess(
          np.abs(np.linalg.cond(ms) - condition_number),
          condition_number * 0.01)

      error = linear_algebra.matrix_inverse_pth_root(