2

The problem is I have to multiply each element of N vectors to each other. As for example, If there are two vectors name X,Y. Each has three elements. Such as X={X1,x2,x3} and Y={y1,y2,y3} . So the multiplication is 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 easily do it using two 'for' loops. But the problem is the number of vector is variable. It can be X,Y,Z or X,Y or W,X,Y,Z. So how can I multiply them? Is there any mathematical name of this operation.

One of my idea is consider the vectors as one matrix.

3
  • 1
    Thanks @Uchiha for editing it and doing proper formatting. Commented Jul 8, 2015 at 11:54
  • what kind of result do you have in mind for 3 vectors? Can you give an example (with only 2 numbers per vector to keep it short)? Also, can you guarantee that all vectors have the same dimensions (numbers in them)? Commented Jul 8, 2015 at 12:00
  • vectors wil contain integer or decimal.Nope i cannot guarantee that all vectors have the same dimensions. :( Commented Jul 8, 2015 at 12:05

3 Answers 3

1

Here is the solution for your problem. Syntax may be different or depends on programming language you are using. It will initially store the first element and then perform multiplication of upcoming arrays. So each and every element of arrays are being multiplied one by 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 to $main $main = [ $X, $Y, $Z ]; $result = array(); foreach($main as $m) { $result = multiplyVector($result,$m); } echo "<pre>";print_r($result);die; 

And the result is 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 ) 
Sign up to request clarification or add additional context in comments.

10 Comments

thanks for the syntax. But my problem is the number of arrays are not fixed. It can be two or more. If there are more array i add more loops. So the code changes. I want to have a fixed function which can compute the multiplication
Do you mean something like this? $A = [ 1, 2, 3 ]; $B = [ 7, 8, 9, 10 ]; $C = [ 1, 2, 8 ]; ........ $Z = [ 1, 5, 8 ];
if $Z comes in picture then will it be like this ? M1={X1*Y1*Z1},
then it will be like following code M1={X1*Y1*Z1}, M2={X1*Y1*Z2}, M3={X1*Y1*Z3}, M4={X1*Y2*Z1}, M5={X1*Y2*Z2}, M6={X1*Y2*Z3}, M7={X1*Y3*Z1}, M8={X1*Y3*Z2}, M8={X1*Y3*Z3}, M10={X2*Y1*Z1}, M11={X2*Y1*Z2}, M12={X2*Y1*Z3},
Ok got it. Let me do that and will come in few moments.
|
1

You can use recursion to iterate over all possible combinations of elements of vectors, that's a standard approach when you need to generate some combinatoric objects. Something like this (don't remember php syntax well, so it's just php-like pseudocode):

// $X is the array of vectors, that is the first vector is // $X[0][0], $X[0][1], $X[0][2], ... // the second is $X[1][0], $X[1][1], $X[1][2], ... // and so on function rec($X, $i, $prod, $res) // $i is the number of vector from where we currently choose // $prod is current product // $res is the list of all found products if ($i>length($X)) append($res, $prod) return for ($j=0; $j<length($X[$i]); $j++) // choose element $j from vector $i rec($X, $i+1, $prod*$X[$i][$j], $res) ... $res=array(); rec($X, 0, 1, $res); 

1 Comment

Thanks . I am trying to understand .
1

If you are implementing in php then you can use foreach loop instead of for loop shown as below: If you have two vectors with unknown number of values then use this.

$x = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $y = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); foreach ($x as $key => $value) { foreach ($y as $k => $v) { echo $value * $v . " "; } } 

Note: $x can hold any number of values and so as $y.

If you dont know how many vectors or arrays you need to multiply try like below:

function multiply_arrays() { $args = func_get_args(); $number_of_args = count($args); for ($i = 0; $i < ($number_of_args - 1); $i++) { if (is_array($multiplied_array)) { $multiplied_array = multiply_two_arrays($multiplied_array, $args[$i]); } else { $multiplied_array = multiply_two_arrays($args[$i], $args[$i + 1]); } } return $multiplied_array; } function multiply_two_arrays($x, $y) { $multi_array = array(); foreach ($x as $key => $value) { foreach ($y as $k => $v) { $multi_array[] = $value * $v; } } return $multi_array; } $x = array(1, 2, 3); $y = array(1, 2, 3); $z = array(1, 2, 3); $a = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $b = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); $c = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); echo "<pre>"; print_r(multiply_arrays($x, $a, $b, $c)); echo "</pre>"; 

Thanks

5 Comments

This will work, but the number of arrays is predetermined. You'd want something that takes in a random number of params. A combination of func_num_args and some recursivness should do the trick.
can you please explain how can i use recursivness ?
if you have two vectors or arrays in php terms then foreach will loop through all the values in that vector or array. we dont need to worry about the number of elements or values in that vector
@Andrew I think my new edit of the answer will solve single dimensions array multiplications problem, if you are unaware of number of vectors or arrays you want to multiply
@ViswanathPolaki Good answer. +1 as far as I'm concerned.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.