Calculating Determinants in Visual Basic: Cramer's Rule Method

  • Thread starter Thread starter Shahil
  • Start date Start date
  • Tags Tags
    Determinants
Click For Summary
The discussion centers on creating a Visual Basic program to calculate 4x4 determinants using Cramer's Rule. The initial approach involves decomposing the matrix into smaller matrices, but participants note that this method is lengthy and inefficient for larger matrices. A recursive function for calculating determinants is suggested, emphasizing the need for proper implementation of the determinant formula, including the alternating sign factor. The conversation also explores the potential of Gaussian elimination as a more efficient alternative, with users sharing code snippets and seeking further clarification on implementing these methods. Overall, the thread highlights challenges in coding determinant calculations and the exploration of various mathematical approaches.
Shahil
Messages
115
Reaction score
21
:cry: :cry: :cry:

Nobody wants to help me in the Software foum so I'm hoping someone loves me enough here to gimme some help! :shy:


Basically, I need a Visual Basic program that can work out 4x4 determinants.

I need it for a project and practical usage as well.

Anyway - the method I deviced uses slowly decomposing the 4x4 into 3x3's then 2x2's and hence calculate an answer.

Problem 1: IT'S LONG (ie. 24 terms made up of 4 different matrix entires for each!)
Problem 2: If I need a 5x5...

So :shy:

help me please! I was thinking about using triangularisation but doing it is one thing, CODING :surprise: it is another!

Anyway, hope SOMEONE can help me!
 
Physics news on Phys.org
I remember writing a function just like this in C, I'm not familiar with VB but I can give some general tips. First of all, recognize that determinants can be calculated recursively, in fact they are. That is, for some m x m matrix:

A\ =\ [a_{ij}]

\det A\ =\ \sum a_{1j}A_{1j}\mbox{, for j = 1, 2, 3, ... m, and }A_{ij}
is the (m-1) x (m-1) matrix remaining after removing row "i" and column "j" from A.

So, you should have a function that looks something like this:

PHP:
int determinant (int dimension, int [] matrix) {

   // base case
   if (dimension == 2) return (matrix[0]*matrix[3] - matrix[1]*matrix[2]);
   int sum = 0;

   for (int i = 0; i < dimension; i++) {

      // for each "i", you will create a smaller matrix based on the original matrix
      // by removing the first row and the "i"th column.
      int smallMatrix [(dimension - 1)*(dimension - 1)];
      for (int j = 1; j < dimension; j++) {
         for (int k = 0; k < dimension; k++) {
            if (k < i) smallMatrix [(j-1)*(dimension-1) + k] = matrix [j*dimension + k];
            if (k > i) smallMatrix [(j-1)*(dimension-1) + k - 1] = matrix [j*dimension + k];
         }
      }

      // after creating the smaller matrix, multiply the "i"th element in the first
      // row by the determinant of the smaller matrix.
      sum += (matrix [i])*(determinant (dimension - 1, smallMatrix));
   }
   return sum;
}
 
Thanks for the help (though I don't understand the code!)

The formula helped though and I figure out something! It was similar in nature to your code (only in VB) and I got 95% for it! Woopee! Only, it gives the wrong determinants (I think it's reducing the rows a bit too much!)
 
Thanks for the help (though I don't understand the code!)

The formula helped though and I figure out something! It was similar in nature to your code (only in VB) and I got 95% for it! Woopee! Only, it gives the wrong determinants (I think it's reducing the rows a bit too much!)
 
shahil :)
 
I'm back here again!

Just a thought on the C code AKG: WOuld you suggest I use this method to find a determinant then solve a matrix using Cramer's Rule or is there a much easier way?

BTW I've moved up from VB to C code (not C++ yet coz I don't have the software and I'm running Linux so C is better!) and I want to solve huge matrices ie. up to 10x10.
 
Yeah, it gives the wrong determinants, because something is missing. It was:

\det A\ =\ \sum _{j = 1} ^m a_{1j}A_{1j}

and should have been:

\det A\ =\ \sum _{j = 1} ^m (-1)^{(j+1)} a_{1j}A_{1j}

It's easy to adjust your code to compensate for this.

What do you mean "solve a matrix"? You mean solve for X where AX = Y? I suppose I would do what you said, use Cramer's rule. If you're terribly concerned with efficiency, you may want to find a more efficient way, this algorithm, as far as I can tell, will take take time on the enormous order of O(n(n!)³) for nxn matrices. Once you type in the matrix and the Y vector, this should work nicely and give you the solution, so it's easy, but it will take time. Try it with simple matrices, 2x2 or 3x3 to start. See how high you can go before it starts taking too long, I would suspect it would be too slow pretty fast, although I don't know of a more efficient algorithm.

I would suggest doing some research of your own on algorithms to find determinants.
 
Thanks for the help! Though..I'm just a bit confused how to actually implement your correction!

Also, I thought about using my VB Gauss reduction method to solve this problem but...it seems that the GuI/non-GUI interface thingy is screwing me around and I'm seriously lost!

Can you help me a liccle bit more then :blushing:

BTW - sorry for taking so long to reply ---> having MAJORT posting problems (stupid varsity computers...)
 
It should be simple. Change
PHP:
sum += (matrix [i])*(determinant (dimension - 1, smallMatrix));
to
PHP:
if (i % 2 == 1) sum += (matrix [i])*(determinant (dimension - 1, smallMatrix));
else sum-= (matrix [i])*(determinant (dimension - 1, smallMatrix));
If you want to include the math library, I believe there's a function Math.pow(x, y) which gives you x^y, so you could just replace it with:
PHP:
sum += (Math.pow(-1, i + 1))*(matrix [i])*(determinant (dimension - 1, smallMatrix));
Now, I think Gaussian elimination would give you a more efficient solution, but the algorithm would be harder to implement as far as I can tell. Note that with the determinant way, if you get the determinant of the original matrix to be zero, you know right away that you don't have a unique solution (and may not have a solution at all).
 
Last edited:
  • #10
Kewl...ja, think the math thing should work!

I'll post the Gauss method I intended using maybe tomorrow (it's not on this computer that I'm using now!) And I shall try out your way!

Thanks a lot!
 
  • #11
Hey! This is what I got using row reduction. I THINK it works (well it worked so far at least!)Take a look. method.

PHP:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
 int   i, j,dummy,m, n,k;
  double sum, dum;


 printf("\n enter no. of unknowns \n");
 scanf("\n%d",&n);

 double A[n][n+1];
 double B[n];
 int Row[n];

 for (i = 1; i <= n; i++)
 {
  for (j = 1; j <= n+1; j++)
  {
  printf("\nA[%d][%d] = ",i, j);
  scanf("%lf", &A[i - 1][j - 1]);
  }
}


/*   pivots  */

  for (k = 1; k <= n; k++) Row[k-1] = k-1;


for (i = 1; i <= n - 1; i++)
    {
      for (j = i + 1; j <= n; j++)
      {
        if ( fabs(A[Row[j-1]][i-1]) > fabs(A[Row[i-1]][i-1]) )
         {
           dummy    = Row[i-1];
           Row[i-1] = Row[j-1];
           Row[j-1] = dummy;
         }
      }
      for (j = i + 1; j <= n ; j++)
       {
         dum = A[Row[j-1]][i-1]/A[Row[i-1]][i-1];
         for (m = i + 1; m <= n + 1; m++)
         {
           A[Row[j-1]][m-1] -= dum*A[Row[i-1]][m-1];
         }
       }
    }


  /* back subst*/

  B[n-1] = A[Row[n-1]][n] / A[Row[n-1]][n-1];
  for (j = n - 1; j >= 1; j--)
   {
    sum = 0;
    for (m = j + 1; m <= n; m++)
    {
     sum += A[Row[j-1]][m-1] * B[m-1];
    }
    B[j-1] = (A[Row[j-1]][n] - sum) / A[Row[j-1]][j-1];
   }




 /* output */

 for (i = 1; i <= n; i++)
 {
   for (j = 1; j <= n + 1 ; j++)
    {
      printf("%12.4f", A[i-1][j-1]);
    }
   printf("\n");
 }

 for (j = 1; j <= n; j++)
  {
   printf("Unknown#%d = %6.2f\n", j, B[j-1]);
  }

}

It is a bit long and involved so I would like to know the Cramer's Rule method!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
9K
  • · Replies 0 ·
Replies
0
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
Replies
7
Views
3K
Replies
2
Views
2K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
1K