Skip to content

Commit

Permalink
STY: PEP8 and pyflakes fixes for numpy/matrixlib/tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
charris committed Jul 25, 2015
1 parent 1e99323 commit 0aef5ec
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 110 deletions.
200 changes: 99 additions & 101 deletions numpy/matrixlib/tests/test_defmatrix.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,36 @@
from __future__ import division, absolute_import, print_function

from numpy.testing import *
from numpy.core import *
import collections

import numpy as np
from numpy import matrix, asmatrix, bmat
from numpy.testing import (
TestCase, run_module_suite, assert_, assert_equal, assert_almost_equal,
assert_array_equal, assert_array_almost_equal, assert_raises
)
from numpy.matrixlib.defmatrix import matrix_power
from numpy.matrixlib import mat
import numpy as np
import collections

class TestCtor(TestCase):
def test_basic(self):
A = array([[1, 2], [3, 4]])
A = np.array([[1, 2], [3, 4]])
mA = matrix(A)
assert_(all(mA.A == A))
assert_(np.all(mA.A == A))

B = bmat("A,A;A,A")
C = bmat([[A, A], [A, A]])
D = array([[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2],
[3, 4, 3, 4]])
assert_(all(B.A == D))
assert_(all(C.A == D))

E = array([[5, 6], [7, 8]])
D = np.array([[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2],
[3, 4, 3, 4]])
assert_(np.all(B.A == D))
assert_(np.all(C.A == D))

E = np.array([[5, 6], [7, 8]])
AEresult = matrix([[1, 2, 5, 6], [3, 4, 7, 8]])
assert_(all(bmat([A, E]) == AEresult))
assert_(np.all(bmat([A, E]) == AEresult))

vec = arange(5)
vec = np.arange(5)
mvec = matrix(vec)
assert_(mvec.shape == (1, 5))

Expand All @@ -36,26 +39,23 @@ def test_exceptions(self):
assert_raises(TypeError, matrix, "invalid")

def test_bmat_nondefault_str(self):
A = array([[1, 2], [3, 4]])
B = array([[5, 6], [7, 8]])
Aresult = array([[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2],
[3, 4, 3, 4]])
Bresult = array([[5, 6, 5, 6],
[7, 8, 7, 8],
[5, 6, 5, 6],
[7, 8, 7, 8]])
mixresult = array([[1, 2, 5, 6],
[3, 4, 7, 8],
[5, 6, 1, 2],
[7, 8, 3, 4]])
assert_(all(bmat("A,A;A,A") == Aresult))
assert_(all(bmat("A,A;A,A", ldict={'A':B}) == Aresult))
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
Aresult = np.array([[1, 2, 1, 2],
[3, 4, 3, 4],
[1, 2, 1, 2],
[3, 4, 3, 4]])
mixresult = np.array([[1, 2, 5, 6],
[3, 4, 7, 8],
[5, 6, 1, 2],
[7, 8, 3, 4]])
assert_(np.all(bmat("A,A;A,A") == Aresult))
assert_(np.all(bmat("A,A;A,A", ldict={'A':B}) == Aresult))
assert_raises(TypeError, bmat, "A,A;A,A", gdict={'A':B})
assert_(all(bmat("A,A;A,A", ldict={'A':A}, gdict={'A':B}) == Aresult))
assert_(
np.all(bmat("A,A;A,A", ldict={'A':A}, gdict={'A':B}) == Aresult))
b2 = bmat("A,B;C,D", ldict={'A':A,'B':B}, gdict={'C':B,'D':A})
assert_(all(b2 == mixresult))
assert_(np.all(b2 == mixresult))


class TestProperties(TestCase):
Expand All @@ -78,7 +78,6 @@ def test_sum(self):
assert_array_equal(sum1, np.sum(M, axis=1))
assert_equal(sumall, np.sum(M))


def test_prod(self):
x = matrix([[1, 2, 3], [4, 5, 6]])
assert_equal(x.prod(), 720)
Expand Down Expand Up @@ -115,8 +114,8 @@ def test_min(self):
def test_ptp(self):
x = np.arange(4).reshape((2, 2))
assert_(x.ptp() == 3)
assert_(all(x.ptp(0) == array([2, 2])))
assert_(all(x.ptp(1) == array([1, 1])))
assert_(np.all(x.ptp(0) == np.array([2, 2])))
assert_(np.all(x.ptp(1) == np.array([1, 1])))

def test_var(self):
x = np.arange(9).reshape((3, 3))
Expand All @@ -127,53 +126,53 @@ def test_var(self):
def test_basic(self):
import numpy.linalg as linalg

A = array([[1., 2.],
[3., 4.]])
A = np.array([[1., 2.],
[3., 4.]])
mA = matrix(A)
assert_(allclose(linalg.inv(A), mA.I))
assert_(all(array(transpose(A) == mA.T)))
assert_(all(array(transpose(A) == mA.H)))
assert_(all(A == mA.A))
assert_(np.allclose(linalg.inv(A), mA.I))
assert_(np.all(np.array(np.transpose(A) == mA.T)))
assert_(np.all(np.array(np.transpose(A) == mA.H)))
assert_(np.all(A == mA.A))

B = A + 2j*A
mB = matrix(B)
assert_(allclose(linalg.inv(B), mB.I))
assert_(all(array(transpose(B) == mB.T)))
assert_(all(array(conjugate(transpose(B)) == mB.H)))
assert_(np.allclose(linalg.inv(B), mB.I))
assert_(np.all(np.array(np.transpose(B) == mB.T)))
assert_(np.all(np.array(np.transpose(B).conj() == mB.H)))

def test_pinv(self):
x = matrix(arange(6).reshape(2, 3))
x = matrix(np.arange(6).reshape(2, 3))
xpinv = matrix([[-0.77777778, 0.27777778],
[-0.11111111, 0.11111111],
[ 0.55555556, -0.05555556]])
assert_almost_equal(x.I, xpinv)

def test_comparisons(self):
A = arange(100).reshape(10, 10)
A = np.arange(100).reshape(10, 10)
mA = matrix(A)
mB = matrix(A) + 0.1
assert_(all(mB == A+0.1))
assert_(all(mB == matrix(A+0.1)))
assert_(not any(mB == matrix(A-0.1)))
assert_(all(mA < mB))
assert_(all(mA <= mB))
assert_(all(mA <= mA))
assert_(not any(mA < mA))

assert_(not any(mB < mA))
assert_(all(mB >= mA))
assert_(all(mB >= mB))
assert_(not any(mB > mB))

assert_(all(mA == mA))
assert_(not any(mA == mB))
assert_(all(mB != mA))

assert_(not all(abs(mA) > 0))
assert_(all(abs(mB > 0)))
assert_(np.all(mB == A+0.1))
assert_(np.all(mB == matrix(A+0.1)))
assert_(not np.any(mB == matrix(A-0.1)))
assert_(np.all(mA < mB))
assert_(np.all(mA <= mB))
assert_(np.all(mA <= mA))
assert_(not np.any(mA < mA))

assert_(not np.any(mB < mA))
assert_(np.all(mB >= mA))
assert_(np.all(mB >= mB))
assert_(not np.any(mB > mB))

assert_(np.all(mA == mA))
assert_(not np.any(mA == mB))
assert_(np.all(mB != mA))

assert_(not np.all(abs(mA) > 0))
assert_(np.all(abs(mB > 0)))

def test_asmatrix(self):
A = arange(100).reshape(10, 10)
A = np.arange(100).reshape(10, 10)
mA = asmatrix(A)
A[0, 0] = -10
assert_(A[0, 0] == mA[0, 0])
Expand All @@ -189,49 +188,48 @@ def test_repr(self):

class TestCasting(TestCase):
def test_basic(self):
A = arange(100).reshape(10, 10)
A = np.arange(100).reshape(10, 10)
mA = matrix(A)

mB = mA.copy()
O = ones((10, 10), float64) * 0.1
O = np.ones((10, 10), np.float64) * 0.1
mB = mB + O
assert_(mB.dtype.type == float64)
assert_(all(mA != mB))
assert_(all(mB == mA+0.1))
assert_(mB.dtype.type == np.float64)
assert_(np.all(mA != mB))
assert_(np.all(mB == mA+0.1))

mC = mA.copy()
O = ones((10, 10), complex128)
O = np.ones((10, 10), np.complex128)
mC = mC * O
assert_(mC.dtype.type == complex128)
assert_(all(mA != mB))
assert_(mC.dtype.type == np.complex128)
assert_(np.all(mA != mB))


class TestAlgebra(TestCase):
def test_basic(self):
import numpy.linalg as linalg

A = array([[1., 2.],
[3., 4.]])
A = np.array([[1., 2.], [3., 4.]])
mA = matrix(A)

B = identity(2)
B = np.identity(2)
for i in range(6):
assert_(allclose((mA ** i).A, B))
B = dot(B, A)
assert_(np.allclose((mA ** i).A, B))
B = np.dot(B, A)

Ainv = linalg.inv(A)
B = identity(2)
B = np.identity(2)
for i in range(6):
assert_(allclose((mA ** -i).A, B))
B = dot(B, Ainv)
assert_(np.allclose((mA ** -i).A, B))
B = np.dot(B, Ainv)

assert_(allclose((mA * mA).A, dot(A, A)))
assert_(allclose((mA + mA).A, (A + A)))
assert_(allclose((3*mA).A, (3*A)))
assert_(np.allclose((mA * mA).A, np.dot(A, A)))
assert_(np.allclose((mA + mA).A, (A + A)))
assert_(np.allclose((3*mA).A, (3*A)))

mA2 = matrix(A)
mA2 *= 3
assert_(allclose(mA2.A, 3*A))
assert_(np.allclose(mA2.A, 3*A))

def test_pow(self):
"""Test raising a matrix to an integer power works as expected."""
Expand Down Expand Up @@ -312,10 +310,10 @@ def test_instance_methods(self):

class TestIndexing(TestCase):
def test_basic(self):
x = asmatrix(zeros((3, 2), float))
y = zeros((3, 1), float)
x = asmatrix(np.zeros((3, 2), float))
y = np.zeros((3, 1), float)
y[:, 0] = [0.8, 0.2, 0.3]
x[:, 1] = y>0.5
x[:, 1] = y > 0.5
assert_equal(x, [[0, 1], [0, 0], [0, 0]])


Expand All @@ -330,7 +328,7 @@ def test_dimesions(self):

def test_array_from_matrix_list(self):
a = self.a
x = array([a, a])
x = np.array([a, a])
assert_equal(x.shape, [2, 2, 2])

def test_array_to_list(self):
Expand Down Expand Up @@ -362,7 +360,7 @@ def test_matrix_element(self):
assert_equal(x[:, 0].shape, x.shape)

def test_scalar_indexing(self):
x = asmatrix(zeros((3, 2), float))
x = asmatrix(np.zeros((3, 2), float))
assert_equal(x[0, 0], x[0][0])

def test_row_column_indexing(self):
Expand All @@ -373,14 +371,14 @@ def test_row_column_indexing(self):
assert_array_equal(x[:, 1], [[0], [1]])

def test_boolean_indexing(self):
A = arange(6)
A = np.arange(6)
A.shape = (3, 2)
x = asmatrix(A)
assert_array_equal(x[:, array([True, False])], x[:, 0])
assert_array_equal(x[array([True, False, False]),:], x[0,:])
assert_array_equal(x[:, np.array([True, False])], x[:, 0])
assert_array_equal(x[np.array([True, False, False]),:], x[0,:])

def test_list_indexing(self):
A = arange(6)
A = np.arange(6)
A.shape = (3, 2)
x = asmatrix(A)
assert_array_equal(x[:, [1, 0]], x[:, ::-1])
Expand All @@ -389,8 +387,8 @@ def test_list_indexing(self):

class TestPower(TestCase):
def test_returntype(self):
a = array([[0, 1], [0, 0]])
assert_(type(matrix_power(a, 2)) is ndarray)
a = np.array([[0, 1], [0, 0]])
assert_(type(matrix_power(a, 2)) is np.ndarray)
a = mat(a)
assert_(type(matrix_power(a, 2)) is matrix)

Expand All @@ -400,7 +398,7 @@ def test_list(self):

class TestShape(TestCase):
def setUp(self):
self.a = array([[1], [2]])
self.a = np.array([[1], [2]])
self.m = matrix([[1], [2]])

def test_shape(self):
Expand All @@ -420,7 +418,7 @@ def test_member_flatten(self):
assert_equal(self.m.flatten().shape, (1, 2))

def test_numpy_ravel_order(self):
x = array([[1, 2, 3], [4, 5, 6]])
x = np.array([[1, 2, 3], [4, 5, 6]])
assert_equal(np.ravel(x), [1, 2, 3, 4, 5, 6])
assert_equal(np.ravel(x, order='F'), [1, 4, 2, 5, 3, 6])
assert_equal(np.ravel(x.T), [1, 4, 2, 5, 3, 6])
Expand Down
4 changes: 3 additions & 1 deletion numpy/matrixlib/tests/test_multiarray.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from __future__ import division, absolute_import, print_function

import numpy as np
from numpy.testing import *
from numpy.testing import (
TestCase, run_module_suite, assert_, assert_equal, assert_array_equal
)

class TestView(TestCase):
def test_type(self):
Expand Down
Loading

0 comments on commit 0aef5ec

Please sign in to comment.