2

我想使用将基本矩阵转换为格式Ryacas的函数来进行符号矩阵运算。该函数的结果似乎与格式匹配。但是当我尝试将矩阵相乘时,错误RRyacasRyacas

# Error in aa %*% aa : requires numeric/complex matrix/vector arguments

抛出。下面的代码是一个显示这种情况的最小示例。

请问有什么建议吗?

library(Ryacas)

conv.mat <- function(x) {
  conv <- lapply(1:nrow(x), function(i) paste0(x[i, ], collapse = ", "))
  conv <- paste0("List(", paste0("List(", unlist(conv), ")", collapse = ", "), ")")
  noquote(conv)
}

# Writing a matrix manually for Ryacas format

a <- List(List(1, 2), List(3, 7))
a * a
# expression(list(list(7, 16), list(24, 55)))


# Writing a matrix in R and convert it to Ryacas format by the function conv.mat

aa <- matrix(c(1, 2, 3, 7), 2, byrow = TRUE)
aa <- conv.mat(aa)
# [1] List(List(1, 2), List(3, 7))

aa * aa
# Error in aa * aa : non-numeric argument to binary operator
4

1 回答 1

0

Firstly, to multiply Ryacas matrices you want aa * aa rather than aa %*% aa. But that alone doesn't help in your case as conv.mat doesn't give exactly what we need (an expression).

We may use, e.g.,

conv.mat <- function(x)
  do.call(List, lapply(1:nrow(x), function(r) do.call(List, as.list(x[r, ]))))

Then

M <- matrix(c(1, 2, 3, 7), 2, byrow = TRUE)
M %*% M
#      [,1] [,2]
# [1,]    7   16
# [2,]   24   55
M <- conv.mat(M)
M * M
# expression(list(list(7, 16), list(24, 55)))
于 2018-12-14T11:14:33.290 回答