Dear Alexandr, dear Sage team,
On Aug 13, 1:20 pm, "Alexandr Batalshikov" <
abatalshi...@gmail.com>
wrote:
> How to access vector elements by list of indexes in sage R interface?
...
> print v[r.c([2,3])] # answer is wrong!
I guess it should be v[r.c(2,3)] (without the square brackets around
2,3).
Nevertheless I can confirm your observation that something goes wrong:
sage: v=r('c(3,5,9,1)')
sage: v[r.c(2,3)]
[1] 3
Work around: One can pass a string with a definition that R
understands:
sage: v['c(2,3)']
[1] 5 9
However, i think this is a work-around but not a solution.
v[n] is the same as v.__getitem__(n). After looking at the source code
of v.__getitem__, i think that it forgot one case.
v.__getitem__(n) proceeds like this:
1. If n is a string, then return r(
v.name()+'['+n+']'). This is why
the above work around works.
2. If n is neither a string nor a tuple, then return r(
v.name()
+'['+str(n)+']'), which is the source of the problem.
3. In the remainging case (if n is a tuple), something else is done.
Problem:
r.c(2,3) is of type <class 'sage.interfaces.r.RElement'> --- so what
happens? Apparently Case 2. applies, and thus the return is equivalent
to
sage: r(
v.name()+'[[1] 2 3]')
[1] 3
I suggest that v.__getitem__(n) should test
"isinstance(n,sage.interfaces.r.RElement)" and then return something
equivalent to
r(
v.name()+'['+
n.name()+']')
(which in the source code will be P.new('%s[%s]'%(self._name,
n.name())) )
The result were
sage: r('%s[%s]'%(
v.name(),r.c(2,3).name()))
[1] 5 9
which is correct
Question to the Sage team: Do you agree with that analysis, and shall
i open a ticket?
Yours
Simon