Exemplo n.º 1
0
 def test_basic3(self):
     # b is a 3-d matrix.
     c = np.array([1, 2, -3, -5])
     b = np.arange(24).reshape(4, 3, 2)
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
Exemplo n.º 2
0
 def test_complex(self):
     # Complex b and c
     c = np.array([1+2j, -3, 4j, 5])
     b = np.arange(8).reshape(4, 2) + 0.5j
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
Exemplo n.º 3
0
 def test_complex(self):
     # Complex b and c
     c = np.array([1 + 2j, -3, 4j, 5])
     b = np.arange(8).reshape(4, 2) + 0.5j
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
Exemplo n.º 4
0
 def test_basic3(self):
     # b is a 3-d matrix.
     c = np.array([1, 2, -3, -5])
     b = np.arange(24).reshape(4, 3, 2)
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
Exemplo n.º 5
0
 def test_singular(self):
     # c gives a singular circulant matrix.
     c = np.array([1, 1, 0, 0])
     b = np.array([1, 2, 3, 4])
     x = solve_circulant(c, b, singular='lstsq')
     y, res, rnk, s = lstsq(circulant(c), b)
     assert_allclose(x, y)
     assert_raises(LinAlgError, solve_circulant, x, y)
Exemplo n.º 6
0
 def test_random_b_and_c(self):
     # Random b and c
     np.random.seed(54321)
     c = np.random.randn(50)
     b = np.random.randn(50)
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
Exemplo n.º 7
0
 def test_singular(self):
     # c gives a singular circulant matrix.
     c = np.array([1, 1, 0, 0])
     b = np.array([1, 2, 3, 4])
     x = solve_circulant(c, b, singular='lstsq')
     y, res, rnk, s = lstsq(circulant(c), b)
     assert_allclose(x, y)
     assert_raises(LinAlgError, solve_circulant, x, y)
Exemplo n.º 8
0
 def test_random_b_and_c(self):
     # Random b and c
     np.random.seed(54321)
     c = np.random.randn(50)
     b = np.random.randn(50)
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
Exemplo n.º 9
0
    def test_axis_args(self):
        # Test use of caxis, baxis and outaxis.

        # c has shape (2, 1, 4)
        c = np.array([[[-1, 2.5, 3, 3.5]], [[1, 6, 6, 6.5]]])

        # b has shape (3, 4)
        b = np.array([[0, 0, 1, 1], [1, 1, 0, 0], [1, -1, 0, 0]])

        x = solve_circulant(c, b, baxis=1)
        assert_equal(x.shape, (4, 2, 3))
        expected = np.empty_like(x)
        expected[:, 0, :] = solve(circulant(c[0]), b.T)
        expected[:, 1, :] = solve(circulant(c[1]), b.T)
        assert_allclose(x, expected)

        x = solve_circulant(c, b, baxis=1, outaxis=-1)
        assert_equal(x.shape, (2, 3, 4))
        assert_allclose(np.rollaxis(x, -1), expected)

        # np.swapaxes(c, 1, 2) has shape (2, 4, 1); b.T has shape (4, 3).
        x = solve_circulant(np.swapaxes(c, 1, 2), b.T, caxis=1)
        assert_equal(x.shape, (4, 2, 3))
        assert_allclose(x, expected)
Exemplo n.º 10
0
    def test_axis_args(self):
        # Test use of caxis, baxis and outaxis.

        # c has shape (2, 1, 4)
        c = np.array([[[-1, 2.5, 3, 3.5]], [[1, 6, 6, 6.5]]])

        # b has shape (3, 4)
        b = np.array([[0, 0, 1, 1], [1, 1, 0, 0], [1, -1, 0, 0]])

        x = solve_circulant(c, b, baxis=1)
        assert_equal(x.shape, (4, 2, 3))
        expected = np.empty_like(x)
        expected[:, 0, :] = solve(circulant(c[0]), b.T)
        expected[:, 1, :] = solve(circulant(c[1]), b.T)
        assert_allclose(x, expected)

        x = solve_circulant(c, b, baxis=1, outaxis=-1)
        assert_equal(x.shape, (2, 3, 4))
        assert_allclose(np.rollaxis(x, -1), expected)

        # np.swapaxes(c, 1, 2) has shape (2, 4, 1); b.T has shape (4, 3).
        x = solve_circulant(np.swapaxes(c, 1, 2), b.T, caxis=1)
        assert_equal(x.shape, (4, 2, 3))
        assert_allclose(x, expected)
def BTCS(x, t, phiold, c):
    nx = len(x)
    nt = len(t)
    M = np.zeros(nt)
    V = np.zeros(nt)
    TV = np.zeros(nt)
    "We are going to solve a linear system of equations, where the matrix of "
    "coefficients is a circulant matrix, which is only defined by its first"
    "column that we will represent as a vector named 'q' "
    q = np.zeros(nx)
    q[0] = 1
    q[1] = -c / 2
    q[-1] = c / 2
    for it in range(nt):
        phiold = linalg.solve_circulant(q, phiold)
        M[it] = first_mom(phiold)
        V[it] = second_mom(phiold)
        TV[it] = tot_var(phiold)
    return phiold, M, V, TV
Exemplo n.º 12
0
 def test_basic1(self):
     c = np.array([1, 2, 3, 5])
     b = np.array([1, -1, 1, 0])
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)
# Sistemas de ecuaciones lineales
linalg.solve(
    matriz, vector
)  # Resolver sistema de ecuaciones (resuelve por matriz triangular superior)
linalg.solve(
    matriz, vector, lower=True
)  # Resolver sistema de ecuaciones (resuelve por matriz triangular inferior)
linalg.solve(
    matriz, vector, overwrite_a=True
)  # Resolver sistema de ecuaciones (Sobrescribe la matriz para optimizar)
linalg.solve(
    matriz, vector, overwrite_b=True
)  # Resolver sistema de ecuaciones (Sobrescribe el vector para optimizar)
linalg.solve_circulant(
    coeficientes, vector
)  # Resolver sistema de ecuaciones con coeficiente de una Matriz circular
linalg.solve_triangular(
    matriz, vector
)  # Resolver sistema de ecuaciones asumiendo Matriz Triangular Superior
linalg.solve_triangular(
    matriz, vector, lower=True
)  # Resolver sistema de ecuaciones asumiendo Matriz Triangular Inferior
linalg.solve_triangular(
    (columnas, filas), vector
)  # Resolver sistema de ecuaciones con la primera fila y columna de la Matriz de Toeplitz usando la recursion de Levinson
linalg.lu_solve(
    (lu, piv), vector
)  # Resolver sistema de ecuaciones dada la decomposición LU (lu: Matriz que contiene a L y U, piv: Indices que representan los unos de la matriz P en filas)
linalg.lu_solve(linalg.lu_factor(matriz),
                vector)  # Resolver sistema de ecuaciones por decomposición LU
Exemplo n.º 14
0
 def test_basic1(self):
     c = np.array([1, 2, 3, 5])
     b = np.array([1, -1, 1, 0])
     x = solve_circulant(c, b)
     y = solve(circulant(c), b)
     assert_allclose(x, y)