示例#1
0
def solve(A, b):
    m, n = A.shape

    # augmentedA = [A|b]
    augmentedA = np.zeros((m, n + 1), float)
    augmentedA[:, :n] = A
    augmentedA[:, n] = b
    R, rankAb, nre = LA.rref(augmentedA)

    # compute the rank of A
    rankA = 0
    i = 0
    j = 0
    while (i < m and j < n):
        pivot = R[i, j]
        if (pivot == 0):
            j += 1
        else:
            rankA += 1
            i += 1
            j += 1
    print(rankA)
    print(rankAb)
    # rankA == rankAb == n : only solution
    # rankA == rankAb < n  : infinite solutions
    # rankAb > rankA : no solution
    if (rankA == rankAb):
        if (rankA == n):
            x = np.array(R[:n, n])
            return x
        else:  # rankA<n
            print('infinite solutions')
    else:  # rankAb>rankA
        print('no solution')
示例#2
0
def inv(M):
    # check dimension of M
    m, n = M.shape
    if (m != n):
        print('It is not a square matrix, no inverse define !')
    else:
        augmentedM = np.zeros((n, 2 * n), float)
        augmentedM[:, :n] = M[:, :]
        for i in range(n):
            augmentedM[i, n + i] = 1

        # reduced row echelon form
        R, rank, nre = LA.rref(augmentedM)
        # check if M is full rank or not
        fullRank = 1
        for i in range(n):
            if (R[i, i] == 0):
                fullRank = 0
                break

        if (fullRank == 0):
            print('The matrix is singular. Try pseudo-inverse!')
        else:
            inverse = np.array(R[:, n:])
            return inverse
示例#3
0
文件: LA.py 项目: ntudavid/LA_lab
def solve(A,b):
    m,n = A.shape

    # augmentedA = [A|b]
    augmentedA = np.zeros((m,n+1),float)
    augmentedA[:,:n] = A
    augmentedA[:,n] = b
    R, rankAb, nre = LA.rref(augmentedA)

    # compute the rank of A
    rankA = 0
    i = 0
    j = 0
    while (i<m and j<n):
        pivot = R[i,j]
        if(pivot==0):
            j += 1
        else:
            rankA += 1
            i += 1
            j += 1
    print(rankA)
    print(rankAb)
    # rankA == rankAb == n : only solution
    # rankA == rankAb < n  : infinite solutions
    # rankAb > rankA : no solution
    if (rankA == rankAb):
        if (rankA==n):
            x=np.array(R[:n,n])
            return x
        else: # rankA<n
            print('infinite solutions')
    else: # rankAb>rankA
        print('no solution')
示例#4
0
文件: LA.py 项目: ntudavid/LA_lab
def inv(M):
    # check dimension of M
    m,n = M.shape
    if (m!=n):
        print('It is not a square matrix, no inverse define !')
    else :
        augmentedM = np.zeros((n,2*n),float)
        augmentedM[:,:n] = M[:,:]
        for i in range(n):
            augmentedM[i,n+i]=1

        # reduced row echelon form               
        R, rank, nre = LA.rref(augmentedM)
        # check if M is full rank or not
        fullRank=1;
        for i in range(n):
            if(R[i,i]==0):
                fullRank=0
                break;
        
        if (fullRank==0):
            print('The matrix is singular. Try pseudo-inverse!')
        else:
            inverse = np.array(R[:,n:])
            return inverse
示例#5
0
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 15 2015

Modified on Fri Oct 15 2015

@author: david

"""

import LA
import numpy as np

#A=np.array([[1,2,1],[4,5,2],[4,8,3]])
A=np.array([[1,2],[4,5],[7,8]])
#A=np.array([[0,2,3],[0,5,6],[0,8,9]])
#A=np.array([[1,2,3],[3,5,6]])
print(A)
U=LA.upperTri(A)
R=LA.rref(A)
print(U)
print(R)
print(LA.det(A))
print(LA.inv(A))
b=np.array([3,6,9])
print(LA.solve(A,b))
示例#6
0
文件: runLA.py 项目: ntudavid/LA_lab
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 15 2015

Modified on Fri Oct 15 2015

@author: david

"""

import LA
import numpy as np

# A=np.array([[1,2,1],[4,5,2],[4,8,3]])
A = np.array([[1, 2], [4, 5], [7, 8]])
# A=np.array([[0,2,3],[0,5,6],[0,8,9]])
# A=np.array([[1,2,3],[3,5,6]])
print(A)
U = LA.upperTri(A)
R = LA.rref(A)
print(U)
print(R)
print(LA.det(A))
print(LA.inv(A))
b = np.array([3, 6, 9])
print(LA.solve(A, b))