Skip to content

Commit 9003572

Browse files
author
m.lohmann
committed
Added docstrings for range parameter
1 parent d1c4d68 commit 9003572

File tree

2 files changed

+61
-15
lines changed

2 files changed

+61
-15
lines changed

odl/solvers/functional/default_functionals.py

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from odl.solvers.functional.functional import (Functional,
1616
FunctionalQuadraticPerturb)
1717
from odl.space import ProductSpace
18-
from odl.set import RealNumbers#, ComplexNumbers
18+
from odl.set import RealNumbers
1919
from odl.operator import (Operator, ConstantOperator, ZeroOperator,
2020
ScalingOperator, DiagonalOperator, PointwiseNorm)
2121
from odl.solvers.nonsmooth.proximal_operators import (
@@ -69,10 +69,13 @@ def __init__(self, domain, exponent, range=None):
6969
Domain of the functional.
7070
exponent : float
7171
Exponent for the norm (``p``).
72+
range : `Field`, optional
73+
Range of the functional. The default `None` will be treated as
74+
`RealNumbers`.
7275
"""
7376
if range is None and getattr(domain, 'is_complex', False):
7477
range = RealNumbers()
75-
78+
7679
super(LpNorm, self).__init__(
7780
domain=domain, linear=False, grad_lipschitz=np.nan, range=range)
7881
self.exponent = float(exponent)
@@ -227,6 +230,9 @@ def __init__(self, vfspace, exponent=None, range=None):
227230
0 and 1 are currently not supported due to numerical
228231
instability. Infinity gives the supremum norm.
229232
Default: ``vfspace.exponent``, usually 2.
233+
range : `Field`, optional
234+
Range of the functional. The default `None` will be treated as
235+
`vfspace.field`.
230236
231237
Examples
232238
--------
@@ -356,6 +362,9 @@ def __init__(self, vfspace, exponent=None, range=None):
356362
0 and 1 are currently not supported due to numerical
357363
instability. Infinity gives the supremum norm.
358364
Default: ``vfspace.exponent``, usually 2.
365+
range : `Field`, optional
366+
Range of the functional. The default `None` will be treated as
367+
`vfspace.field`.
359368
360369
Examples
361370
--------
@@ -466,6 +475,9 @@ def __init__(self, domain, exponent, range=None):
466475
Domain of the functional.
467476
exponent : int or infinity
468477
Specifies wich norm to use.
478+
range : `Field`, optional
479+
Range of the functional. The default `None` will be treated as
480+
`domain.field`.
469481
"""
470482
super(IndicatorLpUnitBall, self).__init__(domain=domain, linear=False,
471483
range=range)
@@ -512,7 +524,6 @@ def convex_conj(self):
512524
[BC2011] Bauschke, H H, and Combettes, P L. *Convex analysis and
513525
monotone operator theory in Hilbert spaces*. Springer, 2011.
514526
"""
515-
# HELP: I guess it needs `range`, too
516527
if self.exponent == np.inf:
517528
return L1Norm(self.domain, range=self.range_)
518529
elif self.exponent == 2:
@@ -582,6 +593,9 @@ def __init__(self, domain, range=None):
582593
----------
583594
domain : `DiscreteLp` or `TensorSpace`
584595
Domain of the functional.
596+
range : `Field`, optional
597+
Range of the functional. The default `None` will be treated as
598+
`RealNumbers`.
585599
"""
586600
super(L1Norm, self).__init__(domain=domain, exponent=1, range=range)
587601

@@ -620,6 +634,9 @@ def __init__(self, domain, range=None):
620634
----------
621635
domain : `DiscreteLp` or `TensorSpace`
622636
Domain of the functional.
637+
range : `Field`, optional
638+
Range of the functional. The default `None` will be treated as
639+
`RealNumbers`.
623640
"""
624641
super(L2Norm, self).__init__(domain=domain, exponent=2, range=range)
625642

@@ -666,6 +683,9 @@ def __init__(self, domain, range=None):
666683
----------
667684
domain : `DiscreteLp` or `TensorSpace`
668685
Domain of the functional.
686+
range : `Field`, optional
687+
Range of the functional. The default `None` will be treated as
688+
`RealNumbers`.
669689
"""
670690
super(L2NormSquared, self).__init__(
671691
domain=domain, linear=False, grad_lipschitz=2, range=range)
@@ -723,6 +743,9 @@ def __init__(self, domain, constant, range=None):
723743
Domain of the functional.
724744
constant : element in ``domain.field``
725745
The constant value of the functional
746+
range : `Field`, optional
747+
Range of the functional. The default `None` will be treated as
748+
`domain.field`.
726749
"""
727750
super(ConstantFunctional, self).__init__(
728751
domain=domain, linear=(constant == 0), grad_lipschitz=0,
@@ -781,9 +804,12 @@ def __init__(self, domain, range=None):
781804
----------
782805
domain : `LinearSpace`
783806
Domain of the functional.
807+
range : `Field`, optional
808+
Range of the functional. The default `None` will be treated as
809+
`domain.field`.
784810
"""
785811
super(ZeroFunctional, self).__init__(domain=domain, constant=0,
786-
range=range)
812+
range=range)
787813

788814
def __repr__(self):
789815
"""Return ``repr(self)``."""
@@ -874,6 +900,9 @@ def __init__(self, domain, lower=None, upper=None, range=None):
874900
upper : ``domain.field`` element or ``domain`` `element-like`, optional
875901
The upper bound.
876902
Default: ``None``, interpreted as +infinity
903+
range : `Field`, optional
904+
Range of the functional. The default `None` will be treated as
905+
`domain.field`.
877906
878907
Examples
879908
--------
@@ -929,6 +958,9 @@ def __init__(self, domain, range=None):
929958
----------
930959
domain : `LinearSpace`
931960
Domain of the functional.
961+
range : `Field`, optional
962+
Range of the functional. The default `None` will be treated as
963+
`domain.field`.
932964
933965
Examples
934966
--------
@@ -963,6 +995,9 @@ def __init__(self, domain, constant=0, range=None):
963995
Domain of the functional.
964996
constant : element in ``domain.field``, optional
965997
The constant value of the functional
998+
range : `Field`, optional
999+
Range of the functional. The default `None` will be treated as
1000+
`domain.field`.
9661001
9671002
Examples
9681003
--------
@@ -1107,7 +1142,6 @@ def __init__(self, domain, prior=None):
11071142
>>> func(x)
11081143
3.0
11091144
"""
1110-
# HELP: range is domain.field, correct?
11111145
super(KullbackLeibler, self).__init__(
11121146
domain=domain, linear=False, grad_lipschitz=np.nan)
11131147

@@ -1955,7 +1989,9 @@ def __init__(self, domain, outer_exp=1, singular_vector_exp=2, range=None):
19551989
Exponent for the outer norm.
19561990
singular_vector_exp : {1, 2, inf}, optional
19571991
Exponent for the norm for the singular vectors.
1958-
1992+
range : `Field`, optional
1993+
Range of the functional. The default `None` will be treated as
1994+
`RealNumbers`.
19591995
Examples
19601996
--------
19611997
Simple example, nuclear norm of matrix valued function with all ones
@@ -1974,7 +2010,8 @@ def __init__(self, domain, outer_exp=1, singular_vector_exp=2, range=None):
19742010
'`ProductSpace`s')
19752011
if (not domain.is_power_space or not domain[0].is_power_space):
19762012
raise TypeError('`domain` must be of the form `TensorSpace^(nxm)`')
1977-
2013+
if range is None:
2014+
range = RealNumbers()
19782015
super(NuclearNorm, self).__init__(
19792016
domain=domain, linear=False, grad_lipschitz=np.nan, range=range)
19802017

@@ -2166,7 +2203,9 @@ def __init__(self, domain, outer_exp=1, singular_vector_exp=2, range=None):
21662203
Exponent for the outer norm.
21672204
singular_vector_exp : {1, 2, inf}, optional
21682205
Exponent for the norm for the singular vectors.
2169-
2206+
range : `Field`, optional
2207+
Range of the functional. The default `None` will be treated as
2208+
`domain.field` or it that doesn't exist `RealNumbers`.
21702209
Examples
21712210
--------
21722211
Simple example, nuclear norm of matrix valued function with all ones
@@ -2282,7 +2321,8 @@ def __init__(self, functional, sigma=1.0, range=RealNumbers()):
22822321
sigma : positive float, optional
22832322
The scalar ``sigma`` in the definition of the Moreau envelope.
22842323
Larger values mean stronger smoothing.
2285-
2324+
range : `Field`, optional
2325+
Range of the functional. The default is `RealNumbers`.
22862326
Examples
22872327
--------
22882328
Create smoothed l1 norm:
@@ -2354,6 +2394,8 @@ def __init__(self, domain, gamma, range=RealNumbers()):
23542394
the functional is non-smooth and corresponds to the usual L1 norm.
23552395
For ``gamma > 0``, it has a ``1/gamma``-Lipschitz gradient so that
23562396
its convex conjugate is ``gamma``-strongly convex.
2397+
range : `Field`, optional
2398+
Range of the functional. The default is `RealNumbers`.
23572399
23582400
Examples
23592401
--------

odl/solvers/functional/functional.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,21 @@ def __init__(self, domain, linear=False, grad_lipschitz=np.nan,
6161
If `True`, the functional is considered as linear.
6262
grad_lipschitz : float, optional
6363
The Lipschitz constant of the gradient. Default: ``nan``
64+
range : `Field`, optional
65+
Range of the functional. The default `None` will be treated as
66+
`domain.field`.
6467
"""
6568
# Cannot use `super(Functional, self)` here since that breaks
6669
# subclasses with multiple inheritance (at least those where both
6770
# parents implement `__init__`, e.g., in `ScalingFunctional`)
68-
default_range = getattr(domain, 'field', None)
6971
if range is None:
72+
default_range = getattr(domain, 'field', None)
7073
if default_range is None:
74+
# HELP: I think this is not correct. How to do it properly?
7175
raise TypeError('Could not infere range for `functional` {!r}'
72-
.format(self))
73-
else: range = default_range
76+
''.format(self))
77+
else:
78+
range = default_range
7479

7580
Operator.__init__(self, domain=domain, range=range, linear=linear)
7681
self.__grad_lipschitz = float(grad_lipschitz)
@@ -696,8 +701,7 @@ def __init__(self, func, vector):
696701
''.format(func))
697702

698703
OperatorRightVectorMult.__init__(self, operator=func, vector=vector)
699-
# HELP: before it was Functional.__init__(self, domain=func.domain).
700-
# But doesn't this loose the information on grad_lipschitz and linear?
704+
# HELP: grad_lipschitz = func.grad_lipschitz * ||constant||_\infty ?
701705
Functional.__init__(self, domain=func.domain,
702706
grad_lipschitz=func.grad_lipschitz,
703707
linear=func.is_linear,
@@ -1325,7 +1329,7 @@ def __init__(self, func):
13251329
if not isinstance(func, Functional):
13261330
raise TypeError('`func` {} is not a `Functional` instance'
13271331
''.format(func))
1328-
1332+
13291333
super(FunctionalDefaultConvexConjugate, self).__init__(
13301334
domain=func.domain, linear=func.is_linear, range=func.range)
13311335
self.__convex_conj = func

0 commit comments

Comments
 (0)