Skip to content
This repository has been archived by the owner on Nov 18, 2023. It is now read-only.

Guard call to inverse #40

Closed
wants to merge 1 commit into from
Closed

Conversation

istathar
Copy link
Contributor

Use detLU to check that the input matrix is not singular before attempting to calculate the inverse.

Would close #38, though in a clumsy fashion.

Right now the inverse function (or rather, internal rref) has a ton of error checking. I feel like there should be two paths to calculate the inverse; a safe, guarded one that accepts it might be more expensive to run (in which case might as well use the Caley-Hamilton method) and an unsafe, express, assume-you-know-what-you're-doing, maybe

unsafeInverse :: Matrix a -> Matrix a

anyway, patch attached

AfC

Use `detLU` to check that the input matrix is not singular before
attempting to calculate the inverse.

Closes Daniel-Diaz#38.
@@ -543,12 +543,14 @@ transpose m = matrix (ncols m) (nrows m) $ \(i,j) -> m ! (j,i)

-- | /O(rows*rows*rows) = O(cols*cols*cols)/. The inverse of a square matrix.
-- Uses naive Gaussian elimination formula.
inverse :: (Fractional a, Eq a) => Matrix a -> Either String (Matrix a)
inverse :: (Fractional a, Ord a) => Matrix a -> Either String (Matrix a)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did this change from Eq to Ord?

Copy link
Owner

Choose a reason for hiding this comment

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

Because detLU requires Ord.

inverse m
| ncols m /= nrows m
= Left
$ "Inverting non-square matrix with dimensions "
++ show (sizeStr (ncols m) (nrows m))
| detLU m == 0
Copy link
Contributor

Choose a reason for hiding this comment

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

with regards to determining the determinant and performance: I think we should go with safety instead of performance in this library because if someone really wants a performant matrix library they would probably use some GPU accelerated c library.

@istathar istathar closed this Apr 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

inverse could check to see if matrix invertable
3 participants