def __getitem__(self, key): assert isinstance(key, tuple) assert len(key) == 2 key_is_tuple_of_slices = all([isinstance(key_i, slice) for key_i in key]) key_is_tuple_of_tuples_or_lists = all([isinstance(key_i, (list, tuple)) for key_i in key]) key_is_tuple_of_int = all([isinstance(key_i, int) for key_i in key]) if ( key_is_tuple_of_slices # matrix[:5, :5] or key_is_tuple_of_tuples_or_lists # matrix[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] ): assert key_is_tuple_of_slices is not key_is_tuple_of_tuples_or_lists if key_is_tuple_of_slices: # matrix[:5, :5] output_content = self.content[ wrapping.Slicer(*slice_to_array(self, key, self._component_name_to_basis_component_length, self._component_name_to_basis_component_index))] output_size = slice_to_size(self, key, self._component_name_to_basis_component_length) elif key_is_tuple_of_tuples_or_lists: # matrix[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] output_content = self.content[wrapping.Slicer(*key)] output_size = (len(key[0]), len(key[1])) # Prepare output assert len(output_size) == 2 output = Matrix_Class.__new__(type(self), output_size[0], output_size[1], output_content) output.__init__(output_size[0], output_size[1], output_content) # Preserve auxiliary attributes related to basis functions matrix output._component_name_to_basis_component_index = self._component_name_to_basis_component_index if ( self._component_name_to_basis_component_length[0] is None and self._component_name_to_basis_component_length[1] is None ): output._component_name_to_basis_component_length = (None, None) else: if key_is_tuple_of_slices: # matrix[:5, :5] output._component_name_to_basis_component_length = tuple(output_size) elif key_is_tuple_of_tuples_or_lists: # matrix[[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]] component_name_to_basis_component_length = [None, None] for i in range(2): if len(self._component_name_to_basis_component_length[i]) == 1: for (component_name, _) in self._component_name_to_basis_component_length[i].items(): break component_name_to_basis_component_length_i = OnlineSizeDict() component_name_to_basis_component_length_i[component_name] = len(key[i]) component_name_to_basis_component_length[i] = component_name_to_basis_component_length_i else: raise NotImplementedError( "Matrix.__getitem__ with list or tuple input arguments" + " has not been implemented yet for the case of multiple components") output._component_name_to_basis_component_length = tuple( component_name_to_basis_component_length) return output elif key_is_tuple_of_int: # matrix[5, 5] output = self.content[key] assert isinstance(output, Number) return output else: raise TypeError("Unsupported key type in Matrix.__getitem__")
def __getitem__(self, key): if (isinstance(key, slice) # vector[:5] or isinstance(key, (list, tuple)) # vector[[0, 1, 2, 3, 4]] ): if isinstance(key, slice): # vector[:5] output_content = self.content[wrapping.Slicer( slice_to_array( self, key, self._component_name_to_basis_component_length, self._component_name_to_basis_component_index))] output_size = slice_to_size( self, key, self._component_name_to_basis_component_length) elif isinstance(key, (list, tuple)): # vector[[0, 1, 2, 3, 4]] output_content = self.content[wrapping.Slicer(key)] output_size = (len(key), ) # Prepare output assert len(output_size) == 1 output = Vector_Class.__new__(type(self), output_size[0], output_content) output.__init__(output_size[0], output_content) # Preserve auxiliary attributes related to basis functions matrix output._component_name_to_basis_component_index = self._component_name_to_basis_component_index if self._component_name_to_basis_component_length is None: output._component_name_to_basis_component_length = None else: if isinstance(key, slice): # vector[:5] output._component_name_to_basis_component_length = output_size[ 0] elif isinstance(key, (list, tuple)): # vector[[0, 1, 2, 3, 4]] if len(self._component_name_to_basis_component_length ) == 1: for ( component_name, _ ) in self._component_name_to_basis_component_length.items( ): break component_name_to_basis_component_length = OnlineSizeDict( ) component_name_to_basis_component_length[ component_name] = len(key) output._component_name_to_basis_component_length = component_name_to_basis_component_length else: raise NotImplementedError( "Vector.__getitem__ with list or tuple input arguments has not been implemented yet" + " for the case of multiple components") return output elif isinstance(key, int): # vector[5] output = self.content[key] assert isinstance(output, Number) return output else: raise TypeError("Unsupported key type in Vector.__getitem__")