Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F60884631
trained_model_rational.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, May 3, 03:59
Size
5 KB
Mime Type
text/x-python
Expires
Sun, May 5, 03:59 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17434495
Attached To
R6746 RationalROMPy
trained_model_rational.py
View Options
# 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
verbosityManager
as
vbMng
,
freepar
as
fp
from
rrompy.utilities.exception_manager
import
RROMPyException
from
rrompy.parameter
import
(
checkParameter
,
checkParameterList
,
emptyParameterList
)
from
rrompy.sampling
import
sampleList
__all__
=
[
'TrainedModelRational'
]
class
TrainedModelRational
(
TrainedModel
):
"""
ROM approximant evaluation for rational 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
)[
0
]
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
=
[])
->
sampList
:
"""
Evaluate rational numerator at arbitrary parameter.
Args:
mu: Target parameter.
"""
mu
=
checkParameterList
(
mu
,
self
.
data
.
npar
)[
0
]
vbMng
(
self
,
"INIT"
,
"Evaluating numerator at mu = {}."
.
format
(
mu
),
17
)
muCenter
=
self
.
centerNormalize
(
mu
)
p
=
sampleList
(
self
.
data
.
P
(
muCenter
))
vbMng
(
self
,
"DEL"
,
"Done evaluating numerator."
,
17
)
return
p
def
getQVal
(
self
,
mu
:
Np1D
,
der
:
List
[
int
]
=
None
,
scl
:
Np1D
=
None
)
->
Np1D
:
"""
Evaluate rational denominator at arbitrary parameter.
Args:
mu: Target parameter.
der(optional): Derivatives to take before evaluation.
"""
mu
=
checkParameterList
(
mu
,
self
.
data
.
npar
)[
0
]
vbMng
(
self
,
"INIT"
,
"Evaluating denominator at mu = {}."
.
format
(
mu
),
17
)
muCenter
=
self
.
centerNormalize
(
mu
)
q
=
self
.
data
.
Q
(
muCenter
,
der
,
scl
)
vbMng
(
self
,
"DEL"
,
"Done evaluating denominator."
,
17
)
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
)[
0
]
if
(
not
hasattr
(
self
,
"lastSolvedApproxReduced"
)
or
self
.
lastSolvedApproxReduced
!=
mu
):
vbMng
(
self
,
"INIT"
,
"Evaluating approximant at mu = {}."
.
format
(
mu
),
12
)
self
.
uApproxReduced
=
self
.
getPVal
(
mu
)
/
self
.
getQVal
(
mu
)
vbMng
(
self
,
"DEL"
,
"Done evaluating approximant."
,
12
)
self
.
lastSolvedApproxReduced
=
mu
return
self
.
uApproxReduced
def
getPoles
(
self
,
*
args
,
**
kwargs
)
->
Np1D
:
"""
Obtain approximant poles.
Returns:
Numpy complex vector of poles.
"""
if
len
(
args
)
+
len
(
kwargs
)
>
1
:
raise
RROMPyException
((
"Wrong number of parameters passed. "
"Only 1 available."
))
elif
len
(
args
)
+
len
(
kwargs
)
==
1
:
if
len
(
args
)
==
1
:
mVals
=
args
[
0
]
else
:
mVals
=
kwargs
[
"marginalVals"
]
if
not
hasattr
(
mVals
,
"__len__"
):
mVals
=
[
mVals
]
mVals
=
list
(
mVals
)
else
:
mVals
=
[
fp
]
try
:
rDim
=
mVals
.
index
(
fp
)
if
rDim
<
len
(
mVals
)
-
1
and
fp
in
mVals
[
rDim
+
1
:]:
raise
except
:
raise
RROMPyException
((
"Exactly 1 'freepar' entry in "
"marginalVals must be provided."
))
mVals
[
rDim
]
=
self
.
data
.
mu0
(
rDim
)
mVals
=
self
.
centerNormalize
(
checkParameter
(
mVals
,
len
(
mVals
)))
mVals
=
list
(
mVals
.
data
.
flatten
())
mVals
[
rDim
]
=
fp
return
np
.
power
(
self
.
data
.
mu0
(
rDim
)
**
self
.
data
.
rescalingExp
[
rDim
]
+
self
.
data
.
scaleFactor
[
rDim
]
*
self
.
data
.
Q
.
roots
(
mVals
),
1.
/
self
.
data
.
rescalingExp
[
rDim
])
def
getResidues
(
self
,
*
args
,
**
kwargs
)
->
Np1D
:
"""
Obtain approximant residues.
Returns:
Numpy matrix with residues as columns.
"""
pls
=
self
.
getPoles
(
*
args
,
**
kwargs
)
if
len
(
args
)
==
1
:
mVals
=
args
[
0
]
else
:
mVals
=
kwargs
[
"marginalVals"
]
if
not
hasattr
(
mVals
,
"__len__"
):
mVals
=
[
mVals
]
mVals
=
list
(
mVals
)
rDim
=
mVals
.
index
(
fp
)
poles
=
emptyParameterList
()
poles
.
reset
((
len
(
pls
),
self
.
data
.
npar
),
dtype
=
pls
.
dtype
)
for
k
,
pl
in
enumerate
(
pls
):
poles
[
k
]
=
mVals
poles
.
data
[
k
,
rDim
]
=
pl
res
=
(
self
.
data
.
projMat
.
dot
(
self
.
getPVal
(
poles
)
.
data
)
/
self
.
getQVal
(
poles
,
list
(
1
*
(
np
.
arange
(
self
.
data
.
npar
)
==
rDim
))))
return
pls
,
res
Event Timeline
Log In to Comment