2018-10-04 14:37:37 +01:00
---
id: 599c333915e0ea32d04d4bec
2020-11-27 19:02:05 +01:00
title: Element-wise operations
2018-10-04 14:37:37 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302252
2021-01-13 03:31:00 +01:00
dashedName: element-wise-operations
2018-10-04 14:37:37 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-03-02 17:42:56 +09:00
Implement basic element-wise matrix-matrix and scalar-matrix operations.
2020-11-27 19:02:05 +01:00
**Implement:**
2019-03-02 17:42:56 +09:00
< ul >
< li > addition< / li >
< li > subtraction< / li >
< li > multiplication< / li >
< li > division< / li >
< li > exponentiation< / li >
< / ul >
2020-11-27 19:02:05 +01:00
2019-03-02 17:42:56 +09:00
The first parameter will be the operation to be performed, for example, "m_add" for matrix addition and "s_add" for scalar addition. The second and third parameters will be the matrices on which the operations are to be performed.
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
`operation` should be a function.
```js
assert(typeof operation === 'function');
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
`operation("m_add",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[2,4],[6,8]]` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(
operation(
'm_add',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[2, 4],
[6, 8]
]
);
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
`operation("s_add",[[1,2],[3,4]],2)` should return `[[3,4],[5,6]]` .
2018-10-04 14:37:37 +01:00
```js
2020-11-27 19:02:05 +01:00
assert.deepEqual(
operation(
's_add',
[
[1, 2],
[3, 4]
],
2
),
[
[3, 4],
[5, 6]
]
);
```
2020-09-15 09:57:40 -07:00
2020-11-27 19:02:05 +01:00
`operation("m_sub",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[0,0],[0,0]]` .
```js
assert.deepEqual(
operation(
'm_sub',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[0, 0],
[0, 0]
]
);
2018-10-04 14:37:37 +01:00
```
2020-11-27 19:02:05 +01:00
`operation("m_mult",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[1,4],[9,16]]` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(
operation(
'm_mult',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[1, 4],
[9, 16]
]
);
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
`operation("m_div",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[1,1],[1,1]]` .
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.deepEqual(
operation(
'm_div',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[1, 1],
[1, 1]
]
);
```
`operation("m_exp",[[1,2],[3,4]],[[1,2],[3,4]])` should return `[[1,4],[27,256]]` .
```js
assert.deepEqual(
operation(
'm_exp',
[
[1, 2],
[3, 4]
],
[
[1, 2],
[3, 4]
]
),
[
[1, 4],
[27, 256]
]
);
```
`operation("m_add",[[1,2,3,4],[5,6,7,8]],[[9,10,11,12],[13,14,15,16]])` should return `[[10,12,14,16],[18,20,22,24]]` .
```js
assert.deepEqual(
operation(
'm_add',
[
[1, 2, 3, 4],
[5, 6, 7, 8]
],
[
[9, 10, 11, 12],
[13, 14, 15, 16]
]
),
[
[10, 12, 14, 16],
[18, 20, 22, 24]
]
);
```
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
2018-10-04 14:37:37 +01:00
2020-11-27 19:02:05 +01:00
## --seed-contents--
```js
function operation(op, arr1, arr2) {
}
```
# --solutions--
2018-10-04 14:37:37 +01:00
```js
function operation(op, arr1, arr2) {
const ops = {
add: ((a, b) => a + b),
sub: ((a, b) => a - b),
mult: ((a, b) => a * b),
div: ((a, b) => a / b),
exp: ((a, b) => Math.pow(a, b))
};
const ifm = op.startsWith('m');
const doOp = ops[op.substring(2)];
for (let i = 0; i < arr1.length ; i + + ) {
for (let j = 0; j < arr1 [ 0 ] . length ; j + + ) {
arr1[i][j] = doOp(arr1[i][j], (ifm) ? (arr2[i][j]) : (arr2));
}
}
return arr1;
}
```