Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F85444482
utilities.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, Sep 29, 06:52
Size
5 KB
Mime Type
text/x-python
Expires
Tue, Oct 1, 06:52 (2 d)
Engine
blob
Format
Raw Data
Handle
21182525
Attached To
R6746 RationalROMPy
utilities.py
View Options
#!/usr/bin/python
import
os
import
warnings
import
numpy
as
np
from
RROMPyTypes
import
Any
,
ListAny
,
DictAny
,
Np1DLst
,
List
,
Np1D
def
findDictStrKey
(
key
:
Any
,
keyList
:
ListAny
):
for
akey
in
keyList
:
if
isinstance
(
key
,
str
)
and
key
.
lower
()
==
akey
.
lower
():
return
akey
return
None
def
purgeList
(
lst
:
ListAny
,
allowedEntries
:
ListAny
=
[],
silent
:
bool
=
False
,
complement
:
bool
=
False
,
listname
:
str
=
""
):
if
listname
!=
""
:
listname
=
" in "
+
listname
lstcp
=
[]
for
x
in
lst
:
ax
=
findDictStrKey
(
x
,
allowedEntries
)
if
(
ax
is
None
)
!=
complement
:
if
not
silent
:
warnings
.
warn
(
"Ignoring entry {0}{1}."
.
format
(
x
,
listname
))
else
:
lstcp
=
lstcp
+
[
ax
]
return
lstcp
def
purgeDict
(
dct
:
DictAny
,
allowedKeys
:
ListAny
=
[],
silent
:
bool
=
False
,
complement
:
bool
=
False
,
dictname
:
str
=
""
):
if
dictname
!=
""
:
dictname
=
" in "
+
dictname
dctcp
=
{}
for
key
in
dct
.
keys
():
akey
=
findDictStrKey
(
key
,
allowedKeys
)
if
(
akey
is
None
)
!=
complement
:
if
not
silent
:
warnings
.
warn
(
"Ignoring key {0}{2} with value {1}."
\
.
format
(
key
,
dct
[
key
],
dictname
))
else
:
if
akey
is
None
:
akey
=
key
dctcp
[
akey
]
=
dct
[
key
]
return
dctcp
def
getNewFilename
(
prefix
:
str
=
""
,
extension
:
str
=
"dat"
)
->
str
:
n
=
len
(
extension
)
+
1
filename
=
"{}{}.{}"
.
format
(
prefix
,
np
.
random
.
randint
(
0
,
10
),
extension
)
while
os
.
path
.
exists
(
filename
):
filename
=
filename
[:
-
n
]
+
"{}.{}"
.
format
(
np
.
random
.
randint
(
10
),
extension
)
return
filename
prime_v
=
[]
#memoization vector
def
squareResonances
(
a
:
int
,
b
:
int
,
zero_terms
:
bool
=
True
):
spectrum
=
[]
a
=
max
(
int
(
np
.
floor
(
a
)),
0
)
b
=
max
(
int
(
np
.
ceil
(
b
)),
0
)
global
prime_v
if
len
(
prime_v
)
==
0
:
prime_v
=
[
2
,
3
]
if
a
>
prime_v
[
-
1
]:
for
i
in
range
(
prime_v
[
-
1
],
a
,
2
):
get_next_prime_factor
(
i
)
for
i
in
range
(
a
,
b
+
1
):
spectrum
=
spectrum
+
[
i
]
*
count_square_sums
(
i
,
zero_terms
)
return
np
.
array
(
spectrum
)
def
get_next_prime_factor
(
n
:
int
):
global
prime_v
for
x
in
prime_v
:
if
n
%
x
==
0
:
return
x
if
prime_v
[
-
1
]
<
n
:
prime_v
=
prime_v
+
[
n
]
return
n
def
prime_factorize
(
n
:
int
):
factors
=
[]
number
=
n
while
number
>
1
:
factor
=
get_next_prime_factor
(
number
)
factors
.
append
(
factor
)
number
=
int
(
number
/
factor
)
if
n
<
-
1
:
factors
[
0
]
=
-
factors
[
0
]
return
list
(
factors
)
def
count_square_sums
(
n
:
int
,
zero_terms
:
bool
=
True
):
if
n
<
2
:
return
(
n
+
1
)
*
zero_terms
factors
=
prime_factorize
(
n
)
funique
,
fcounts
=
np
.
unique
(
factors
,
return_counts
=
True
)
count
=
1
for
fac
,
con
in
zip
(
funique
,
fcounts
):
#using number theory magic
if
fac
%
4
==
1
:
count
=
count
*
(
con
+
1
)
elif
fac
%
4
==
3
and
con
%
2
==
1
:
return
0
return
count
+
(
2
*
zero_terms
-
1
)
*
(
int
(
n
**
.
5
)
**
2
==
n
)
def
listify
(
u
:
Np1DLst
,
d
:
int
)
->
List
[
Np1D
]:
"""
Convert numpy array into list-like format.
Args:
u: numpy complex array with function dofs or list of such.
d: secondary dimension.
Returns:
List-like numpy array of numpy arrays.
"""
if
isinstance
(
u
,
(
list
,))
or
len
(
u
)
==
d
:
if
len
(
u
)
!=
d
:
raise
Exception
((
"Input list lenght must be equal to "
"augmentation dimension d."
))
N
=
len
(
u
[
0
])
for
x
in
u
[
1
:]:
if
len
(
x
)
!=
N
:
raise
Exception
((
"Elements of input list must all have "
"the same lenght."
))
return
np
.
array
(
u
)
else
:
N
=
int
(
len
(
u
)
/
d
)
if
not
np
.
isclose
(
len
(
u
),
d
*
N
):
raise
Exception
((
"Input vector lenght must be multiple of "
"augmentation dimension d."
))
v
=
[
None
]
*
d
for
i
in
range
(
d
):
v
[
i
]
=
u
[
i
*
N
:
(
i
+
1
)
*
N
]
return
np
.
array
(
v
)
def
vectorify
(
u
:
Np1DLst
,
d
:
int
)
->
Np1D
:
"""
Convert list-like object into numpy array.
Args:
u: numpy complex array with function dofs or list of such.
d: secondary dimension.
Returns:
Numpy arrays.
"""
if
isinstance
(
u
,
(
list
,))
or
len
(
u
)
==
d
:
if
len
(
u
)
!=
d
:
raise
Exception
((
"Input list lenght must be equal to "
"augmentation dimension d."
))
N
=
len
(
u
[
0
])
for
x
in
u
[
1
:]:
if
len
(
x
)
!=
N
:
raise
Exception
((
"Elements of input list must all have "
"the same lenght."
))
return
np
.
concatenate
(
u
)
else
:
N
=
int
(
len
(
u
)
/
d
)
if
not
np
.
isclose
(
len
(
u
),
d
*
N
):
raise
Exception
((
"Input vector lenght must be multiple of"
"augmentation dimension d."
))
return
u
Event Timeline
Log In to Comment