从 math.matrix 中获取元素 Javascript

Get element from math.matrix in Javascript

我正在使用 math.js 库中的矩阵元素。

我创建了一个矩阵:

 let eye = math.matrix([
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
    ]);

但是当我尝试访问矩阵的一个元素时,它总是未定义。

console.log(eye[0][0]) -> undefined 
console.log(eye[0])  -> undefined 

有什么建议吗?我已经通读了 math.js 库的文档,但对如何访问单个元素一无所知。

而且我确实需要使用这个库,因为我正在进行基于矩阵的运算(矩阵乘法)。

您可能想尝试结合使用 subsetindex 以获得索引处的值。

来源:mathjs

math.subset(eye, math.index(0, 0)) // 1
math.subset(eye, math.index(0, 1)) // 0

如果你打印出eye,你会得到以下对象。

{
  _mathjs: "DenseMatrix",
  _data: [
    [ 1, 0, 0 ],
    [ 0, 1, 0 ],
    [ 0, 0, 1 ]
  ],
  _size: [ 3, 3 ]
}

但这只是用于保存有关矩阵信息的内部结构,您需要调用帮助程序方法,如文档中所示。

如果要访问数据,可以随时直接查询内部matrix数据。这是个坏主意,因为这些字段都被视为私有的;内部字段(以下划线开头)。

const __print = (x) => console.log(JSON.stringify(x));

let eye = math.matrix([
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1]
]);
   
__print(eye._data[0][0]); // 1

__print(eye._data[0]);    // [1, 0, 0]
__print(eye._data[1]);    // [0, 1, 0]
__print(eye._data[2]);    // [0, 0, 1]
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/8.1.1/math.js"></script>

建议您使用库中提供的辅助方法。

注意: 微调我的响应后,我在 GitHub 上的 Issue #230 中找到了类似的检索行的方法。

您需要计算出矩阵中的列数。之后,您可以从所需的行索引开始获取一个子集,范围从 0 到行中的列数。然后必须将结果展平。

const __print = (x) => console.log(JSON.stringify(x));

let eye = math.matrix([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]);

const __row = (m, r) =>
  (([ rows, cols ]) => math.flatten(math.subset(m,
    math.index(r, math.range(0, cols))).valueOf()))
  (m.size());
  
const __col = (m, c) =>
  (([ rows, cols ]) => math.flatten(math.subset(m,
    math.index(math.range(0, rows), c)).valueOf()))
  (m.size());
  
const __cell = (m, r, c) =>
  math.subset(m, math.index(r, c));

__print(__cell(eye, 0, 0)); // 1

__print(__row(eye, 0));     // [1, 2, 3]
__print(__row(eye, 1));     // [4, 5, 6]
__print(__row(eye, 2));     // [7, 8, 9]

__print(__col(eye, 0));     // [1, 4, 7]
__print(__col(eye, 1));     // [2, 5, 8]
__print(__col(eye, 2));     // [3, 6, 9]
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/8.1.1/math.js"></script>

原来它们已经实现了,但你仍然需要将结果展平。

const __print = (x) => console.log(JSON.stringify(x));

let eye = math.matrix([
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9]
]);

const __row = (m, r) =>
  math.flatten(math.row(m, r).valueOf());
  
const __col = (m, c) =>
  math.flatten(math.column(m, c).valueOf());
  
const __cell = (m, r, c) =>
  math.subset(m, math.index(r, c));

__print(__cell(eye, 0, 0)); // 1

__print(__row(eye, 0));     // [1, 2, 3]
__print(__row(eye, 1));     // [4, 5, 6]
__print(__row(eye, 2));     // [7, 8, 9]

__print(__col(eye, 0));     // [1, 4, 7]
__print(__col(eye, 1));     // [2, 5, 8]
__print(__col(eye, 2));     // [3, 6, 9]
.as-console-wrapper { top: 0; max-height: 100% !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/8.1.1/math.js"></script>

你试图达到 eye 的内容,好像 eye 是二维 js 数组,这绝对不是真的。 math.matrix returns 某些对象,研究其结构的最佳方法是 console.log(eye) (在 Chrome 或其他现代浏览器中)。 Or/and 阅读有关方法的文档