# Matrix calculus

**URL:** <https://forum.xojo.com/t/matrix-calculus/17117>\
**Category:** General\
**Created:** [June 8, 2014, 10:20am UTC](https://forum.xojo.com/t/matrix-calculus/17117 "2014-06-08T10:20:01Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Djamel\_AIT\_AMRANE](https://forum.xojo.com/user_avatar/forum.xojo.com/djamel_ait_amrane/32/20260_2.png) [@Djamel\_AIT\_AMRANE](https://forum.xojo.com/u/Djamel_AIT_AMRANE)\
**Post date:** [June 8, 2014, 10:20am UTC](https://forum.xojo.com/t/matrix-calculus/17117/1 "2014-06-08T10:20:01Z")

</div>

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]

---

<div class="post-metadata">

**Author:** ![Torsten\_Gaidies](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Torsten\_Gaidies](https://forum.xojo.com/u/Torsten_Gaidies)\
**Post date:** [June 8, 2014, 2:54pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/2 "2014-06-08T14:54:59Z")

</div>

What does not work? Please give more details.

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

---

<div class="post-metadata">

**Author:** ![Djamel\_AIT\_AMRANE](https://forum.xojo.com/user_avatar/forum.xojo.com/djamel_ait_amrane/32/20260_2.png) [@Djamel\_AIT\_AMRANE](https://forum.xojo.com/u/Djamel_AIT_AMRANE)\
**Post date:** [June 8, 2014, 3:33pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/3 "2014-06-08T15:33:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Djamel\_AIT\_AMRANE](https://forum.xojo.com/user_avatar/forum.xojo.com/djamel_ait_amrane/32/20260_2.png) [@Djamel\_AIT\_AMRANE](https://forum.xojo.com/u/Djamel_AIT_AMRANE)\
**Post date:** [June 8, 2014, 3:35pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/4 "2014-06-08T15:35:58Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![Torsten\_Gaidies](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Torsten\_Gaidies](https://forum.xojo.com/u/Torsten_Gaidies)\
**Post date:** [June 8, 2014, 3:52pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/5 "2014-06-08T15:52:07Z")

</div>

Hi,

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

> [@](#):
>
> 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
> return 0.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  
> End Function

---

<div class="post-metadata">

**Author:** ![Djamel\_AIT\_AMRANE](https://forum.xojo.com/user_avatar/forum.xojo.com/djamel_ait_amrane/32/20260_2.png) [@Djamel\_AIT\_AMRANE](https://forum.xojo.com/u/Djamel_AIT_AMRANE)\
**Post date:** [June 8, 2014, 4:03pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/6 "2014-06-08T16:03:11Z")

</div>

[quote]Hi,

if Det = 12 is the solution, this could be the code.[/quote]  
This screen shot describe what happen in Xojo code:  
[![](http://s26.postimg.org/fx66c705h/Xojo_Matrix.jpg)](http://postimg.org/image/fx66c705h/)

---

<div class="post-metadata">

**Author:** ![Djamel\_AIT\_AMRANE](https://forum.xojo.com/user_avatar/forum.xojo.com/djamel_ait_amrane/32/20260_2.png) [@Djamel\_AIT\_AMRANE](https://forum.xojo.com/u/Djamel_AIT_AMRANE)\
**Post date:** [June 8, 2014, 4:04pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/7 "2014-06-08T16:04:03Z")

</div>

the Det value = 12 is correct.

---

<div class="post-metadata">

**Author:** ![Torsten\_Gaidies](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Torsten\_Gaidies](https://forum.xojo.com/u/Torsten_Gaidies)\
**Post date:** [June 8, 2014, 4:11pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/8 "2014-06-08T16:11:32Z")

</div>

I used this code to call the function described before

```auto
  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.

---

<div class="post-metadata">

**Author:** ![Djamel\_AIT\_AMRANE](https://forum.xojo.com/user_avatar/forum.xojo.com/djamel_ait_amrane/32/20260_2.png) [@Djamel\_AIT\_AMRANE](https://forum.xojo.com/u/Djamel_AIT_AMRANE)\
**Post date:** [June 8, 2014, 4:58pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/9 "2014-06-08T16:58:56Z")

</div>

> [@](#):
>
> 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]

---

<div class="post-metadata">

**Author:** ![Torsten\_Gaidies](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Torsten\_Gaidies](https://forum.xojo.com/u/Torsten_Gaidies)\
**Post date:** [June 8, 2014, 7:11pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/10 "2014-06-08T19:11:03Z")

</div>

Hello Djamel,

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

My function definition

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

Your function definition

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

Could you see the difference in the parameter list?

Next thing  
code changed by me

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

You still use this code

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

---

<div class="post-metadata">

**Author:** ![Djamel\_AIT\_AMRANE](https://forum.xojo.com/user_avatar/forum.xojo.com/djamel_ait_amrane/32/20260_2.png) [@Djamel\_AIT\_AMRANE](https://forum.xojo.com/u/Djamel_AIT_AMRANE)\
**Post date:** [June 8, 2014, 7:29pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/11 "2014-06-08T19:29:42Z")

</div>

Function Code Modified as you request:

```auto
  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

---

<div class="post-metadata">

**Author:** ![Torsten\_Gaidies](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/c67d28/32.png) [@Torsten\_Gaidies](https://forum.xojo.com/u/Torsten_Gaidies)\
**Post date:** [June 8, 2014, 7:42pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/12 "2014-06-08T19:42:51Z")

</div>

It should be

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

---

<div class="post-metadata">

**Author:** ![Djamel\_AIT\_AMRANE](https://forum.xojo.com/user_avatar/forum.xojo.com/djamel_ait_amrane/32/20260_2.png) [@Djamel\_AIT\_AMRANE](https://forum.xojo.com/u/Djamel_AIT_AMRANE)\
**Post date:** [June 8, 2014, 8:10pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/13 "2014-06-08T20:10:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 5:15pm UTC](https://forum.xojo.com/t/matrix-calculus/17117/14 "2020-10-29T17:15:40Z")

</div>


