def test(self): m, n = random.randint(1, 3), random.randint(1, 3) A, B = 100 * np.random.rand(m, n), 100 * np.random.rand(m, n) A_ = import_array(A) B_ = import_array(B) equality, log = _assert_mp_equals_np(A_ + B_, A + B) self.assertTrue(equality, msg=log)
def test(self): # Test multiplying (m,k)*(k,n) matrices m, n = random.randint(1, 3), random.randint(1, 3) k = random.randint(1, 3) A, B = 100 * np.random.rand(m, k), 100 * np.random.rand(k, n) A_ = import_array(A) B_ = import_array(B) AB_np = A @ B AB_mp = A_ @ B_ equality, log = _assert_mp_equals_np(AB_mp, AB_np) self.assertTrue(equality, msg=log)
def test(self): m, n = 6, 5 A = 100 * np.random.rand(m, n) A_ = import_array(A) B = 100 * np.random.rand(m, n) B_ = import_array(B) A_[2, :] = B_[2, :] A[2, :] = B[2, :] A_mp = A_ A_np = A equality, log = _assert_mp_equals_np(A_mp, A_np) self.assertTrue(equality, msg=log)
def memory_check(): """Manually investigating memory usage to verify views are effective. For a matrix with one million doubles, a numpy array is approximately 25x more memory-efficient than an MPMatrix. Most of this is overhead: modifying the context precision leads to very minor increases in context precision. Views have negligible memory costs. """ m, n = 1000, 1000 print("Pre initialization") print(hpy().heap()) # 14.3 Mb A = 100 * np.random.rand(m, n) print("NP initialized") print(hpy().heap()) # 22.3 Mb A_ = import_array(A) print("MP initialized") print(hpy().heap()) # 213 Mb # Adding a view is less than 1Mb overhead print("Adding a minimal list implementation of the same data") B_ = [mpfr(x) for x in A.flatten()] print(hpy().heap()) # 294 Mb
def test(self): m, n = 6, 5 A = 100 * np.random.rand(m, n) A_ = import_array(A) B = 100 * np.random.rand(m, n) B_ = import_array(B) A_[:, 2] = B_[:, 2] A[:, 2] = B[:, 2] A_mp = A_ A_np = A equality, log = _assert_mp_equals_np(A_mp, A_np) log += "\nListing shapes. A: {}, A_: {}, B: {}, B_: {}".format( A.shape, A_.shape, B.shape, B_.shape) log += "\nListing A matrices. A: {}\n\n A_: {}\n".format(A, A_) log += "\nColumn to write: {}".format(B[:, 2]) self.assertTrue(equality, msg=log)
def test(self): m, n = 1, 5 A = 100 * np.random.rand(m, n) A_ = import_array(A) A_mp = A_[2] A_np = A[0, 2] equality = _ptwise_vals_equal(A_mp, A_np, None) self.assertTrue(equality, msg="MP: {}, NP: {}".format(A_mp, A_np))
def test(self): m, n = 1, 5 A = 100 * np.random.rand(m, n) A_ = import_array(A) A_mp = A_[0, 1:] A_np = A[:, 1:] equality, log = _assert_mp_equals_np(A_mp, A_np) self.assertTrue(equality, msg=log)
def test(self): m, n = 4, 1 A = 100 * np.random.rand(m, n) A_ = import_array(A) k = 0 A_mp = A_[:, k] A_np = A[k, :, np.newaxis] equality, log = _assert_mp_equals_np(A_mp, A_np) self.assertTrue(equality, msg=log)
def test(self): A = np.random.rand(5, 1) A_ = import_array(A) npv, npbeta = self.nph(A) mpv, mpbeta = A_._house() equality, log = _assert_mp_equals_np(mpv, npv) log += "A: {}\nnpv: {}\nmpv: {}\nnpbeta: {}, mpbeta: {}".format( A, npv, mpv, npbeta, mpbeta) self.assertTrue(equality, msg=log)
def test(self): m, n = 4, 5 A = 100 * np.random.rand(m, n) A_ = import_array(A) k = 3 A_mp = A_[:, k] A_np = A[:, k, np.newaxis] equality, log = _assert_mp_equals_np(A_mp, A_np) self.assertTrue(equality, msg=log + "\nMP: {}, \n\nNP: {}".format(A_mp, A_np))
def test(self): m, n = 4, 5 A = 100 * np.random.rand(m, n) A_ = import_array(A) npT = A.T mpT = A_.T() equality, log = _assert_mp_equals_np(mpT, npT) log += "\nListing transposed matrices. mpT: {}\n\n npT: {}\n".format( mpT, npT) log += "\nOriginal np matrix: {}".format(A) log += "\nmpT[0,1]: {}. Without transpose: {}".format( mpT[0, 1], A_[0, 1]) self.assertTrue(equality, msg=log)
def test(self): m, n = 6, 5 A = 100 * np.random.rand(m, n) npQ, npR = np.linalg.qr(A) A_ = import_array(A) mpQ, mpR = A_.QR() equality, log = _assert_mp_equals_np(mpQ, npQ) log += "\nmpQ:\n{} \nnpQ:\n{}".format(mpQ, npQ) log += "\nmpR:\n{} \nnpR:\n{}".format(mpR, npR) self.assertTrue(equality, msg=log) equality, = _assert_mp_equals_np(mpR, npR) self.assertTrue(equality, msg=log)
def test(self): # Test scaling with string m, n = random.randint(1, 3), random.randint(1, 3) A = 100 * np.random.rand(m, n) c = random.random() # float in [0,1) A_ = import_array(A) cA_np = c * A cA_mp = A_ * (c) equality, log = _assert_mp_equals_np(cA_mp, cA_np) self.assertTrue(equality, msg=log) # Test __imul__ A_ *= c equality, log = _assert_mp_equals_np(A_, cA_np) self.assertTrue(equality, msg=log)
def test(self): n = random.randint(1, 3) A = 100 * np.random.rand(n, n) A_ = import_array(A) equality, log = _assert_mp_equals_np(A_, A) self.assertTrue(equality, msg=log)