多维数组乘法 [英] Multidimensional array multiplication

查看:160
本文介绍了多维数组乘法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想在PHP code进行复制矩阵乘法,那里有我的矩阵如下:

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))

子阵列的数量(<强> 2 )相当于在矩阵的列数,而元件的每个子阵列的数目(的 2 )重新presents在矩阵的行数

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.

在code将需要:


  • 帐户不同维度的矩阵。

  • 中确认时两个矩阵不能相乘(其中,在矩阵A的列数是不相同的矩阵B中的行数)。

  • 可能占标量乘法,其中矩阵的每个元素乘以一个常数。

我重视幻灯片<一个href=\"http://www.slideboom.com/$p$psentations/779010/slides?pk=4108-d50c-27ae-e9cc-094f-9aa0-43a4-9e2d\"相对=nofollow称号=功能的示例幻灯片>这里解释了code应该达到什么样的(两个例子)。

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

推荐答案

下面是我的(啰嗦)解决方案。我要去尝试,看看我能在地方简化。需要注意的是:

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


  • 这个解决方案并不占标量乘法,但是这是比较容易整合,如果你想将其列入。假设一个标量是一个元素的数组 - 在这种情况下,在其他命令简单地包括计数()命令认识到,如果阵列中的一个(或多个)是一个标量和应用乘法功能使用相应的 array_map

  • 我假设数组遵循矩阵形式 - 例如一列
    不能有比另一种更元素。你能解释这
    正式通过确保每个子阵列具有相同数量的
    元素。

  • 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.

的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!";

}


?>

这篇关于多维数组乘法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆