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

Added a .pinv() method to MultiVectors plus tests #367

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions clifford/_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,29 @@ def leftLaInvJIT(value):

return leftLaInvJIT

@_cached_property
def pinv_func(self):
"""
Get a function that returns left-pseudoinverse using a computational
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the left pseudo-inverse equal to the right pseudo-inverse (i.e., can you find a counterexample where it isn't?)

Copy link
Author

@tBuLi tBuLi Oct 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I can't come up with a counter example. Something interesting and perhaps related I noticed is that if I replace the original np.linalg.solve with np.linalg.lstsq I get for the test example

Expected :0.5 + (3002399751580329.5^e2) - (0.5^e3) - (0.75^e13) - (3002399751580329.5^e23)
Actual   :0.5 - (0.5^e3)

I think it is save to say that this is undesired behavior. The pinv function clearly behaves much gentler. And in the numpy docs I see no mention of left and right actually, so perhaps we should leave that distinction altogether? Since it seems to return the input element anyway in case of singular behavior I don't see how that could be different left and right.

linear algebra method proposed by Christian Perwass. Identical to
`inv_func` except the inverse has been replaced by a pseudo inverse.
"""
mult_table = self.gmt
k_list, l_list, m_list = mult_table.coords
mult_table_vals = mult_table.data
n_dims = mult_table.shape[1]

identity = np.zeros((n_dims,))
identity[self._basis_blade_order.bitmap_to_index[0]] = 1

@_numba_utils.njit
def leftLaPInvJIT(value):
intermed = _numba_val_get_left_gmt_matrix(value, k_list, l_list, m_list, mult_table_vals, n_dims)
sol = np.linalg.pinv(intermed) @ identity
return sol

return leftLaPInvJIT

def get_left_gmt_matrix(self, x):
"""
This produces the matrix X that performs left multiplication with x
Expand Down
7 changes: 7 additions & 0 deletions clifford/_multivector.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,13 @@ def inv(self) -> 'MultiVector':
leftInv = leftLaInv
rightInv = leftLaInv

def pinv(self) -> 'MultiVector':
"""Returns the pseudoinverse using a computational
linear algebra method proposed by Christian Perwass. Identical to
`inv` when an inverse exists.
"""
return self._newMV(self.layout.pinv_func(self.value))
eric-wieser marked this conversation as resolved.
Show resolved Hide resolved

def dual(self, I=None) -> 'MultiVector':
r"""The dual of the multivector against the given subspace I, :math:`\tilde M = MI^{-1}`

Expand Down
11 changes: 11 additions & 0 deletions clifford/test/test_clifford.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,17 @@ def test_indexing(self, g3):
e12[1 + e12]
assert e12[(2, 1)] == -1

def test_pinv(self, g3):
layout, blades = g3, g3.blades
e2 = blades['e2']
e3 = blades['e3']
x = 0.5*(1-e3) # Singular
y = 2*e2 + 5*e3 # invertible
with pytest.raises(ValueError):
x.leftLaInv()
assert x == x.pinv()
assert y.pinv() == y.leftLaInv()

def test_add_float64(self, g3):
'''
test array_wrap method to take control addition from numpy array
Expand Down