Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F65633575
marginalize.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
Wed, Jun 5, 04:24
Size
2 KB
Mime Type
text/x-python
Expires
Fri, Jun 7, 04:24 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
18104642
Attached To
R6746 RationalROMPy
marginalize.py
View Options
# Copyright (C) 2018-2020 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/>.
#
from
copy
import
deepcopy
as
copy
from
numpy
import
array
,
polynomial
as
po
from
collections.abc
import
Iterable
from
.base
import
polybases
from
rrompy.utilities.base.types
import
Np1D
,
Np2D
from
rrompy.utilities.base
import
freepar
as
fp
from
rrompy.utilities.exception_manager
import
RROMPyException
__all__
=
[
'polymarginalize'
]
def
polymarginalize
(
c
:
Np2D
,
basis
:
str
,
marginalVals
:
Np1D
=
[
fp
],
nMarginal
:
int
=
None
)
->
Np1D
:
"""Marginalize out variable in polynomial."""
if
not
hasattr
(
c
,
"ndim"
):
c
=
array
(
c
)
ndim
=
c
.
ndim
if
not
isinstance
(
marginalVals
,
Iterable
):
marginalVals
=
[
marginalVals
]
marginalVals
=
list
(
marginalVals
)
if
basis
.
upper
()
not
in
polybases
:
raise
RROMPyException
(
"Polynomial basis not recognized."
)
polyvalbase
=
{
"CHEBYSHEV"
:
po
.
chebyshev
.
chebval
,
"LEGENDRE"
:
po
.
legendre
.
legval
,
"MONOMIAL"
:
po
.
polynomial
.
polyval
}[
basis
.
upper
()]
if
ndim
<
len
(
marginalVals
):
raise
RROMPyException
((
"Number of variables exceeds "
"dimensionality of coefficient tensor."
))
marginalDims
=
[]
for
j
in
range
(
len
(
marginalVals
)):
if
marginalVals
[
j
]
==
fp
:
marginalDims
+=
[
c
.
shape
[
j
]]
if
nMarginal
is
not
None
and
len
(
marginalDims
)
!=
nMarginal
:
raise
RROMPyException
((
"Exactly {} 'freepar' entries in marginalVals "
"must be provided."
)
.
format
(
nMarginal
))
cEff
=
[
copy
(
c
)]
for
d
in
range
(
ndim
):
if
marginalVals
[
d
]
!=
fp
:
for
dj
in
range
(
len
(
cEff
)):
cEff
[
dj
]
=
polyvalbase
(
marginalVals
[
d
],
cEff
[
dj
],
tensor
=
False
)
else
:
cEff2
=
[]
for
dj
in
range
(
len
(
cEff
)):
cEff2
+=
list
(
cEff
[
dj
])
cEff
=
copy
(
cEff2
)
return
array
(
cEff
)
.
reshape
(
tuple
(
marginalDims
))
Event Timeline
Log In to Comment