# Path: pactools/utils/maths.py
# def norm(x):
#     """Compute the Euclidean or Frobenius norm of x.
# 
#     Returns the Euclidean norm when x is a vector, the Frobenius norm when x
#     is a matrix (2-d array). More precise than sqrt(squared_norm(x)).
#     """
#     x = np.asarray(x)
# 
#     if np.any(np.iscomplex(x)):
#         return np.sqrt(squared_norm(x))
#     else:
#         nrm2, = linalg.get_blas_funcs(['nrm2'], [x])
#         return nrm2(x)
# 
# def squared_norm(x):
#     """Squared Euclidean or Frobenius norm of x.
# 
#     Returns the Euclidean norm when x is a vector, the Frobenius norm when x
#     is a matrix (2-d array). Faster than norm(x) ** 2.
#     """
#     x = np.asarray(x)
#     x = x.ravel(order='K')
#     return np.dot(x, np.conj(x))
# 
# def argmax_2d(a):
#     """Return the tuple (i, j) where a[i, j] = a.max()"""
#     return np.unravel_index(np.argmax(a), a.shape)
# 
# def is_power2(num):
#     """Test if num is a power of 2. (int -> bool)"""
#     num = int(num)
#     return num != 0 and ((num & (num - 1)) == 0)
# 
# Path: pactools/utils/maths.py
# def prime_factors(num):
#     """
#     Decomposition in prime factor.
#     Used to find signal length that speed up Hilbert transform.
# 
#     Parameters
#     ----------
#     num : int
# 
#     Returns
#     -------
#     decomposition : list of int
#         List of prime factors sorted by ascending order
#     """
#     assert num > 0
#     assert isinstance(num, int)
# 
#     decomposition = []
#     for i in chain([2], range(3, int(np.sqrt(num)) + 1, 2)):
#         while num % i == 0:
#             num = num // i
#             decomposition.append(i)
#         if num == 1:
#             break
#     if num != 1:
#         decomposition.append(num)
#     return decomposition
# 
# Path: pactools/utils/testing.py
# def assert_array_not_almost_equal(*args, **kwargs):
#     def func():
# 
# Path: pactools/utils/testing.py
# def assert_array_not_almost_equal(*args, **kwargs):
#     def func():
# 
# Path: pactools/utils/testing.py
# def assert_array_not_almost_equal(*args, **kwargs):
#     def func():


import numpy as np
from pactools.utils.maths import norm, squared_norm, argmax_2d, is_power2
from pactools.utils.maths import prime_factors
from pactools.utils.testing import assert_equal
from pactools.utils.testing import assert_array_almost_equal
from pactools.utils.testing import assert_true, assert_false



def test_norm():
    # Test that norm and squared_norm are consistent
    rng = np.random.RandomState(0)
    for sig in (rng.randn(10), rng.randn(4, 3), [1 + 1j, 3, 6], -9):
        assert_array_almost_equal(norm(sig) ** 2, squared_norm(sig))


def test_argmax_2d():
    # Test that argmax_2d gives the correct indices of the max
    rng = np.random.RandomState(0)
    a = rng.randn(4, 3)
    i, j = argmax_2d(a)

# Path: pactools/utils/maths.py
# def norm(x):
#     """Compute the Euclidean or Frobenius norm of x.
# 
#     Returns the Euclidean norm when x is a vector, the Frobenius norm when x
#     is a matrix (2-d array). More precise than sqrt(squared_norm(x)).
#     """
#     x = np.asarray(x)
# 
#     if np.any(np.iscomplex(x)):
#         return np.sqrt(squared_norm(x))
#     else:
#         nrm2, = linalg.get_blas_funcs(['nrm2'], [x])
#         return nrm2(x)
# 
# def squared_norm(x):
#     """Squared Euclidean or Frobenius norm of x.
# 
#     Returns the Euclidean norm when x is a vector, the Frobenius norm when x
#     is a matrix (2-d array). Faster than norm(x) ** 2.
#     """
#     x = np.asarray(x)
#     x = x.ravel(order='K')
#     return np.dot(x, np.conj(x))
# 
# def argmax_2d(a):
#     """Return the tuple (i, j) where a[i, j] = a.max()"""
#     return np.unravel_index(np.argmax(a), a.shape)
# 
# def is_power2(num):
#     """Test if num is a power of 2. (int -> bool)"""
#     num = int(num)
#     return num != 0 and ((num & (num - 1)) == 0)
# 
# Path: pactools/utils/maths.py
# def prime_factors(num):
#     """
#     Decomposition in prime factor.
#     Used to find signal length that speed up Hilbert transform.
# 
#     Parameters
#     ----------
#     num : int
# 
#     Returns
#     -------
#     decomposition : list of int
#         List of prime factors sorted by ascending order
#     """
#     assert num > 0
#     assert isinstance(num, int)
# 
#     decomposition = []
#     for i in chain([2], range(3, int(np.sqrt(num)) + 1, 2)):
#         while num % i == 0:
#             num = num // i
#             decomposition.append(i)
#         if num == 1:
#             break
#     if num != 1:
#         decomposition.append(num)
#     return decomposition
# 
# Path: pactools/utils/testing.py
# def assert_array_not_almost_equal(*args, **kwargs):
#     def func():
# 
# Path: pactools/utils/testing.py
# def assert_array_not_almost_equal(*args, **kwargs):
#     def func():
# 
# Path: pactools/utils/testing.py
# def assert_array_not_almost_equal(*args, **kwargs):
#     def func():


# Path: pactools/utils/tests/test_maths.py
import numpy as np
from pactools.utils.maths import norm, squared_norm, argmax_2d, is_power2
from pactools.utils.maths import prime_factors
from pactools.utils.testing import assert_equal
from pactools.utils.testing import assert_array_almost_equal
from pactools.utils.testing import assert_true, assert_false




def test_norm():
    # Test that norm and squared_norm are consistent
    rng = np.random.RandomState(0)
    for sig in (rng.randn(10), rng.randn(4, 3), [1 + 1j, 3, 6], -9):
        assert_array_almost_equal(norm(sig) ** 2, squared_norm(sig))


def test_argmax_2d():
    # Test that argmax_2d gives the correct indices of the max
    rng = np.random.RandomState(0)
    a = rng.randn(4, 3)
    i, j = argmax_2d(a)

    assert_equal(a[i, j], a.max())