Home > front end >  I want a mathematical formula to find the product of both array items but without going one by one s
I want a mathematical formula to find the product of both array items but without going one by one s

Time:09-30

I have 2 array with same length

a=[1,2,3,4]
b=[5,6,7,8]

I want to find product between 2 arrays.

My exact question is : Is there any way to find product without using this way

(a[0]*b[0]   a[1]*b[1]  a[2]*b[2]  a[3]*b[3] )

Example:

a=[1,2,3,4] b=[5,6,7,8] a=[1,2,3,4] => sum of a =>10 b=[5,6,7,8] => sum of b=> 26 a b = 10 26 => 36.

Here we can get sum without doing this (a[0] b[0] a[1] b[1] a[2] b[2] a[3] b[3])

Like this i want to get product between two arrays

CodePudding user response:

Something like this should work:

function multiplyArrays(firstArray, secondArray){
    if (firstArray.length !== secondArray.length)
        return;

    let total = 0;
    for (let i = 0; i < firstArray.length; i  )
        total  = firstArray[i] * secondArray[i];
    return total;
}

CodePudding user response:

You can't actually do multiplication without multiplication. You can hide the * sign under some library, such as Lodash.

Here are both solutions. And lodash without * sign.

Live Demo:

const arr1 = [1,2,3];
const arr2 = [4,5,6];

const sumProductJS =  arr1.reduce((r,v,i) => r v*arr2[i], 0);

const sumProductLodash = _.zipWith(arr1, arr2, _.multiply).reduce(_.add);

console.log('Vanila:', sumProductJS);
console.log('Lodash:', sumProductLodash);
.as-console-wrapper { max-height: 100% !important; top: 0 }
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>

CodePudding user response:

Is there any way to find the product of two vectors without multiplying their component values?

Answer: No


Forumlae

  1. The first formula for calculating the scalar product of two vectors in the same vector space sums the product of their vector components:

    a.b = Σ aibi ... for i = 1 to n

    This is the formula presented in the post as a base line. A JavaScript implementation example:

     const dotProduct = (v1, v2) => {
        let sigma;
        if( v1.length == v2.length) {
            sigma = v1.reduce( (sigma, Vi, i) => sigma  = Vi * v2[i], 0);
        }
        return sigma;
     }
    
  2. The second formula for scalar product is

    a.b = ║a║ ║b║ cosθ

    meaning take the product of the length of the first vector, the length of the second, and the cosine of the angle between them.

    • The length of a vector is the square root of the sum of the squares of its components (Pythagorous in n dimensions). So each length calculation involves summing the square of a vector's components anyway: the dot product of a vector with itself, before taking the square root.

    • The formula for the cosine of the angle between two vectors uses the equivalence of the two formula:

      cos(θ) = a.b / (║a║ ║b║)

      requiring the dot product to be calculated first.

    Hence computing the second formula value has no advantage over using the first formula directly, and may introduce computational inaccuracies because of additional steps taken to obtain square root values.

See also "Finding vector length for higher dimensions" and "cosine of angle between two vectors";


_If the question arises because of issues encountered with matrix multiplication you may wish to search using terms such as "large matrix multiplication by computer". While not familiar with the field the topic has certainly been investigated.

  • Related