Exemple #1
0
    def __init__(self, alphas, control_points, discr_space, ft_kernel):
        """Initialize a new instance.

        Parameters
        ----------
        alphas : `ProductSpaceElement`
            Displacement parameters in which the derivative is evaluated
        control_points : `TensorGrid` or `array-like`
            The points ``x_j`` controlling the deformation. They can
            be given either as a tensor grid or as a point array. In
            the latter case, its shape must be ``(N, n)``, where
            ``n`` is the dimension of the template space, and ``N``
            the number of ``alpha_j``, i.e. the size of (each
            component of) ``par_space``.
        discr_space : `DiscreteSpace`
            Space of the image grid of the template.
        kernel : `callable`
            Function to determine the kernel at the control points ``K(y_j)``
            The function must accept a real variable and return a real number.
        """

        super().__init__(alphas, control_points, discr_space, ft_kernel)

        # Switch domain and range
        self.discr_space = discr_space
        self.range_space = ProductSpace(self.discr_space,
                                        self.discr_space.ndim)
        Operator.__init__(self, self.range_space, alphas.space, linear=True)
Exemple #2
0
    def __init__(self, space):
        """Operator that extracts the imaginary part of a vector.

        Parameters
        ----------
        space : `FnBase`
            Space which imaginary part should be taken, needs to implement
            ``space.real_space``.

        Examples
        --------
        Take the imaginary part of complex vector:

        >>> c3 = odl.cn(3)
        >>> op = ImagPart(c3)
        >>> op([1 + 2j, 2, 3 - 1j])
        rn(3).element([2.0, 0.0, -1.0])

        The operator is the zero operator on real spaces:

        >>> r3 = odl.rn(3)
        >>> op = ImagPart(r3)
        >>> op([1, 2, 3])
        rn(3).element([0.0, 0.0, 0.0])
        """
        real_space = space.real_space
        linear = (space == real_space)
        Operator.__init__(self, space, real_space, linear=linear)
Exemple #3
0
    def __init__(self, space):
        """Operator that extracts the imaginary part of a vector.

        Parameters
        ----------
        space : `FnBase`
            Space which imaginary part should be taken, needs to implement
            ``space.real_space``.

        Examples
        --------
        Take the imaginary part of complex vector:

        >>> c3 = odl.cn(3)
        >>> op = ImagPart(c3)
        >>> op([1 + 2j, 2, 3 - 1j])
        rn(3).element([2.0, 0.0, -1.0])

        The operator is the zero operator on real spaces:

        >>> r3 = odl.rn(3)
        >>> op = ImagPart(r3)
        >>> op([1, 2, 3])
        rn(3).element([0.0, 0.0, 0.0])
        """
        real_space = space.real_space
        linear = (space == real_space)
        Operator.__init__(self, space, real_space, linear=linear)
Exemple #4
0
    def __init__(self, space, linear=False, grad_lipschitz=np.nan):
        """Initialize a new instance.

        Parameters
        ----------
        space : `LinearSpace`
            The domain of this functional, i.e., the set of elements to
            which this functional can be applied.
        linear : bool, optional
            If `True`, the functional is considered as linear.
        grad_lipschitz : float, optional
            The Lipschitz constant of the gradient. Default: ``nan``
        """
        Operator.__init__(self, domain=space, range=space.field, linear=linear)
        self.__grad_lipschitz = float(grad_lipschitz)
Exemple #5
0
    def __init__(self, space, linear=False, grad_lipschitz=np.nan):
        """Initialize a new instance.

        Parameters
        ----------
        space : `LinearSpace`
            The domain of this functional, i.e., the set of elements to
            which this functional can be applied.
        linear : bool, optional
            If `True`, the functional is considered as linear.
        grad_lipschitz : float, optional
            The Lipschitz constant of the gradient. Default: ``nan``
        """
        Operator.__init__(self, domain=space,
                          range=space.field, linear=linear)
        self.__grad_lipschitz = float(grad_lipschitz)
Exemple #6
0
    def __init__(self, space, linear=False, grad_lipschitz=np.nan):
        """Initialize a new instance.

        Parameters
        ----------
        space : `LinearSpace`
            The domain of this functional, i.e., the set of elements to
            which this functional can be applied.
        linear : bool, optional
            If `True`, the functional is considered as linear.
        grad_lipschitz : float, optional
            The Lipschitz constant of the gradient. Default: ``nan``
        """
        # Cannot use `super(Functional, self)` here since that breaks
        # subclasses with multiple inheritance (at least those where both
        # parents implement `__init__`, e.g., in `ScalingFunctional`)
        Operator.__init__(self, domain=space, range=space.field, linear=linear)
        self.__grad_lipschitz = float(grad_lipschitz)
Exemple #7
0
    def __init__(self, space, scalar=1):
        """Initialize a new instance.

        Parameters
        ----------
        space : `FnBase`
            Space which real part should be taken, needs to implement
            ``space.complex_space``.
        scalar : ``space.complex_space.field`` element, optional
            Scalar which the incomming vectors should be multiplied by in order
            to get the complex vector.

        Examples
        --------
        Embed real vector into complex space:

        >>> r3 = odl.rn(3)
        >>> op = ComplexEmbedding(r3)
        >>> op([1, 2, 3])
        cn(3).element([(1+0j), (2+0j), (3+0j)])

        Embed real vector as imaginary part into complex space:

        >>> op = ComplexEmbedding(r3, scalar=1j)
        >>> op([1, 2, 3])
        cn(3).element([1j, 2j, 3j])

        On complex spaces the operator is the same as simple multiplication by
        scalar:

        >>> c3 = odl.cn(3)
        >>> op = ComplexEmbedding(c3, scalar=1 + 2j)
        >>> op([1 + 1j, 2 + 2j, 3 + 3j])
        cn(3).element([(-1+3j), (-2+6j), (-3+9j)])
        """
        complex_space = space.complex_space
        self.scalar = complex_space.field.element(scalar)
        Operator.__init__(self, space, complex_space, linear=True)
Exemple #8
0
    def __init__(self, space, scalar=1):
        """Initialize a new instance.

        Parameters
        ----------
        space : `FnBase`
            Space which real part should be taken, needs to implement
            ``space.complex_space``.
        scalar : ``space.complex_space.field`` element, optional
            Scalar which the incomming vectors should be multiplied by in order
            to get the complex vector.

        Examples
        --------
        Embed real vector into complex space:

        >>> r3 = odl.rn(3)
        >>> op = ComplexEmbedding(r3)
        >>> op([1, 2, 3])
        cn(3).element([(1+0j), (2+0j), (3+0j)])

        Embed real vector as imaginary part into complex space:

        >>> op = ComplexEmbedding(r3, scalar=1j)
        >>> op([1, 2, 3])
        cn(3).element([1j, 2j, 3j])

        On complex spaces the operator is the same as simple multiplication by
        scalar:

        >>> c3 = odl.cn(3)
        >>> op = ComplexEmbedding(c3, scalar=1 + 2j)
        >>> op([1 + 1j, 2 + 2j, 3 + 3j])
        cn(3).element([(-1+3j), (-2+6j), (-3+9j)])
        """
        complex_space = space.complex_space
        self.scalar = complex_space.field.element(scalar)
        Operator.__init__(self, space, complex_space, linear=True)
Exemple #9
0
    def __init__(self, space):
        """Initialize a new instance.

        Parameters
        ----------
        space : `FnBase`
            Space which real part should be taken, needs to implement
            ``space.real_space``.

        Examples
        --------
        Take the real part of complex vector:

        >>> c3 = odl.cn(3)
        >>> op = RealPart(c3)
        >>> op([1 + 2j, 2, 3 - 1j])
        rn(3).element([1.0, 2.0, 3.0])

        The operator is the identity on real spaces:

        >>> r3 = odl.rn(3)
        >>> op = RealPart(r3)
        >>> op([1, 2, 3])
        rn(3).element([1.0, 2.0, 3.0])

        The operator also works on other `FnBase` spaces such as
        `DiscreteLp` spaces:

        >>> r3 = odl.uniform_discr(0, 1, 3, dtype=complex)
        >>> op = RealPart(r3)
        >>> op([1, 2, 3])
        uniform_discr(0.0, 1.0, 3).element([1.0, 2.0, 3.0])
        """
        real_space = space.real_space
        linear = (space == real_space)
        Operator.__init__(self, space, real_space, linear=linear)
Exemple #10
0
    def __init__(self, space):
        """Initialize a new instance.

        Parameters
        ----------
        space : `FnBase`
            Space which real part should be taken, needs to implement
            ``space.real_space``.

        Examples
        --------
        Take the real part of complex vector:

        >>> c2 = odl.cn(2)
        >>> op = odl.ComplexModulus(c2)
        >>> op([3 + 4j, 2])
        rn(2).element([5.0, 2.0])

        The operator is the absolute value on real spaces:

        >>> r2 = odl.rn(2)
        >>> op = odl.ComplexModulus(r2)
        >>> op([1, -2])
        rn(2).element([1.0, 2.0])

        The operator also works on other `FnBase` spaces such as
        `DiscreteLp` spaces:

        >>> r2 = odl.uniform_discr(0, 1, 2, dtype=complex)
        >>> op = odl.ComplexModulus(r2)
        >>> op([3 + 4j, 2])
        uniform_discr(0.0, 1.0, 2).element([5.0, 2.0])
        """
        real_space = space.real_space
        linear = (space == real_space)
        Operator.__init__(self, space, real_space, linear=linear)
Exemple #11
0
    def __init__(self, space):
        """Initialize a new instance.

        Parameters
        ----------
        space : `FnBase`
            Space which real part should be taken, needs to implement
            ``space.real_space``.

        Examples
        --------
        Take the real part of complex vector:

        >>> c3 = odl.cn(3)
        >>> op = RealPart(c3)
        >>> op([1 + 2j, 2, 3 - 1j])
        rn(3).element([1.0, 2.0, 3.0])

        The operator is the identity on real spaces:

        >>> r3 = odl.rn(3)
        >>> op = RealPart(r3)
        >>> op([1, 2, 3])
        rn(3).element([1.0, 2.0, 3.0])

        The operator also works on other `FnBase` spaces such as
        `DiscreteLp` spaces:

        >>> r3 = odl.uniform_discr(0, 1, 3, dtype=complex)
        >>> op = RealPart(r3)
        >>> op([1, 2, 3])
        uniform_discr(0.0, 1.0, 3).element([1.0, 2.0, 3.0])
        """
        real_space = space.real_space
        linear = (space == real_space)
        Operator.__init__(self, space, real_space, linear=linear)
Exemple #12
0
    def __init__(self, map_type, fset, partition, dspace, linear=False,
                 **kwargs):
        """Initialize a new instance.

        Parameters
        ----------
        map_type : {'sampling', 'interpolation'}
            The type of operator
        fset : `FunctionSet`
            The non-discretized (abstract) set of functions to be
            discretized
        partition : `RectPartition`
            Partition of (a subset of) ``fset.domain`` based on a
            `TensorGrid`
        dspace : `NtuplesBase`
            Data space providing containers for the values of a
            discretized object. Its `NtuplesBase.size` must be equal
            to the total number of grid points.
        linear : bool
            Create a linear operator if `True`, otherwise a non-linear
            operator.
        order : {'C', 'F'}, optional
            Ordering of the axes in the data storage. 'C' means the
            first axis varies slowest, the last axis fastest;
            vice versa for 'F'.
            Default: 'C'
        """
        map_type_ = str(map_type).lower()
        if map_type_ not in ('sampling', 'interpolation'):
            raise ValueError('mapping type {!r} not understood.'
                             ''.format(map_type))
        if not isinstance(fset, FunctionSet):
            raise TypeError('function set {!r} is not a `FunctionSet` '
                            'instance.'.format(fset))

        if not isinstance(partition, RectPartition):
            raise TypeError('grid {!r} is not a `TensorGrid` instance.'
                            ''.format(partition))
        if not isinstance(dspace, NtuplesBase):
            raise TypeError('data space {!r} is not an `NtuplesBase` instance.'
                            ''.format(dspace))

        if not fset.domain.contains_set(partition):
            raise ValueError('{} not contained in the domain {} '
                             'of the function set {}.'
                             ''.format(partition, fset.domain, fset))

        if dspace.size != partition.size:
            raise ValueError('size {} of the data space {} not equal '
                             'to the size {} of the partition.'
                             ''.format(dspace.size, dspace, partition.size))

        dom = fset if map_type_ == 'sampling' else dspace
        ran = dspace if map_type_ == 'sampling' else fset
        Operator.__init__(self, dom, ran, linear=linear)
        self._partition = partition

        if self.is_linear:
            if not isinstance(fset, FunctionSpace):
                raise TypeError('function space {!r} is not a `FunctionSpace` '
                                'instance.'.format(fset))
            if not isinstance(dspace, FnBase):
                raise TypeError('data space {!r} is not an `FnBase` instance.'
                                ''.format(dspace))
            if fset.field != dspace.field:
                raise ValueError('field {} of the function space and field '
                                 '{} of the data space are not equal.'
                                 ''.format(fset.field, dspace.field))

        order = str(kwargs.pop('order', 'C'))
        if str(order).upper() not in ('C', 'F'):
            raise ValueError('order {!r} not recognized.'.format(order))
        else:
            self._order = str(order).upper()
Exemple #13
0
 def __init__(self, space, index):
     """ Initialize and instance."""
     self.index = int(index)
     Operator.__init__(self, space, space.field, True)
Exemple #14
0
    def __init__(self, map_type, fset, grid, dspace, order="C", linear=False):
        """Initialize a new instance.

        Parameters
        ----------
        map_type : {'restriction', 'extension'}
            The type of operator
        fset : `FunctionSet`
            The undiscretized (abstract) set of functions to be
            discretized
        grid :  `TensorGrid`
            The grid on which to evaluate. Must be contained in
            the common domain of the function set.
        dspace : `NtuplesBase`
            Data space providing containers for the values of a
            discretized object. Its `NtuplesBase.size` must be equal
            to the total number of grid points.
        order : {'C', 'F'}, optional
            Ordering of the values in the flat data arrays. 'C'
            means the first grid axis varies slowest, the last fastest,
            'F' vice versa.
        linear : bool
            Create a linear operator if `True`, otherwise a non-linear
            operator.
        """
        map_type_ = str(map_type).lower()
        if map_type_ not in ("restriction", "extension"):
            raise ValueError("mapping type {!r} not understood." "".format(map_type))
        if not isinstance(fset, FunctionSet):
            raise TypeError("function set {!r} is not a `FunctionSet` " "instance.".format(fset))

        if not isinstance(grid, TensorGrid):
            raise TypeError("grid {!r} is not a `TensorGrid` instance." "".format(grid))
        if not isinstance(dspace, NtuplesBase):
            raise TypeError("data space {!r} is not an `NtuplesBase` instance." "".format(dspace))

        # TODO: this method is expected to exist, which is the case for
        # interval products. It could be a general optional `Set` method
        if not fset.domain.contains_set(grid):
            raise ValueError(
                "grid {} not contained in the domain {} of the " "function set {}.".format(grid, fset.domain, fset)
            )

        if dspace.size != grid.ntotal:
            raise ValueError(
                "size {} of the data space {} not equal "
                "to the total number {} of grid points."
                "".format(dspace.size, dspace, grid.ntotal)
            )

        self._order = str(order).upper()
        if self.order not in ("C", "F"):
            raise ValueError("ordering {!r} not understood.".format(order))

        dom = fset if map_type_ == "restriction" else dspace
        ran = dspace if map_type_ == "restriction" else fset
        Operator.__init__(self, dom, ran, linear=linear)
        self._grid = grid

        if self.is_linear:
            if not isinstance(fset, FunctionSpace):
                raise TypeError("function space {!r} is not a `FunctionSpace` " "instance.".format(fset))
            if not isinstance(dspace, FnBase):
                raise TypeError("data space {!r} is not an `FnBase` instance." "".format(dspace))
            if fset.field != dspace.field:
                raise ValueError(
                    "field {} of the function space and field "
                    "{} of the data space are not equal."
                    "".format(fset.field, dspace.field)
                )