Ejemplo n.º 1
0
def minquadcont(f, a, b, grau):
    grau += 1
    g = []
    for i in range(grau):
        g.append(x**i)
    B = np.zeros((grau,grau))
    D = np.zeros((grau,1))
    for i in range(grau):
        for j in range(grau):
            B[i][j] = integrate(g[i]*g[j], (x, a, b))
        D[i][0] = integrate(g[i]*f, (x, a, b))

    print("A matriz dos coeficientes do sistema, no qual denotamos por B é")
    print(prettymatrix.matrix_to_string(B, name='B = '))
    print("E a matriz coluna cuja cada entrada é <g_i,f> é:")
    print(prettymatrix.matrix_to_string(D, name='D = '))

    Y = sistLinear(B, D, grau)

    print("Solução do sistema B*Y=D via eliminação de Gauss com  pivotamento parcial:")
    print(prettymatrix.matrix_to_string(Y, name='Y = '))

    P = 0
    for i in range(grau):
        P += Y[i][0]*x**i
    print("Polinômio g(x) = ", P)
Ejemplo n.º 2
0
def Cholesky(A, B):
    ordem = np.shape(A)
    if ordem[0] == 0: return print("Isso não é uma matriz")
    if ordem[0] == ordem[1]:
        det = Determinantes(A, ordem[0])
        if( det[1] <= 0):
            print("Determinante <= 0 para k = " + str(det[0]))
            return
        trans = Transposta(A, ordem[0])
        if( not trans ):
            return print("Matriz não é igual sua transposta")

        mG = np.zeros((ordem[0],ordem[1]))
        mG = valoresG(A, mG, ordem[0])
        print(prettymatrix.matrix_to_string(mG, name='G = '))

        mY = sistLinear(mG, B, ordem[0])
        print(prettymatrix.matrix_to_string(mY, name='Y = '))

        transpG = np.zeros((ordem[0],ordem[1]))
        for i in range(ordem[0]):
            for j in range(ordem[0]):
                transpG[i][j] = mG[j][i]

        transpG = np.asarray(transpG)

        mX = sistLinear(transpG, mY, ordem[0])
        print(prettymatrix.matrix_to_string(mX, name='X = '))
Ejemplo n.º 3
0
def MA(A, B, ordem):
    MA = np.zeros((ordem,ordem))
    MB = np.zeros(ordem)

    for i in range(ordem):
        MB[i] = B[i]
        for j in range(ordem):
            MA[i][j] = A[i][j]

    mAum = np.zeros((ordem,ordem+1))
    for k in range(ordem-1):
        pivotParcial(A,B,MA,MB,ordem,k)
        for i in range(k+1,ordem):
            MB[i] = round(B[i] - (B[k] * A[i][k])/A[k][k], 8)
            for j in range(ordem):
                MA[i][j] = round(A[i][j] - (A[k][j]*A[i][k])/A[k][k], 8)

        for i in range(ordem):
            for j in range(ordem+1):
                if j == ordem:
                    mAum[i][j] = MB[i]
                else:
                    mAum[i][j] = MA[i][j]
        mAum = np.asarray(mAum)
        print(prettymatrix.matrix_to_string(mAum, name='Matriz aumentada = '))

        for i in range(ordem):
            B[i] = MB[i]
            for j in range(ordem):
                A[i][j] = MA[i][j]
    return MA, MB
Ejemplo n.º 4
0
def Gauss(A, B):
    ordem = np.shape(A)
    if ordem[0] == 0: return print("Isso não é uma matriz")
    if ordem[0] != ordem[1]: return print("Esta matriz não é quadrada")
    mA, mB = MA(A, B, ordem[0])
    mX = sistLinear(mA, mB, ordem[0])
    print(prettymatrix.matrix_to_string(mX, name='X = '))
Ejemplo n.º 5
0
def minquaddis(pontos, grau):
    pts = len(pontos)
    g = np.zeros((grau+1,pts))
    f = []
    for j in range(pts):
        for i in range(grau+1):
            g[i][j] = pontos[j][0]**i
        f.append(pontos[j][1])
    
    print("Vetores")
    for i in range(grau+1):
        print("g"+str(i+1)+" = ", g[i])
    print("f = ", f)
    print("")

    B = np.zeros((grau+1,grau+1))
    for i in range(grau+1):
        for j in range(grau+1):
            soma = 0
            for k in range(pts):
                soma += g[i][k] * g[j][k]
            B[i][j] = soma
    print("A matriz dos coeficientes do sistema, no qual denotamos por B é")
    print(prettymatrix.matrix_to_string(B, name='B = '))

    print("E a matriz coluna cuja cada entrada é <g_i,f> é:")
    D = []
    for i in range(grau+1):
        soma = 0
        for k in range(pts):
            soma += g[i][k] * f[k]
        D.append([soma])
    D = np.asarray(D)
    print(prettymatrix.matrix_to_string(D, name='D = '))

    print("Solução do sistema B*Y=D via eliminação de Gauss com  pivotamento parcial:")
    Y = sistLinear(B,D,grau+1)
    print(prettymatrix.matrix_to_string(Y, name='Y = '))

    p = 0
    for i in range(grau+1):
        p += Y[i][0]*x**i

    print("Polinômio g(x) = ",p)
 def test_2_x_2_matrix_with_long_name(self):
     expected = ("W_long_name\n"
                 "┌        ┐ \n"
                 "│ 00 000 │ \n"
                 "│ 0  00  │ \n"
                 "└        ┘ ")
     actual = prettymatrix.matrix_to_string(np.array([['00', '000'],
                                                      ['0', '00']]),
                                            name='W_long_name')
     self.assertEqual(expected, actual)
Ejemplo n.º 7
0
def LU(A,B):
    ordem = np.shape(A)
    if ordem[0] == 0: return print("Isso não é uma matriz")
    if ordem[0] != ordem[1]: return print("Esta matriz não é quadrada")
    det = Determinantes(A, ordem[0]-1)
    if( det[1] == 0):
        return print("Determinante = 0 para k = " + str(det[0]))

    L = np.eye(ordem[0])
    U = np.zeros((ordem[0],ordem[1]))

    mLU = valoresLU(L, U, A, ordem[0])

    mY = sistLinear(L, B, ordem[0])
    mX = sistLinear(U, mY, ordem[0])

    print(prettymatrix.matrices_to_string(L, U, names=['L','U']))
    print(prettymatrix.matrix_to_string(mY, name='Y = '))
    print(prettymatrix.matrix_to_string(mX, name='X = '))
 def test_11x11_matrix_inserts_ellipses(self):
     expected = ("┌                   ┐\n"
                 "│ 0 0 0 … … … 0 0 0 │\n"
                 "│ 0 0 0 … … … 0 0 0 │\n"
                 "│ 0 0 0 … … … 0 0 0 │\n"
                 "│ … … … … … … … … … │\n"
                 "│ … … … … … … … … … │\n"
                 "│ … … … … … … … … … │\n"
                 "│ 0 0 0 … … … 0 0 0 │\n"
                 "│ 0 0 0 … … … 0 0 0 │\n"
                 "│ 0 0 0 … … … 0 0 0 │\n"
                 "└                   ┘")
     actual = prettymatrix.matrix_to_string(np.full((11, 11), '0'))
     self.assertEqual(expected, actual)
 def test_10x10_matrix_renders_normally(self):
     expected = ("┌                     ┐\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "│ 0 0 0 0 0 0 0 0 0 0 │\n"
                 "└                     ┘")
     actual = prettymatrix.matrix_to_string(np.full((10, 10), '0'))
     self.assertEqual(expected, actual)
Ejemplo n.º 10
0
def condicaoF(A, ordem, tipo):
    F = np.zeros((ordem,ordem))
    for i in range(ordem):
        for j in range(ordem):
            if i != j:
                F[i][j] = -(A[i][j]/A[i][i])
    print(prettymatrix.matrix_to_string(F, name='Matriz F = '))
    print("Cálculo da soma em valor absoluto de cada linha da matriz F:")
    if norma_linha(F) < 1:
        return true
    elif tipo == 1:
        print("Cálculo da soma em valor absoluto de cada coluna da matriz F:")
        if norma_coluna(F) < 1:
            return true
    else:
        return false
 def test_empty_matrix(self):
     expected = ("┌  ┐\n" "└  ┘")
     actual = prettymatrix.matrix_to_string(np.full((0, 0), ''))
     self.assertEqual(expected, actual)
 def test_2_x_2_matrix_with_single_and_multi_digit_cells_4(self):
     expected = ("┌        ┐\n" "│ 00 000 │\n" "│ 0  00  │\n" "└        ┘")
     actual = prettymatrix.matrix_to_string(
         np.array([['00', '000'], ['0', '00']]))
     self.assertEqual(expected, actual)
 def test_2_x_2_matrix_with_single_digit_cells(self):
     expected = ("┌     ┐\n" "│ 0 0 │\n" "│ 0 0 │\n" "└     ┘")
     actual = prettymatrix.matrix_to_string(np.full((2, 2), '0'))
     self.assertEqual(expected, actual)
 def test_1_x_1_matrix_with_multi_digit_cell(self):
     expected = ("┌    ┐\n" "│ 00 │\n" "└    ┘")
     actual = prettymatrix.matrix_to_string(np.full((1, 1), '00'))
     self.assertEqual(expected, actual)
 def test_2_x_1_matrix(self):
     expected = ("┌   ┐\n" "│ 0 │\n" "│ 0 │\n" "└   ┘")
     actual = prettymatrix.matrix_to_string(np.full((2, 1), '0'))
     self.assertEqual(expected, actual)
 def test_empty_matrix_with_name_and_dimensions(self):
     expected = ("M    \n" "(0x0)\n" "┌  ┐ \n" "└  ┘ ")
     actual = prettymatrix.matrix_to_string(np.full((0, 0), ''),
                                            name='M',
                                            include_dimensions=True)
     self.assertEqual(expected, actual)
 def test_empty_matrix_with_long_name(self):
     expected = ("W_x_y\n" "┌  ┐ \n" "└  ┘ ")
     actual = prettymatrix.matrix_to_string(np.full((0, 0), ''),
                                            name='W_x_y')
     self.assertEqual(expected, actual)
 def test_1_x_2_matrix_with_short_name(self):
     expected = ("W      \n" "┌     ┐\n" "│ 0 0 │\n" "└     ┘")
     actual = prettymatrix.matrix_to_string(np.full((1, 2), '0'), name='W')
     self.assertEqual(expected, actual)
Ejemplo n.º 19
0
def spline(pontos, valor):
    h = []
    for i in range(1,len(pontos)):
        h.append(pontos[i][0] - pontos[i-1][0])
    M = np.zeros((len(h)-1,len(h)-1))
    for i in range(len(h)-1):
        if i == 0:
            M[i][i]   = 2*(h[i]+h[i+1])
            M[i][i+1] = h[i+1]
        elif i == len(h)-2:
            M[i][i]   = 2*(h[i]+h[i+1])
            M[i][i-1] = h[i]
        else:
            M[i][i]   = 2*(h[i]+h[i+1])
            M[i][i-1] = h[i]
            M[i][i+1] = h[i+1]

    print(prettymatrix.matrix_to_string(M, name='Matriz = '))
    B = np.zeros((len(h)-1,1))
    for i in range(1,len(h)):
        B[i-1][0] = 6*((pontos[i+1][1]-pontos[i][1])/h[i]) - 6*((pontos[i][1]-pontos[i-1][1])/h[i-1])

    print(prettymatrix.matrix_to_string(B, name='B = '))
    mu = sistLinear(M, B, len(h)-1)
    print("Spline natural: \u03BC0 = 0, \u03BC"+str(len(h))+" = 0\n")
    print("Resolvendo o sistema linear M*Y=B, temos:")
    print('\u03BC1 = ', mu[0][0])
    print('\u03BC2 = ', mu[1][0])

    alpha = np.zeros(len(h))
    beta  = np.zeros(len(h))
    gamma = np.zeros(len(h))
    for i in range(len(h)):
        if i == 0:
            alpha[i] = ((pontos[i+1][1]-pontos[i][1])/h[i]) - ((mu[i][0]/6)*h[i]) - ((0/3)*h[i])
            beta[i] = 0/2
            gamma[i] = (mu[i][0]-0)/(6*h[i])
        elif i == len(mu):
            alpha[i] = ((pontos[i+1][1]-pontos[i][1])/h[i]) - ((0/6)*h[i]) - ((mu[i-1]/3)*h[i])
            beta[i] = mu[i-1][0]/2
            gamma[i] = (0-mu[i-1][0])/(6*h[i])
        else:
            alpha[i] = ((pontos[i+1][1]-pontos[i][1])/h[i]) - ((mu[i][0]/6)*h[i]) - ((mu[i-1]/3)*h[i])
            beta[i] = mu[i-1][0]/2
            gamma[i] = (mu[i][0]-mu[i-1][0])/(6*h[i])
    
    i = np.linspace(0,len(alpha)-1,len(alpha))
    Table = PrettyTable()
    Table.add_column("i",i)
    Table.add_column("\u03B1",alpha)
    Table.add_column("\u03B2",beta)
    Table.add_column("\u03B3",gamma)
    print("\nCoeficientes dos polinomios da spline:")
    print(Table)

    S = []
    for i in range(len(alpha)):
        # print(pontos[i][1] +(alpha[i]*(x-pontos[i][0])))
        S.append(pontos[i][1] + (alpha[i]*(x-pontos[i][0])) + (beta[i]*(x-pontos[i][0])**2) + (gamma[i]*(x-pontos[i][0])**3))
    
    print("\nSpline cúbica natural:\n")
    for i in range(len(S)):
        print("P"+str(i)+"(x) = "+str(simplify(S[i]))+" , Intervalo=["+str(pontos[i][0])+","+str(pontos[i+1][0])+"]")
    print("")

    c = 0
    for i in range(1,len(pontos)):
        intervalo = [pontos[i-1][0],pontos[i][0]]
        if valor >= intervalo[0] and valor < intervalo[1]:
            c = copy.copy(i)
            break
    print("Queremos encontrar o valor para f("+str(valor)+") então devemos usar P"+str(c-1)+" pois x = "+str(valor)+" está contido no intervalo = ",intervalo)
    print("\nLogo, a função em x = "+str(valor)+" é aproximadamente: ",S[1].subs(x,valor))
 def test_1_x_1_matrix_with_long_name(self):
     expected = ("W_x_y_z\n" "┌   ┐  \n" "│ 0 │  \n" "└   ┘  ")
     actual = prettymatrix.matrix_to_string(np.full((1, 1), '0'),
                                            name='W_x_y_z')
     self.assertEqual(expected, actual)
 def test_2_x_1_matrix_with_dimensions(self):
     expected = ("(2x1)\n" "┌   ┐\n" "│ 0 │\n" "│ 0 │\n" "└   ┘")
     actual = prettymatrix.matrix_to_string(np.full((2, 1), '0'),
                                            include_dimensions=True)
     self.assertEqual(expected, actual)