Page MenuHomec4science

trained_model_pade.py
No OneTemporary

File Metadata

Created
Wed, Oct 2, 20:21

trained_model_pade.py

# 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 <http://www.gnu.org/licenses/>.
#
import numpy as np
from . import TrainedModel
from rrompy.utilities.base.types import (Np1D, List, paramVal, paramList,
sampList)
from rrompy.utilities.base import verbosityDepth
from rrompy.utilities.poly_fitting.polynomial import polyval, polyroots
from rrompy.utilities.exception_manager import RROMPyAssert
from rrompy.parameter import checkParameterList
from rrompy.sampling import sampleList
__all__ = ['TrainedModelPade']
class TrainedModelPade(TrainedModel):
"""
ROM approximant evaluation for Pade' approximant.
Attributes:
Data: dictionary with all that can be pickled.
"""
def centerNormalize(self, mu : paramList = [],
mu0 : paramVal = None) -> paramList:
"""
Compute normalized parameter to be plugged into approximant.
Args:
mu: Parameter(s) 1.
mu0: Parameter(s) 2. If None, set to self.data.mu0.
Returns:
Normalized parameter.
"""
mu, _ = checkParameterList(mu, self.data.npar)
if mu0 is None: mu0 = self.data.mu0
rad = ((mu ** self.data.rescalingExp - mu0 ** self.data.rescalingExp)
/ self.data.scaleFactor)
return rad
def getPVal(self, mu : paramList = [], der : List[int] = None) -> sampList:
"""
Evaluate Pade' numerator at arbitrary parameter.
Args:
mu: Target parameter.
der(optional): Derivatives to take before evaluation.
"""
mu, _ = checkParameterList(mu, self.data.npar)
if self.verbosity >= 17:
verbosityDepth("INIT", ("Evaluating numerator at mu = "
"{}.").format(mu),
timestamp = self.timestamp)
muCenter = self.centerNormalize(mu)
p = sampleList(polyval(muCenter, self.data.P.T,
self.data.polytype, der))
if self.verbosity >= 17:
verbosityDepth("DEL", "Done evaluating numerator.",
timestamp = self.timestamp)
return p
def getQVal(self, mu:Np1D, der : List[int] = None,
scl : Np1D = None) -> Np1D:
"""
Evaluate Pade' denominator at arbitrary parameter.
Args:
mu: Target parameter.
der(optional): Derivatives to take before evaluation.
"""
mu, _ = checkParameterList(mu, self.data.npar)
if self.verbosity >= 17:
verbosityDepth("INIT", ("Evaluating denominator at mu = "
"{}.").format(mu),
timestamp = self.timestamp)
muCenter = self.centerNormalize(mu)
q = polyval(muCenter, self.data.Q, self.data.polytype, der, scl)
if self.verbosity >= 17:
verbosityDepth("DEL", "Done evaluating denominator.",
timestamp = self.timestamp)
return q
def getApproxReduced(self, mu : paramList = []) -> sampList:
"""
Evaluate reduced representation of approximant at arbitrary parameter.
Args:
mu: Target parameter.
"""
mu, _ = checkParameterList(mu, self.data.npar)
if (not hasattr(self, "lastSolvedAppReduced")
or self.lastSolvedAppReduced != mu):
if self.verbosity >= 12:
verbosityDepth("INIT", ("Evaluating approximant at mu = "
"{}.").format(mu),
timestamp = self.timestamp)
self.uAppReduced = self.getPVal(mu) / self.getQVal(mu)
if self.verbosity >= 12:
verbosityDepth("DEL", "Done evaluating approximant.",
timestamp = self.timestamp)
self.lastSolvedAppReduced = mu
return self.uAppReduced
def getPoles(self) -> Np1D:
"""
Obtain approximant poles.
Returns:
Numpy complex vector of poles.
"""
RROMPyAssert(self.data.npar, 1, "Number of parameters")
return np.power(self.data.mu0(0) ** self.data.rescalingExp[0]
+ self.data.scaleFactor
* polyroots(self.data.Q, self.data.polytype),
1. / self.data.rescalingExp[0])
def getResidues(self) -> Np1D:
"""
Obtain approximant residues.
Returns:
Numpy matrix with residues as columns.
"""
pls = self.getPoles()
poles, _ = checkParameterList(pls, 1)
res = (self.data.projMat.dot(self.getPVal(poles).data)
/ self.getQVal(poles, 1))
return pls, res

Event Timeline