diff --git a/rrompy/parameter/parameter_sampling/__init__.py b/rrompy/parameter/parameter_sampling/__init__.py index d395bd7..7bef076 100644 --- a/rrompy/parameter/parameter_sampling/__init__.py +++ b/rrompy/parameter/parameter_sampling/__init__.py @@ -1,40 +1,42 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # from .generic_sampler import GenericSampler from .generic_quadrature_sampler import GenericQuadratureSampler +from .generic_random_sampler import GenericRandomSampler from .manual_sampler import ManualSampler from .segment import QuadratureSampler, QuadratureSamplerTotal, RandomSampler from .shape import (FFTSampler, QuadratureBoxSampler, QuadratureCircleSampler, RandomBoxSampler, RandomCircleSampler) __all__ = [ 'GenericSampler', 'GenericQuadratureSampler', + 'GenericRandomSampler', 'ManualSampler', 'QuadratureSampler', 'QuadratureSamplerTotal', 'RandomSampler', 'FFTSampler', 'QuadratureBoxSampler', 'QuadratureCircleSampler', 'RandomBoxSampler', 'RandomCircleSampler' ] diff --git a/rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py b/rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py index 1fa92dc..f0e9356 100644 --- a/rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py +++ b/rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py @@ -1,51 +1,53 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # from .generic_sampler import GenericSampler from rrompy.utilities.base.types import List, paramList from rrompy.utilities.exception_manager import RROMPyException +_allowedQuadratureKinds = ["UNIFORM", "CHEBYSHEV", "EXTENDEDCHEBYSHEV", + "GAUSSLEGENDRE", "EXTENDEDGAUSSLEGENDRE"] + __all__ = ['GenericQuadratureSampler'] class GenericQuadratureSampler(GenericSampler): """Generator of quadrature sample points.""" def __init__(self, lims:paramList, kind : str = "UNIFORM", scalingExp : List[float] = None): super().__init__(lims = lims, scalingExp = scalingExp) - self._allowedKinds = ["UNIFORM", "CHEBYSHEV", "EXTENDEDCHEBYSHEV", - "GAUSSLEGENDRE", "EXTENDEDGAUSSLEGENDRE"] + self._allowedKinds = _allowedQuadratureKinds self.kind = kind def __str__(self) -> str: return "{}_{}".format(super().__str__(), self.kind) def __repr__(self) -> str: return self.__str__() + " at " + hex(id(self)) @property def kind(self): """Value of kind.""" return self._kind @kind.setter def kind(self, kind): kind = kind.upper().strip().replace(" ","") if kind not in self._allowedKinds: raise RROMPyException("Generator kind not recognized.") self._kind = kind diff --git a/rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py b/rrompy/parameter/parameter_sampling/generic_random_sampler.py similarity index 75% copy from rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py copy to rrompy/parameter/parameter_sampling/generic_random_sampler.py index 1fa92dc..104b1d6 100644 --- a/rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py +++ b/rrompy/parameter/parameter_sampling/generic_random_sampler.py @@ -1,51 +1,51 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # from .generic_sampler import GenericSampler from rrompy.utilities.base.types import List, paramList from rrompy.utilities.exception_manager import RROMPyException -__all__ = ['GenericQuadratureSampler'] +_allowedRandomKinds = ["UNIFORM", "HALTON", "SOBOL"] -class GenericQuadratureSampler(GenericSampler): - """Generator of quadrature sample points.""" +__all__ = ['GenericRandomSampler'] + +class GenericRandomSampler(GenericSampler): + """Generator of random sample points.""" def __init__(self, lims:paramList, kind : str = "UNIFORM", - scalingExp : List[float] = None): + scalingExp : List[float] = None, seed : int = 42): super().__init__(lims = lims, scalingExp = scalingExp) - self._allowedKinds = ["UNIFORM", "CHEBYSHEV", "EXTENDEDCHEBYSHEV", - "GAUSSLEGENDRE", "EXTENDEDGAUSSLEGENDRE"] + self._allowedKinds = _allowedRandomKinds self.kind = kind - + self.seed = seed + def __str__(self) -> str: return "{}_{}".format(super().__str__(), self.kind) def __repr__(self) -> str: return self.__str__() + " at " + hex(id(self)) @property def kind(self): """Value of kind.""" return self._kind @kind.setter def kind(self, kind): - kind = kind.upper().strip().replace(" ","") - if kind not in self._allowedKinds: + if kind.upper() not in self._allowedKinds: raise RROMPyException("Generator kind not recognized.") - self._kind = kind - + self._kind = kind.upper() diff --git a/rrompy/parameter/parameter_sampling/generic_sampler.py b/rrompy/parameter/parameter_sampling/generic_sampler.py index c0b9834..6e74eef 100644 --- a/rrompy/parameter/parameter_sampling/generic_sampler.py +++ b/rrompy/parameter/parameter_sampling/generic_sampler.py @@ -1,77 +1,87 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # +import numpy as np from abc import abstractmethod from rrompy.utilities.base.types import List, paramList from rrompy.utilities.exception_manager import RROMPyException, RROMPyAssert from rrompy.parameter import checkParameterList __all__ = ['GenericSampler'] class GenericSampler: """ABSTRACT. Generic generator of sample points.""" def __init__(self, lims:paramList, scalingExp : List[float] = None): self.lims = lims self.scalingExp = scalingExp def name(self) -> str: return self.__class__.__name__ def __str__(self) -> str: return "{}[{}_{}]".format(self.name(), self.lims[0], self.lims[1]) def __repr__(self) -> str: return self.__str__() + " at " + hex(id(self)) def __eq__(self, other) -> bool: return self.__dict__ == other.__dict__ @property def npar(self): """Number of parameters.""" return self._lims.shape[1] + def normalFoci(self, d : int = 0): + return [-1., 1.] + + def groundPotential(self, d : int = 0): + fp = self.normalFoci(d)[1] + fpa = np.abs(fp) + if np.isclose(fpa, 0.) or np.isclose(fpa, 1.): return 1. + return (1. + np.abs(1. - fp ** 2.) ** .5) / fpa + @property def lims(self): """Value of lims.""" return self._lims @lims.setter def lims(self, lims): lims = checkParameterList(lims)[0] if len(lims) != 2: raise RROMPyException("2 limits must be specified.") self._lims = lims @property def scalingExp(self): """Value of scalingExp.""" return self._scalingExp @scalingExp.setter def scalingExp(self, scalingExp): if scalingExp is None: scalingExp = [1.] * self.npar if not hasattr(scalingExp, "__len__"): scalingExp = [scalingExp] RROMPyAssert(self.npar, len(scalingExp), "Number of scaling terms") self._scalingExp = scalingExp @abstractmethod def generatePoints(self, n:int, reorder : bool = True) -> paramList: """Array of points.""" pass diff --git a/rrompy/parameter/parameter_sampling/manual_sampler.py b/rrompy/parameter/parameter_sampling/manual_sampler.py index 2132633..08526b3 100644 --- a/rrompy/parameter/parameter_sampling/manual_sampler.py +++ b/rrompy/parameter/parameter_sampling/manual_sampler.py @@ -1,60 +1,65 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # import numpy as np from copy import deepcopy as copy from rrompy.parameter.parameter_sampling.generic_sampler import GenericSampler from rrompy.utilities.base.types import List, paramList from rrompy.parameter import checkParameterList __all__ = ['ManualSampler'] class ManualSampler(GenericSampler): """Manual generator of sample points.""" def __init__(self, lims:paramList, points:paramList, - scalingExp : List[float] = None): + scalingExp : List[float] = None, + normalFoci : List[np.complex] = [-1., 1.]): super().__init__(lims = lims, scalingExp = scalingExp) self.points = points + self._normalFoci = normalFoci + def normalFoci(self, d : int = 0): + return self._normalFoci + @property def points(self): """Value of points.""" return self._points @points.setter def points(self, points): points = checkParameterList(points, self.npar)[0] self._points = points def __str__(self) -> str: return "{}[{}]".format(self.name(), "_".join(map(str, self.points))) def __repr__(self) -> str: return self.__str__() + " at " + hex(id(self)) def generatePoints(self, n:int, reorder : bool = True) -> paramList: """Array of sample points.""" if n > len(self.points): pts = copy(self.points) for j in range(int(np.ceil(n / len(self.points)))): pts.append(self.points) else: pts = self.points x = checkParameterList(pts[list(range(n))], self.npar)[0] return x diff --git a/rrompy/parameter/parameter_sampling/segment/random_sampler.py b/rrompy/parameter/parameter_sampling/segment/random_sampler.py index 301d852..a755c90 100644 --- a/rrompy/parameter/parameter_sampling/segment/random_sampler.py +++ b/rrompy/parameter/parameter_sampling/segment/random_sampler.py @@ -1,71 +1,47 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # import numpy as np -from rrompy.parameter.parameter_sampling.generic_sampler import GenericSampler +from rrompy.parameter.parameter_sampling.generic_random_sampler import ( + GenericRandomSampler) from rrompy.utilities.numerical import haltonGenerate, sobolGenerate -from rrompy.utilities.base.types import List, paramList -from rrompy.utilities.exception_manager import RROMPyException +from rrompy.utilities.base.types import paramList from rrompy.parameter import checkParameterList __all__ = ['RandomSampler'] -class RandomSampler(GenericSampler): +class RandomSampler(GenericRandomSampler): """Generator of (quasi-)random sample points.""" - allowedKinds = ["UNIFORM", "HALTON", "SOBOL"] - - def __init__(self, lims:paramList, kind : str = "UNIFORM", - scalingExp : List[float] = None, seed : int = 42): - super().__init__(lims = lims, scalingExp = scalingExp) - self.kind = kind - self.seed = seed - - def __str__(self) -> str: - return "{}_{}".format(super().__str__(), self.kind) - - def __repr__(self) -> str: - return self.__str__() + " at " + hex(id(self)) - - @property - def kind(self): - """Value of kind.""" - return self._kind - @kind.setter - def kind(self, kind): - if kind.upper() not in self.allowedKinds: - raise RROMPyException("Generator kind not recognized.") - self._kind = kind.upper() - def generatePoints(self, n:int, reorder : bool = True) -> paramList: """Array of quadrature points.""" if self.kind == "UNIFORM": np.random.seed(self.seed) xmat = np.random.uniform(size = (n, self.npar)) elif self.kind == "HALTON": xmat = haltonGenerate(self.npar, n, self.seed) else: xmat = sobolGenerate(self.npar, n, self.seed) for d in range(self.npar): a = self.lims(0, d) ** self.scalingExp[d] b = self.lims(1, d) ** self.scalingExp[d] xmat[:, d] = a + (b - a) * xmat[:, d] xmat[:, d] **= 1. / self.scalingExp[d] x = checkParameterList(xmat, self.npar)[0] return x diff --git a/rrompy/parameter/parameter_sampling/shape/__init__.py b/rrompy/parameter/parameter_sampling/shape/__init__.py index 86a8d66..40e0793 100644 --- a/rrompy/parameter/parameter_sampling/shape/__init__.py +++ b/rrompy/parameter/parameter_sampling/shape/__init__.py @@ -1,37 +1,39 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # from .generic_shape_sampler import GenericShapeSampler from .generic_shape_quadrature_sampler import GenericShapeQuadratureSampler +from .generic_shape_random_sampler import GenericShapeRandomSampler from .fft_sampler import FFTSampler from .quadrature_box_sampler import QuadratureBoxSampler from .quadrature_circle_sampler import QuadratureCircleSampler from .random_box_sampler import RandomBoxSampler from .random_circle_sampler import RandomCircleSampler __all__ = [ 'GenericShapeSampler', 'GenericShapeQuadratureSampler', + 'GenericShapeRandomSampler', 'FFTSampler', 'QuadratureBoxSampler', 'QuadratureCircleSampler', 'RandomBoxSampler', 'RandomCircleSampler' ] diff --git a/rrompy/parameter/parameter_sampling/shape/generic_shape_quadrature_sampler.py b/rrompy/parameter/parameter_sampling/shape/generic_shape_quadrature_sampler.py index e95607d..9fb1718 100644 --- a/rrompy/parameter/parameter_sampling/shape/generic_shape_quadrature_sampler.py +++ b/rrompy/parameter/parameter_sampling/shape/generic_shape_quadrature_sampler.py @@ -1,46 +1,55 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # -import numpy as np +from rrompy.parameter.parameter_sampling.shape.generic_shape_sampler import ( + GenericShapeSampler) from rrompy.parameter.parameter_sampling.generic_quadrature_sampler import ( - GenericQuadratureSampler) + _allowedQuadratureKinds) from rrompy.utilities.base.types import List, paramList -from rrompy.utilities.exception_manager import RROMPyAssert +from rrompy.utilities.exception_manager import RROMPyException __all__ = ['GenericShapeQuadratureSampler'] -class GenericShapeQuadratureSampler(GenericQuadratureSampler): +class GenericShapeQuadratureSampler(GenericShapeSampler): """Generator of quadrature sample points on shapes.""" def __init__(self, lims:paramList, kind : str = "UNIFORM", axisRatios : List[float] = None, scalingExp : List[float] = None): - super().__init__(lims = lims, kind = kind, scalingExp = scalingExp) - self.axisRatios = axisRatios - + super().__init__(lims = lims, axisRatios = axisRatios, + scalingExp = scalingExp) + self._allowedKinds = _allowedQuadratureKinds + self.kind = kind + + def __str__(self) -> str: + return "{}_{}".format(super().__str__(), self.kind) + + def __repr__(self) -> str: + return self.__str__() + " at " + hex(id(self)) + @property - def axisRatios(self): - """Value of axisRatios.""" - return self._axisRatios - @axisRatios.setter - def axisRatios(self, axisRatios): - if axisRatios is None: - axisRatios = [1.] * self.npar - if not hasattr(axisRatios, "__len__"): axisRatios = [axisRatios] - RROMPyAssert(self.npar, len(axisRatios), "Number of axis ratios terms") - self._axisRatios = [np.abs(x) for x in axisRatios] + def kind(self): + """Value of kind.""" + return self._kind + @kind.setter + def kind(self, kind): + kind = kind.upper().strip().replace(" ","") + if kind not in self._allowedKinds: + raise RROMPyException("Generator kind not recognized.") + self._kind = kind + diff --git a/rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py b/rrompy/parameter/parameter_sampling/shape/generic_shape_random_sampler.py similarity index 60% copy from rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py copy to rrompy/parameter/parameter_sampling/shape/generic_shape_random_sampler.py index 1fa92dc..c1a52dc 100644 --- a/rrompy/parameter/parameter_sampling/generic_quadrature_sampler.py +++ b/rrompy/parameter/parameter_sampling/shape/generic_shape_random_sampler.py @@ -1,51 +1,54 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # -from .generic_sampler import GenericSampler +from rrompy.parameter.parameter_sampling.shape.generic_shape_sampler import ( + GenericShapeSampler) +from rrompy.parameter.parameter_sampling.generic_random_sampler import ( + _allowedRandomKinds) from rrompy.utilities.base.types import List, paramList from rrompy.utilities.exception_manager import RROMPyException -__all__ = ['GenericQuadratureSampler'] - -class GenericQuadratureSampler(GenericSampler): - """Generator of quadrature sample points.""" +__all__ = ['GenericShapeRandomSampler'] +class GenericShapeRandomSampler(GenericShapeSampler): + """Generator of random sample points on shapes.""" + def __init__(self, lims:paramList, kind : str = "UNIFORM", - scalingExp : List[float] = None): - super().__init__(lims = lims, scalingExp = scalingExp) - self._allowedKinds = ["UNIFORM", "CHEBYSHEV", "EXTENDEDCHEBYSHEV", - "GAUSSLEGENDRE", "EXTENDEDGAUSSLEGENDRE"] + axisRatios : List[float] = None, + scalingExp : List[float] = None, seed : int = 42): + super().__init__(lims = lims, axisRatios = axisRatios, + scalingExp = scalingExp) + self._allowedKinds = _allowedRandomKinds self.kind = kind - + self.seed = seed + def __str__(self) -> str: return "{}_{}".format(super().__str__(), self.kind) def __repr__(self) -> str: return self.__str__() + " at " + hex(id(self)) @property def kind(self): """Value of kind.""" return self._kind @kind.setter def kind(self, kind): - kind = kind.upper().strip().replace(" ","") - if kind not in self._allowedKinds: + if kind.upper() not in self._allowedKinds: raise RROMPyException("Generator kind not recognized.") - self._kind = kind - + self._kind = kind.upper() diff --git a/rrompy/parameter/parameter_sampling/shape/generic_shape_sampler.py b/rrompy/parameter/parameter_sampling/shape/generic_shape_sampler.py index 81d37e1..09c1403 100644 --- a/rrompy/parameter/parameter_sampling/shape/generic_shape_sampler.py +++ b/rrompy/parameter/parameter_sampling/shape/generic_shape_sampler.py @@ -1,44 +1,48 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # import numpy as np from rrompy.parameter.parameter_sampling.generic_sampler import GenericSampler from rrompy.utilities.base.types import List, paramList from rrompy.utilities.exception_manager import RROMPyAssert __all__ = ['GenericShapeSampler'] class GenericShapeSampler(GenericSampler): """Generator of sample points on boxes or ellipses.""" def __init__(self, lims:paramList, axisRatios : List[float] = None, scalingExp : List[float] = None): super().__init__(lims = lims, scalingExp = scalingExp) self.axisRatios = axisRatios + def normalFoci(self, d : int = 0): + focus = (1. + 0.j - self.axisRatios[d] ** 2.) ** .5 + return [- focus, focus] + @property def axisRatios(self): """Value of axisRatios.""" return self._axisRatios @axisRatios.setter def axisRatios(self, axisRatios): if axisRatios is None: axisRatios = [1.] * self.npar if not hasattr(axisRatios, "__len__"): axisRatios = [axisRatios] RROMPyAssert(self.npar, len(axisRatios), "Number of axis ratios terms") self._axisRatios = [np.abs(x) for x in axisRatios] diff --git a/rrompy/parameter/parameter_sampling/shape/quadrature_circle_sampler.py b/rrompy/parameter/parameter_sampling/shape/quadrature_circle_sampler.py index 460e6eb..94b1f20 100644 --- a/rrompy/parameter/parameter_sampling/shape/quadrature_circle_sampler.py +++ b/rrompy/parameter/parameter_sampling/shape/quadrature_circle_sampler.py @@ -1,66 +1,65 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # import numpy as np from .generic_shape_quadrature_sampler import GenericShapeQuadratureSampler from rrompy.utilities.base.types import paramList from rrompy.utilities.numerical import (lowDiscrepancy, kroneckerer, - cutOffDistance, quadraturePointsGenerate) + potential, quadraturePointsGenerate) from rrompy.parameter import checkParameterList __all__ = ['QuadratureCircleSampler'] class QuadratureCircleSampler(GenericShapeQuadratureSampler): """Generator of quadrature sample points on ellipses.""" def generatePoints(self, n:int, reorder : bool = True) -> paramList: """Array of sample points.""" n1d = int(np.ceil(n ** (1. / self.npar))) xds, nds = [None] * self.npar, [None] * self.npar for d in range(self.npar): ax = self.axisRatios[d] - focus = (1. + 0.j - ax ** 2.) ** .5 - scorebase = cutOffDistance(np.ones(1), [- focus, focus]) + scorebase = self.groundPotential(d) a = self.lims(0, d) ** self.scalingExp[d] b = self.lims(1, d) ** self.scalingExp[d] c, r = (a + b) / 2., (a - b) / 2. n1dx = int(np.ceil((n1d * 4. / np.pi / ax) ** .5)) n1dy = int(np.ceil(n1d * 4. / np.pi / n1dx)) even, Z = True, [] while len(Z) < n1d: Xdx, Xdy = np.meshgrid( quadraturePointsGenerate(n1dx, self.kind), quadraturePointsGenerate(n1dy, self.kind)) Z = Xdx.flatten() + 1.j * ax * Xdy.flatten() - ptscore = cutOffDistance(Z, [- focus, focus]) + ptscore = potential(Z, self.normalFoci(d)) Z = Z[ptscore <= scorebase] if even: n1dx += 1 else: n1dy += 1 xds[d] = (c + r * Z) ** (1. / self.scalingExp[d]) nds[d] = len(Z) nleft, nright = 1, np.prod(nds) xmat = np.empty((nright, self.npar), dtype = np.complex) for d in range(self.npar): nright //= nds[d] xmat[:, d] = kroneckerer(xds[d], nleft, nright) nleft *= nds[d] if len(xmat) > 1 and reorder: fejerOrdering = [len(xmat) - 1] + lowDiscrepancy(len(xmat) - 1) xmat = xmat[fejerOrdering, :] x = checkParameterList(xmat, self.npar)[0] return x diff --git a/rrompy/parameter/parameter_sampling/shape/random_box_sampler.py b/rrompy/parameter/parameter_sampling/shape/random_box_sampler.py index 933f009..0fc7a57 100644 --- a/rrompy/parameter/parameter_sampling/shape/random_box_sampler.py +++ b/rrompy/parameter/parameter_sampling/shape/random_box_sampler.py @@ -1,86 +1,59 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # import numpy as np -from .generic_shape_sampler import GenericShapeSampler +from .generic_shape_random_sampler import GenericShapeRandomSampler from rrompy.utilities.numerical import haltonGenerate, sobolGenerate -from rrompy.utilities.base.types import List, paramList -from rrompy.utilities.exception_manager import RROMPyException +from rrompy.utilities.base.types import paramList from rrompy.parameter import checkParameterList __all__ = ['RandomBoxSampler'] -class RandomBoxSampler(GenericShapeSampler): +class RandomBoxSampler(GenericShapeRandomSampler): """Generator of (quasi-)random sample points on boxes.""" - allowedKinds = ["UNIFORM", "HALTON", "SOBOL"] - - def __init__(self, lims:paramList, kind : str = "UNIFORM", - axisRatios : List[float] = None, - scalingExp : List[float] = None, seed : int = 42): - super().__init__(lims = lims, scalingExp = scalingExp, - axisRatios = axisRatios) - self.kind = kind - self.seed = seed - - def __str__(self) -> str: - return "{}_{}".format(super().__str__(), self.kind) - - def __repr__(self) -> str: - return self.__str__() + " at " + hex(id(self)) - - @property - def kind(self): - """Value of kind.""" - return self._kind - @kind.setter - def kind(self, kind): - if kind.upper() not in self.allowedKinds: - raise RROMPyException("Generator kind not recognized.") - self._kind = kind.upper() - def generatePoints(self, n:int, reorder : bool = True) -> paramList: """Array of quadrature points.""" nEff = int(np.ceil(n * np.prod( [max(x, 1. / x) for x in self.axisRatios]))) xmat2 = [] while len(xmat2) < n: if self.kind == "UNIFORM": np.random.seed(self.seed) xmat2 = np.random.uniform(size = (nEff, 2 * self.npar)) elif self.kind == "HALTON": xmat2 = haltonGenerate(2 * self.npar, nEff, self.seed) else: xmat2 = sobolGenerate(2 * self.npar, nEff, self.seed) for d in range(self.npar): ax = self.axisRatios[d] if ax <= 1.: xmat2 = xmat2[xmat2[:, 2 * d + 1] <= ax] else: xmat2 = xmat2[xmat2[:, 2 * d] <= 1. / ax] xmat2[:, 2 * d : 2 * d + 2] *= ax nEff += 1 xmat = np.empty((n, self.npar), dtype = np.complex) for d in range(self.npar): a = self.lims(0, d) ** self.scalingExp[d] b = self.lims(1, d) ** self.scalingExp[d] xmat[:, d] = a + (b - a) * (xmat2[: n, 2 * d] + 1.j * self.axisRatios[d] * (xmat2[: n, 2 * d + 1] - .5)) xmat[:, d] **= 1. / self.scalingExp[d] x = checkParameterList(xmat, self.npar)[0] return x diff --git a/rrompy/parameter/parameter_sampling/shape/random_circle_sampler.py b/rrompy/parameter/parameter_sampling/shape/random_circle_sampler.py index 6a3ab1f..f3408a1 100644 --- a/rrompy/parameter/parameter_sampling/shape/random_circle_sampler.py +++ b/rrompy/parameter/parameter_sampling/shape/random_circle_sampler.py @@ -1,89 +1,62 @@ # Copyright (C) 2018 by the RROMPy authors # # This file is part of RROMPy. # # RROMPy is free software: you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # RROMPy is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with RROMPy. If not, see . # import numpy as np -from .generic_shape_sampler import GenericShapeSampler +from .generic_shape_random_sampler import GenericShapeRandomSampler from rrompy.utilities.numerical import (haltonGenerate, sobolGenerate, - cutOffDistance) -from rrompy.utilities.base.types import List, paramList -from rrompy.utilities.exception_manager import RROMPyException + potential) +from rrompy.utilities.base.types import paramList from rrompy.parameter import checkParameterList __all__ = ['RandomCircleSampler'] -class RandomCircleSampler(GenericShapeSampler): +class RandomCircleSampler(GenericShapeRandomSampler): """Generator of (quasi-)random sample points on ellipses.""" - allowedKinds = ["UNIFORM", "HALTON", "SOBOL"] - - def __init__(self, lims:paramList, kind : str = "UNIFORM", - axisRatios : List[float] = None, - scalingExp : List[float] = None, seed : int = 42): - super().__init__(lims = lims, scalingExp = scalingExp, - axisRatios = axisRatios) - self.kind = kind - self.seed = seed - - def __str__(self) -> str: - return "{}_{}".format(super().__str__(), self.kind) - - def __repr__(self) -> str: - return self.__str__() + " at " + hex(id(self)) - - @property - def kind(self): - """Value of kind.""" - return self._kind - @kind.setter - def kind(self, kind): - if kind.upper() not in self.allowedKinds: - raise RROMPyException("Generator kind not recognized.") - self._kind = kind.upper() - def generatePoints(self, n:int, reorder : bool = True) -> paramList: """Array of quadrature points.""" nEff = int(np.ceil(n * (4. / np.pi) ** self.npar * np.prod( [max(x, 1. / x) for x in self.axisRatios]))) xmat2 = [] while len(xmat2) < n: if self.kind == "UNIFORM": np.random.seed(self.seed) xmat2 = np.random.uniform(size = (nEff, 2 * self.npar)) elif self.kind == "HALTON": xmat2 = haltonGenerate(2 * self.npar, nEff, self.seed) else: xmat2 = sobolGenerate(2 * self.npar, nEff, self.seed) + xmat2 = xmat2 * 2. - 1. for d in range(self.npar): ax = self.axisRatios[d] - focus = .5 * (1. + 0.j - ax ** 2.) ** .5 - foci = [- focus + .5 + .5j * ax, focus + .5 + .5j * ax] - scorebase = cutOffDistance(.5j * ax * np.ones(1), foci) if ax > 1.: xmat2[:, 2 * d : 2 * d + 2] *= ax Z = xmat2[:, 2 * d] + 1.j * ax * xmat2[:, 2 * d + 1] - ptscore = cutOffDistance(Z, foci) - xmat2 = xmat2[ptscore <= scorebase] + ptscore = potential(Z, self.normalFoci(d)) + xmat2 = xmat2[ptscore <= self.groundPotential(d)] nEff += 1 xmat = np.empty((n, self.npar), dtype = np.complex) for d in range(self.npar): + ax = self.axisRatios[d] a = self.lims(0, d) ** self.scalingExp[d] b = self.lims(1, d) ** self.scalingExp[d] - xmat[:, d] = a + (b - a) * (xmat2[: n, 2 * d] - + 1.j * self.axisRatios[d] * (xmat2[: n, 2 * d + 1] - .5)) + c, r = (a + b) / 2., (a - b) / 2. + xmat[:, d] = c + r * (xmat2[: n, 2 * d] + + 1.j * ax * (xmat2[: n, 2 * d + 1] - .5)) xmat[:, d] **= 1. / self.scalingExp[d] x = checkParameterList(xmat, self.npar)[0] return x