-
-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Break Up Moore Penrose Test Into Smaller Tests For Clarity and Ease of Debugging #284
Changes from all commits
ceab081
df133b7
c2cefc1
205918f
b67cdf9
d5d36d0
69f4777
2cc404e
e825deb
c287f90
4fd471a
3b949d3
9d77e63
15fdee1
007b67e
00708b1
8cdda2d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,37 +5,72 @@ Class { | |
} | ||
|
||
{ #category : #running } | ||
PMQRTest >> mpTestFunction: aMatrix [ | ||
|
||
| inv mult | | ||
inv := aMatrix mpInverse. | ||
mult := inv * aMatrix. | ||
self assert: (aMatrix * mult closeTo: aMatrix). | ||
self assert: mult * inv closeTo: inv. | ||
self assert: mult transpose closeTo: mult. | ||
mult := aMatrix * inv. | ||
self assert: mult transpose closeTo: mult | ||
PMQRTest >> assert: inverse isMoorePenroseInverseOf: aMatrix [ | ||
|
||
"https://en.wikipedia.org/wiki/Moore–Penrose_inverse#Definition" | ||
|
||
| identityMatrix | | ||
"These two assertions are what define a pseudoinverse. They are known as | ||
the Moore–Penrose conditions of which there are four, but here we have two. The other two | ||
are that (A * A+) and A+ * A are Hermitian. | ||
" | ||
self assert: aMatrix * inverse * aMatrix closeTo: aMatrix. | ||
self assert: inverse * aMatrix * inverse closeTo: inverse. | ||
|
||
identityMatrix := aMatrix * inverse. | ||
self assert: identityMatrix transpose closeTo: identityMatrix. | ||
self assert: identityMatrix * aMatrix closeTo: aMatrix. | ||
|
||
"Pseudoinversion commutes with transposition, complex conjugation, and taking the conjugate transpose" | ||
self | ||
assert: aMatrix transpose mpInverse | ||
closeTo: aMatrix mpInverse transpose. | ||
] | ||
|
||
{ #category : #tests } | ||
PMQRTest >> testMPInverse [ | ||
PMQRTest >> testMoorePenroseInverseOfLargeNonRandomMatrixAndItsTranspose [ | ||
| a inverse transposeOfA | | ||
a := PMMatrix new initializeRows: | ||
#( #( 5 40 1 2.5 ) #( 0 0 1 2.5 ) #( 0 0 1 2.5 ) ). | ||
inverse := a mpInverse . | ||
self assert: inverse isMoorePenroseInverseOf: a. | ||
|
||
transposeOfA := a transpose. | ||
inverse := transposeOfA mpInverse . | ||
self assert: inverse isMoorePenroseInverseOf: transposeOfA. | ||
] | ||
|
||
| a | | ||
{ #category : #tests } | ||
PMQRTest >> testMoorePenroseInverseOfNonRandomMatrix [ | ||
| a inverse | | ||
a := PMMatrix new initializeRows: | ||
#( #( 5 40 1 ) #( 0 0 1 ) #( 0 0 1 ) ). | ||
self mpTestFunction: a. | ||
a := a * (PMMatrix rows: 3 columns: 3 random: 5.0). | ||
self mpTestFunction: a. | ||
inverse := a mpInverse . | ||
self assert: inverse isMoorePenroseInverseOf: a. | ||
] | ||
|
||
{ #category : #tests } | ||
PMQRTest >> testMoorePenroseInverseOfProductOfMatrices [ | ||
| a inverse | | ||
a := PMMatrix new initializeRows: | ||
#( #( 5 40 1 2.5 ) #( 0 0 1 2.5 ) #( 0 0 1 2.5 ) ). | ||
self mpTestFunction: a. | ||
a := a transpose. | ||
self mpTestFunction: a. | ||
3 timesRepeat: [ | ||
a := PMMatrix rows: 3 columns: 3 random: 1.0. | ||
self assert: (a mpInverse closeTo: a inverse). | ||
a := PMSymmetricMatrix new: 4 random: 1.0. | ||
self assert: (a mpInverse closeTo: a inverse) ] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am seeking a mathematician to help me in understand the needs of this test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It appears, after splitting the testMoorePenroseInverse test up, this part was the source of the random test failure. I infer from the repetition that this may be some kind of exhaustive data-driven test. I've changed this so as to get a more deterministic test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great work Hemal ! |
||
#( #( 5 40 1 ) #( 0 0 1 ) #( 0 0 1 ) ). | ||
|
||
a := a * (PMMatrix rows: 3 columns: 3 random: 5.0). | ||
inverse := a mpInverse . | ||
self assert: inverse isMoorePenroseInverseOf: a. | ||
] | ||
|
||
{ #category : #tests } | ||
PMQRTest >> testMoorePenroseInverseOfRandomMatrixIsAnInverse [ | ||
" | ||
Proofs for the properties below can be found in literature: | ||
If A has real entries, then so does A+ | ||
If A is invertible, its pseudoinverse is its inverse. That is, A+ = A**−1 | ||
" | ||
|
||
| a | | ||
a := PMSymmetricMatrix new: 4 random: 1.0. | ||
self assert: (a mpInverse closeTo: a inverse) | ||
] | ||
|
||
{ #category : #tests } | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Asserting that
A x A+
is Hermitian failed, sadly.