def get_array(self, indexed, args=None): """ Return an array of Indexed/EinsteinTerm objects. :arg sympy.Indexed indexed: A SymPy Indexed term. :arg args: A tuple of arguments to be provided to a function (e.g. the LeviCivita function). By default this is None. :returns: An array of Indexed/EinsteinTerm objects :rtype: sympy.MutableDenseNDimArray, if it has indices else Einstein Term will be returned """ if len(self.get_indices()) == 0: value = self value.is_commutative = True if args: array = IndexedBase('%s' % value)[args] else: array = value return array array = MutableDenseNDimArray.zeros(*indexed.shape) from_indices = indexed.indices for index in numpy.ndindex(*indexed.shape): index_map = self.map_indices(from_indices, index) value = self.get_expanded(index_map) value.is_commutative = True if args: # If this is a function such as the Levi-Civita function, then it will have arguments (e.g. x0, x1, t) which need to be included here. array[index] = IndexedBase('%s' % value)[args] else: array[index] = value return array
def get_array(self, indexed): """ Return an array of LeviCivita objects comprising the appropriate indices given in the user's equations. """ array = MutableDenseNDimArray.zeros(*indexed.shape) for index in numpy.ndindex(*indexed.shape): array[index[:]] = LeviCivita(*index) return array
def get_indexed(self, ndim, indexed, arrays, new_array_name): derivative_function = self.args[0] base = IndexedBase('%s' % derivative_function) evaluated, index_structure = evaluate_expression( derivative_function, arrays, indexed) if index_structure: derivative_structure = index_structure functions = [base[index_structure]] else: derivative_structure = [] functions = [base] for arg in self.args[1:]: derivative_structure = derivative_structure + list( indexed[arg].indices) functions.append(indexed[arg]) shape = [] for number, index in enumerate(derivative_structure): if isinstance(index, Idx): shape += [ndim] else: shape += [index] # Fill an array of the same shape as the derivative structure with the actual SymPy Derivative objects. derivative = MutableDenseNDimArray.zeros(*shape) for index in numpy.ndindex(*shape): index_map = self.split_index(index, functions) derivative[index] = self.apply_derivative(index_map, arrays, functions, evaluated) # Apply the contraction structure outer_indices = remove_repeated_index(derivative_structure) if derivative_structure: derivative = apply_contraction(outer_indices, derivative_structure, derivative) if outer_indices: new_outer_indices = [ outer for outer in outer_indices if outer != 1 ] if new_outer_indices == outer_indices: indexed_object_name = IndexedBase( new_array_name, shape=tuple([ndim for x in outer_indices ]))[tuple(outer_indices)] else: raise ValueError("Indices do not match: ", new_outer_indices, outer_indices) indexed_object_name.is_commutative = False indexed[self] = indexed_object_name arrays[indexed_object_name] = derivative else: indexed_object_name = EinsteinTerm(new_array_name) indexed[self] = indexed_object_name arrays[indexed_object_name] = derivative return arrays, indexed
def test_NDim_array_conv(): MD = MutableDenseNDimArray([x, y, z]) MS = MutableSparseNDimArray([x, y, z]) ID = ImmutableDenseNDimArray([x, y, z]) IS = ImmutableSparseNDimArray([x, y, z]) assert MD.as_immutable() == ID assert MD.as_mutable() == MD assert MS.as_immutable() == IS assert MS.as_mutable() == MS assert ID.as_immutable() == ID assert ID.as_mutable() == MD assert IS.as_immutable() == IS assert IS.as_mutable() == MS
def test_NDArray(): from sympy.tensor.array import ( MutableDenseNDimArray, ImmutableDenseNDimArray, MutableSparseNDimArray, ImmutableSparseNDimArray, ) example = MutableDenseNDimArray( [ [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]], [[13, 14, 15, 16], [17, 18, 19, 20], [21, 22, 23, 24]], ] ) assert ( mcode(example) == "{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, " "{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}}" ) example = ImmutableDenseNDimArray(example) assert ( mcode(example) == "{{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}, " "{{13, 14, 15, 16}, {17, 18, 19, 20}, {21, 22, 23, 24}}}" ) example = MutableSparseNDimArray(example) assert ( mcode(example) == "SparseArray[{" "{1, 1, 1} -> 1, {1, 1, 2} -> 2, {1, 1, 3} -> 3, " "{1, 1, 4} -> 4, {1, 2, 1} -> 5, {1, 2, 2} -> 6, " "{1, 2, 3} -> 7, {1, 2, 4} -> 8, {1, 3, 1} -> 9, " "{1, 3, 2} -> 10, {1, 3, 3} -> 11, {1, 3, 4} -> 12, " "{2, 1, 1} -> 13, {2, 1, 2} -> 14, {2, 1, 3} -> 15, " "{2, 1, 4} -> 16, {2, 2, 1} -> 17, {2, 2, 2} -> 18, " "{2, 2, 3} -> 19, {2, 2, 4} -> 20, {2, 3, 1} -> 21, " "{2, 3, 2} -> 22, {2, 3, 3} -> 23, {2, 3, 4} -> 24" "}, {2, 3, 4}]" ) example = ImmutableSparseNDimArray(example) assert ( mcode(example) == "SparseArray[{" "{1, 1, 1} -> 1, {1, 1, 2} -> 2, {1, 1, 3} -> 3, " "{1, 1, 4} -> 4, {1, 2, 1} -> 5, {1, 2, 2} -> 6, " "{1, 2, 3} -> 7, {1, 2, 4} -> 8, {1, 3, 1} -> 9, " "{1, 3, 2} -> 10, {1, 3, 3} -> 11, {1, 3, 4} -> 12, " "{2, 1, 1} -> 13, {2, 1, 2} -> 14, {2, 1, 3} -> 15, " "{2, 1, 4} -> 16, {2, 2, 1} -> 17, {2, 2, 2} -> 18, " "{2, 2, 3} -> 19, {2, 2, 4} -> 20, {2, 3, 1} -> 21, " "{2, 3, 2} -> 22, {2, 3, 3} -> 23, {2, 3, 4} -> 24" "}, {2, 3, 4}]" )
def init_equation(self): """ Create storage arrays for equations. The equation name starts with an underscore `_` followed by the variable name. Included equations are defined in `self._algeb_int`, `self._algeb_intf`, and `self._state_int`. Returns ------- None """ # NOTE: this function is not being used. algeb_state_list = self._algeb_int + self._algeb_intf + self._state_int if len(algeb_state_list) == 0: return logger.debug( f'\n--> {self.__class__.__name__}: Entering _init_equation()') for item in (self._algeb_int + self._algeb_intf + self._state_int): eq_name = f'_{item}' # equation names starts with "_" and follows with the corresponding var name self.__dict__[eq_name] = MutableDenseNDimArray([0] * self.n) logger.debug(self.__dict__[eq_name])
def _array_subs(arr, var, val): arr = MutableDenseNDimArray(arr) for i in range(len(arr)): index = arr._get_tuple_index(i) arr[index] = arr[index].subs(var, val) return arr.tolist()