Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I invert a matrix in F#?

Tags:

matrix

f#

I need to perform some basic OLS regression using F#. To do this I need some Linear Algebra functions, but I'm confused as to what's out there. I can't find any way to invert a matrix. There is some documentation for a library called Microsoft.FSharp.Math.LinearAlgebra, but I don't know if that exists anymore.

like image 621
Jonathan Beerhalter Avatar asked Mar 20 '09 19:03

Jonathan Beerhalter


People also ask

How do you invert a matrix?

To find the inverse of a 2x2 matrix: swap the positions of a and d, put negatives in front of b and c, and divide everything by the determinant (ad-bc).

How do you invert a 3x3 matrix?

What is the Inverse of 3x3 Matrix? The inverse of a 3x3 matrix, say A, is a matrix of the same order denoted by A-1 where AA-1 = A-1A = I, where I is the identity matrix of order 3x3. i.e., I = ⎡⎢⎣100010010⎤⎥⎦ [ 1 0 0 0 1 0 0 1 0 ] .

How do you find the inverse of a function?

How do you find the inverse of a function? To find the inverse of a function, write the function y as a function of x i.e. y = f(x) and then solve for x as a function of y.


2 Answers

If you add the FSharp Powerpack to your project (in .NET references), you can use various functionality of the matrix library

edit: you also need to add the experimental library Fsharp.Powerpack.MathProviders, then you can call as follows

open    Microsoft.FSharp.Math
let m = Matrix.create 10 10 1.2
let m2 = Experimental.LinearAlgebra.Inverse m
like image 192
Codingday Avatar answered Sep 19 '22 22:09

Codingday


FlyingFrog do a Numerics library which contains Matrix inversion amongst many other functions.

Not sure which is preferable, that or the (apparently deprecated) 'experimental' code from the PowerPack. I guess you could always keep the source code for the managed bit of the PowerPack version in a safe place, still available here:

C:\Program Files\FSharp-1.9.6.2\source\fsharp\FSharp.PowerPack\math\lapack\linear_algebra_managed.fs.
like image 31
Benjol Avatar answered Sep 17 '22 22:09

Benjol