Ejemplo n.º 1
0
 def test_expm_multiply_interval_vector(self):
     np.random.seed(1234)
     interval = {'start': 0.1, 'stop': 3.2, 'endpoint': True}
     for num, n in product([14, 13, 2], [1, 2, 5, 20, 40]):
         A = scipy.linalg.inv(np.random.randn(n, n))
         v = np.random.randn(n)
         samples = np.linspace(num=num, **interval)
         X = expm_multiply(A, v, num=num, **interval)
         for solution, t in zip(X, samples):
             assert_allclose(solution, sp_expm(t * A).dot(v))
         # test for linear operator with unknown trace -> estimate trace
         Xguess = estimated(expm_multiply)(aslinearoperator(A),
                                           v,
                                           num=num,
                                           **interval)
         # test for linear operator with given trace
         Xgiven = expm_multiply(aslinearoperator(A),
                                v,
                                num=num,
                                **interval,
                                traceA=np.trace(A))
         # test robustness for linear operator with wrong trace
         Xwrong = expm_multiply(aslinearoperator(A),
                                v,
                                num=num,
                                **interval,
                                traceA=np.trace(A) * 5)
         for sol_guess, sol_given, sol_wrong, t in zip(
                 Xguess, Xgiven, Xwrong, samples):
             correct = sp_expm(t * A).dot(v)
             assert_allclose(sol_guess, correct)
             assert_allclose(sol_given, correct)
             assert_allclose(sol_wrong, correct)
Ejemplo n.º 2
0
 def test_expm_multiply_interval_matrix(self):
     np.random.seed(1234)
     interval = {'start': 0.1, 'stop': 3.2, 'endpoint': True}
     for num, n, k in product([14, 13, 2], [1, 2, 5, 20, 40], [1, 2]):
         A = scipy.linalg.inv(np.random.randn(n, n))
         B = np.random.randn(n, k)
         samples = np.linspace(num=num, **interval)
         X = expm_multiply(A, B, num=num, **interval)
         for solution, t in zip(X, samples):
             assert_allclose(solution, sp_expm(t*A).dot(B))
         X = estimated(expm_multiply)(aslinearoperator(A), B, num=num,
                                      **interval)
         for solution, t in zip(X, samples):
             assert_allclose(solution, sp_expm(t*A).dot(B))
Ejemplo n.º 3
0
 def test_sparse_expm_multiply_interval(self):
     np.random.seed(1234)
     start = 0.1
     stop = 3.2
     n = 40
     k = 3
     endpoint = True
     for num in (14, 13, 2):
         A = scipy.sparse.rand(n, n, density=0.05)
         B = np.random.randn(n, k)
         v = np.random.randn(n)
         for target in (B, v):
             X = expm_multiply(A,
                               target,
                               start=start,
                               stop=stop,
                               num=num,
                               endpoint=endpoint)
             samples = np.linspace(start=start,
                                   stop=stop,
                                   num=num,
                                   endpoint=endpoint)
             with suppress_warnings() as sup:
                 sup.filter(SparseEfficiencyWarning,
                            "splu converted its input to CSC format")
                 sup.filter(
                     SparseEfficiencyWarning,
                     "spsolve is more efficient when sparse b is in the CSC matrix format"
                 )
                 for solution, t in zip(X, samples):
                     assert_allclose(solution, sp_expm(t * A).dot(target))
Ejemplo n.º 4
0
 def _help_test_specific_expm_interval_status(self, target_status):
     np.random.seed(1234)
     start = 0.1
     stop = 3.2
     num = 13
     endpoint = True
     n = 5
     k = 2
     nrepeats = 10
     nsuccesses = 0
     for num in [14, 13, 2] * nrepeats:
         A = np.random.randn(n, n)
         B = np.random.randn(n, k)
         status = _expm_multiply_interval(A, B,
                 start=start, stop=stop, num=num, endpoint=endpoint,
                 status_only=True)
         if status == target_status:
             X, status = _expm_multiply_interval(A, B,
                     start=start, stop=stop, num=num, endpoint=endpoint,
                     status_only=False)
             assert_equal(X.shape, (num, n, k))
             samples = np.linspace(start=start, stop=stop,
                     num=num, endpoint=endpoint)
             for solution, t in zip(X, samples):
                 assert_allclose(solution, sp_expm(t*A).dot(B))
             nsuccesses += 1
     if not nsuccesses:
         msg = 'failed to find a status-' + str(target_status) + ' interval'
         raise Exception(msg)
Ejemplo n.º 5
0
 def test_scaled_expm_multiply_single_timepoint(self):
     np.random.seed(1234)
     t = 0.1
     n = 5
     k = 2
     A = np.random.randn(n, n)
     B = np.random.randn(n, k)
     observed = _expm_multiply_simple(A, B, t=t)
     expected = sp_expm(t * A).dot(B)
     assert_allclose(observed, expected)
Ejemplo n.º 6
0
 def test_matrix_vector_multiply(self):
     np.random.seed(1234)
     n = 40
     nsamples = 10
     for i in range(nsamples):
         A = scipy.linalg.inv(np.random.randn(n, n))
         v = np.random.randn(n)
         observed = expm_multiply(A, v)
         expected = np.dot(sp_expm(A), v)
         assert_allclose(observed, expected)
Ejemplo n.º 7
0
 def test_expm_multiply(self):
     np.random.seed(1234)
     n = 40
     k = 3
     nsamples = 10
     for i in range(nsamples):
         A = scipy.linalg.inv(np.random.randn(n, n))
         B = np.random.randn(n, k)
         observed = expm_multiply(A, B)
         expected = np.dot(sp_expm(A), B)
         assert_allclose(observed, expected)
Ejemplo n.º 8
0
 def test_scaled_expm_multiply(self):
     np.random.seed(1234)
     n = 40
     k = 3
     nsamples = 10
     for i in range(nsamples):
         for t in (0.2, 1.0, 1.5):
             with np.errstate(invalid='ignore'):
                 A = scipy.linalg.inv(np.random.randn(n, n))
                 B = np.random.randn(n, k)
                 observed = _expm_multiply_simple(A, B, t=t)
                 expected = np.dot(sp_expm(t * A), B)
                 assert_allclose(observed, expected)
Ejemplo n.º 9
0
def test_expm_multiply_dtype(dtype_a, dtype_b, b_is_matrix):
    """Make sure `expm_multiply` handles all numerical dtypes correctly."""
    assert_allclose_ = (partial(assert_allclose, rtol=1e-3, atol=1e-5)
                        if {dtype_a, dtype_b} & IMPRECISE else assert_allclose)
    rng = np.random.default_rng(1234)
    # test data
    n = 7
    b_shape = (n, 3) if b_is_matrix else (n, )
    if dtype_a in REAL_DTYPES:
        A = scipy.linalg.inv(rng.random([n, n])).astype(dtype_a)
    else:
        A = scipy.linalg.inv(
            rng.random([n, n]) + 1j*rng.random([n, n])
        ).astype(dtype_a)
    if dtype_b in REAL_DTYPES:
        B = (2*rng.random(b_shape)).astype(dtype_b)
    else:
        B = (rng.random(b_shape) + 1j*rng.random(b_shape)).astype(dtype_b)

    # single application
    sol_mat = expm_multiply(A, B)
    sol_op = estimated(expm_multiply)(aslinearoperator(A), B)
    direct_sol = np.dot(sp_expm(A), B)
    assert_allclose_(sol_mat, direct_sol)
    assert_allclose_(sol_op, direct_sol)
    sol_op = expm_multiply(aslinearoperator(A), B, traceA=np.trace(A))
    assert_allclose_(sol_op, direct_sol)

    # for time points
    interval = {'start': 0.1, 'stop': 3.2, 'num': 13, 'endpoint': True}
    samples = np.linspace(**interval)
    X_mat = expm_multiply(A, B, **interval)
    X_op = estimated(expm_multiply)(aslinearoperator(A), B, **interval)
    for sol_mat, sol_op, t in zip(X_mat, X_op, samples):
        direct_sol = sp_expm(t*A).dot(B)
        assert_allclose_(sol_mat, direct_sol)
        assert_allclose_(sol_op, direct_sol)
Ejemplo n.º 10
0
 def test_expm_multiply_interval_vector(self):
     np.random.seed(1234)
     start = 0.1
     stop = 3.2
     endpoint = True
     for num in (14, 13, 2):
         for n in (1, 2, 5, 20, 40):
             A = scipy.linalg.inv(np.random.randn(n, n))
             v = np.random.randn(n)
             X = expm_multiply(A, v,
                     start=start, stop=stop, num=num, endpoint=endpoint)
             samples = np.linspace(start=start, stop=stop,
                     num=num, endpoint=endpoint)
             for solution, t in zip(X, samples):
                 assert_allclose(solution, sp_expm(t*A).dot(v))
Ejemplo n.º 11
0
 def test_scaled_expm_multiply(self):
     np.random.seed(1234)
     n = 40
     k = 3
     nsamples = 10
     for i, t in product(range(nsamples), [0.2, 1.0, 1.5]):
         with np.errstate(invalid='ignore'):
             A = scipy.linalg.inv(np.random.randn(n, n))
             B = np.random.randn(n, k)
             observed = _expm_multiply_simple(A, B, t=t)
             expected = np.dot(sp_expm(t * A), B)
             assert_allclose(observed, expected)
             observed = estimated(_expm_multiply_simple)(
                 aslinearoperator(A), B, t=t)
             assert_allclose(observed, expected)
Ejemplo n.º 12
0
 def test_expm_multiply(self):
     np.random.seed(1234)
     n = 40
     k = 3
     nsamples = 10
     for i in range(nsamples):
         A = scipy.linalg.inv(np.random.randn(n, n))
         B = np.random.randn(n, k)
         observed = expm_multiply(A, B)
         expected = np.dot(sp_expm(A), B)
         assert_allclose(observed, expected)
         observed = estimated(expm_multiply)(aslinearoperator(A), B)
         assert_allclose(observed, expected)
         traceA = np.trace(A)
         observed = expm_multiply(aslinearoperator(A), B, traceA=traceA)
         assert_allclose(observed, expected)
Ejemplo n.º 13
0
 def test_sparse_expm_multiply(self):
     np.random.seed(1234)
     n = 40
     k = 3
     nsamples = 10
     for i in range(nsamples):
         A = scipy.sparse.rand(n, n, density=0.05)
         B = np.random.randn(n, k)
         observed = expm_multiply(A, B)
         with suppress_warnings() as sup:
             sup.filter(SparseEfficiencyWarning,
                        "splu requires CSC matrix format")
             sup.filter(SparseEfficiencyWarning,
                        "spsolve is more efficient when sparse b is in the"
                        " CSC matrix format")
             expected = sp_expm(A).dot(B)
         assert_allclose(observed, expected)
Ejemplo n.º 14
0
 def time_expm(self, n, format):
     if format == 'sparse':
         sp_expm(self.A_sparse)
     elif format == 'dense':
         scipy.linalg.expm(self.A_dense)