# HG changeset patch
# User Simon King <king@mathematik.unijena.de>
# Date 1218698169 7200
# Node ID 108c50ddb254a072e8af416e75d2fb04389c170a
# Parent 6417e6c84a2a6f4851d87a7702a6257624ec8f13
Extend the features of __getitem__ for RElement
diff r 6417e6c84a2a r 108c50ddb254 sage/interfaces/r.py
a

b


1159  1159  
1160  1160  def __getitem__(self, n): 
1161  1161  """ 
1162   Return nth element of self. 
 1162  Return element(s) of self. 
1163  1163  
1164  1164  INPUT: 
1165   n  an integer 
 1165  n  an integer, a tuple, a string that makes sense to R, or an RElement 
1166  1166  OUTPUT: 
1167  1167  RElement 
1168  1168  
… 
… 

1178  1178  [1] 10.4 3.1 6.4 21.7 
1179  1179  sage: x[3] 
1180  1180  [1] 10.4 5.6 6.4 21.7 
1181   sage: x[4] 
1182   [1] 6.4 
 1181  sage: x['c(2,3)'] 
 1182  [1] 5.6 3.1 
 1183  sage: key = r.c(2,3) 
 1184  sage: x[key] 
 1185  [1] 5.6 3.1 
 1186  sage: m = r.array('1:3',r.c(2,4,2)) 
 1187  sage: m 
 1188  , , 1 
 1189  [,1] [,2] [,3] [,4] 
 1190  [1,] 1 3 2 1 
 1191  [2,] 2 1 3 2 
 1192  , , 2 
 1193  [,1] [,2] [,3] [,4] 
 1194  [1,] 3 2 1 3 
 1195  [2,] 1 3 2 1 
 1196  sage: m[1,2,2] 
 1197  [1] 2 
 1198  sage: m[1,r.c(1,2),1] 
 1199  [1] 1 3 
1183  1200  """ 
1184  1201  P = self._check_valid() 
1185  1202  if isinstance(n, basestring): 
1186  1203  n = n.replace('self', self._name) 
1187  1204  return P.new('%s[%s]'%(self._name, n)) 
1188   elif not isinstance(n, tuple): 
 1205  elif (hasattr(n,'parent') and n.parent() is P): # the key is RElement itself 
 1206  return P.new('%s[%s]'%(self._name, n.name())) 
 1207  elif not isinstance(n,tuple): 
1189  1208  return P.new('%s[%s]'%(self._name, n)) 
1190  1209  else: 
1191   return P.new('%s[%s]'%(self._name, str(n)[1:1])) 
 1210  L = [] 
 1211  for i in xrange(len(n)): 
 1212  if (hasattr(n[i],'parent') and n[i].parent() is P): 
 1213  L.append(n[i].name()) 
 1214  else: 
 1215  L.append(str(n[i])) 
 1216  return P.new('%s[%s]'%(self._name, ','.join(L))) 
1192  1217  
1193  1218  def __nonzero__(self): 
1194  1219  """ 