Game Maker Games, Articles, Tutorials & More

Game Maker Network


Howdy, Guest! Please sign in or register an account.

Print

matrix_determinant

By Daniel · January 1, 2009

Returns the determinant of some square matrix M using the recursive method. M should be a ds_grid filled with real entries.

/* matrix_determinant(M)
 *
 * Returns the determinant of some square matrix M. This should be a ds_grid
 * filled with real entries.
 */

var size; size = ds_grid_width(argument0);

// Special case
if (size == 0)
  return 1;
// Base case
if (size == 1)
  return ds_grid_get(argument0, 0, 0);


// General case
var sum, i, subM, xx, yy;

sum = 0;
for (i = 0; i < size; i += 1) {
  subM = ds_grid_create(size - 1, size - 1);
  for (xx = 0; xx < size; xx += 1) {
    if (xx == i)
      continue;
    for (yy = 1; yy < size; yy += 1)
      ds_grid_set(subM, xx - 1 * (xx > i), yy - 1, ds_grid_get(argument0, xx, yy));
  }
  sum += ds_grid_get(argument0, i, 0) * matrix_determinant(subM)
         * (1 - 2 * (i & 1));
}

return sum;

Categories: Mathematics

Comments

There are no comments to display.

Post a Comment

You must be signed in to post comments.

Advertisement