def __getitem__(self, i): """This operation returns either a single value or a subview. Examples: >>> m = matrix(array=[[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]]) >>> m[1] # row 1 vector(dtype=float64, shape=(3), data=[4.,5.,6.]) >>> m[:,1] # column 1 vector(dtype=float64, shape=(3), data=[2.,5.,8.]) >>> m[0,0] # element (0,0) 1.0 >>> m[0:2,0:3:2] # a submatrix matrix(dtype=float64, shape=(2,2) [[1.0, 3.0], [4.0, 6.0]]) """ if type(i) != tuple: # must be a row accessor if isinstance(i, slice): return matrix( block=_block.subblock(self.block, (i, slice(0, None)))) else: return vector(block=self.block.row(i)) assert len(i) == 2 if isinstance(i[0], slice) and isinstance(i[1], slice): return matrix(block=_block.subblock(self.block, i)) elif isinstance(i[0], slice): return vector( block=_block.subblock(self.block.col(i[1]), (i[0], ))) elif isinstance(i[1], slice): return vector( block=_block.subblock(self.block.row(i[0]), (i[1], ))) else: return self.block.get(i[0], i[1])
def __getitem__(self, i): """This operation returns either a single value or a subview. Examples: >>> m = matrix(array=[[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]]) >>> m[1] # row 1 vector(dtype=float64, shape=(3), data=[4.,5.,6.]) >>> m[:,1] # column 1 vector(dtype=float64, shape=(3), data=[2.,5.,8.]) >>> m[0,0] # element (0,0) 1.0 >>> m[0:2,0:3:2] # a submatrix matrix(dtype=float64, shape=(2,2) [[1.0, 3.0], [4.0, 6.0]]) """ if type(i) != tuple: # must be a row accessor if isinstance(i, slice): return matrix(block=_block.subblock(self.block, (i,slice(0,None)))) else: return vector(block=self.block.row(i)) assert len(i) == 2 if isinstance(i[0], slice) and isinstance(i[1], slice): return matrix(block=_block.subblock(self.block, i)) elif isinstance(i[0], slice): return vector(block=_block.subblock(self.block.col(i[1]), (i[0],))) elif isinstance(i[1], slice): return vector(block=_block.subblock(self.block.row(i[0]), (i[1],))) else: return self.block.get(i[0], i[1])
def __setitem__(self, i, value): """Assign to a single value or a subvector, depending on the type of 'i'. Examples: >>> v = vector(array=[1.,2.,3.,4.]) >>> v[1] = 42. >>> v vector(dtype=float64, shape=(4) data=[1.0, 42., 3.0, 4.0]) >>> v[0:4:2] = 42. >>> v vector(dtype=float64, shape=(4) data=[42.0, 42.0, 42.0, 4.0]) """ if isinstance(value, vector): value = value.block if isinstance(i, slice): _block.subblock(self.block, (i,)).assign(value) else: self.block.put(i, value)
def __setitem__(self, i, value): """Assign to a single value or a subvector, depending on the type of 'i'. Examples: >>> v = vector(array=[1.,2.,3.,4.]) >>> v[1] = 42. >>> v vector(dtype=float64, shape=(4) data=[1.0, 42., 3.0, 4.0]) >>> v[0:4:2] = 42. >>> v vector(dtype=float64, shape=(4) data=[42.0, 42.0, 42.0, 4.0]) """ if isinstance(value, vector): value = value.block if isinstance(i, slice): _block.subblock(self.block, (i, )).assign(value) else: self.block.put(i, value)
def __setitem__(self, i, value): """Assign to a single value or a subview. Examples: >>> m = matrix(array=[[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]]) >>> m[1] = 42. # row 1 >>> m matrix(dtype=float64, shape=(3,3) [[1.0, 2.0, 3.0], [42.0, 42.0, 42.0], [7.0, 8.0, 9.0]]) >>> m[:,1] = 7. # column 1 >>> m matrix(dtype=float64, shape=(3,3) [[1.0, 7.0, 3.0], [42.0, 7.0, 42.0], [7.0, 7.0, 9.0]]) >>> m[0,0] = 12. # element (0,0) matrix(dtype=float64, shape=(3,3) [[12.0, 7.0, 3.0], [42.0, 7.0, 42.0], [7.0, 7.0, 9.0]]) >>> m[0:2,0:3:2] = 13 # a submatrix matrix(dtype=float64, shape=(3,3) [[13.0, 7.0, 13.0], [13.0, 7.0, 13.0], [7.0, 7.0, 9.0]]) """ if isinstance(value, (vector, matrix)): value = value.block if type(i) != tuple: # must be a row accessor if isinstance(i, slice): _block.subblock(self.block, (i, slice(0, None))).assign(value) else: self.block.row(i).assign(value) return assert len(i) == 2 if isinstance(i[0], slice) and isinstance(i[1], slice): if i[0] == slice(None) and i[1] == slice(None): self.block.assign(value) else: _block.subblock(self.block, i).assign(value) elif isinstance(i[0], slice): _block.subblock(self.block.col(i[1]), (i[0], )).assign(value) elif isinstance(i[1], slice): _block.subblock(self.block.row(i[0]), (i[1], )).assign(value) else: self.block.put(i[0], i[1], value)
def __setitem__(self, i, value): """Assign to a single value or a subview. Examples: >>> m = matrix(array=[[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]]) >>> m[1] = 42. # row 1 >>> m matrix(dtype=float64, shape=(3,3) [[1.0, 2.0, 3.0], [42.0, 42.0, 42.0], [7.0, 8.0, 9.0]]) >>> m[:,1] = 7. # column 1 >>> m matrix(dtype=float64, shape=(3,3) [[1.0, 7.0, 3.0], [42.0, 7.0, 42.0], [7.0, 7.0, 9.0]]) >>> m[0,0] = 12. # element (0,0) matrix(dtype=float64, shape=(3,3) [[12.0, 7.0, 3.0], [42.0, 7.0, 42.0], [7.0, 7.0, 9.0]]) >>> m[0:2,0:3:2] = 13 # a submatrix matrix(dtype=float64, shape=(3,3) [[13.0, 7.0, 13.0], [13.0, 7.0, 13.0], [7.0, 7.0, 9.0]]) """ if isinstance(value, (vector, matrix)): value = value.block if type(i) != tuple: # must be a row accessor if isinstance(i, slice): _block.subblock(self.block, (i,slice(0,None))).assign(value) else: self.block.row(i).assign(value) return assert len(i) == 2 if isinstance(i[0], slice) and isinstance(i[1], slice): if i[0] == slice(None) and i[1] == slice(None): self.block.assign(value) else: _block.subblock(self.block, i).assign(value) elif isinstance(i[0], slice): _block.subblock(self.block.col(i[1]), (i[0],)).assign(value) elif isinstance(i[1], slice): _block.subblock(self.block.row(i[0]), (i[1],)).assign(value) else: self.block.put(i[0], i[1], value)
def __getitem__(self, i): """This operation returns either a single value or a subvector. Examples: >>> v = vector(array=[1.,2.,3.,4.]) >>> v[1] 2. >>> v[0:4:2] vector(dtype=float64, shape=(2) data=[1.0, 3.0]) """ if isinstance(i, slice): return vector(block=_block.subblock(self.block, (i,))) else: return self.block.get(i)
def __getitem__(self, i): """This operation returns either a single value or a subvector. Examples: >>> v = vector(array=[1.,2.,3.,4.]) >>> v[1] 2. >>> v[0:4:2] vector(dtype=float64, shape=(2) data=[1.0, 3.0]) """ if isinstance(i, slice): return vector(block=_block.subblock(self.block, (i, ))) else: return self.block.get(i)