Opened 13 years ago

Closed 13 years ago

#3838 closed defect (fixed)

[with patch, positive review] Element access for RElement

Reported by: SimonKing Owned by: SimonKing
Priority: major Milestone: sage-3.1.3
Component: interfaces Keywords: r interface, element access
Cc: AlexGhitza Merged in:
Authors: Reviewers:
Report Upstream: Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

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 (4)

RElementAccess.patch (1.7 KB) - added by SimonKing 13 years ago.
Patch relative to 3.1.alpha0
RElementAccess2.patch (1.3 KB) - added by SimonKing 13 years ago.
Correction for the first patch
RElementAccess3.patch (1.0 KB) - added by SimonKing 13 years ago.
To be applied after the two previous patches
RElementAccessNew.patch (2.3 KB) - added by SimonKing 13 years ago.
Replaces all previous patches

Download all attachments as: .zip

Change History (12)

Changed 13 years ago by SimonKing

Patch relative to 3.1.alpha0

comment:1 Changed 13 years ago by mabshoff

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

Changed 13 years ago by SimonKing

Correction for the first patch

comment:2 Changed 13 years ago 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.

comment:3 follow-up: Changed 13 years ago 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.

Changed 13 years ago by SimonKing

To be applied after the two previous patches

comment:4 in reply to: ↑ 3 ; follow-up: Changed 13 years ago 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.

Changed 13 years ago by SimonKing

Replaces all previous patches

comment:5 in reply to: ↑ 4 Changed 13 years ago 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.

comment:6 Changed 13 years ago by AlexGhitza

  • Cc AlexGhitza added

comment:7 Changed 13 years ago 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

comment:8 Changed 13 years ago by mabshoff

  • Resolution set to fixed
  • Status changed from new to closed

Merged RElementAccessNew.patch in Sage 3.1.3.alpha1

Note: See TracTickets for help on using tickets.