Game Maker Games, Articles, Tutorials & More

Game Maker Network


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

Print

matrix_product

By Daniel · January 1, 2009

Returns the matrix product of M1 and M2. These should both be ds_grids filled with real entries. This script uses the simple O(n^3) technique.

/* matrix_product(M1, M2)
 *
 * Returns the matrix product of M1 and M2. These should both be ds_grids
 * filled with real entries.
 *
 * This script uses the simple O(n^3) technique.
 */

var w, h, n, prod, xx, yy, sum, i;

w = ds_grid_width(argument1);
h = ds_grid_height(argument0);
n = ds_grid_width(argument0);
prod = ds_grid_create(w, h);

for (xx = 0; xx < w; xx += 1) {
  for (yy = 0; yy < h; yy += 1) {
    sum = 0;
    for (i = 0; i < n; i += 1)
      sum += ds_grid_get(argument0, yy, i) * ds_grid_get(argument1, i, xx);
    ds_grid_set(prod, xx, yy, sum);
  }
}

return prod;

Categories: Mathematics

Comments

There are no comments to display.

Post a Comment

You must be signed in to post comments.

Advertisement