Skip to content
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

Merged
merged 17 commits into from
Jun 30, 2022
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 59 additions & 24 deletions src/Math-Tests-Matrix/PMQRTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Copy link
Contributor Author

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.

"
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) ]
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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.

Copy link
Member

Choose a reason for hiding this comment

The 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 }
Expand Down