freeCodeCamp/curriculum/challenges/chinese/10-coding-interview-prep/rosetta-code/element-wise-operations.md

2.4 KiB
Raw Blame History

id title challengeType videoUrl
599c333915e0ea32d04d4bec 元素操作 5

--description--

实现基本的元素矩阵 - 矩阵和标量矩阵运算。

实行:

:: *另外

:: *减法

:: *乘法

:: *分裂

:: *取幂

第一个参数是要执行的操作例如用于矩阵加法的“m_add”和用于标量加法的“s_add”。第二和第三参数将是要在其上执行操作的矩阵。

--hints--

operation是一种功能。

assert(typeof operation === 'function');

operation("m_add",[[1,2],[3,4]],[[1,2],[3,4]])应返回[[2,4],[6,8]]

assert.deepEqual(
  operation(
    'm_add',
    [
      [1, 2],
      [3, 4]
    ],
    [
      [1, 2],
      [3, 4]
    ]
  ),
  [
    [2, 4],
    [6, 8]
  ]
);

operation("s_add",[[1,2],[3,4]],[[1,2],[3,4]])应返回[[3,4],[5,6]]

assert.deepEqual(
  operation(
    's_add',
    [
      [1, 2],
      [3, 4]
    ],
    2
  ),
  [
    [3, 4],
    [5, 6]
  ]
);

operation("m_sub",[[1,2],[3,4]],[[1,2],[3,4]])应返回[[0,0],[0,0]]

assert.deepEqual(
  operation(
    'm_sub',
    [
      [1, 2],
      [3, 4]
    ],
    [
      [1, 2],
      [3, 4]
    ]
  ),
  [
    [0, 0],
    [0, 0]
  ]
);

operation("m_mult",[[1,2],[3,4]],[[1,2],[3,4]])应该返回[[1,4],[9,16]]

assert.deepEqual(
  operation(
    'm_mult',
    [
      [1, 2],
      [3, 4]
    ],
    [
      [1, 2],
      [3, 4]
    ]
  ),
  [
    [1, 4],
    [9, 16]
  ]
);

operation("m_div",[[1,2],[3,4]],[[1,2],[3,4]])应返回[[1,1],[1,1]]

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]])应返回[[1,4],[27,256]]

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]])应该返回[[10,12,14,16],[18,20,22,24]]

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]
  ]
);

--solutions--