예제 #1
0
 def __setitem__(self, key, value):
     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_slice_and_int = isinstance(key[0], slice) and isinstance(key[1], int)
     key_is_tuple_of_int_and_slice = isinstance(key[0], int) and isinstance(key[1], slice)
     key_is_tuple_of_int = all([isinstance(key_i, int) for key_i in key])
     if key_is_tuple_of_slices:  # matrix[:5, :5]
         converted_key = wrapping.Slicer(
             *slice_to_array(self, key, self._component_name_to_basis_component_length,
                             self._component_name_to_basis_component_index))
         if isinstance(value, type(self)):
             value = value.content
         self.content[converted_key] = value
     elif key_is_tuple_of_slice_and_int:  # matrix[:5, 5]
         converted_key_0 = wrapping.Slicer(
             slice_to_array(self, key[0], self._component_name_to_basis_component_length[0],
                            self._component_name_to_basis_component_index[0]))
         converted_key = (converted_key_0, key[1])
         if isinstance(value, backend.Vector.Type()):
             value = value.content
         self.content[converted_key] = value
     elif key_is_tuple_of_int_and_slice:  # matrix[5, :5]
         converted_key_1 = wrapping.Slicer(
             slice_to_array(self, key[1], self._component_name_to_basis_component_length[1],
                            self._component_name_to_basis_component_index[1]))
         converted_key = (key[0], converted_key_1)
         if isinstance(value, backend.Vector.Type()):
             value = value.content
         self.content[converted_key] = value
     elif key_is_tuple_of_int:  # matrix[5, 5]
         self.content[key] = value
     else:
         raise TypeError("Unsupported key type in Matrix.__setitem__")
예제 #2
0
 def _prepare_trivial_precomputed_slice(self):
     empty_slice = slice(None)
     assert self._type in ("error_estimation_operators_11", "error_estimation_operators_21", "error_estimation_operators_22", "operators")
     if self._type == "error_estimation_operators_11":
         pass # nothing to be done (scalar content)
     elif self._type == "error_estimation_operators_21":
         assert "delayed_functions" in self._content
         assert len(self._content["delayed_functions"]) is 2
         assert "delayed_functions_shape" in self._content
         
         slice_ = slice_to_array(self._content["delayed_functions_shape"], empty_slice, self._content["delayed_functions_shape"]._component_name_to_basis_component_length, self._content["delayed_functions_shape"]._component_name_to_basis_component_index)
         self._precomputed_slices[slice_] = self
     elif self._type == "error_estimation_operators_22":
         assert "delayed_functions" in self._content
         assert len(self._content["delayed_functions"]) is 2
         assert "delayed_functions_shape" in self._content
         
         slice_ = slice_to_array(self._content["delayed_functions_shape"], (empty_slice, empty_slice), self._content["delayed_functions_shape"]._component_name_to_basis_component_length, self._content["delayed_functions_shape"]._component_name_to_basis_component_index)
         self._precomputed_slices[slice_] = self
     elif self._type == "operators":
         assert len(self._content["basis_functions"]) in (0, 1, 2)
         assert "basis_functions_shape" in self._content
         
         if len(self._content["basis_functions"]) is 0:
             pass # nothing to be done (scalar content)
         elif len(self._content["basis_functions"]) is 1:
             slice_ = slice_to_array(self._content["basis_functions_shape"], empty_slice, self._content["basis_functions_shape"]._component_name_to_basis_component_length, self._content["basis_functions_shape"]._component_name_to_basis_component_index)
             self._precomputed_slices[slice_] = self
         elif len(self._content["basis_functions"]) is 2:
             slices = slice_to_array(self._content["basis_functions_shape"], (empty_slice, empty_slice), self._content["basis_functions_shape"]._component_name_to_basis_component_length, self._content["basis_functions_shape"]._component_name_to_basis_component_index)
             self._precomputed_slices[slices] = self
         else:
             raise ValueError("Invalid length")
     else:
         raise ValueError("Invalid type")
예제 #3
0
 def __getitem__(self, key):
     assert self._type in ("error_estimation_operators_22", "operators")
     if self._type == "error_estimation_operators_22":
         assert len(key) is 2
         assert "delayed_functions" in self._content
         assert len(self._content["delayed_functions"]) is 2
         assert "delayed_functions_shape" in self._content
         
         slice_ = slice_to_array(self._content["delayed_functions_shape"], key, self._content["delayed_functions_shape"]._component_name_to_basis_component_length, self._content["delayed_functions_shape"]._component_name_to_basis_component_index)
         
         if slice_ in self._precomputed_slices:
             return self._precomputed_slices[slice_]
         else:
             output = NonAffineExpansionStorage.__new__(type(self), *self._shape)
             output.__init__(*self._shape)
             output._type = self._type
             output._content["inner_product_matrix"] = self._content["inner_product_matrix"]
             output._content["delayed_functions"] = [NonAffineExpansionStorageContent_Base(self._shape[0], dtype=object), NonAffineExpansionStorageContent_Base(self._shape[1], dtype=object)]
             for q in range(self._shape[0]):
                 output._content["delayed_functions"][0][q] = self._content["delayed_functions"][0][q][key[0]]
             for q in range(self._shape[1]):
                 output._content["delayed_functions"][1][q] = self._content["delayed_functions"][1][q][key[1]]
             output._content["delayed_functions_shape"] = DelayedTransposeShape((output._content["delayed_functions"][0][0], output._content["delayed_functions"][1][0]))
             self._precomputed_slices[slice_] = output
             return output
     elif self._type == "operators":
         assert len(key) is 2
         assert "basis_functions" in self._content
         assert len(self._content["basis_functions"]) is 2
         assert "basis_functions_shape" in self._content
         
         slices = slice_to_array(self._content["basis_functions_shape"], key, self._content["basis_functions_shape"]._component_name_to_basis_component_length, self._content["basis_functions_shape"]._component_name_to_basis_component_index)
         
         if slices in self._precomputed_slices:
             return self._precomputed_slices[slices]
         else:
             output = NonAffineExpansionStorage.__new__(type(self), *self._shape)
             output.__init__(*self._shape)
             output._type = self._type
             output._content["truth_operators"] = self._content["truth_operators"]
             output._content["truth_operators_as_expansion_storage"] = self._content["truth_operators_as_expansion_storage"]
             output._content["basis_functions"] = list()
             output._content["basis_functions"].append(self._content["basis_functions"][0][key[0]])
             output._content["basis_functions"].append(self._content["basis_functions"][1][key[1]])
             output._content["basis_functions_shape"] = DelayedTransposeShape(output._content["basis_functions"])
             self._precomputed_slices[slices] = output
             return output
     else:
         raise ValueError("Invalid type")
예제 #4
0
        def __getitem__(self, key):
            """
            return the subtensors of size "key" for every element in content. (e.g. submatrices [1:5,1:5] of the affine expansion of A)
            """
            it = AffineExpansionStorageContent_Iterator(
                self._content,
                flags=["multi_index", "refs_ok"],
                op_flags=["readonly"])
            slices = slice_to_array(
                self._content[it.multi_index], key,
                self._component_name_to_basis_component_length,
                self._component_name_to_basis_component_index)

            if slices in self._precomputed_slices:
                return self._precomputed_slices[slices]
            else:
                output = _AffineExpansionStorage.__new__(
                    type(self), *self._content.shape)
                output.__init__(*self._content.shape)
                while not it.finished:
                    # Slice content and assign
                    output[it.multi_index] = self._do_slicing(
                        self._content[it.multi_index], key)
                    # Increment
                    it.iternext()
                self._precomputed_slices[slices] = output
                return output
예제 #5
0
 def _prepare_trivial_precomputed_slice(self, item):
     empty_slice = slice(None)
     slices = slice_to_array(
         item.vector, empty_slice,
         self._component_name_to_basis_component_length,
         self._component_name_to_basis_component_index)
     self._precomputed_slices[slices] = self
예제 #6
0
 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__")
예제 #7
0
 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__")
예제 #8
0
파일: vector.py 프로젝트: nadia-el/RBniCS
 def __setitem__(self, key, value):
     if (
         isinstance(key, slice)  # vector[:5]
         or isinstance(key, (list, tuple))  # vector[[0, 1, 2, 3, 4]]
     ):
         if isinstance(key, slice):  # vector[:5]
             converted_key = wrapping.Slicer(
                 slice_to_array(self, key, self._component_name_to_basis_component_length,
                                self._component_name_to_basis_component_index))
         elif isinstance(key, (list, tuple)):  # vector[[0, 1, 2, 3, 4]]
             converted_key = wrapping.Slicer(key)
         if isinstance(value, type(self)):
             value = value.content
         self.content[converted_key] = value
     elif isinstance(key, int):  # vector[5]
         self.content[key] = value
     else:
         raise TypeError("Unsupported key type in Vector.__setitem__")