예제 #1
0
    def sparse(self):
        '''
        Conversion of a FullTensor into a SparseTensor.

        Returns
        -------
        tensap.SparseTensor
            A SparseTensor representation of the FullTensor.

        '''
        dat = np.reshape(self.data, -1, order='F')
        ind = np.nonzero(dat)[0]
        indices = tensap.MultiIndices.ind2sub(self.shape, ind)
        return tensap.SparseTensor(dat, indices, self.shape)
예제 #2
0
    def sparse(self):
        '''
        Convert the DiagonalTensor to a tensap.SparseTensor.

        Returns
        -------
        tensap.SparseTensor
            The DiagonalTensor as a tensap.SparseTensor.

        '''
        ind = np.nonzero(self.data)[0]
        data = self.data[self.data != 0]
        indices = tensap.MultiIndices(
            np.tile(np.reshape(ind, [-1, 1]), (1, self.order)))
        return tensap.SparseTensor(data, indices, self.shape)
예제 #3
0
    def eval_on_tensor_grid(self, x):
        '''
        Evaluate the Function on a grid x.

        Parameters
        ----------
        x : tensap.TensorGrid
            The tensap.ensorGrid used for the evaluation..

        Raises
        ------
        NotImplementedError
            If the method is not implemented.
        ValueError
            If x is not a tensap.TensorGrid object.

        Returns
        -------
        fx : numpy.ndarray
            The evaluation of the Function on the grid.

        '''
        x_a = x.array()
        fx = self.eval(x_a)

        if isinstance(x, tensap.SparseTensorGrid):
            if np.all(self.output_shape == 1):
                fx = tensap.SparseTensor(fx, x.indices, x.shape)
            else:
                raise NotImplementedError('Method not implemented.')
        elif isinstance(x, tensap.FullTensorGrid):
            if np.all(self.output_shape == 1):
                shape = x.shape
                if self.dim > 1:
                    fx = np.reshape(fx, shape, order='F')
            else:
                shape = np.concatenate((np.atleast_1d(x.shape),
                                        np.atleast_1d(self.output_shape)))
                fx = np.reshape(fx, shape, order='F')
            fx = tensap.FullTensor(fx, np.size(shape), shape)
        else:
            raise ValueError('A TensorGrid object must be provided.')

        return fx