Page MenuHomec4science

trained_model_pade.py
No OneTemporary

File Metadata

Created
Fri, May 3, 12:16

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
from rrompy.utilities.base import verbosityDepth
from rrompy.utilities.poly_fitting import polyvalder, polyroots
__all__ = ['TrainedModelPade']
class TrainedModelPade(TrainedModel):
"""
ROM approximant evaluation for Pade' approximant.
Attributes:
Data: dictionary with all that can be pickled.
"""
def radiusPade(self, mu:Np1D, mu0 : float = None) -> float:
"""
Compute translated radius to be plugged into Pade' approximant.
Args:
mu: Parameter(s) 1.
mu0: Parameter(s) 2. If None, set to self.data.mu0.
Returns:
Translated radius to be plugged into Pade' approximant.
"""
if mu0 is None: mu0 = self.data.mu0
return (np.power(mu, self.data.rescalingExp)
- np.power(mu0, self.data.rescalingExp)) / self.data.scaleFactor
def getPVal(self, mu:Np1D, der : int = 0):
"""
Evaluate Pade' numerator at arbitrary parameter.
Args:
mu: Target parameter.
der(optional): Derivatives to take before evaluation.
"""
if self.verbosity >= 10:
mustr = mu
try:
nmu = len(mu)
if nmu > 2:
mustr = "[{} ..({}).. {}]".format(mu[0], nmu - 2, mu[-1])
except: pass
verbosityDepth("INIT", ("Evaluating numerator at mu = "
"{}.").format(mustr),
timestamp = self.timestamp)
try:
len(mu)
except:
mu = [mu]
p = polyvalder[self.data.polytype](self.radiusPade(mu),
self.data.P.T, der)
if len(mu) == 1:
p = p.flatten()
if self.verbosity >= 10:
verbosityDepth("DEL", "Done evaluating numerator.",
timestamp = self.timestamp)
return p
def getQVal(self, mu:Np1D, der : int = 0, scl : float = 1.):
"""
Evaluate Pade' denominator at arbitrary parameter.
Args:
mu: Target parameter.
der(optional): Derivatives to take before evaluation.
"""
if self.verbosity >= 10:
mustr = mu
try:
nmu = len(mu)
if nmu > 2:
mustr = "[{} ..({}).. {}]".format(mu[0], nmu - 2, mu[-1])
except: pass
verbosityDepth("INIT", ("Evaluating denominator at mu = "
"{}.").format(mustr),
timestamp = self.timestamp)
q = polyvalder[self.data.polytype](self.radiusPade(mu),
self.data.Q, der, scl)
if self.verbosity >= 10:
verbosityDepth("DEL", "Done evaluating denominator.",
timestamp = self.timestamp)
return q
def getApproxReduced(self, mu:complex):
"""
Evaluate reduced representation of approximant at arbitrary parameter.
Args:
mu: Target parameter.
"""
if (not hasattr(self, "lastSolvedAppReduced")
or not np.isclose(self.lastSolvedAppReduced, mu)):
if self.verbosity >= 5:
mustr = mu
try:
nmu = len(mu)
if nmu > 2:
mustr = "[{} ..({}).. {}]".format(mu[0], nmu - 2,
mu[-1])
except: pass
verbosityDepth("INIT", ("Evaluating approximant at mu = "
"{}.").format(mustr),
timestamp = self.timestamp)
self.uAppReduced = self.getPVal(mu) / self.getQVal(mu)
if self.verbosity >= 5:
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.
"""
return np.power(np.power(self.data.mu0, self.data.rescalingExp)
+ self.data.scaleFactor
* polyroots[self.data.polytype](self.data.Q),
1. / self.data.rescalingExp)
def getResidues(self) -> Np1D:
"""
Obtain approximant residues.
Returns:
Numpy matrix with residues as columns.
"""
poles = self.getPoles()
return (self.data.projMat.dot(self.getPVal(poles))
/ self.getQVal(poles, 1))

Event Timeline