Ticket #3838 (closed defect: fixed)

Opened 4 months ago

Last modified 2 months ago

[with patch, positive review] Element access for RElement

Reported by: SimonKing Assigned to: SimonKing
Priority: major Milestone: sage-3.1.3
Component: interfaces Keywords: r interface, element access
Cc: AlexGhitza

Description

On http://groups.google.com/group/sage-support/browse_thread/thread/4868d601510e9642?hl=en, Alexandr Batalshikov pointed out that

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

works in R, but the corresponding statement in Sage does not:

sage: v = r.c(3,5,9,1)
sage: n = r.c(2,3)
sage: v[n]
[1] 3

I believe this is a defect. With the attached patch, the following works:

sage: v = r.c(3,5,9,1)
sage: n = r.c(2,3)
sage: v[n]
[1] 5 9
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

Attachments

RElementAccess.patch (1.7 kB) - added by SimonKing on 08/13/2008 10:27:53 AM.
Patch relative to 3.1.alpha0
RElementAccess2.patch (1.3 kB) - added by SimonKing on 08/13/2008 01:00:50 PM.
Correction for the first patch
RElementAccess3.patch (1.0 kB) - added by SimonKing on 08/13/2008 11:53:45 PM.
To be applied after the two previous patches
RElementAccessNew.patch (2.3 kB) - added by SimonKing on 08/14/2008 12:17:12 AM.
Replaces all previous patches

Change History

08/13/2008 10:27:53 AM changed by SimonKing

  • attachment RElementAccess.patch added.

Patch relative to 3.1.alpha0

08/13/2008 10:29:16 AM changed by mabshoff

  • summary changed from Element access for RElement to [with patch, needs review] Element access for RElement.

08/13/2008 01:00:50 PM changed by SimonKing

  • attachment RElementAccess2.patch added.

Correction for the first patch

08/13/2008 01:02:04 PM changed by SimonKing

I just realized that it is not a good idea to make v[2,3] return a vector, because if v is an array, v[2,3] should return a single entry of the array.

The new patch (that should be applied after the first one) takes this into account. Now we have:

sage: v = r.c(3,5,9,1)
sage: n = r.c(2,3)
sage: v[n]
[1] 5 9
sage: v[-n]
[1] 3 1

as above, and

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[n]
[1] 2 3

I think this is better than the first approach, but still allows to use an RElement as index.

(follow-up: ↓ 4 ) 08/13/2008 03:58:36 PM changed by schilly

hi, I like the second approach, but just for the sake of completeness and with the future in mind: could you add a doctest for accessing elements of a three dimensional array? I know they can happen and i think it would be good to cover them.

08/13/2008 11:53:45 PM changed by SimonKing

  • attachment RElementAccess3.patch added.

To be applied after the two previous patches

(in reply to: ↑ 3 ; follow-up: ↓ 5 ) 08/14/2008 12:04:14 AM changed by SimonKing

Replying to schilly:

hi, I like the second approach, but just for the sake of completeness and with the future in mind: could you add a doctest for accessing elements of a three dimensional array? I know they can happen and i think it would be good to cover them.

No problem, that works already with the previous version:

sage: m = r.array('1:3', r.c(2,4,2))
sage: m
, , 1
     [,1] [,2] [,3] [,4]
[1,]    1    3    2    1
[2,]    2    1    3    2

, , 2
     [,1] [,2] [,3] [,4]
[1,]    3    2    1    3
[2,]    1    3    2    1

sage: m[1,2,2]
[1] 2
sage: m[1,3,2]
[1] 1

I changed the doc-tests accordingly (by the third patch).

However, i just realize that mixing integer and r.c does not work:

sage: m = r.array('1:3', r.c(2,4,2))
sage: r(m.name()+'[1,c(1,2),1]')
[1] 1 3    # the output how it should be
sage: m[1,r.c(1,2),1]
[1] 2      # wrong output

I'll work on this problem.

08/14/2008 12:17:12 AM changed by SimonKing

  • attachment RElementAccessNew.patch added.

Replaces all previous patches

(in reply to: ↑ 4 ) 08/14/2008 12:24:01 AM changed by SimonKing

Replying to SimonKing:

I'll work on this problem.

The most recent patch replaces all previous patches and should apply to 3.1.alpha0. Here is the new feature:

sage: m = r.array('1:3', r.c(2,4,2))
sage: m
, , 1
     [,1] [,2] [,3] [,4]
[1,]    1    3    2    1
[2,]    2    1    3    2

, , 2
     [,1] [,2] [,3] [,4]
[1,]    3    2    1    3
[2,]    1    3    2    1
sage: m[1,r.c(1,2),1]
[1] 1 3
sage: m[1,r.c(1,3),r.c(1,2)]
     [,1] [,2]
[1,]    1    3
[2,]    2    1

The doctests provide examples for that type of usage.

09/20/2008 04:35:51 PM changed by AlexGhitza

  • cc set to AlexGhitza.

09/23/2008 12:57:32 AM changed by AlexGhitza

  • summary changed from [with patch, needs review] Element access for RElement to [with patch, positive review] Element access for RElement.

applies to 3.1.3.alpha0

09/23/2008 03:24:33 AM changed by mabshoff

  • status changed from new to closed.
  • resolution set to fixed.

Merged RElementAccessNew.patch in Sage 3.1.3.alpha1