Repository: numpy/numpy
File: numpy/linalg/tests/test_linalg.py (Python)

Code before:
    x = np.arange(6).reshape((2, 3))
    actual = np.linalg.matrix_transpose(x)
    expected = x.T

    assert_equal(actual, expected)

    with assert_raises_regex(
        ValueError, "array must be at least 2-dimensional"
    ):
        np.linalg.matrix_transpose(x[:, 0])


def test_matrix_norm():
    x = np.arange(9).reshape((3, 3))
    actual = np.linalg.matrix_norm(x)

    assert_almost_equal(actual, np.float64(14.2828), double_decimal=3)

    actual = np.linalg.matrix_norm(x, keepdims=True)

    assert_almost_equal(actual, np.array([[14.2828]]), double_decimal=3)


def test_matrix_norm_empty():
    for shape in [(0, 2), (2, 0), (0, 0)]:
        x = np.zeros(shape)

        assert_equal(np.linalg.matrix_norm(x, ord=2), 0)
        assert_equal(np.linalg.matrix_norm(x, ord=-2), np.inf)

        assert_equal(np.linalg.matrix_norm(x, ord=1), 0)
        y = np.inf if x.shape[1] == 0 else 0
        assert_equal(np.linalg.matrix_norm(x, ord=-1), y)

        assert_equal(np.linalg.matrix_norm(x, ord=np.inf), 0)
        z = np.inf if x.shape[0] == 0 else 0
        assert_equal(np.linalg.matrix_norm(x, ord=-np.inf), z)


def test_vector_norm():
    x = np.arange(9).reshape((3, 3))
    actual = np.linalg.vector_norm(x)

    assert_almost_equal(actual, np.float64(14.2828), double_decimal=3)

    actual = np.linalg.vector_norm(x, axis=0)

    assert_almost_equal(
        actual, np.array([6.7082, 8.124, 9.6436]), double_decimal=3
    )


Review comment (suggestion):
This only tests for type `float64`. Could you also add a case for a few other types? (not all types are needed, maybe just int and float32)

Code after:
    x = np.arange(6).reshape((2, 3))
    actual = np.linalg.matrix_transpose(x)
    expected = x.T

    assert_equal(actual, expected)

    with assert_raises_regex(
        ValueError, "array must be at least 2-dimensional"
    ):
        np.linalg.matrix_transpose(x[:, 0])


def test_matrix_norm():
    x = np.arange(9).reshape((3, 3))
    actual = np.linalg.matrix_norm(x)

    assert_almost_equal(actual, np.float64(14.2828), double_decimal=3)

    actual = np.linalg.matrix_norm(x, keepdims=True)

    assert_almost_equal(actual, np.array([[14.2828]]), double_decimal=3)


def test_matrix_norm_empty():
    for shape in [(0, 2), (2, 0), (0, 0)]:
        for dtype in [np.float64, np.float32, np.int32]:
            x = np.zeros(shape, dtype)
            assert_equal(np.linalg.matrix_norm(x, ord="fro"), 0)
            assert_equal(np.linalg.matrix_norm(x, ord="nuc"), 0)
            assert_equal(np.linalg.matrix_norm(x, ord=1), 0)
            assert_equal(np.linalg.matrix_norm(x, ord=2), 0)
            assert_equal(np.linalg.matrix_norm(x, ord=np.inf), 0)

def test_vector_norm():
    x = np.arange(9).reshape((3, 3))
    actual = np.linalg.vector_norm(x)

    assert_almost_equal(actual, np.float64(14.2828), double_decimal=3)

    actual = np.linalg.vector_norm(x, axis=0)

    assert_almost_equal(
        actual, np.array([6.7082, 8.124, 9.6436]), double_decimal=3
    )

    actual = np.linalg.vector_norm(x, keepdims=True)
    expected = np.full((1, 1), 14.2828, dtype='float64')
    assert_equal(actual.shape, expected.shape)
    assert_almost_equal(actual, expected, double_decimal=3)