Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[parameters/functionals] Add LincombParameterFunctional #950

Merged
merged 8 commits into from Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS.md
Expand Up @@ -18,6 +18,7 @@
* Tim Keil, tim.keil@uni-muenster.de
* second order derivatives for parameters
* speed up of lincomb operators
* add LincombParameterFunctional

* Luca Mechelli, luca.mechelli@uni-konstanz.de
* speed up of lincomb operators
Expand Down
57 changes: 56 additions & 1 deletion src/pymor/parameters/functionals.py
Expand Up @@ -45,8 +45,25 @@ def d_mu(self, parameter, index=0):
def __call__(self, mu=None):
return self.evaluate(mu)

def __add__(self, other):
if isinstance(other, Number):
if other == 0:
return self
other = ConstantParameterFunctional(other)
if isinstance(other, ParameterFunctional):
return LincombParameterFunctional([self, other], [1., 1.])
else:
return NotImplemented

__radd__ = __add__

def __sub__(self, other):
if isinstance(other, ParameterFunctional):
return LincombParameterFunctional([self, other], [1., -1.])
else:
return self + (- other)

def __mul__(self, other):
from pymor.parameters.functionals import ProductParameterFunctional
if not isinstance(other, (Number, ParameterFunctional)):
return NotImplemented
return ProductParameterFunctional([self, other])
Expand Down Expand Up @@ -314,6 +331,44 @@ def d_mu(self, parameter, index=0):
return self.with_(constant_value=0, name=self.name + '_d_mu')


class LincombParameterFunctional(ParameterFunctional):
"""A |ParameterFunctional| representing a linear combination of |ParameterFunctionals|.

The coefficients must be provided as scalars.

Parameters
----------
functionals
List of |ParameterFunctionals| whose linear combination is formed.
coefficients
A list of scalar coefficients.
name
Name of the functional.

Attributes
----------
functionals
coefficients
"""

def __init__(self, functionals, coefficients, name=None):
assert len(functionals) > 0
assert len(functionals) == len(coefficients)
assert all(isinstance(f, ParameterFunctional) for f in functionals)
assert all(isinstance(c, Number) for c in coefficients)
TiKeil marked this conversation as resolved.
Show resolved Hide resolved
functionals = tuple(functionals)
coefficients = tuple(coefficients)
self.__auto_init(locals())

def evaluate(self, mu=None):
assert self.parameters.assert_compatible(mu)
return sum(c * f(mu) for c, f in zip(self.coefficients, self.functionals))

def d_mu(self, parameter, index=0):
functionals_d_mu = [f.d_mu(parameter, index) for f in self.functionals]
return self.with_(functionals=functionals_d_mu, name=self.name + '_d_mu')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would probably need to be adapted elsewhere (in a different PR), but maybe name=f'{self.name}_d_{parameter}_{index}' would be more helpful?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good point !



class MinThetaParameterFunctional(ParameterFunctional):
"""|ParameterFunctional| implementing the min-theta approach from [Haa17]_ (Proposition 2.35).

Expand Down
39 changes: 39 additions & 0 deletions src/pymortests/parameter_functionals.py
@@ -0,0 +1,39 @@
# This file is part of the pyMOR project (http://www.pymor.org).
# Copyright 2013-2020 pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

from pymortests.base import runmodule
import pytest

import numpy as np

from pymor.parameters.functionals import ProjectionParameterFunctional, ExpressionParameterFunctional, LincombParameterFunctional
from pymor.basic import Mu


def test_LincombParameterFunctional():
dict_of_d_mus = {'mu': ['200 * mu[0]', '2 * mu[0]'], 'nu': ['cos(nu[0])']}

epf = ExpressionParameterFunctional('100 * mu[0]**2 + 2 * mu[1] * mu[0] + sin(nu[0])',
{'mu': 2, 'nu': 1},
'functional_with_derivative_and_second_derivative',
dict_of_d_mus)
pf = ProjectionParameterFunctional('mu', 2, 0)
mu = Mu({'mu': [10,2], 'nu': [3]})

zero = pf - pf
two_pf = pf + pf
three_pf = pf + 2*pf
pf_plus_one = pf + 1
sum_ = epf + pf
pf_squared = (pf + 2*epf) * (pf - 2*epf) + 4 * epf * epf

assert zero(mu) == 0
assert two_pf(mu) == 2 * pf(mu)
assert three_pf(mu) == 3 * pf(mu)
assert pf_plus_one(mu) == pf(mu) + 1
assert sum_(mu) == epf(mu) + pf(mu)
assert pf_squared(mu) == pf(mu) * pf(mu)
assert sum_.d_mu('mu', 0)(mu) == epf.d_mu('mu', 0)(mu) + pf.d_mu('mu', 0)(mu)
assert sum_.d_mu('mu', 1)(mu) == epf.d_mu('mu', 1)(mu) + pf.d_mu('mu', 1)(mu)
assert sum_.d_mu('nu', 0)(mu) == epf.d_mu('nu', 0)(mu) + pf.d_mu('nu', 0)(mu)