Author Topic: Determinant of a (Square Integer) Matrix  (Read 4215 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Determinant of a (Square Integer) Matrix
« on: March 11, 2022, 09:52:48 pm »
By recursive calls:
Code: QB64: [Select]
  1. Option _Explicit ' determinantOfMatrix.bas b+ 2022-03-11 translate from
  2. ''''// C++ program to find Determinant of a matrix
  3. ''''#include <iostream>
  4. ''''using namespace std;
  5. ' Hope the above isn't important because WTH?
  6.  
  7. ''''// Dimension of input square matrix
  8. ''''#define N 4
  9. ' Again not sure??  oh this is for testing 4 x 4, the 3 x 3 is commented out
  10.  
  11.  
  12.  
  13.  
  14. ''''// Driver program to test above functions
  15. ' that are defined below :)
  16.  
  17. ''''int main()
  18. ''''{
  19. ''''    /* int mat[N][N] = {{6, 1, 1},
  20. ''''                     {4, -2, 5},
  21. ''''                     {2, 8, 7}}; */
  22.  
  23. ''''    int mat[N][N] = { { 1, 0, 2, -1 },
  24. ''''                      { 3, 0, 0, 5 },
  25. ''''                      { 2, 1, 4, -3 },
  26. ''''                      { 1, 0, 5, 0 } };
  27. ' well crap we need a way to read in a Matrix from a string
  28.  
  29. Dim m$
  30. m$ = "1,2;3,4"
  31. ReDim As Integer mat(0, 0)
  32. readStr2Mat m$, mat()
  33. displayMat mat(), 2, 2
  34. Print " D ="; determinantOfMatrix%(mat(), 2)
  35. m$ = "2, -3,1;2,0,-1 ;1, 4, 5"
  36. ReDim As Integer mat(0, 0)
  37. readStr2Mat m$, mat()
  38. displayMat mat(), 3, 3
  39. Print " D ="; determinantOfMatrix%(mat(), 3)
  40. m$ = "1, 0, 2, -1;3, 0, 0, 5;2, 1, 4, -3;1, 0, 5, 0 "
  41. ReDim As Integer mat(0, 0)
  42. readStr2Mat m$, mat()
  43. displayMat mat(), 4, 4
  44. Print " D ="; determinantOfMatrix%(mat(), 4)
  45.  
  46. ''''    // Function call
  47. ''''    cout <<"Determinant of the matrix is : " << determinantOfMatrix(mat, N);
  48. ''''    return 0;
  49. ''''}
  50.  
  51. ''''// this code is contributed by shivanisinghss2110
  52. ''''Output
  53.  
  54.  
  55. ' Move from near start to end
  56. ''''// Function to get cofactor of mat[p][q] in temp[][]. n is
  57. ''''// current dimension of mat[][]
  58. ''''void getCofactor(int mat[N][N], int temp[N][N], int p,
  59. ''''                 int q, int n)
  60.  
  61. Sub getCofactor (mat() As Integer, temp() As Integer, p As Integer, q As Integer, n As Integer)
  62.     Dim row, col, i, j
  63.     'Print "getCoFactor p, q, n: "; p, q, n
  64.  
  65.     ''''{
  66.     ''''    int i = 0, j = 0;
  67.  
  68.     ''''    // Looping for each element of the matrix
  69.     ''''    for (int row = 0; row < n; row++)
  70.     ''''    {
  71.     ''''        for (int col = 0; col < n; col++)
  72.     ''''        {
  73.     For row = 0 To n - 1
  74.         For col = 0 To n - 1
  75.  
  76.             ''''            //  Copying into temporary matrix only those
  77.             ''''            //  element which are not in given row and
  78.             ''''            //  column
  79.             ''''            if (row != p && col != q)
  80.             If (row <> p) And (col <> q) Then
  81.                 ''''            {
  82.                 ''''                temp[i][j++] = mat[row][col];
  83.  
  84.                 ' is j increased before or after temp(i, j) is assigned???
  85.                 temp(i, j) = mat(row, col)
  86.                 j = j + 1 ' I think j should be increased after temp copy  so temp(0,0) is filled
  87.                 If j = n - 1 Then
  88.                     j = 0: i = i + 1
  89.                 End If
  90.  
  91.                 ''''                // Row is filled, so increase row index and
  92.                 ''''                // reset col index
  93.                 ''''                if (j == n - 1)
  94.                 ''''                {
  95.                 ''''                    j = 0;
  96.                 ''''                    i++;
  97.                 ''''                }
  98.                 ''''            }
  99.                 ''''        }
  100.                 ''''    }
  101.                 ''''}
  102.             End If
  103.         Next
  104.     Next
  105.  
  106.  
  107. ' also moved
  108. ''''/* Recursive function for finding determinant of matrix.
  109. ''''   n is current dimension of mat[][]. */
  110. ''''int determinantOfMatrix(int mat[N][N], int n)
  111. Function determinantOfMatrix% (mat() As Integer, n As Integer)
  112.     ''''{
  113.     ''''    int D = 0; // Initialize result
  114.     Dim As Integer D, sign, f
  115.  
  116.     ''''    //  Base case : if matrix contains single element
  117.     ''''    if (n == 1)
  118.     ''''        return mat[0][0];
  119.  
  120.     If n = 1 Then
  121.         determinantOfMatrix% = mat(0, 0)
  122.     Else
  123.         ''''    int temp[N][N]; // To store cofactors
  124.         Dim temp(n, n) As Integer
  125.         ''''    int sign = 1; // To store sign multiplier
  126.         sign = 1
  127.  
  128.         ''''    // Iterate for each element of first row
  129.         ''''    for (int f = 0; f < n; f++)
  130.         For f = 0 To n - 1
  131.  
  132.             ''''    {
  133.             ''''        // Getting Cofactor of mat[0][f]
  134.             ''''        getCofactor(mat, temp, 0, f, n);
  135.             getCofactor mat(), temp(), 0, f, n
  136.             ''''        D += sign * mat[0][f]
  137.             ''''             * determinantOfMatrix(temp, n - 1);
  138.             D = D + sign * mat(0, f) * determinantOfMatrix%(temp(), n - 1)
  139.             ''''        // terms are to be added with alternate sign
  140.             ''''        sign = -sign;
  141.             sign = -sign
  142.             ''''    }
  143.         Next
  144.         ''''    return D;
  145.         determinantOfMatrix% = D
  146.     End If
  147.     ''''}
  148.  
  149. 'also moved
  150. ''''/* function for displaying the matrix */
  151. ''''void display(int mat[N][N], int row, int col)
  152. Sub displayMat (mat() As Integer, row As Integer, col As Integer)
  153.     Dim As Integer i, j
  154.     ''''{
  155.     ''''    for (int i = 0; i < row; i++)
  156.     ''''    {
  157.     ''''        for (int j = 0; j < col; j++)
  158.     ''''            cout <<"  " <<  mat[i][j];
  159.     ''''        cout <<"n";
  160.     ' the hell
  161.     For i = 0 To row - 1
  162.         For j = 0 To col - 1
  163.             Print Right$("   " + _Trim$(Str$(mat(i, j))), 3); ' hey no more than 2 digit numbers OK?
  164.             ''''    }
  165.             ''''}
  166.         Next
  167.         Print
  168.     Next
  169.  
  170. Sub readStr2Mat (s$, mat() As Integer) ' commas separate columns, semi-colons separate rows
  171.     Dim As Integer i, j
  172.     ReDim rows$(0)
  173.     Split s$, ";", rows$()
  174.     ReDim As Integer mat(UBound(rows$), UBound(rows$))
  175.     For i = 0 To UBound(rows$)
  176.         ReDim nums$(0)
  177.         Split rows$(i), ",", nums$()
  178.         For j = 0 To UBound(nums$)
  179.             mat(i, j) = Val(nums$(j))
  180.         Next
  181.     Next
  182.  
  183. Sub Split (SplitMeString As String, delim As String, loadMeArray() As String)
  184.     Dim curpos As Long, arrpos As Long, LD As Long, dpos As Long 'fix use the Lbound the array already has
  185.     curpos = 1: arrpos = LBound(loadMeArray): LD = Len(delim)
  186.     dpos = InStr(curpos, SplitMeString, delim)
  187.     Do Until dpos = 0
  188.         loadMeArray(arrpos) = Mid$(SplitMeString, curpos, dpos - curpos)
  189.         arrpos = arrpos + 1
  190.         If arrpos > UBound(loadMeArray) Then ReDim _Preserve loadMeArray(LBound(loadMeArray) To UBound(loadMeArray) + 1000) As String
  191.         curpos = dpos + LD
  192.         dpos = InStr(curpos, SplitMeString, delim)
  193.     Loop
  194.     loadMeArray(arrpos) = Mid$(SplitMeString, curpos)
  195.     ReDim _Preserve loadMeArray(LBound(loadMeArray) To arrpos) As String 'get the ubound correct
  196.  
  197.  

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Determinant of a (Square Integer) Matrix
« Reply #1 on: March 12, 2022, 10:03:03 am »
hi bplus
here's a challenge for you, find the inverse of an integer matrix which inverse is an integer matrix

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Determinant of a (Square Integer) Matrix
« Reply #2 on: March 12, 2022, 05:43:26 pm »
Adj() / D

All Integer, hmm... for what purpose?

Offline jack

  • Seasoned Forum Regular
  • Posts: 408
    • View Profile
Re: Determinant of a (Square Integer) Matrix
« Reply #3 on: March 12, 2022, 06:03:24 pm »
to show example of an inverse, it looks so much better when the inverse is all integers and it's an interesting task
here's a very simple program that will generate a couple matrix whose inverse is also integer
Code: QB64: [Select]
  1. Dim As _Integer64 n, i, j
  2. n = 4
  3. For j = 1 To n
  4.     For i = 1 To n
  5.         Print Using "#########"; binomial(n + j - 1, i - 1);
  6.     Next
  7.     Print
  8. For i = 1 To n
  9.     For j = 1 To n
  10.         Print Using "#########"; binomial(n + j - 1, i - 1);
  11.     Next
  12.     Print
  13.  
  14.     Dim As _Integer64 f, i
  15.     f = 1&&
  16.     For i = 1&& To n
  17.         f = f * i
  18.     Next
  19.     fac = f
  20.  
  21. Function binomial&& (n As _Integer64, k As _Integer64)
  22.     binomial = fac(n) / (fac(k) * fac(n - k))
  23.  
you can make variations of the matrix by adding or subtracting rows or columns you can also raise the matrix to some integer power and it's inverse will still be integer
« Last Edit: March 12, 2022, 06:13:00 pm by jack »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Determinant of a (Square Integer) Matrix
« Reply #4 on: March 13, 2022, 01:18:24 pm »
Thankyou @BSpinoza I think I've looked over that guys stuff or similar before, old, old style QB with line numbers and worse foreign language so don't know what he's talking about. Looks like it is wealth of tech code but I think I'd rather translate C++ code.

This translation stuff is not worth effort unless I know for what this may be used and don't say for mass transporter, I already did that joke ;-))