Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F61248449
vander.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
Sun, May 5, 11:37
Size
5 KB
Mime Type
text/x-python
Expires
Tue, May 7, 11:37 (2 d)
Engine
blob
Format
Raw Data
Handle
17485389
Attached To
R6746 RationalROMPy
vander.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
collections.abc
import
Iterable
from
.base
import
polybases
from
.der
import
polyder
from
rrompy.utilities.base.types
import
Np1D
,
Np2D
,
List
,
paramList
from
rrompy.utilities.numerical.degree
import
totalDegreeSet
from
rrompy.parameter
import
checkParameterList
from
rrompy.utilities.exception_manager
import
RROMPyException
,
RROMPyAssert
__all__
=
[
'polyvander'
]
def
firstDerTransition
(
dim
:
int
,
TDirac
:
List
[
Np2D
],
basis
:
str
,
scl
:
Np1D
=
None
)
->
Np2D
:
"""Manage step from function samples to function derivatives."""
C_m
=
np
.
zeros
((
dim
,
len
(
TDirac
),
len
(
TDirac
)),
dtype
=
float
)
for
j
,
Tj
in
enumerate
(
TDirac
):
m
,
om
=
[
0
]
*
dim
,
[(
0
,
0
)]
*
dim
for
idx
in
range
(
dim
):
m
[
idx
],
om
[
idx
]
=
1
,
(
0
,
1
)
J_der
=
polyder
(
Tj
,
basis
,
m
,
scl
)
if
J_der
.
size
!=
len
(
TDirac
):
J_der
=
np
.
pad
(
J_der
,
mode
=
"constant"
,
pad_width
=
om
)
C_m
[
idx
,
:,
j
]
=
np
.
ravel
(
J_der
)
m
[
idx
],
om
[
idx
]
=
0
,
(
0
,
0
)
return
C_m
def
polyvander
(
x
:
paramList
,
degs
:
List
[
int
],
basis
:
str
,
derIdxs
:
List
[
List
[
List
[
int
]]]
=
None
,
reorder
:
List
[
int
]
=
None
,
scl
:
Np1D
=
None
,
forceTotalDegree
:
bool
=
False
)
->
Np2D
:
"""
Compute full Hermite-Vandermonde matrix with specified derivative
directions.
E.g. assume that we want to obtain the Vandermonde matrix for
(value, derx, derx2) at x = [0, 0],
(value, dery) at x = [1, 0],
(dery, derxy) at x = [0, 0],
of degree 3 in x and 1 in y, using Chebyshev polynomials.
This can be done by
polyvander([[0, 0], [1, 0]], # unique sample points
[3, 1], # polynomial degree
"chebyshev", # polynomial family
[
[[0, 0], [1, 0], [2, 0], [0, 1], [1, 1]],
# derivative directions at first point
[[0, 0], [0, 1]] # derivative directions at second point
],
[0, 1, 2, 5, 6, 3, 4] # reorder indices
)
"""
x
=
checkParameterList
(
x
)
dim
=
x
.
shape
[
1
]
totalDeg
=
(
forceTotalDegree
or
not
isinstance
(
degs
,
(
list
,
tuple
,
np
.
ndarray
,)))
if
forceTotalDegree
and
isinstance
(
degs
,
(
list
,
tuple
,
np
.
ndarray
,)):
if
np
.
any
(
np
.
array
(
degs
)
!=
degs
[
0
]):
raise
RROMPyException
((
"Cannot force total degree if prescribed "
"degrees are different"
))
degs
=
degs
[
0
]
if
not
isinstance
(
degs
,
(
list
,
tuple
,
np
.
ndarray
,)):
degs
=
[
degs
]
*
dim
RROMPyAssert
(
len
(
degs
),
dim
,
"Number of parameters"
)
x_un
,
idx_un
=
x
.
unique
(
return_inverse
=
True
)
if
len
(
x_un
)
<
len
(
x
):
raise
RROMPyException
(
"Sample points must be distinct."
)
del
x_un
if
basis
.
upper
()
not
in
polybases
:
raise
RROMPyException
(
"Polynomial basis not recognized."
)
vanderbase
=
{
"CHEBYSHEV"
:
np
.
polynomial
.
chebyshev
.
chebvander
,
"LEGENDRE"
:
np
.
polynomial
.
legendre
.
legvander
,
"MONOMIAL"
:
np
.
polynomial
.
polynomial
.
polyvander
}[
basis
.
upper
()]
VanBase
=
vanderbase
(
x
(
0
),
degs
[
0
])
for
j
in
range
(
1
,
dim
):
VNext
=
vanderbase
(
x
(
j
),
degs
[
j
])
for
jj
in
range
(
j
):
VNext
=
np
.
expand_dims
(
VNext
,
1
)
VanBase
=
VanBase
[
...
,
None
]
*
VNext
VanBase
=
VanBase
.
reshape
((
len
(
x
),
-
1
))
if
derIdxs
is
None
or
VanBase
.
shape
[
-
1
]
<=
1
:
Van
=
VanBase
else
:
derFlat
,
idxRep
=
[],
[]
for
j
,
derIdx
in
enumerate
(
derIdxs
):
derFlat
+=
derIdx
[:]
idxRep
+=
[
j
]
*
len
(
derIdx
[:])
for
j
in
range
(
len
(
derFlat
)):
if
not
isinstance
(
derFlat
[
j
],
Iterable
):
derFlat
[
j
]
=
[
derFlat
[
j
]]
RROMPyAssert
(
len
(
derFlat
[
j
]),
dim
,
"Number of dimensions"
)
#manage mixed derivatives
TDirac
=
[
y
.
reshape
([
d
+
1
for
d
in
degs
])
for
y
in
np
.
eye
(
VanBase
.
shape
[
-
1
],
dtype
=
int
)]
Cs_loc
=
firstDerTransition
(
dim
,
TDirac
,
basis
,
scl
)
Van
=
np
.
empty
((
len
(
derFlat
),
VanBase
.
shape
[
-
1
]),
dtype
=
VanBase
.
dtype
)
for
j
in
range
(
len
(
derFlat
)):
Van
[
j
,
:]
=
VanBase
[
idxRep
[
j
],
:]
for
k
in
range
(
dim
):
for
der
in
range
(
derFlat
[
j
][
k
]):
Van
[
j
,
:]
=
Van
[
j
,
:]
.
dot
(
Cs_loc
[
k
])
/
(
der
+
1
)
if
reorder
is
not
None
:
Van
=
Van
[
reorder
,
:]
if
not
totalDeg
:
return
Van
derIdxs
,
mask
=
totalDegreeSet
(
degs
[
0
],
dim
,
return_mask
=
True
)
ordIdxs
=
np
.
empty
(
len
(
derIdxs
),
dtype
=
int
)
derTotal
=
np
.
array
([
np
.
sum
(
y
)
for
y
in
derIdxs
])
idxPrev
=
0
rangeAux
=
np
.
arange
(
len
(
derIdxs
))
for
j
in
range
(
degs
[
0
]
+
1
):
idxLocal
=
rangeAux
[
derTotal
==
j
][::
-
1
]
idxPrev
+=
len
(
idxLocal
)
ordIdxs
[
idxPrev
-
len
(
idxLocal
)
:
idxPrev
]
=
idxLocal
return
Van
[:,
mask
][:,
ordIdxs
]
Event Timeline
Log In to Comment