Exemple #1
0
    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
Exemple #2
0
    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
Exemple #3
0
    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