php - How to multiply each element of N vectors to each other N^N times -
the problem have multiply each element of n vectors each other. example, if there 2 vectors name x,y. each has 3 elements. such x={x1,x2,x3} , y={y1,y2,y3} . multiplication following
m1={x1*y1}, m2={x1*y2}, m3={x1*y3}, m4={x2*y1}, m5={x2*y2}, m6={x2*y3}, m7={x3*y1}, m8={x3*y2}, m9={x4*y3} i can using 2 'for' loops. problem number of vector variable. can x,y,z or x,y or w,x,y,z. how can multiply them? there mathematical name of operation.
one of idea consider vectors 1 matrix.
here solution problem. syntax may different or depends on programming language using. store first element , perform multiplication of upcoming arrays. each , every element of arrays being multiplied 1 one.
<?php function multiplyvector($a=array(),$b) { $count_a = count($a); $count_b = count($b); if($count_a) { for($i=0;$i<$count_a;$i++) { for($j=0;$j<$count_b;$j++) { $result[] = $a[$i] * $b[$j]; } } } else { $result = $b; } return $result; } $x = [ 1, 2, 3 ]; $y = [ 7, 8, 9, 10 ]; $z = [ 10, 20, 50]; // add multiple array $main $main = [ $x, $y, $z ]; $result = array(); foreach($main $m) { $result = multiplyvector($result,$m); } echo "<pre>";print_r($result);die; and result here.
array ( [0] => 70 [1] => 140 [2] => 350 [3] => 80 [4] => 160 [5] => 400 [6] => 90 [7] => 180 [8] => 450 [9] => 100 [10] => 200 [11] => 500 [12] => 140 [13] => 280 [14] => 700 [15] => 160 [16] => 320 [17] => 800 [18] => 180 [19] => 360 [20] => 900 [21] => 200 [22] => 400 [23] => 1000 [24] => 210 [25] => 420 [26] => 1050 [27] => 240 [28] => 480 [29] => 1200 [30] => 270 [31] => 540 [32] => 1350 [33] => 300 [34] => 600 [35] => 1500 )
Comments
Post a Comment