Skip to content

Commit

Permalink
ENH: support exponent -inf and harmonize norm defs
Browse files Browse the repository at this point in the history
  • Loading branch information
kohr-h committed Jun 30, 2018
1 parent 12fd9c1 commit 90671c1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 63 deletions.
8 changes: 5 additions & 3 deletions odl/discr/lp_discr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1920,9 +1920,11 @@ def uniform_discr_frompartition(partition, dtype=None, impl='numpy', **kwargs):

weighting = kwargs.pop('weighting', None)
exponent = kwargs.pop('exponent', 2.0)
if (weighting is None and
is_numeric_dtype(dtype) and
exponent != float('inf')):
if (
weighting is None and
is_numeric_dtype(dtype) and
exponent not in (float('inf'), -float('inf'))
):
weighting = np.concatenate([np.ones(len(fspace.shape_out)),
partition.cell_sides])

Expand Down
115 changes: 55 additions & 60 deletions odl/space/npy_tensors.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class NumpyTensorSpace(TensorSpace):
"""

def __init__(self, shape, dtype=None, **kwargs):
"""Initialize a new instance.
r"""Initialize a new instance.
Parameters
----------
Expand Down Expand Up @@ -165,39 +165,39 @@ def __init__(self, shape, dtype=None, **kwargs):
-----
- A distance function or metric on a space :math:`X`
is a mapping
:math:`d:X \\times X \\to \mathbb{R}`
:math:`d:X \times X \to \mathbb{R}`
satisfying the following conditions for all space elements
:math:`x, y, z`:
* :math:`d(x, y) \geq 0`,
* :math:`d(x, y) = 0 \Leftrightarrow x = y`,
* :math:`d(x, y) = d(y, x)`,
* :math:`d(x, y) \\leq d(x, z) + d(z, y)`.
* :math:`d(x, y) \leq d(x, z) + d(z, y)`.
- A norm on a space :math:`X` is a mapping
:math:`\| \cdot \|:X \\to \mathbb{R}`
:math:`\| \cdot \|:X \to \mathbb{R}`
satisfying the following conditions for all
space elements :math:`x, y`: and scalars :math:`s`:
* :math:`\| x\| \geq 0`,
* :math:`\| x\| = 0 \Leftrightarrow x = 0`,
* :math:`\| sx\| = |s| \cdot \| x \|`,
* :math:`\| x+y\| \\leq \| x\| +
* :math:`\| x+y\| \leq \| x\| +
\| y\|`.
- An inner product on a space :math:`X` over a field
:math:`\mathbb{F} = \mathbb{R}` or :math:`\mathbb{C}` is a
mapping
:math:`\\langle\cdot, \cdot\\rangle: X \\times
X \\to \mathbb{F}`
:math:`\langle\cdot, \cdot\rangle: X \times
X \to \mathbb{F}`
satisfying the following conditions for all
space elements :math:`x, y, z`: and scalars :math:`s`:
* :math:`\\langle x, y\\rangle =
\overline{\\langle y, x\\rangle}`,
* :math:`\\langle sx + y, z\\rangle = s \\langle x, z\\rangle +
\\langle y, z\\rangle`,
* :math:`\\langle x, x\\rangle = 0 \Leftrightarrow x = 0`.
* :math:`\langle x, y\rangle =
\overline{\langle y, x\rangle}`,
* :math:`\langle sx + y, z\rangle = s \langle x, z\rangle +
\langle y, z\rangle`,
* :math:`\langle x, x\rangle = 0 \Leftrightarrow x = 0`.
Examples
--------
Expand Down Expand Up @@ -2116,8 +2116,9 @@ def _pnorm_diagweight_impl(x, p, w):
# BLAS dot or nrm2
xp = np.abs(x.ravel(order))
if p == float('inf'):
xp *= w.ravel(order)
return np.max(xp)
elif p == -float('inf'):
return np.min(xp)
else:
xp = np.power(xp, p, out=xp)
xp *= w.ravel(order)
Expand Down Expand Up @@ -2242,7 +2243,7 @@ class NumpyTensorSpaceArrayWeighting(ArrayWeighting):
"""

def __init__(self, array, exponent=2.0):
"""Initialize a new instance.
r"""Initialize a new instance.
Parameters
----------
Expand All @@ -2260,39 +2261,37 @@ def __init__(self, array, exponent=2.0):
:math:`W` is defined as
.. math::
\\langle A, B\\rangle_W :=
\\langle W \odot A, B\\rangle =
\\langle w \odot a, b\\rangle =
\langle A, B\rangle_W :=
\langle W \odot A, B\rangle =
\langle w \odot a, b\rangle =
b^{\mathrm{H}} (w \odot a),
where :math:`a, b, w` are the "flattened" counterparts of
tensors :math:`A, B, W`, respectively, :math:`b^{\mathrm{H}}`
stands for transposed complex conjugate and :math:`w \odot a`
for element-wise multiplication.
- For other exponents, only norm and dist are defined. In the
case of exponent :math:`\\infty`, the weighted norm is
- For other exponents, only norm and dist are defined. In the case
of finite exponent, it is (using point-wise exponentiation)
.. math::
\| A\|_{W, \\infty} :=
\| W \odot A\|_{\\infty} =
\| w \odot a\|_{\\infty},
\| A\|_{W, p} :=
\| W^{1/p} \odot A\|_p =
\| w^{1/p} \odot a\|_p,
otherwise it is (using point-wise exponentiation)
and for :math:`\pm \infty` we have
.. math::
\| A\|_{W, p} :=
\| W^{1/p} \odot A\|_{p} =
\| w^{1/p} \odot a\|_{\\infty}.
\| A\|_{W, \pm \infty} :=
\| W \odot A\|_{\pm \infty} =
\| w \odot a\|_{\pm \infty}.
- Note that this definition does **not** fulfill the limit
property in :math:`p`, i.e.
Note that this definition is chosen such that the limit
property in :math:`p` holds, i.e.
.. math::
\| A\|_{W, p} \\not\\to
\| A\|_{W, \\infty} \quad (p \\to \\infty)
unless all weights are equal to 1.
\| A\|_{W, p} \to
\| A\|_{W, \infty} \quad (p \to \infty).
- The array :math:`W` may only have positive entries, otherwise
it does not define an inner product or norm, respectively. This
Expand Down Expand Up @@ -2369,7 +2368,7 @@ class NumpyTensorSpacePerAxisWeighting(PerAxisWeighting):
"""

def __init__(self, factors, exponent=2.0):
"""Initialize a new instance.
r"""Initialize a new instance.
Parameters
----------
Expand All @@ -2396,31 +2395,30 @@ def __init__(self, factors, exponent=2.0):
- For exponent 2.0, a new weighted inner product is given as
.. math::
\\langle a, b\\rangle_v :=
\\langle v \odot a, b\\rangle =
\langle a, b\rangle_v :=
\langle v \odot a, b\rangle =
b^{\mathrm{H}} (v \odot a),
where :math:`b^{\mathrm{H}}` stands for transposed complex
conjugate and ":math:`\odot`" for pointwise product.
- For other exponents, only norm and dist are defined. In the
case of exponent :math:`\\infty`, the weighted norm is defined
as
- For other exponents, only norm and dist are defined. In the case
of finite exponent, it is (using point-wise exponentiation)
.. math::
\| a \|_{v, \\infty} := \| a \|_{\\infty},
\| a \|_{v, p} := \| v^{1/p} \odot a \|_{p},
otherwise it is
and for :math:`\pm \infty` we have
.. math::
\| a \|_{v, p} := \| v^{1/p} \odot a \|_{p}.
\| a \|_{v, \pm \infty} := \| a \|_{\pm \infty},
Note that this definition is chosen such that the limit
property in :math:`p` holds, i.e.
.. math::
\| a\|_{v, p} \\to
\| a \|_{v, \\infty} \quad (p \\to \\infty).
\| a\|_{v, p} \to
\| a \|_{v, \infty} \quad (p \to \infty).
"""
# TODO: allow 3-tuples for `bdry, inner, bdry` type factors
conv_factors = []
Expand Down Expand Up @@ -2635,7 +2633,7 @@ class NumpyTensorSpaceConstWeighting(ConstWeighting):
"""

def __init__(self, const, exponent=2.0):
"""Initialize a new instance.
r"""Initialize a new instance.
Parameters
----------
Expand All @@ -2651,35 +2649,32 @@ def __init__(self, const, exponent=2.0):
:math:`c` is defined as
.. math::
\\langle a, b\\rangle_c :=
c \, \\langle a, b\\rangle_c =
\langle a, b\rangle_c :=
c \, \langle a, b\rangle_c =
c \, b^{\mathrm{H}} a,
where :math:`b^{\mathrm{H}}` standing for transposed complex
conjugate.
- For other exponents, only norm and dist are defined. In the
case of exponent :math:`\\infty`, the weighted norm is defined
as
- For other exponents, only norm and dist are defined. In the case
of finite exponent, it is
.. math::
\| a \|_{c, \\infty} :=
c\, \| a \|_{\\infty},
\| a \|_{c, p} :=
c^{1/p}\, \| a \|_{p},
otherwise it is
and for :math:`\pm \infty` we have
.. math::
\| a \|_{c, p} :=
c^{1/p}\, \| a \|_{p}.
\| a \|_{c, \pm \infty} :=
\| a \|_{\pm \infty}.
- Note that this definition does **not** fulfill the limit
property in :math:`p`, i.e.
Note that this definition is chosen such that the limit
property in :math:`p` holds, i.e.
.. math::
\| a\|_{c, p} \\not\\to
\| a \|_{c, \\infty} \quad (p \\to \\infty)
unless :math:`c = 1`.
\| a\|_{c, p} \to
\| a \|_{c, \infty} \quad (p \to \infty)
- The constant must be positive, otherwise it does not define an
inner product or norm, respectively.
Expand Down

0 comments on commit 90671c1

Please sign in to comment.