如何将n个相同长度的数组相互相乘?

huangapple go评论55阅读模式
英文:

How to multiply n arrays of the same length with each other?

问题

我想要将相同长度的数组相乘。数组的总数 (input.length) 是已知的,但可以是从 0 到 n 的任何值。

const input = [
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4]
];

const output = [1, 16, 81, 256];
// 1*1*1*1, 2*2*2*2, 3*3*3*3, 4*4*4*4

我在 Stack Overflow 上找到了将两个数组相乘的解决方案,通过将它们映射到彼此,但没有找到 n 个数组相乘的解决方案。

我对任何提示都感激不尽。

英文:

I want to multiply arrays of the same length. The total number of arrays (input.length) is known, but it can be everything between 0 and n.

const input = [
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4]
];

const output = [1, 16, 81, 256];
// 1*1*1*1, 2*2*2*2, 3*3*3*3, 4*4*4*4

I found solutions on SO for multiplying two arrays by mapping them against each other, but not for n arrays.

I am grateful for any hints.

答案1

得分: 6

如果您想要对数组进行乘法操作,最佳解决方案是使用Array#Reduce函数。您只需要像我使用multiplyArray一样引入一个函数来处理如何计算两个数组的乘积。

const input = [
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4]
];

const multiplyArray = (arr1, arr2) => {
  if (arr1.length !== arr2.length) {
    throw new Error('数组长度不同');
  }
  
  arr1.forEach((elem, index) => {
    arr1[index] = elem * arr2[index];
  });
  
  return arr1;
};

const output = input.reduce((acc, curr) => {
  if (acc === null) return curr;
  
  return multiplyArray(acc, curr);
}, null);

console.log(output);
英文:

If you want to multiply arrays, the best solution would be the Array#Reduce function

You here only need to introduce a function as I did with multiplyArray to handle how to compute to arrays together

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = [
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4]
];

const multiplyArray = (arr1, arr2) =&gt; {
  if(arr1.length !== arr2.length) {
    throw new Error(&#39;Array have not the same length&#39;);
  }
  
  arr1.forEach((elem, index) =&gt; {
    arr1[index] = elem * arr2[index]
  })
  
  return arr1
}

const output = input.reduce((acc, curr) =&gt; {
  if(acc === null) return curr
  
  return multiplyArray(acc, curr)
}, null)

console.log(output)

<!-- end snippet -->

答案2

得分: 2

你可以使用Array.prototype.map()Array.prototype.reduce()Array.prototype.every()结合使用,创建一个名为multiplyAcrossArrays的函数,该函数具有用于检查子数组长度的验证功能。

const input = [[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]]

const multiplyAcrossArrays = (input) => {
  if (!input.every(subArray => subArray.length === input[0].length)) {
    throw new Error('All sub-arrays must have the same length')
  }

  return input[0].map((_, i) => input.reduce((a, c) => a * c[i], 1))
}

console.log(multiplyAcrossArrays(input))
英文:

You can use Array.prototype.map() combined with Array.prototype.reduce() and Array.prototype.every() to create a function multiplyAcrossArrays with validation to check length of sub-arrays

Code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const input = [[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4],[1, 2, 3, 4]]

const multiplyAcrossArrays = (input) =&gt; {
  if (!input.every(subArray =&gt; subArray.length === input[0].length)) {
    throw new Error(&#39;All sub-arrays must have the same length&#39;)
  }

  return input[0].map((_, i) =&gt; input.reduce((a, c) =&gt; a * c[i], 1))
}

console.log(multiplyAcrossArrays(input))

<!-- end snippet -->

答案3

得分: 0

const input = [
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4],
[1, 2, 3, 4]
]

const result = input.reduce((filtered, current) => {
filtered.push(current.reduce((accumulator, currNum) => {
accumulator.push(Math.pow(currNum, input.length))
return accumulator
}, []))

return filtered
}, [])

console.log(result)

英文:
const input = [
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4]
]

const result = input.reduce((filtered, current) =&gt; {
  filtered.push(current.reduce((accumulator, currNum) =&gt; {
    accumulator.push(Math.pow(currNum, input.length))
    return accumulator
  }, []))

  return filtered
}, [])
    
console.log(result)

答案4

得分: 0

根据您需要此类行为的频率,实现一个通用的transpose函数可能值得麻烦。此函数会交换所提供矩阵的行和列。有关更多详细信息,请参阅维基百科链接。

function transpose(array) {
  if (!array.length) return [];

  const length = array[0].length;
  if (!array.every(item => Array.isArray(item) && item.length === length)) {
    throw new Error("not all elements are arrays or have the same length");
  }

  return array[0].map((_, index) => array.map(row => row[index]));
}

有了这个辅助函数,解决方案将变得非常简单,只需对矩阵进行转置,然后通过将值相乘来将每一行减少为单个值。

const output = transpose(input).map((row) => row.reduce((a, b) => a * b));
// => [1, 16, 81, 256]

如果您不想引入这个辅助函数,我可能仍然会使用类似的解决方案。

const output = input[0].map((_, index) => (
  input.map(row => row[index]).reduce((a, b) => a * b)
));
// => [1, 16, 81, 256]

请注意,此答案假定input中至少有1行,并假定正确的数据结构。如果情况并非总是如此,您可能需要添加额外的数据结构验证。

英文:

Depending on how often you'll need this type of behaviour, it might be worth the trouble to implement a general purpose transpose function. This switches rows and columns in the provided matrix. See the Wikipedia link for additional details.

function transpose(array) {
  if (!array.length) return [];

  const length = array[0].length;
  if (!array.every(item =&gt; Array.isArray(item) &amp;&amp; item.length === length)) {
    throw new Error(&quot;not all elements are arrays or have the same length&quot;);
  }

  return array[0].map((_, index) =&gt; array.map(row =&gt; row[index]));
}

With this helper defined a solution would be as simple as transposing the matrix, then reducing each row to a single value by multiplying the values.

const output = transpose(input).map((row) =&gt; row.reduce((a, b) =&gt; a * b));
//=&gt; [1, 16, 81, 256]

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

const input = [
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4]
];

const output = transpose(input).map((row) =&gt; row.reduce((a, b) =&gt; a * b));
console.log(output);

// helpers
function transpose(array) {
  if (!array.length) return [];

  const length = array[0].length;
  if (!array.every(item =&gt; Array.isArray(item) &amp;&amp; item.length === length)) {
    throw new Error(&quot;not all elements are arrays or have the same length&quot;);
  }

  return array[0].map((_, index) =&gt; array.map(row =&gt; row[index]));
}

<!-- end snippet -->


If you don't feel like introducing the helper, I would probably still use a similar solution.

const output = input[0].map((_, index) =&gt; (
  input.map(row =&gt; row[index]).reduce((a, b) =&gt; a * b)
));
//=&gt; [1, 16, 81, 256]

<!-- begin snippet: js hide: true console: true babel: false -->

<!-- language: lang-js -->

const input = [
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4],
  [1, 2, 3, 4]
];

const output = input[0].map((_, index) =&gt; (
  input.map(row =&gt; row[index]).reduce((a, b) =&gt; a * b)
));
console.log(output);

<!-- end snippet -->

Note that this answer assumes at least 1 row in input and assumes the correct data structure. If this is not always the case you might want to add additional data structure validation.

huangapple
  • 本文由 发表于 2023年7月18日 14:22:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709998.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定