Post Reply 
Function to calculate the nullspace
12-27-2019, 12:07 AM (This post was last modified: 12-27-2019 12:53 AM by hamorabi.)
Post: #1
Function to calculate the nullspace
The Prime has built-in functions to calculate the column and row spaces. For some reason, there's no function for the nullspace.

I'd like to share this piece of code (written in Python syntax) that calculates the nullspace of a matrix. The left nullspace can be calculate by taking the transpose of the matrix first.

Here's a 7x10 random matrix that you can try.

PHP Code:
[[13,31,27,33,77,47,77,2,95,42][95,60,98,44,84,58,64,89,37,63][9,33,18,13,68,72,34,27,3,95][7,61,12,88,55,24,62,7,37,72][53,2,20,67,33,3,82,73,40,32][71,90,46,45,14,64,95,35,67,7][69,67,9,40,93,34,64,88,25,81]] 


PHP Code:
#cas
def nullspace(mat):
    
rowDim(mat)
    
colDim(mat)
    
mat RREF(mat)
    
pivcols = [] 
    
freecols = []
    
res = []
    
jj:=0;
    
ii:=0;
    while((
jj<n) and (ii<m)):
        while (
mat[ii,jj]==and jj<n):
            
freecols.append(jj)
            
jj=jj+1
        
if (jj<n):    
            if ((
mat[ii,jj])!=0):
                
pivcols.append(jj)
            else:
                
freecols.append(jj)
        
jj=jj+1
        ii
=ii+1
    
#For fat matrices, add the columns to the right of the last pivot
    
if (jj<n):
        
freecols concat(freecols,range(jj,n))
    
nfree len(freecols)
    
#Return the 0 matrix
    
if (nfree==0):
        return 
TRN([for x in range(0,n)])
    for 
kk in range(0,nfree):
        
sol = [for x in range(0,n)]
        
sol[freecols[kk]] = 1
        
for ii in range(n-nfree-1,1,-1):
            
sol[pivcols[ii]] = -sum(mat[ii,rr]*sol[rr],rr,pivcols[ii],n-1)
        
res concat(res,TRN(sol));    
    return 
res
#end 
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Function to calculate the nullspace - hamorabi - 12-27-2019 12:07 AM



User(s) browsing this thread: 1 Guest(s)