コード例 #1
0
ファイル: discretization.py プロジェクト: LarsWestergren/odl
    def __init__(self, uspace, dspace, restr=None, ext=None, **kwargs):
        """Abstract initialization method.

        Intended to be called by subclasses for proper type checking
        and setting of attributes.

        Parameters
        ----------
        uspace : :class:`~odl.LinearSpace`
            The (abstract) space to be discretized
        dspace : :class:`~odl.space.base_ntuples.FnBase`
            Data space providing containers for the values of a
            discretized object. Its
            :attr:`~odl.space.base_ntuples.FnBase.field` attribute
            must be the same as ``uspace.field``.
        restr : :class:`~odl.Operator`, linear, optional
            Operator mapping a :attr:`RawDiscretization.uspace` element
            to a :attr:`RawDiscretization.dspace` element. Must satisfy
            ``restr.domain == uspace``, ``restr.range == dspace``
        ext : :class:`~odl.Operator`, linear, optional
            Operator mapping a :attr:`RawDiscretization.dspace` element
            to a :attr:`RawDiscretization.uspace` element. Must satisfy
            ``ext.domain == dspace``, ``ext.range == uspace``.
        """
        super().__init__(uspace, dspace, restr, ext, **kwargs)
        FnBase.__init__(self, dspace.size, dspace.dtype)

        if not isinstance(uspace, LinearSpace):
            raise TypeError('undiscretized space {} not a `LinearSpace` '
                            'instance.'.format(uspace))

        if not isinstance(dspace, FnBase):
            raise TypeError('data space {} not an `FnBase` instance.'
                            ''.format(dspace))

        if uspace.field != dspace.field:
            raise ValueError('fields {} and {} of the undiscretized and '
                             'data spaces, resp., are not equal.'
                             ''.format(uspace.field, dspace.field))

        if restr is not None:
            if not isinstance(restr, Operator):
                raise TypeError('restriction operator {} is not a '
                                '`Operator` instance.'.format(restr))

            if not restr.is_linear:
                raise TypeError('restriction operator {} is not '
                                'linear'.format(restr))

        if ext is not None:
            if not isinstance(ext, Operator):
                raise TypeError('extension operator {} is not a '
                                '`Operator` instance.'.format(ext))

            if not ext.is_linear:
                raise TypeError('extension operator {} is not '
                                'linear'.format(ext))
コード例 #2
0
ファイル: discretization.py プロジェクト: NikEfth/odl
    def __init__(self, uspace, dspace, sampling=None, interpol=None):
        """Abstract initialization method.

        Intended to be called by subclasses for proper type checking
        and setting of attributes.

        Parameters
        ----------
        uspace : `LinearSpace`
            The (abstract) space to be discretized
        dspace : `FnBase`
            Data space providing containers for the values of a
            discretized object. Its `FnBase.field` attribute
            must be the same as ``uspace.field``.
        sampling : `Operator`, linear, optional
            Operator mapping a `RawDiscretization.uspace` element
            to a `RawDiscretization.dspace` element. Must satisfy
            ``sampling.domain == uspace``, ``sampling.range == dspace``
        interpol : `Operator`, linear, optional
            Operator mapping a `RawDiscretization.dspace` element
            to a `RawDiscretization.uspace` element. Must satisfy
            ``interpol.domain == dspace``, ``interpol.range == uspace``.
        """
        RawDiscretization.__init__(self, uspace, dspace, sampling, interpol)
        FnBase.__init__(self, dspace.size, dspace.dtype)

        if not isinstance(uspace, LinearSpace):
            raise TypeError('undiscretized space {!r} not a LinearSpace '
                            'instance.'.format(uspace))

        if not isinstance(dspace, FnBase):
            raise TypeError('data space {!r} not an FnBase instance.'
                            ''.format(dspace))

        if uspace.field != dspace.field:
            raise ValueError('fields {} and {} of the undiscretized and '
                             'data spaces, resp., are not equal.'
                             ''.format(uspace.field, dspace.field))

        if sampling is not None:
            if not isinstance(sampling, Operator):
                raise TypeError('sampling operator {!r} is not an '
                                'Operator instance.'.format(sampling))

            if not sampling.is_linear:
                raise TypeError('sampling operator {!r} is not '
                                'linear'.format(sampling))

        if interpol is not None:
            if not isinstance(interpol, Operator):
                raise TypeError('interpolation operator {!r} is not an '
                                'Operator instance.'.format(interpol))

            if not interpol.is_linear:
                raise TypeError('interpolation operator {!r} is not '
                                'linear'.format(interpol))
コード例 #3
0
ファイル: discretization.py プロジェクト: rajmund/odl
    def __init__(self, uspace, dspace, sampling=None, interpol=None):
        """Abstract initialization method.

        Intended to be called by subclasses for proper type checking
        and setting of attributes.

        Parameters
        ----------
        uspace : `LinearSpace`
            The (abstract) space to be discretized
        dspace : `FnBase`
            Data space providing containers for the values of a
            discretized object. Its `FnBase.field` attribute
            must be the same as ``uspace.field``.
        sampling : `Operator`, linear, optional
            Operator mapping a `DiscretizedSet.uspace` element
            to a `DiscretizedSet.dspace` element. Must satisfy
            ``sampling.domain == uspace``, ``sampling.range == dspace``
        interpol : `Operator`, linear, optional
            Operator mapping a `DiscretizedSet.dspace` element
            to a `DiscretizedSet.uspace` element. Must satisfy
            ``interpol.domain == dspace``, ``interpol.range == uspace``.
        """
        DiscretizedSet.__init__(self, uspace, dspace, sampling, interpol)
        FnBase.__init__(self, dspace.size, dspace.dtype)

        if not isinstance(uspace, LinearSpace):
            raise TypeError('`uspace` {!r} not a LinearSpace '
                            'instance'.format(uspace))

        if not isinstance(dspace, FnBase):
            raise TypeError('`dspace` {!r} not an FnBase instance'
                            ''.format(dspace))

        if uspace.field != dspace.field:
            raise ValueError('fields {} and {} of the undiscretized and '
                             'data spaces, resp., are not equal'
                             ''.format(uspace.field, dspace.field))

        if sampling is not None and not sampling.is_linear:
            raise TypeError('`sampling` {!r} is not linear'
                            ''.format(sampling))

        if interpol is not None and not interpol.is_linear:
            raise TypeError('`interpol` {!r} is not linear'
                            ''.format(interpol))
コード例 #4
0
ファイル: simple_rn.py プロジェクト: odlgroup/odl
 def __init__(self, size):
     FnBase.__init__(self, size, np.float)
コード例 #5
0
 def __init__(self, size):
     FnBase.__init__(self, size, np.float)
コード例 #6
0
ファイル: cu_ntuples.py プロジェクト: odlgroup/odlcuda
    def __init__(self, size, dtype='float32', **kwargs):
        """Initialize a new instance.

        Parameters
        ----------
        size : positive `int`
            The number of dimensions of the space
        dtype : `object`
            The data type of the storage array. Can be provided in any
            way the `numpy.dtype` function understands, most notably
            as built-in type, as one of NumPy's internal datatype
            objects or as string.

            Only scalar data types are allowed.

        weight : optional
            Use weighted inner product, norm, and dist. The following
            types are supported as ``weight``:

            `FnWeightingBase` :
            Use this weighting as-is. Compatibility with this
            space's elements is not checked during init.

            `float` :
            Weighting by a constant

            `array-like` :
            Weighting by a vector (1-dim. array, corresponds to
            a diagonal matrix). Note that the array is stored in
            main memory, which results in slower space functions
            due to a copy during evaluation.

            `CudaFnVector` :
            same as 1-dim. array-like, except that copying is
            avoided if the ``dtype`` of the vector is the
            same as this space's ``dtype``.

            Default: no weighting

            This option cannot be combined with ``dist``, ``norm``
            or ``inner``.

        exponent : positive `float`, optional
            Exponent of the norm. For values other than 2.0, no
            inner product is defined.

            This option is ignored if ``dist``, ``norm`` or
            ``inner`` is given.

            Default: 2.0

        dist : `callable`, optional
            The distance function defining a metric on the space.
            It must accept two `FnVector` arguments and
            fulfill the following mathematical conditions for any
            three vectors ``x, y, z``:

            - ``dist(x, y) >= 0``
            - ``dist(x, y) = 0``  if and only if  ``x = y``
            - ``dist(x, y) = dist(y, x)``
            - ``dist(x, y) <= dist(x, z) + dist(z, y)``

            This option cannot be combined with ``weight``,
            ``norm`` or ``inner``.

        norm : `callable`, optional
            The norm implementation. It must accept an
            `FnVector` argument, return a `float` and satisfy the
            following conditions for all vectors ``x, y`` and scalars
            ``s``:

            - ``||x|| >= 0``
            - ``||x|| = 0``  if and only if  ``x = 0``
            - ``||s * x|| = |s| * ||x||``
            - ``||x + y|| <= ||x|| + ||y||``

            By default, ``norm(x)`` is calculated as ``inner(x, x)``.

            This option cannot be combined with ``weight``,
            ``dist`` or ``inner``.

        inner : `callable`, optional
            The inner product implementation. It must accept two
            `FnVector` arguments, return a element from
            the field of the space (real or complex number) and
            satisfy the following conditions for all vectors
            ``x, y, z`` and scalars ``s``:

            - ``<x, y> = conj(<y, x>)``
            - ``<s*x + y, z> = s * <x, z> + <y, z>``
            - ``<x, x> = 0``  if and only if  ``x = 0``

            This option cannot be combined with ``weight``,
            ``dist`` or ``norm``.
        """
        FnBase.__init__(self, size, dtype)
        CudaNtuples.__init__(self, size, dtype)

        dist = kwargs.pop('dist', None)
        norm = kwargs.pop('norm', None)
        inner = kwargs.pop('inner', None)
        weight = kwargs.pop('weight', None)
        exponent = kwargs.pop('exponent', 2.0)

        # Check validity of option combination (3 or 4 out of 4 must be None)
        if sum(x is None for x in (dist, norm, inner, weight)) < 3:
            raise ValueError('invalid combination of options `weight`, '
                             '`dist`, `norm` and `inner`')
        if weight is not None:
            if isinstance(weight, WeightingBase):
                self._weighting = weight
            elif np.isscalar(weight):
                self._weighting = CudaFnConstWeighting(
                    weight, exponent=exponent)
            elif isinstance(weight, CudaFnVector):
                self._weighting = CudaFnVectorWeighting(
                    weight, exponent=exponent)
            else:
                # Must make a CudaFnVector from the array
                weight = self.element(np.asarray(weight))
                if weight.ndim == 1:
                    self._weighting = CudaFnVectorWeighting(
                        weight, exponent=exponent)
                else:
                    raise ValueError('invalid weight argument {!r}'
                                     ''.format(weight))
        elif dist is not None:
            self._weighting = CudaFnCustomDist(dist)
        elif norm is not None:
            self._weighting = CudaFnCustomNorm(norm)
        elif inner is not None:
            # Use fast dist implementation
            self._weighting = CudaFnCustomInnerProduct(
                inner, dist_using_inner=True)
        else:  # all None -> no weighing
            self._weighting = CudaFnNoWeighting(exponent)
コード例 #7
0
    def __init__(self, size, dtype='float32', **kwargs):
        """Initialize a new instance.

        Parameters
        ----------
        size : positive `int`
            The number of dimensions of the space
        dtype : `object`
            The data type of the storage array. Can be provided in any
            way the `numpy.dtype` function understands, most notably
            as built-in type, as one of NumPy's internal datatype
            objects or as string.

            Only scalar data types are allowed.

        weighting : optional
            Use weighted inner product, norm, and dist. The following
            types are supported as ``weight``:

            `FnWeightingBase` :
            Use this weighting as-is. Compatibility with this
            space's elements is not checked during init.

            `float` :
            Weighting by a constant

            `array-like` :
            Weighting by a vector (1-dim. array, corresponds to
            a diagonal matrix). Note that the array is stored in
            main memory, which results in slower space functions
            due to a copy during evaluation.

            `CudaFnVector` :
            same as 1-dim. array-like, except that copying is
            avoided if the ``dtype`` of the vector is the
            same as this space's ``dtype``.

            Default: no weighting

            This option cannot be combined with ``dist``, ``norm``
            or ``inner``.

        exponent : positive `float`, optional
            Exponent of the norm. For values other than 2.0, no
            inner product is defined.

            This option is ignored if ``dist``, ``norm`` or
            ``inner`` is given.

            Default: 2.0

        dist : `callable`, optional
            The distance function defining a metric on the space.
            It must accept two `FnVector` arguments and
            fulfill the following mathematical conditions for any
            three vectors ``x, y, z``:

            - ``dist(x, y) >= 0``
            - ``dist(x, y) = 0``  if and only if  ``x = y``
            - ``dist(x, y) = dist(y, x)``
            - ``dist(x, y) <= dist(x, z) + dist(z, y)``

            This option cannot be combined with ``weight``,
            ``norm`` or ``inner``.

        norm : `callable`, optional
            The norm implementation. It must accept an
            `FnVector` argument, return a `float` and satisfy the
            following conditions for all vectors ``x, y`` and scalars
            ``s``:

            - ``||x|| >= 0``
            - ``||x|| = 0``  if and only if  ``x = 0``
            - ``||s * x|| = |s| * ||x||``
            - ``||x + y|| <= ||x|| + ||y||``

            By default, ``norm(x)`` is calculated as ``inner(x, x)``.

            This option cannot be combined with ``weight``,
            ``dist`` or ``inner``.

        inner : `callable`, optional
            The inner product implementation. It must accept two
            `FnVector` arguments, return a element from
            the field of the space (real or complex number) and
            satisfy the following conditions for all vectors
            ``x, y, z`` and scalars ``s``:

            - ``<x, y> = conj(<y, x>)``
            - ``<s*x + y, z> = s * <x, z> + <y, z>``
            - ``<x, x> = 0``  if and only if  ``x = 0``

            This option cannot be combined with ``weight``,
            ``dist`` or ``norm``.
        """
        FnBase.__init__(self, size, dtype)
        CudaNtuples.__init__(self, size, dtype)

        dist = kwargs.pop('dist', None)
        norm = kwargs.pop('norm', None)
        inner = kwargs.pop('inner', None)
        weighting = kwargs.pop('weighting', None)
        exponent = kwargs.pop('exponent', 2.0)

        # Check validity of option combination (3 or 4 out of 4 must be None)
        if sum(x is None for x in (dist, norm, inner, weighting)) < 3:
            raise ValueError('invalid combination of options `weight`, '
                             '`dist`, `norm` and `inner`')
        if weighting is not None:
            if isinstance(weighting, Weighting):
                self.__weighting = weighting
            elif np.isscalar(weighting):
                self.__weighting = CudaFnConstWeighting(weighting,
                                                        exponent=exponent)
            elif isinstance(weighting, CudaFnVector):
                self.__weighting = CudaFnArrayWeighting(weighting,
                                                        exponent=exponent)
            else:
                # Must make a CudaFnVector from the array
                weighting = self.element(np.asarray(weighting))
                if weighting.ndim == 1:
                    self.__weighting = CudaFnArrayWeighting(weighting,
                                                            exponent=exponent)
                else:
                    raise ValueError('invalid weighting argument {!r}'
                                     ''.format(weighting))
        elif dist is not None:
            self.__weighting = CudaFnCustomDist(dist)
        elif norm is not None:
            self.__weighting = CudaFnCustomNorm(norm)
        elif inner is not None:
            # Use fast dist implementation
            self.__weighting = CudaFnCustomInner(inner, dist_using_inner=True)
        else:  # all None -> no weighing
            self.__weighting = CudaFnNoWeighting(exponent)