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
12-27-2019, 06:54 AM
Post: #2
RE: Function to calculate the nullspace
(12-27-2019 12:07 AM)hamorabi Wrote:  The Prime has built-in functions to calculate the column and row spaces. For some reason, there's no function for the nullspace.

Wikipedia says that "The kernel of a matrix, also called the null space, is the kernel of the linear map defined by the matrix." So maybe Prime's "ker" function (or its "jacobi_symbol" function) is related to the "nullspace" you refer to, since Prime's doc says that they are related to the "kernel" of a matrix (whatever that means).

<0|ΙΈ|0>
-Joe-
Visit this user's website Find all posts by this user
Quote this message in a reply
12-27-2019, 08:36 AM (This post was last modified: 12-27-2019 08:47 AM by Stevetuc.)
Post: #3
RE: Function to calculate the nullspace
(12-27-2019 06:54 AM)Joe Horn Wrote:  
(12-27-2019 12:07 AM)hamorabi Wrote:  The Prime has built-in functions to calculate the column and row spaces. For some reason, there's no function for the nullspace.

Wikipedia says that "The kernel of a matrix, also called the null space, is the kernel of the linear map defined by the matrix." So maybe Prime's "ker" function (or its "jacobi_symbol" function) is related to the "nullspace" you refer to, since Prime's doc says that they are related to the "kernel" of a matrix (whatever that means).

It seems that above nullspace(mat) code is equivalent to
-transpose(ker(mat))
Find all posts by this user
Quote this message in a reply
12-27-2019, 10:51 AM
Post: #4
RE: Function to calculate the nullspace
(12-27-2019 08:36 AM)Stevetuc Wrote:  
(12-27-2019 06:54 AM)Joe Horn Wrote:  Wikipedia says that "The kernel of a matrix, also called the null space, is the kernel of the linear map defined by the matrix." So maybe Prime's "ker" function (or its "jacobi_symbol" function) is related to the "nullspace" you refer to, since Prime's doc says that they are related to the "kernel" of a matrix (whatever that means).

It seems that above nullspace(mat) code is equivalent to
-transpose(ker(mat))

You're right. It seems that I just wasted my time. The prime has a function called colspace, rowspace, but no nullspace. However, nullspace is available on xcas, which suggested to me that nullspace was one of those functions that didn't make it to the Prime.

I'm thinking of writing a couple of functions that can do vector calculus functions in different coordinates. At the moment, you can do the curl on the Prime but only using rectangular coordinates, unless I'm mistaken. Is it possible to do the curl in spherical coordinates on the Prime? I don't want to waste time on something that is already available.
Find all posts by this user
Quote this message in a reply
Post Reply 




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