Matrix calculus

Hello,
I have to convert a matrices Vb6 code to Xojo.
i have Create a matrix class, but code does not work.
in Vb6 the code is :

[code] 'Calling Procedure

Dim Det as double
Dim MatA(3, 3) As Double

MatA(1, 1) = 3
MatA(1, 2) = 1 
MatA(1, 3) = 0
MatA(2, 1) = 1
MatA(2, 2) = 2
MatA(2, 3) = 1
MatA(3, 1) = 0
MatA(3, 2) = 1
MatA(3, 3) = 3


Det = MDet(3,MatA)

Public Function MDet(n As Integer, MatA() As Double) As Double

Dim i As Integer, j As Integer, k As Integer, nIs As Integer, nJs As Integer
Dim f As Double, det As Double, q As Double, d As Double

f = 1
det = 1


For k = 1 To n - 1
    q = 0
    For i = k To n
        For j = k To n
            d = Abs(MatA(i, j))
            If (d > q) Then
                q = d
                nIs = i
                nJs = j
            End If
        Next j
    Next i

    
    If (q + 1 = 1) Then
        MDet = 0
        Exit Function
    End If

    If (nIs <> k) Then
        f = -f
        For j = k To n
            d = MatA(k, j)
            MatA(k, j) = MatA(nIs, j)
            MatA(nIs, j) = d
        Next j
    End If

   
    If (nJs <> k) Then
        f = -f
        For i = k To n
            d = MatA(i, nJs)
            MatA(i, nJs) = MatA(i, k)
            MatA(i, k) = d
        Next i
    End If

    
    det = det * MatA(k, k)
    
    
    For i = k + 1 To n
        d = MatA(i, k) / MatA(k, k)
        For j = k + 1 To n
            MatA(i, j) = MatA(i, j) - d * MatA(k, j)
        Next j
    Next i
Next k


det = f * det * MatA(n, n)


MDet = det

End Function[/code]

What does not work? Please give more details.

At some point you have to return a value from MDet with: return value.

I can’’ get a det value which is the Matrix determinant.

[quote]What does not work? Please give more details.

At some point you have to return a value from MDet with: return value.[/quote]
The Function MDet must return a number.

Hi,

if Det = 12 is the solution, this could be the code.

[quote]Hi,

if Det = 12 is the solution, this could be the code.[/quote]
This screen shot describe what happen in Xojo code:

the Det value = 12 is correct.

I used this code to call the function described before

  Dim Det as double
  Dim MatA(3, 3) As Double
  
  MatA(1, 1) = 3
  MatA(1, 2) = 1
  MatA(1, 3) = 0
  MatA(2, 1) = 1
  MatA(2, 2) = 2
  MatA(2, 3) = 1
  MatA(3, 1) = 0
  MatA(3, 2) = 1
  MatA(3, 3) = 3
  
  Det = MDet(3,MatA)
  
  System.DebugLog("Det = " + str(det))

Try it to get the result and then change it like you need it.

Unfortunately it does not work in my Xojo compiler.
i have proceed as is shown above, i show you the MDet function as i have wrote under Xojo:

[code]
MDet(n As Integer, MatA() As Double) as double

Dim i As Integer, j As Integer, k As Integer, nIs As Integer, nJs As Integer
Dim f As Double, det As Double, q As Double, d As Double

f = 1
det = 1

For k = 1 To n - 1
q = 0
For i = k To n
For j = k To n
d = Abs(MatA(i, j))
If (d > q) Then
q = d
nIs = i
nJs = j
End If
Next j
Next i

If (q + 1 = 1) Then
  MDet = 0
  Exit Function
End If

If (nIs <> k) Then
  f = -f
  For j = k To n
    d = MatA(k, j)
    MatA(k, j) = MatA(nIs, j)
    MatA(nIs, j) = d
  Next j
End If


If (nJs <> k) Then
  f = -f
  For i = k To n
    d = MatA(i, nJs)
    MatA(i, nJs) = MatA(i, k)
    MatA(i, k) = d
  Next i
End If


det = det * MatA(k, k)


For i = k + 1 To n
  d = MatA(i, k) / MatA(k, k)
  For j = k + 1 To n
    MatA(i, j) = MatA(i, j) - d * MatA(k, j)
  Next j
Next i

Next k

det = f * det * MatA(n, n)

return det [/code]

Hello Djamel,

please have a closer look to the code I posted. It is tested and works.

My function definition

Function Mdet(n as integer, MatA(,) as double) As double

Your function definition

MDet(n As Integer, MatA() As Double) as double

Could you see the difference in the parameter list?

Next thing
code changed by me

  If (q + 1 = 1) Then
    return 0.0
    'Exit Function
  End If

You still use this code

    If (q + 1 = 1) Then
      MDet = 0
      Exit Function
    End If

Function Code Modified as you request:

  Dim i As Integer, j As Integer, k As Integer, nIs As Integer, nJs As Integer
  Dim f As Double, det As Double, q As Double, d As Double
  
  f = 1
  det = 1
  
  
  For k = 1 To n - 1
    q = 0
    For i = k To n
      For j = k To n
        d = Abs(MatA(i, j))
        If (d > q) Then
          q = d
          nIs = i
          nJs = j
        End If
      Next j
    Next i
    
    
    If (q + 1 = 1) Then
      return 0.0
      Exit 
    End If
    
    If (nIs <> k) Then
      f = -f
      For j = k To n
        d = MatA(k, j)
        MatA(k, j) = MatA(nIs, j)
        MatA(nIs, j) = d
      Next j
    End If
    
    
    If (nJs <> k) Then
      f = -f
      For i = k To n
        d = MatA(i, nJs)
        MatA(i, nJs) = MatA(i, k)
        MatA(i, k) = d
      Next i
    End If
    
    
    det = det * MatA(k, k)
    
    
    For i = k + 1 To n
      d = MatA(i, k) / MatA(k, k)
      For j = k + 1 To n
        MatA(i, j) = MatA(i, j) - d * MatA(k, j)
      Next j
    Next i
  Next k
  
  
  det = f * det * MatA(n, n)
  
  return det

The function is defined as follow : MDet(n as integer, MatA() as double) as double
scope: Public

It should be

MDet(n as integer, MatA(,) as double) as double

Wawoo! very nice , it’s running.
thank you very much.