Mentions légales du service
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
faust
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Admin message
GitLab upgrade completed. Current version is 17.11.4.
Show more breadcrumbs
faust group
faust
Commits
952848b6
Commit
952848b6
authored
2 years ago
by
hhakim
Browse files
Options
Downloads
Patches
Plain Diff
Update unit tests of LazyLinearOp to handle the switch to the new impl. by default.
parent
b9db4ab4
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
wrapper/python/pyfaust/tests/TestLazyLinearOp.py
+72
-53
72 additions, 53 deletions
wrapper/python/pyfaust/tests/TestLazyLinearOp.py
wrapper/python/pyfaust/tests/TestLazyLinearOp2.py
+0
-261
0 additions, 261 deletions
wrapper/python/pyfaust/tests/TestLazyLinearOp2.py
with
72 additions
and
314 deletions
wrapper/python/pyfaust/tests/TestLazyLinearOp.py
+
72
−
53
View file @
952848b6
import
unittest
import
pyfaust
as
pf
from
pyfaust.lazylinop
import
LazyLinearOp
,
vstack
,
hstack
from
pyfaust.lazylinop
import
(
LazyLinearOp
,
vstack
,
hstack
,
LazyLinearOp
,
LazyLinearOperator
,
aslazylinearoperator
)
import
numpy.linalg
as
LA
import
numpy
as
np
class
TestLazyLinearOpFaust
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
lop
=
L
azy
L
inear
Op
.
create
(
pf
.
rand
(
10
,
15
))
self
.
lop
=
asl
azy
l
inear
operator
(
pf
.
rand
(
10
,
15
))
self
.
lopA
=
self
.
lop
.
toarray
()
self
.
lop2
=
L
azy
L
inear
Op
.
create
(
pf
.
rand
(
10
,
15
))
self
.
lop2
=
asl
azy
l
inear
operator
(
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
]
))
self
.
lop2A
=
self
.
lop2
.
toarray
()
self
.
lop3
=
L
azy
L
inear
Op
.
create
(
pf
.
rand
(
15
,
10
))
self
.
lop3
=
asl
azy
l
inear
operator
(
pf
.
rand
(
self
.
lop
.
shape
[
1
],
self
.
lop
.
shape
[
0
]
))
self
.
lop3A
=
self
.
lop3
.
toarray
()
def
test_shape
(
self
):
self
.
assertEqual
(
self
.
lop
.
shape
,
self
.
lop
.
eval
()
.
shape
)
self
.
assertEqual
(
self
.
lop
.
shape
,
self
.
lop
A
.
shape
)
def
test_ndim
(
self
):
self
.
assertEqual
(
self
.
lop
.
ndim
,
2
)
...
...
@@ -79,11 +80,11 @@ class TestLazyLinearOpFaust(unittest.TestCase):
def
test_matmul_dot_matvec
(
self
):
from
scipy.sparse
import
csr_matrix
,
issparse
lmul
=
self
.
lop
@
self
.
lop3
self
.
assertTrue
(
pf
.
lazylinop
.
isLazyLinearOp
(
lmul
))
self
.
assertTrue
(
pf
.
lazylinop
.
LazyLinearOp
.
isLazyLinearOp
(
lmul
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul
.
toarray
()
-
(
self
.
lopA
@
self
.
lop3A
)),
0
)
lmul
=
self
.
lop
.
dot
(
self
.
lop3
)
self
.
assertTrue
(
pf
.
lazylinop
.
isLazyLinearOp
(
lmul
))
self
.
assertTrue
(
pf
.
lazylinop
.
LazyLinearOp
.
isLazyLinearOp
(
lmul
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul
.
toarray
()
-
(
self
.
lopA
@
self
.
lop3A
)),
0
)
M
=
np
.
random
.
rand
(
self
.
lop
.
shape
[
1
],
15
)
...
...
@@ -92,10 +93,11 @@ class TestLazyLinearOpFaust(unittest.TestCase):
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lopA
@
M
)),
0
)
lmul2
=
self
.
lop
@
csr_matrix
(
M
)
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lop
@
M
)),
0
)
if
self
.
__class__
==
TestLazyLinearOpFaust
:
lmul2
=
self
.
lop
@
csr_matrix
(
M
)
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lop
@
M
)),
0
)
lmul2
=
self
.
lop
.
matvec
(
M
[:,
0
])
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
...
...
@@ -103,7 +105,7 @@ class TestLazyLinearOpFaust(unittest.TestCase):
0
)
S
=
csr_matrix
(
M
)
lmul3
=
pf
.
lazylinop
.
as
L
azy
L
inear
Op
(
S
)
@
S
.
T
lmul3
=
pf
.
lazylinop
.
as
l
azy
l
inear
operator
(
S
)
@
S
.
T
self
.
assertTrue
(
issparse
(
lmul3
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul3
-
(
M
@
M
.
T
)),
0
)
...
...
@@ -121,14 +123,15 @@ class TestLazyLinearOpFaust(unittest.TestCase):
def
test_mul
(
self
):
v
=
np
.
random
.
rand
(
self
.
lop
.
shape
[
1
])
lmul2
=
self
.
lop
*
v
self
.
assertTrue
(
isinstance
(
lmul2
,
LazyLinearOp
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
.
toarray
()
-
(
self
.
lopA
*
v
)),
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lopA
@
v
)),
0
)
v
=
np
.
random
.
rand
(
1
,
self
.
lop
.
shape
[
1
])
v
=
np
.
random
.
rand
(
self
.
lop
.
shape
[
1
]
,
1
)
lmul2
=
self
.
lop
*
v
self
.
assertTrue
(
isinstance
(
lmul2
,
LazyLinearOp
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
.
toarray
()
-
(
self
.
lopA
*
v
)),
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lopA
@
v
)),
0
)
s
=
np
.
random
.
rand
(
1
,
1
)[
0
,
0
]
lmul2
=
self
.
lop
*
s
self
.
assertTrue
(
isinstance
(
lmul2
,
LazyLinearOp
))
...
...
@@ -136,19 +139,20 @@ class TestLazyLinearOpFaust(unittest.TestCase):
0
)
def
test_rmul
(
self
):
v
=
np
.
random
.
rand
(
self
.
lop
.
shape
[
1
])
v
=
np
.
random
.
rand
(
self
.
lop
.
shape
[
0
])
lmul2
=
v
*
self
.
lop
self
.
assertTrue
(
isinstance
(
lmul2
,
LazyLinearOp
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
.
toarray
()
-
(
v
*
self
.
lopA
)),
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
v
@
self
.
lopA
)),
0
)
v
=
np
.
random
.
rand
(
1
,
self
.
lop
.
shape
[
1
])
lmul2
=
v
*
self
.
lop
self
.
assertTrue
(
isinstance
(
lmul2
,
LazyLinearOp
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
.
toarray
()
-
(
v
*
self
.
lopA
)),
v
=
np
.
random
.
rand
(
1
,
self
.
lop
.
shape
[
0
])
lmul2
=
v
@
self
.
lop
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
v
@
self
.
lopA
)),
0
)
s
=
np
.
random
.
rand
(
1
,
1
)[
0
,
0
]
self
.
assertTrue
(
np
.
isscalar
(
s
))
lmul2
=
s
*
self
.
lop
self
.
assertTrue
(
isinstance
(
lmul2
,
LazyLinearOp
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
.
toarray
()
-
(
s
*
self
.
lopA
)),
...
...
@@ -172,29 +176,29 @@ class TestLazyLinearOpFaust(unittest.TestCase):
0
)
self
.
assertEqual
(
lcat
.
shape
[
0
],
self
.
lop
.
shape
[
0
]
+
self
.
lop
.
shape
[
0
])
# using hstack and vstack
lcat
=
vstack
((
self
.
lop
,
self
.
lop2
,
self
.
lop
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lcat
.
toarray
()
-
np
.
vstack
((
self
.
lopA
,
self
.
lop2A
,
self
.
lopA
))),
0
)
self
.
assertEqual
(
lcat
.
shape
[
0
],
2
*
self
.
lop
.
shape
[
0
]
+
self
.
lop2
.
shape
[
0
])
lcat
=
hstack
((
self
.
lop
,
self
.
lop2
,
self
.
lopA
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lcat
.
toarray
()
-
np
.
hstack
((
self
.
lopA
,
self
.
lop2A
,
self
.
lopA
))),
0
)
self
.
assertEqual
(
lcat
.
shape
[
1
],
2
*
self
.
lop
.
shape
[
1
]
+
self
.
lop2
.
shape
[
1
])
#TODO: re-enable later (when LazyLinearOp will replace LazyLinearOp)
# lcat = vstack((self.lop, self.lop2, self.lop))
# self.assertAlmostEqual(LA.norm(lcat.toarray() - np.vstack((self.lopA,
# self.lop2A,
# self.lopA))),
# 0)
# self.assertEqual(lcat.shape[0], 2 * self.lop.shape[0] + self.lop2.shape[0])
# lcat = hstack((self.lop, self.lop2, self.lopA))
# self.assertAlmostEqual(LA.norm(lcat.toarray() - np.hstack((self.lopA,
# self.lop2A,
# self.lopA))),
# 0)
# self.assertEqual(lcat.shape[1], 2 * self.lop.shape[1] + self.lop2.shape[1])
def
test_chain_ops
(
self
):
lchain
=
self
.
lop
+
self
.
lop2
lchain
=
lchain
@
self
.
lop3
lchain
=
2
*
lchain
v
=
np
.
random
.
rand
(
lchain
.
shape
[
1
])
lchain
=
lchain
*
v
lchain
=
2
*
lchain
*
3
self
.
assertTrue
(
np
.
allclose
(
lchain
.
toarray
(),
6
*
(
self
.
lopA
+
self
.
lop2A
)
@
self
.
lop3A
))
lchain
=
lchain
.
concatenate
(
self
.
lop3
,
axis
=
0
)
mat_ref
=
np
.
vstack
(((
2
*
(
self
.
lopA
+
self
.
lop2A
)
@
self
.
lop3A
)
*
v
,
mat_ref
=
np
.
vstack
(((
2
*
(
self
.
lopA
+
self
.
lop2A
)
@
self
.
lop3A
*
3
)
,
self
.
lop3A
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lchain
.
toarray
()
-
mat_ref
),
0
)
...
...
@@ -207,35 +211,50 @@ class TestLazyLinearOpFaust(unittest.TestCase):
self
.
assertAlmostEqual
(
LA
.
norm
(
lslice
.
toarray
()
-
lsliceA
),
0
)
def
test_real
(
self
):
cF
=
pf
.
rand
(
10
,
15
,
field
=
'
complex
'
)
lcF
=
LazyLinearOp
.
create
(
cF
)
cF
=
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
]
,
field
=
'
complex
'
)
lcF
=
LazyLinearOp
.
create
_from_op
(
cF
)
lcF
=
lcF
.
real
self
.
assertAlmostEqual
(
LA
.
norm
(
lcF
.
toarray
()
-
cF
.
real
.
toarray
()),
0
)
def
test_imag
(
self
):
cF
=
pf
.
rand
(
10
,
15
,
field
=
'
complex
'
)
lcF
=
LazyLinearOp
.
create
(
cF
)
cF
=
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
]
,
field
=
'
complex
'
)
lcF
=
LazyLinearOp
.
create
_from_op
(
cF
)
lcF
=
lcF
.
imag
self
.
assertAlmostEqual
(
LA
.
norm
(
lcF
.
toarray
()
-
cF
.
imag
.
toarray
()),
0
)
def
test_aslazylinop
(
self
):
from
pyfaust.lazylinop
import
asLazyLinearOp
cF
=
pf
.
rand
(
10
,
15
,
field
=
'
complex
'
)
lcF
=
asLazyLinearOp
(
cF
)
self
.
assertTrue
(
pf
.
lazylinop
.
isLazyLinearOp
(
lcF
))
self
.
assertEqual
(
cF
.
shape
,
lcF
.
shape
)
from
pyfaust.lazylinop
import
aslazylinearoperator
cF
=
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
],
field
=
'
complex
'
)
#TODO: re-enable later (when LazyLinearOp will replace LazyLinearOp)
# lcF = aslazylinearoperator(cF)
# self.assertTrue(pf.lazylinop.LazyLinearOp.isLazyLinearOp(lcF))
# self.assertEqual(cF.shape, lcF.shape)
class
TestLazyLinearOpFFTFunc
(
TestLazyLinearOpFaust
):
def
setUp
(
self
):
from
scipy.fft
import
fft
,
ifft
# axis = 0 to be consistent with LazyLinearOp.toarray() which applies
# fft on columns of the matrix, not on the rows (axis=1)
self
.
lop
=
LazyLinearOperator
((
8
,
8
),
matmat
=
lambda
x
:
fft
(
x
,
axis
=
0
),
rmatmat
=
lambda
x
:
8
*
ifft
(
x
,
axis
=
0
))
self
.
lopA
=
self
.
lop
.
toarray
()
self
.
lop2
=
aslazylinearoperator
(
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
]))
self
.
lop2A
=
self
.
lop2
.
toarray
()
self
.
lop3
=
aslazylinearoperator
(
pf
.
rand
(
self
.
lop
.
shape
[
1
],
self
.
lop
.
shape
[
0
]))
self
.
lop3A
=
self
.
lop3
.
toarray
()
class
TestLazyLinearOpFaustKron
(
TestLazyLinearOpFaust
):
def
setUp
(
self
):
from
pyfaust.lazylinop
import
kron
as
lkron
lop_A
=
L
azy
L
inear
Op
.
create
(
pf
.
rand
(
10
,
15
))
lop_B
=
L
azy
L
inear
Op
.
create
(
pf
.
rand
(
10
,
15
))
lop_A
=
asl
azy
l
inear
operator
(
pf
.
rand
(
10
,
15
))
lop_B
=
asl
azy
l
inear
operator
(
pf
.
rand
(
10
,
15
))
self
.
lop
=
lkron
(
lop_A
,
lop_B
)
self
.
lopA
=
self
.
lop
.
toarray
()
self
.
lop2
=
L
azy
L
inear
Op
.
create
(
pf
.
rand
(
*
self
.
lop
.
shape
))
self
.
lop2
=
asl
azy
l
inear
operator
(
pf
.
rand
(
*
self
.
lop
.
shape
))
self
.
lop2A
=
self
.
lop2
.
toarray
()
self
.
lop3
=
L
azy
L
inear
Op
.
create
(
pf
.
rand
(
self
.
lop
.
shape
[
1
],
10
))
self
.
lop3
=
asl
azy
l
inear
operator
(
pf
.
rand
(
self
.
lop
.
shape
[
1
],
10
))
self
.
lop3A
=
self
.
lop3
.
toarray
()
if
'
__main__
'
==
__name__
:
...
...
This diff is collapsed.
Click to expand it.
wrapper/python/pyfaust/tests/TestLazyLinearOp2.py
deleted
100644 → 0
+
0
−
261
View file @
b9db4ab4
import
unittest
import
pyfaust
as
pf
from
pyfaust.lazylinop
import
(
LazyLinearOp2
,
vstack
,
hstack
,
LazyLinearOp2
,
LazyLinearOperator
,
aslazylinearoperator
)
import
numpy.linalg
as
LA
import
numpy
as
np
class
TestLazyLinearOpFaust
(
unittest
.
TestCase
):
def
setUp
(
self
):
self
.
lop
=
aslazylinearoperator
(
pf
.
rand
(
10
,
15
))
self
.
lopA
=
self
.
lop
.
toarray
()
self
.
lop2
=
aslazylinearoperator
(
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
]))
self
.
lop2A
=
self
.
lop2
.
toarray
()
self
.
lop3
=
aslazylinearoperator
(
pf
.
rand
(
self
.
lop
.
shape
[
1
],
self
.
lop
.
shape
[
0
]))
self
.
lop3A
=
self
.
lop3
.
toarray
()
def
test_shape
(
self
):
self
.
assertEqual
(
self
.
lop
.
shape
,
self
.
lopA
.
shape
)
def
test_ndim
(
self
):
self
.
assertEqual
(
self
.
lop
.
ndim
,
2
)
def
test_transp
(
self
):
lopT
=
self
.
lop
.
T
self
.
assertAlmostEqual
(
LA
.
norm
(
lopT
.
toarray
()
-
self
.
lopA
.
T
),
0
)
lopT
=
self
.
lop
.
transpose
()
self
.
assertAlmostEqual
(
LA
.
norm
(
lopT
.
toarray
()
-
self
.
lopA
.
T
),
0
)
self
.
assertEqual
(
self
.
lop
.
shape
[
0
],
lopT
.
shape
[
1
])
self
.
assertEqual
(
self
.
lop
.
shape
[
1
],
lopT
.
shape
[
0
])
def
test_conj
(
self
):
lopC
=
self
.
lop
.
conj
()
self
.
assertAlmostEqual
(
LA
.
norm
(
lopC
.
toarray
()
-
self
.
lopA
.
conj
()),
0
)
def
test_adjoint
(
self
):
lopH
=
self
.
lop
.
H
self
.
assertAlmostEqual
(
LA
.
norm
(
lopH
.
toarray
()
-
self
.
lopA
.
conj
().
T
),
0
)
lopH
=
self
.
lop
.
getH
()
self
.
assertAlmostEqual
(
LA
.
norm
(
lopH
.
toarray
()
-
self
.
lopA
.
conj
().
T
),
0
)
self
.
assertEqual
(
self
.
lop
.
shape
[
0
],
lopH
.
shape
[
1
])
self
.
assertEqual
(
self
.
lop
.
shape
[
1
],
lopH
.
shape
[
0
])
def
test_add
(
self
):
ladd
=
self
.
lop
+
self
.
lop2
self
.
assertAlmostEqual
(
LA
.
norm
(
ladd
.
toarray
()
-
(
self
.
lopA
+
self
.
lop2A
)),
0
)
M
=
np
.
random
.
rand
(
*
self
.
lop
.
shape
)
ladd2
=
self
.
lop
+
M
self
.
assertTrue
(
isinstance
(
ladd2
,
LazyLinearOp2
))
self
.
assertAlmostEqual
(
LA
.
norm
(
ladd2
.
toarray
()
-
(
self
.
lopA
+
M
)),
0
)
def
test_iadd
(
self
):
self
.
assertRaises
(
NotImplementedError
,
self
.
lop
.
__iadd__
,
self
.
lop2
)
def
test_radd
(
self
):
M
=
np
.
random
.
rand
(
*
self
.
lop
.
shape
)
ladd2
=
M
+
self
.
lop
self
.
assertTrue
(
isinstance
(
ladd2
,
LazyLinearOp2
))
self
.
assertAlmostEqual
(
LA
.
norm
(
ladd2
.
toarray
()
-
(
M
+
self
.
lopA
)),
0
)
def
test_sub
(
self
):
lsub
=
self
.
lop
-
self
.
lop2
self
.
assertAlmostEqual
(
LA
.
norm
(
lsub
.
toarray
()
-
(
self
.
lopA
-
self
.
lop2A
)),
0
)
M
=
np
.
random
.
rand
(
*
self
.
lop
.
shape
)
lsub2
=
self
.
lop
-
M
self
.
assertTrue
(
isinstance
(
lsub2
,
LazyLinearOp2
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lsub2
.
toarray
()
-
(
self
.
lopA
-
M
)),
0
)
def
test_rsub
(
self
):
M
=
np
.
random
.
rand
(
*
self
.
lop
.
shape
)
lsub2
=
M
-
self
.
lop
self
.
assertTrue
(
isinstance
(
lsub2
,
LazyLinearOp2
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lsub2
.
toarray
()
-
(
M
-
self
.
lopA
)),
0
)
def
test_isub
(
self
):
self
.
assertRaises
(
NotImplementedError
,
self
.
lop
.
__isub__
,
self
.
lop2
)
def
test_matmul_dot_matvec
(
self
):
from
scipy.sparse
import
csr_matrix
,
issparse
lmul
=
self
.
lop
@
self
.
lop3
self
.
assertTrue
(
pf
.
lazylinop
.
LazyLinearOp2
.
isLazyLinearOp
(
lmul
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul
.
toarray
()
-
(
self
.
lopA
@
self
.
lop3A
)),
0
)
lmul
=
self
.
lop
.
dot
(
self
.
lop3
)
self
.
assertTrue
(
pf
.
lazylinop
.
LazyLinearOp2
.
isLazyLinearOp
(
lmul
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul
.
toarray
()
-
(
self
.
lopA
@
self
.
lop3A
)),
0
)
M
=
np
.
random
.
rand
(
self
.
lop
.
shape
[
1
],
15
)
lmul2
=
self
.
lop
@
M
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lopA
@
M
)),
0
)
if
self
.
__class__
==
TestLazyLinearOpFaust
:
lmul2
=
self
.
lop
@
csr_matrix
(
M
)
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lop
@
M
)),
0
)
lmul2
=
self
.
lop
.
matvec
(
M
[:,
0
])
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lopA
@
M
[:,
0
])),
0
)
S
=
csr_matrix
(
M
)
lmul3
=
pf
.
lazylinop
.
asLazyLinearOp
(
S
)
@
S
.
T
self
.
assertTrue
(
issparse
(
lmul3
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul3
-
(
M
@
M
.
T
)),
0
)
def
test_rmatmul
(
self
):
M
=
np
.
random
.
rand
(
15
,
self
.
lop
.
shape
[
0
])
lmul2
=
M
@
self
.
lop
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
M
@
self
.
lopA
)),
0
)
def
test_imatmul
(
self
):
self
.
assertRaises
(
NotImplementedError
,
self
.
lop
.
__imatmul__
,
self
.
lop2
)
def
test_mul
(
self
):
v
=
np
.
random
.
rand
(
self
.
lop
.
shape
[
1
])
lmul2
=
self
.
lop
*
v
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lopA
@
v
)),
0
)
v
=
np
.
random
.
rand
(
self
.
lop
.
shape
[
1
],
1
)
lmul2
=
self
.
lop
*
v
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
self
.
lopA
@
v
)),
0
)
s
=
np
.
random
.
rand
(
1
,
1
)[
0
,
0
]
lmul2
=
self
.
lop
*
s
self
.
assertTrue
(
isinstance
(
lmul2
,
LazyLinearOp2
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
.
toarray
()
-
(
self
.
lopA
*
s
)),
0
)
def
test_rmul
(
self
):
v
=
np
.
random
.
rand
(
self
.
lop
.
shape
[
0
])
lmul2
=
v
*
self
.
lop
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
v
@
self
.
lopA
)),
0
)
v
=
np
.
random
.
rand
(
1
,
self
.
lop
.
shape
[
0
])
lmul2
=
v
@
self
.
lop
self
.
assertTrue
(
isinstance
(
lmul2
,
np
.
ndarray
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
-
(
v
@
self
.
lopA
)),
0
)
s
=
np
.
random
.
rand
(
1
,
1
)[
0
,
0
]
self
.
assertTrue
(
np
.
isscalar
(
s
))
lmul2
=
s
*
self
.
lop
self
.
assertTrue
(
isinstance
(
lmul2
,
LazyLinearOp2
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lmul2
.
toarray
()
-
(
s
*
self
.
lopA
)),
0
)
def
test_concatenate
(
self
):
lcat
=
self
.
lop
.
concatenate
(
self
.
lop2
,
axis
=
0
)
self
.
assertAlmostEqual
(
LA
.
norm
(
lcat
.
toarray
()
-
np
.
vstack
((
self
.
lopA
,
self
.
lop2A
))),
0
)
self
.
assertEqual
(
lcat
.
shape
[
0
],
self
.
lop
.
shape
[
0
]
+
self
.
lop2
.
shape
[
0
])
lcat
=
self
.
lop
.
concatenate
(
self
.
lop2
,
axis
=
1
)
self
.
assertAlmostEqual
(
LA
.
norm
(
lcat
.
toarray
()
-
np
.
hstack
((
self
.
lopA
,
self
.
lop2A
))),
0
)
self
.
assertEqual
(
lcat
.
shape
[
1
],
self
.
lop
.
shape
[
1
]
+
self
.
lop2
.
shape
[
1
])
# auto concat
lcat
=
self
.
lop
.
concatenate
(
self
.
lop
,
axis
=
0
)
self
.
assertAlmostEqual
(
LA
.
norm
(
lcat
.
toarray
()
-
np
.
vstack
((
self
.
lopA
,
self
.
lopA
))),
0
)
self
.
assertEqual
(
lcat
.
shape
[
0
],
self
.
lop
.
shape
[
0
]
+
self
.
lop
.
shape
[
0
])
# using hstack and vstack
#TODO: re-enable later (when LazyLinearOp2 will replace LazyLinearOp)
# lcat = vstack((self.lop, self.lop2, self.lop))
# self.assertAlmostEqual(LA.norm(lcat.toarray() - np.vstack((self.lopA,
# self.lop2A,
# self.lopA))),
# 0)
# self.assertEqual(lcat.shape[0], 2 * self.lop.shape[0] + self.lop2.shape[0])
# lcat = hstack((self.lop, self.lop2, self.lopA))
# self.assertAlmostEqual(LA.norm(lcat.toarray() - np.hstack((self.lopA,
# self.lop2A,
# self.lopA))),
# 0)
# self.assertEqual(lcat.shape[1], 2 * self.lop.shape[1] + self.lop2.shape[1])
def
test_chain_ops
(
self
):
lchain
=
self
.
lop
+
self
.
lop2
lchain
=
lchain
@
self
.
lop3
lchain
=
2
*
lchain
*
3
self
.
assertTrue
(
np
.
allclose
(
lchain
.
toarray
(),
6
*
(
self
.
lopA
+
self
.
lop2A
)
@
self
.
lop3A
))
lchain
=
lchain
.
concatenate
(
self
.
lop3
,
axis
=
0
)
mat_ref
=
np
.
vstack
(((
2
*
(
self
.
lopA
+
self
.
lop2A
)
@
self
.
lop3A
*
3
),
self
.
lop3A
))
self
.
assertAlmostEqual
(
LA
.
norm
(
lchain
.
toarray
()
-
mat_ref
),
0
)
def
test_get_item
(
self
):
n1
=
self
.
lop
.
shape
[
0
]
//
2
n2
=
self
.
lop
.
shape
[
1
]
//
2
lslice
=
self
.
lop
[
3
:
n1
,
3
:
n2
]
lsliceA
=
self
.
lopA
[
3
:
n1
,
3
:
n2
]
self
.
assertAlmostEqual
(
LA
.
norm
(
lslice
.
toarray
()
-
lsliceA
),
0
)
def
test_real
(
self
):
cF
=
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
],
field
=
'
complex
'
)
lcF
=
LazyLinearOp2
.
create_from_op
(
cF
)
lcF
=
lcF
.
real
self
.
assertAlmostEqual
(
LA
.
norm
(
lcF
.
toarray
()
-
cF
.
real
.
toarray
()),
0
)
def
test_imag
(
self
):
cF
=
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
],
field
=
'
complex
'
)
lcF
=
LazyLinearOp2
.
create_from_op
(
cF
)
lcF
=
lcF
.
imag
self
.
assertAlmostEqual
(
LA
.
norm
(
lcF
.
toarray
()
-
cF
.
imag
.
toarray
()),
0
)
def
test_aslazylinop
(
self
):
from
pyfaust.lazylinop
import
asLazyLinearOp
cF
=
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
],
field
=
'
complex
'
)
#TODO: re-enable later (when LazyLinearOp2 will replace LazyLinearOp)
# lcF = asLazyLinearOp(cF)
# self.assertTrue(pf.lazylinop.LazyLinearOp2.isLazyLinearOp(lcF))
# self.assertEqual(cF.shape, lcF.shape)
class
TestLazyLinearOpFFTFunc
(
TestLazyLinearOpFaust
):
def
setUp
(
self
):
from
scipy.fft
import
fft
,
ifft
# axis = 0 to be consistent with LazyLinearOp2.toarray() which applies
# fft on columns of the matrix, not on the rows (axis=1)
self
.
lop
=
LazyLinearOperator
((
8
,
8
),
matmat
=
lambda
x
:
fft
(
x
,
axis
=
0
),
rmatmat
=
lambda
x
:
8
*
ifft
(
x
,
axis
=
0
))
self
.
lopA
=
self
.
lop
.
toarray
()
self
.
lop2
=
aslazylinearoperator
(
pf
.
rand
(
self
.
lop
.
shape
[
0
],
self
.
lop
.
shape
[
1
]))
self
.
lop2A
=
self
.
lop2
.
toarray
()
self
.
lop3
=
aslazylinearoperator
(
pf
.
rand
(
self
.
lop
.
shape
[
1
],
self
.
lop
.
shape
[
0
]))
self
.
lop3A
=
self
.
lop3
.
toarray
()
class
TestLazyLinearOpFaustKron
(
TestLazyLinearOpFaust
):
def
setUp
(
self
):
from
pyfaust.lazylinop
import
kron2
as
lkron
lop_A
=
aslazylinearoperator
(
pf
.
rand
(
10
,
15
))
lop_B
=
aslazylinearoperator
(
pf
.
rand
(
10
,
15
))
self
.
lop
=
lkron
(
lop_A
,
lop_B
)
self
.
lopA
=
self
.
lop
.
toarray
()
self
.
lop2
=
aslazylinearoperator
(
pf
.
rand
(
*
self
.
lop
.
shape
))
self
.
lop2A
=
self
.
lop2
.
toarray
()
self
.
lop3
=
aslazylinearoperator
(
pf
.
rand
(
self
.
lop
.
shape
[
1
],
10
))
self
.
lop3A
=
self
.
lop3
.
toarray
()
if
'
__main__
'
==
__name__
:
unittest
.
main
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment