2

I would like to do symbolic matrix operations with Ryacas using a function that converts base R matrices to Ryacas format. The result of the function seems to match Ryacas format. But when I attempt to multiply the matrices, the error

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

throws. The code below is a minimal example that shows the case.

Any suggestion, please?

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
mert
  • 371
  • 2
  • 9

1 Answers1

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)))
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
  • Thanks. Actually in my original code, I used `aa * aa`, but I overlooked it here. I tried to use `parse(text=conv)` in the function to get an `expression`, but it was not work. – mert Dec 14 '18 at 17:41