## matrices definistion

Moderators: egami, macek, gesf

swirlee
Moderator
Posts: 2257
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:
Interesting problem. First of all, by your example I'm not sure how you intend to collect the data. I'd suppose you'd have a grid of <input type="text" name="matrix[m][n]"> boxes, where m and n are the column and row, respectively. Since you're using multidimensional arrays (the PHP equivalent of a matrix), so your <input>s would have the following names:

Code: Select all

``matrix_a[0][0]  matrix_a[1][0]  matrix_a[2][0]  matrix_a[3][0]matrix_a[0][1]  matrix_a[1][1]  matrix_a[2][1]  matrix_a[3][1]matrix_a[0][2]  matrix_a[1][2]  matrix_a[2][2]  matrix_a[3][2]``

...and so on. Then you'd have a second, similar grid of <input>s for the n x k matrix.

With me so far? Since m, n, and k are unknown (I'm assuming), you'd just have PHP generate the <input> grid with a nested for() loop, something like this:

Code: Select all

``<form method="GET" action="script.php"><?php\$m_max = 3; // the number of columns in the m x n matrix\$n_max = 2; // the number of rows for m x n, cols for n x k\$k_max = 4; // you get the picture..// generate the m x n <input> gridfor(\$m = 0; \$m < \$m_max; \$m++) {   for(\$n = 0; \$n < \$n_max; \$n++) {      echo '<input type="text" name="matrix_a[' . \$m . '][' . \$n . ']">';   }   echo "<br/>\n";}echo "<br/>\n";// generate the n x k gridfor(\$n = 0; \$n < \$n_max; \$n++) {   for(\$k = 0; \$k < \$k_max; \$k++) {      echo '<input type="text" name="matrix_b[' . \$n . '][' . \$k . ']">';   }   echo "<br/>\n";}?><input type="submit"></form>``

There, now you have two nice grids of text input fields. The first (m x n) will dump into a multidimensional array called \$matrix_a, and the second (n x k) into \$matrix_b.

Now's where it gets interesting. You have to do some math. Either you can read up on matrix multiplication and write your own implementation, or (and this is what I recommend), you can just use someone else's. PEAR has a good Math component, or you could try the PHP Math Library Project, or, if all else fails, just Google it. Have fun!