-1

Suppose I want a code in PHP that replicates matrix multiplication, where my matrices look like:

$matrix_1 = array(array(1,2), array(3,4))

The number of subarrays (2) is equivalent to the number of columns in the matrix, whereas the number of elements in each subarray (2) represents the number of rows in the matrix.

The code would need to:

  • Account for matrices of different dimensions.
  • Recognise when two matrices cannot be multipled (where the number of columns in matrix A is not the same as the number of rows in matrix B).
  • Possibly account for scalar multiplication, where each element of a matrix is multiplied by a constant.

I have attached slides here that explain what the code should achieve (with two examples).

user3749854
  • 75
  • 1
  • 8
  • Is this your homework? – Voitcus Jun 09 '13 at 21:55
  • It may or may not be my homework! I've tried googling but most solutions are a bit messy? Suppose I should give it a go myself though! – user3749854 Jun 09 '13 at 22:02
  • 1
    And what have you done so far? – Jessica Jun 09 '13 at 22:03
  • Very inelegant stuff so far - simply splicing the arrays and forming new arrays out of them. Also I don't have a general solution yet for all matrix dimensions - and am working mostly with square matrices at the moment. – user3749854 Jun 09 '13 at 22:09
  • @user2468585, use link above – sectus Jun 10 '13 at 00:53
  • *"Suppose I should give it a go myself though!"* <- yes you should. that is expected before asking because you should as a programming question, that means you've got a problem programming something. Also the description "messy" is pretty subjective. What is messy for somebody might not be messy for somebody else, so we just can not understand *your problem*. And please don't link some flash sites, you can create images and add them to your question. – hakre Jun 10 '13 at 06:47
  • possible duplicate of [Multidimensional array addition](http://stackoverflow.com/questions/17014239/multidimensional-array-addition) - I can't hinder myself of getting the impression you are really lazy and not programming at all. – hakre Jun 10 '13 at 07:18
  • Maybe I am busy so I am checking online to see if anyone else knows the answer first? Don't be presumptuous. But I'll post what I have for this as an answer later today. – user3749854 Jun 10 '13 at 10:49

1 Answers1

0

Here's my (long-winded) solution. I'm going to try to see if I can simplify this in places. Note that:

  • This solution does not account for scalar multiplication, but this is relatively easy to incorporate if you wanted to include it. Assume a scalar is a one-element array - in which case, in the else command simply include a count() command to recognise if one (or more) of arrays is a scalar and apply a multiplication function accordingly using array_map.
  • I am assuming that the arrays follow matrix form - e.g. one column cannot have more elements than another. You can account for this formally by making sure each subarray has the same number of elements.

The code:

<?php

// FUNCTIONS

function mul($x, $y){
    return ($x * $y);
}

// Example Arrays

$array_1 = array(array(1,4,7), array(2,5,8), array(3,6,9));
$array_2 = array(array(7,6,4), array(5,8,1), array(4,3,2));

// Check for row/column equivalence

$array_1_cols = count($array_1);
$array_1_rows = count($array_1[0]);
$array_2_cols = count($array_2);
$array_2_rows = count($array_2[0]);

// Check to see if matrix multiplication is possible

if($array_1_cols == $array_2_rows) {

$m_cols = $array_2_cols;
$m_rows = $array_1_rows;

$array_3 = array();
$col_index = 1;

// Start loop for each column of the new matrix

while($col_index <= $m_cols) {
$m_col_index = $col_index - 1;
$sub_array[$col_index] = array();

// Start loop for each row of the new matrix

$row_index = 1;
while($row_index <= $m_rows) {
$m_row_index = $row_index - 1;

// Auxiliary array for each row of A
$a_row[$row_index] = array();

$a_index = 1;
while($a_index <= $array_1_cols) {
$start_p = $a_index - 1;
$el_part_[$a_index] = $array_1[$start_p];
$el_part_[$a_index] = $el_part_[$a_index][$m_row_index];
array_push($a_row[$row_index], $el_part_[$a_index]);
++$a_index;
}

// Array for columns of B

$b_col[$col_index] = $array_2[$m_col_index];

// Build matrix C - defined over the rows of A and the columns of B

$c_part[$row_index][$col_index] = array_map('mul', $a_row[$row_index], $b_col[$col_index]);

$c_el[$row_index][$col_index] = array_sum($c_part[$row_index][$col_index]);

array_push($sub_array[$col_index], $c_el[$row_index][$col_index]);

// End row loop

++$row_index;
}

array_push($array_3,$sub_array[$col_index]);

++$col_index;
}

print_r($array_3);

} else {

echo "This is not possible!";

}


?>
user3749854
  • 75
  • 1
  • 8