Python Simple code example of transforming matrix into upper triangular form

  • Thread starter Thread starter psie
  • Start date Start date
  • Tags Tags
    Linear algebra
AI Thread Summary
The discussion revolves around a beginner's implementation of an algorithm to transform matrices into upper triangular form using elementary row operations. The user seeks feedback on their code, expressing concerns about potential redundancy and areas for improvement. Key points include the algorithm's approach, which involves checking for nonzero entries in the first column, swapping rows, and performing necessary subtractions. The user has verified the algorithm's correctness with sample matrices. Feedback provided emphasizes adherence to coding standards such as PEP8, recommending the use of descriptive variable and function names, and avoiding redundant conditions. Additionally, a suggestion is made to follow a structured study of numerical methods, with a link to relevant lecture notes for further learning. The user acknowledges the feedback and expresses a desire to deepen their understanding through practical application.
psie
Messages
315
Reaction score
40
TL;DR Summary
I've been working on an elementary linear algebra exercise, namely that every matrix can be transformed into an upper triangular matrix by using elementary row operations of type 1 and 3 only, that is, by swapping rows and adding a multiple of a row to another. Now, I've tried to implement this in python, and after some struggling, I think I've succeeded, though I don't really know how to be sure 100%.
First, I'm a beginner at this, so sorry if my code looks stupid. I'd be grateful for any feedback concerning this. I wonder if anything's redundant or can be improved?

The idea of the "algorithm" is to start with the first column of the matrix, in particular the first entry below the diagonal entry. Then check the entries in this column to see if there's a nonzero entry. If there is, we swap it with the row containing the diagonal entry (by using type 1 operation) and then use type 3 operation to subtract this nonzero entry (multiplied by the appropriate constant) from all the other nonzero entries in this column. Then we proceed to the next column and repeat this process.

I've checked by hand that the matrices p and q below give the correct outputs (also trying other, bigger matrices).

Python:
import numpy as np

p=np.array([[1,0,1],
            [1,1,1],
            [0,0,1]])

q=np.array([[1,2],
            [2,3],
            [2,2]])

def f(k):

  if not isinstance(k,np.ndarray):
    return print("input is not a numpy array")

  elif len(k.shape)!=2:
    return print("numpy array is not correct shape")

  else:
    for j in range(0,k.shape[1]):

      #if matrix is square, we don't need j=k.shape[1]-1

      if k.shape[0]==k.shape[1] and j==k.shape[1]-1: 
        break

      else:
        for i in range(j+1,k.shape[0]):

          if k[i,j]!=0:

            #swap rows

            k[[j,i]]=k[[i,j]] 

            #this for-loop subtracts the nonzero entry from the rest in a given column

            for s in range(i,k.shape[0]): 
              firstentry=k[s,j]
              c=-firstentry/k[j,j]
              current=k[[s]]
              k[[s]]=current+k[[j]]*c
          
          #maybe this is redudant, but if k[i,j]=0 we should continue unless i=k.shape[0]-1

          elif k[i,j]==0 and i==k.shape[0]-1:  
            break
          else:
            continue
    return k
    
print(f(p),'\n')
print(f(q))
 
Technology news on Phys.org
psie said:
First, I'm a beginner at this, so sorry if my code looks stupid. I'd be grateful for any feedback concerning this.

Use spaces around binary operators and after ,s to help readability (see the PEP8 coding standard and use an IDE tool that enforces it).

[code lang=Python title=Bad]
for s in range(i,k.shape[0]):
firstentry=k[s,j]
c=-firstentry/k[j,j]
current=k[]
k[]=current+k[[j]]*c
[/code]

[code lang=Python title=Better]
for s in range(i, k.shape[0]):
firstentry = k[s, j]
c = -firstentry / k[j, j]
current = k[]
k[] = current + k[[j]] * c
[/code]

  • Use descriptive names for functions.
  • Use descriptive names for variables: it is conventional to use M for a matrix, and it is much easier to avoid mistake if you create a variable e.g. nRows than to refer to M.shape[0].
  • Do not use redundant else conditions
  • Use spaces around binary operators and after ,s to help readability (see the PEP8 coding standard and use an IDE tool that enforces it).

[code lang=Python title=Bad]
def f(k):

if not isinstance(k,np.ndarray):
return print("input is not a numpy array")

elif len(k.shape)!=2:
return print("numpy array is not correct shape")

else:
for j in range(0,k.shape[1]):
[/code]

[code lang=Python title=Better]
def diagonalize(M):

if not isinstance(M, np.ndarray):
return print("input must be a numpy array")

if len(M.shape) != 2:
return print("input must have 2 dimensions")

[nRows, nCols] = M.shape

for col in range(0, nCols):
[/code]
 
  • Like
Likes psie and Baluncore
psie said:
I've been working on an elementary linear algebra exercise, namely that every matrix can be transformed into an upper triangular matrix by using elementary row operations of type 1 and 3 only, that is, by swapping rows and adding a multiple of a row to another.
As far as the algorithm itself goes, working this up from first principles is a worthwhile exercice but only in the context of a structured study of numerical methods. Are you following such a course of study?

If not I suggest these lecture notes: https://people.sc.fsu.edu/~jburkardt/classes/nla_2015/numerical_linear_algebra.pdf
 
pbuk said:
As far as the algorithm itself goes, working this up from first principles is a worthwhile exercice but only in the context of a structured study of numerical methods. Are you following such a course of study?

If not I suggest these lecture notes: https://people.sc.fsu.edu/~jburkardt/classes/nla_2015/numerical_linear_algebra.pdf
Thanks for the feedback and help! :smile: Also the recommendation. I am currently just reading my linear algebra book (a mathematics book) where this was just an exercise in describing the procedure of how to go about it. I immediately felt for trying to do something more with it.
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...
Back
Top