Determinant of a nxn matrix

Post Reply
Henko
Posts: 814
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Determinant of a nxn matrix

Post by Henko »

Code: Select all

' testprogram for function "mat_det"
' library "matlib" is needed
'
option base 1
sz=3
dim m(sz,sz)
for i=1 to sz
  for j=1 to sz ! read m(i,j) ! next j
  next i
data 2,-1,3,1,1,0,-2,0,1
print mat_det(sz,m)
end

' produce the determinant of a nxn matrix
' the input matrix in(,) remains unchanged
' the value of the determinant is given back by the function
'
def mat_det(n,in(,))
dim a(n,n)
mat_copy(n,n,in,a)
for i=1 to n-1
  if i=1 then det=a(i,i) else det=det*a(i,i)
  for j=i+1 to n
    fac=a(j,i)/a(i,i)
    for k=i+1 to n ! a(j,k)=a(j,k)-fac*a(i,k) ! next k
    next j
  next i
mat_det=a(n,n)*det
end def

{/matlib}


Post Reply