icontract + type hints usage examples

parent a53ce08e
Pipeline #66881 failed with stages
in 10 seconds
......@@ -3,16 +3,21 @@
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)
import numpy as np
import icontract
from typing import Union, Tuple
from pymor.core.defaults import defaults
from pymor.core.exceptions import AccuracyError
from pymor.core.logger import getLogger
from pymor.vectorarrays.interface import VectorArray
from pymor.algorithms.basic import contains_zero_vector
@icontract.require(lambda A: (len(A) > 1 or not contains_zero_vector(A)))
@defaults('atol', 'rtol', 'reiterate', 'reiteration_threshold', 'check', 'check_tol')
def gram_schmidt(A, product=None, return_R=False, atol=1e-13, rtol=1e-13, offset=0,
def gram_schmidt(A: VectorArray, product=None, return_R=False, atol=1e-13, rtol=1e-13, offset=0,
reiterate=True, reiteration_threshold=1e-1, check=True, check_tol=1e-3,
copy=True) -> Union[VectorArray, Tuple[VectorArray, np.ndarray]]:
"""Orthonormalize a |VectorArray| using the modified Gram-Schmidt algorithm.
......@@ -18,8 +18,12 @@ and :class:`ImplicitEulerTimeStepper` encapsulate :func:`explicit_euler` and
:func:`implicit_euler` to provide this interface.
from typing import Union
import icontract
from pymor.core.base import ImmutableObject, abstractmethod
from pymor.operators.interface import Operator
from pymor.parameters.base import Mu
from pymor.vectorarrays.interface import VectorArray
......@@ -118,11 +122,11 @@ class ExplicitEulerTimeStepper(TimeStepper):
return explicit_euler(operator, rhs, initial_data, initial_time, end_time, self.nt, mu, num_values)
def implicit_euler(A, F, M, U0, t0, t1, nt, mu=None, num_values=None, solver_options='operator'):
assert isinstance(A, Operator)
assert isinstance(F, (type(None), Operator, VectorArray))
assert isinstance(M, (type(None), Operator))
assert A.source == A.range
@icontract.require(lambda A: A.source == A.range and isinstance(A, Operator))
@icontract.require(lambda F: isinstance(F, (type(None), Operator, VectorArray)))
@icontract.require(lambda M: isinstance(M, (type(None), Operator)))
def implicit_euler(A: Operator, F: Union[None, Operator, VectorArray], M: Union[None, Operator], U0, t0, t1, nt,
mu: Union[None, Mu] = None, num_values=None, solver_options='operator') -> VectorArray:
num_values = num_values or nt + 1
dt = (t1 - t0) / nt
DT = (t1 - t0) / (num_values - 1)
......@@ -505,7 +505,7 @@ class ParameterSpace(ParametricObject):
return [get_param() for _ in range(count)]
def contains(self, mu):
def contains(self, mu: Mu) -> bool:
if not isinstance(mu, Mu):
mu = self.parameters.parse(mu)
if not self.parameters.is_compatible(mu):
......@@ -20,9 +20,6 @@ import pymortests.strategies as pyst
def test_gram_schmidt(vector_array):
U = vector_array
# TODO assumption here masks a potential issue with the algorithm
# where it fails in del instead of a proper error
assume(len(U) > 1 or not contains_zero_vector(U))
V = U.copy()
onb = gram_schmidt(U, copy=True)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment