R interface, elements access

1 view
Skip to first unread message

Alexandr Batalshikov

unread,
Aug 13, 2008, 7:20:58 AM8/13/08
to sage-s...@googlegroups.com
How to access vector elements by list of indexes in sage R interface?

In R this is work:

> v = c(3,5,9,1)
> v[c(2,3)]
[1] 5 9

but in sage not:
v = r.c([3,5,9,1])
print v[2] # ok.
print v[r.c([2,3])] # answer is wrong!

Sage version: 3.06 (in MS Windows, VMWare)

abbat.

Simon King

unread,
Aug 13, 2008, 11:02:52 AM8/13/08
to sage-support
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

Alexandr Batalshikov

unread,
Aug 13, 2008, 1:06:11 PM8/13/08
to sage-s...@googlegroups.com
Dear Simon,
many thanks for your explanations.

Variant like:

sage: r_vec[r.c(2,5).name()]

may be few ugly, but work very well. Thanks you.

Yours
Alexandr.

Simon King

unread,
Aug 13, 2008, 1:43:10 PM8/13/08
to sage-support
Dear Sage team, dear Alexandr,

On Aug 13, 5:02 pm, Simon King <k...@mathematik.uni-jena.de> wrote:
>
> Question to the Sage team: Do you agree with that analysis, and shall
> i open a ticket?

Sorry that i didn't wait: I made a ticket, http://trac.sagemath.org/sage_trac/ticket/3838.

With the patch provided in the ticket, the following works:
sage: v = r.c(3,5,9,1)
sage: key = r.c(2,3)
sage: v[key]
[1] 5 9
sage: v[-key]
[1] 3 1
sage: v[-2]
[1] 3 9 1
sage: v['c(2,3)']
[1] 5 9
sage: v[2,4,3]
[1] 5 1 9
sage: v[2]
[1] 5

Do you think this were a convenient solution? Or should R be addressed
differently?

Yours
Simon

abbat

unread,
Aug 13, 2008, 1:40:02 PM8/13/08
to sage-support
Dear Simon,
many thanks for your explanations.

Variant like:

sage: r_vec[r.c(2,5).name()]

may be few ugly but work very well. Thanks you.

Yours
Alexandr.

Simon King

unread,
Aug 13, 2008, 4:04:55 PM8/13/08
to sage-support
On Aug 13, 7:43 pm, Simon King <k...@mathematik.uni-jena.de> wrote:
> sage: v = r.c(3,5,9,1)
> sage: key = r.c(2,3)
> sage: v[key]
> [1] 5 9
> sage: v[-key]
> [1] 3 1

This is ok, but:

> sage: v[2,4,3]
> [1] 5 1 9

This is bad, because for an array m, one should get a single entry by
m[2,3]. I changed my patch in #3838 accordingly. Now, v[key] as above
still works, but also one can do
sage: m = r.array('1:3', r.c(2,4))
sage: m
[,1] [,2] [,3] [,4]
[1,] 1 3 2 1
[2,] 2 1 3 2
sage: m[1,2]
[1] 3
sage: m[r.c(1,2)]
[1] 1 2

I guess this is a better solution.
Cheers
Simon
Reply all
Reply to author
Forward
0 new messages