def test_sparse_consistent_when_stacked(self): m = 100 nnz = m * 10 n = 10 p = 5 rng = tf.random.Generator.from_seed(0) A = random_sparse((m, m), nnz, rng=rng) b = rng.normal((m, p)) bs = tf.split( # pylint: disable=no-value-for-parameter,redundant-keyword-arg b, p, axis=-1 ) Q_stacked, h_stacked = arnoldi_iteration_tf(A, b, n) Q_sep, h_sep = zip(*(arnoldi_iteration_tf(A, bi, n) for bi in bs)) Q_sep = tf.concat( # pylint: disable=no-value-for-parameter,unexpected-keyword-arg Q_sep, axis=-1 ) h_sep = tf.concat( # pylint: disable=no-value-for-parameter,unexpected-keyword-arg h_sep, axis=-1 ) self.assertAllClose(Q_stacked, Q_sep) self.assertAllClose(h_stacked, h_sep)
def test_symmetric_case(self): m = 100 nnz = m * 10 n = 20 p = 5 rng = tf.random.Generator.from_seed(0) A = random_sparse((m, m), nnz, rng=rng) A = tf.sparse.add(A, tf.sparse.transpose(A, (1, 0))) # make symmetric b = rng.normal((m, p)) Qs, hs = arnoldi_iteration_tf(A, b, n, symmetric=True) Q, h = arnoldi_iteration_tf(A, b, n) self.assertAllClose(Q, Qs, atol=1e-4) self.assertAllClose(h, hs, atol=1e-4)
def test_dense_consistent_with_numpy(self): m = 10 n = 3 rng = tf.random.Generator.from_seed(0) A = rng.normal((m, m)) b = rng.normal((m, 1)) Q_tf, h_tf = arnoldi_iteration_tf(A, b, n) Q_np, h_np = arnoldi_iteration_np(A.numpy(), tf.squeeze(b, 1).numpy(), n) self.assertAllClose(tf.squeeze(Q_tf, axis=-1), Q_np) self.assertAllClose(tf.squeeze(h_tf, axis=-1), h_np)