3

Let's assume I have the following matrix:

{{1, 0, τ^-1},
 {-κ, 1, 0},
 {-(1 - ρr) ψ2, -(1 - ρr) ψ1, 1},
 {0, 0, 0}}

which is equal to:

{{1, 0, 10.}, 
 {-0.2, 1, 0}, 
 {-0.16, -0.32, 1}, 
 {0, 0, 0}}

I then do:

RSet["gama0", {{1, 0, τ^-1}, {-κ, 1,0}, {-(1 - ρr) ψ2, -(1 - ρr) ψ1, 1}, {0, 0, 0}}]

Then REvaluate["gama0"] returns :

{{{1}, {0}, {10.}, {-1}, {0}, {-1}, {-10.}}, 
 {{-0.2}, {1}, {0},{0.2}, {0}, {0}, {-0.5}},
 {{-0.16}, {-0.32}, {1}, {0.16}, {0}, {0},{0}},
 {0, 0, 0, 1, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 0},
 {1, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 0}}

which is not the same matrix (different number of levels in the rows where we had decimal numbers).

Is this a feature or a bug?

MarcoB
  • 67,153
  • 18
  • 91
  • 189
An old man in the sea.
  • 2,527
  • 1
  • 18
  • 26
  • Does the same thing happen if you use the numerical values instead of the symbols, i.e. using RSet["gama0", {{1, 0, 10.}, {-0.2, 1, 0}, {-0.16, -0.32, 1}, {0, 0, 0}}]? – MarcoB May 30 '18 at 16:29

1 Answers1

4

The problem is that you send to R the matrix of mixed integer and real types, which can't be used verbatim as an R Matrix, so RLink interprets this as an R list type. This ambiguity of interpretation has been mentioned in the RLink documentation (section "Type Detection Ambiguities, and How to Force a Given Data Interpretation").

To get what you need in this particular case, you can apply N to your matrix before sending it to R, to convert all numbers to machine-precision reals:

mt = {{1, 0, 10.}, {-0.2, 1, 0}, {-0.16, -0.32, 1}, {0, 0, 0}} 
RSet["gama0", N @ mt]

in which case you will get back the same matrix:

REvaluate["gama0"]

(* {{1., 0., 10.}, {-0.2, 1., 0.}, {-0.16, -0.32, 1.}, {0., 0., 0.}} *)
Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420