# RLink: Setting a matrix with constants returns a different matrix

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?

• 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}}]? May 30, 2018 at 16:29

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.}} *)

• Thanks. I thought it would be something like this and so I tried N[ mt,20] but since the problem persisted, I decided to ask here. ;) May 30, 2018 at 17:08
• @Anoldmaninthesea. Glad this was helpful. Thanks for the accept. May 30, 2018 at 18:29