简体   繁体   English

如何减去这两个 arrays

[英]How to subtract these two arrays

I am trying to subtract two matrices, I have a function that takes in those matrices and returns a new matrix that has been subtracted.我试图减去两个矩阵,我有一个 function 接受这些矩阵并返回一个已减去的新矩阵。 I get this error in node.js: TypeError: (intermediate value).map is not a function我在 node.js 中收到此错误:TypeError: (intermediate value).map is not a function

subtract(a, b) {

  return new Matrix(a.rows, a.cols).map((_, i, j) => a.data[i][j] - b.data[i][j]);
}

This is the function I use from my main file (note: I already have an instance of the class).这是我在主文件中使用的 function(注意:我已经有了该类的实例)。

let m = new Matrix(2, 2);
m.data[0] = [10, 11];
m.data[1] = [12, 13];

let n = new Matrix(2, 2);
n.data[0] = [1, 2];
n.data[1] = [3, 4];

mat.subtract(m, n);

This is the class that I have created:这是我创建的 class:

class Matrix {
    constructor(rows, cols) {
      this.rows = rows;
      this.cols = cols;
      this.index = 0;
      this.rowCount = 0;
  
      //this.matrixData = Array(this.rows).fill().map(() => Array(this.cols).fill(0));
      this.data = Array(this.rows).fill().map(() => Array(this.cols).fill(0));
    }
}

I'm going to recommend a complete code rewrite that focuses on a plain functions instead of classes and methods.我将推荐一个完整的代码重写,它专注于普通函数而不是类和方法。 We begin writing our matrix module below and will add an OOP-style interface in the second section of this post.我们开始在下面编写matrix模块,并将在本文的第二部分添加一个 OOP 风格的界面。 - -

// matrix.js

const matrix = rows =>
  ({ matrix, rows })

const empty = _ =>
  matrix([])

const subtract = (t1, t2) =>
  matrix(t1.rows.map((_, i) => subtractRow(t1.rows[i], t2.rows[i])))

const subtractRow = (r1, r2) =>
  r1.map((v, i) => v - r2[i])

function toString (t)
{ const w =
    Math.max(...t.rows.flat().map(_ => String(_).length))
  const s = 
    t.rows.map(r => r.map(_ => String(_).padStart(w, " ")).join(" "))
  return `[ ${s.join("\n  ")} ]`
}

export { empty, matrix, subtract, toString } 

Next we write our main module that uses the matrix module -接下来我们编写使用matrix模块的main模块 -

// main.js

import { matrix, subtract, toString } from "./matrix.js"

const m1 = matrix([[10, 11], [12, 13]])
const m2 = matrix([[1,2], [3,4]])

console.log(toString(m1))
console.log(toString(m2))
console.log(toString(subtract(m1, m2)))
[ 10 11
  12 13 ]

[ 1 2
  3 4 ]

[ 9 9
  9 9 ]

If you are more comfortable with an OOP-style interface, you we can add that to our matrix module easily.如果您更喜欢 OOP 风格的界面,我们可以轻松地将其添加到我们的matrix模块中。 Notice how our Matrix class it is a simple wrapper around our existing plain functions -请注意我们的Matrix class 它是如何对我们现有的普通函数进行简单包装 -

// matrix.js (continued)

class Matrix
{ constructor(t = empty())
  { this.t = t }

  subtract(other)
  { return new Matrix(subtract(this.t, other.t)) }

  toString()
  { return toString(this.t) }

  static of(rows)
  { return new Matrix(matrix(rows)) }
}

export default Matrix

And here's our main module using our new Matrix interface -这是使用我们新的Matrix界面的main模块 -

// main.js

import Matrix from "./matrix.js"

const m1 = Matrix.of([[10, 11], [12, 13]])
const m2 = Matrix.of([[1,2], [3,4]])

console.log(m1.toString())
console.log(m2.toString())
console.log(m1.subtract(m2).toString())
[ 10 11
  12 13 ]

[ 1 2
  3 4 ]

[ 9 9
  9 9 ]

Chain for days, if you wish -如果你愿意的话,可以连上几天——

console.log(m1.subtract(m2).subtract(m2).toString())
console.log(m2.subtract(m1).subtract(m1).subtract(m1).toString())
[ 8 7
  6 5 ]

[ -29 -31
  -33 -35 ]

As you can see we only write our matrix module once and it is flexible enough to use in functional-style and OOP-style.如您所见,我们只编写一次matrix模块,它足够灵活,可以在函数式OOP 式中使用。 To see this module technique used in another setting, see this answer where we build a linked list.要查看在另一个设置中使用的此模块技术,请参阅我们构建链接列表的这个答案


Wondering why we went through so much trouble in matrix.toString ?想知道为什么我们在matrix.toString中遇到了这么多麻烦? It's so that the matrix can be nicely formatted even when element sizes differ -这样即使元素大小不同,矩阵也可以很好地格式化 -

import { matrix, toString } from "./matrix.js"

const m3 = matrix([
  [1,2,3],
  [11,22,33]
  [111,222,333]
])

console.log(toString(m3))
[   1   2   3
   11  22  33
  111 222 333 ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM