3
$\begingroup$

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?

$\endgroup$
1
  • $\begingroup$ 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}}]? $\endgroup$
    – MarcoB
    May 30 '18 at 16:29
4
$\begingroup$

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.}} *)
$\endgroup$
2
  • $\begingroup$ 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. ;) $\endgroup$ May 30 '18 at 17:08
  • 1
    $\begingroup$ @Anoldmaninthesea. Glad this was helpful. Thanks for the accept. $\endgroup$ May 30 '18 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.