-
-
Save ImkeF/b58537f9469fd125e0179c0c86facac0 to your computer and use it in GitHub Desktop.
Returns the inverse matrix for a matrix stored in an array. As a bonus, its determinant is returned in the metadata.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let func = | |
(Table as table) => | |
let | |
Source = Table, | |
ValuesList = Table.ToRows(Source), | |
Count = List.Count(ValuesList), | |
RowsList = {0..Count - 1}, | |
MatrixWidth = Table.ColumnCount(Source), | |
Result = List.Accumulate( | |
RowsList, | |
[Counter = - 1, MatB = ValuesList, Det = 1], | |
(previous, current) => | |
[Counter = previous[Counter] + 1, | |
Pivot = previous[MatB]{Counter}{Counter}, | |
Det = previous[Det] * Pivot, | |
MatB = List.Transform( | |
Table.ToRecords(Table.FromColumns({previous[MatB], RowsList}, {"Row", "RowIndex"})), | |
(row) => List.Transform( | |
Table.ToRecords(Table.FromColumns({row[Row], {0..MatrixWidth - 1}}, {"Value", "ColumnIndex"})), | |
(MatrixItem) => | |
if MatrixItem[ColumnIndex] <> Counter | |
then if row[RowIndex] = Counter | |
then MatrixItem[Value] / Pivot | |
else MatrixItem[Value] + previous[MatB]{Counter}{MatrixItem[ColumnIndex]} * - row[Row]{Counter} / Pivot | |
else if row[RowIndex] <> Counter | |
then - MatrixItem[Value] / Pivot | |
else 1 / MatrixItem[Value] | |
) | |
)] | |
), | |
Transform = Table.FromRows(Result[MatB]) meta [Determinant = Result[Det]] | |
in | |
Transform , | |
documentation = [ | |
Documentation.Name = " Xls.MINVERSE.pq ", | |
Documentation.Description = " Returns the inverse matrix for a matrix stored in an array. As a bonus, its determinant is returned in the metadata. ", | |
Documentation.LongDescription = " Returns the inverse matrix for a matrix stored in an array. As a bonus, its determinant is returned in the metadata. Algorithm used: FAROOQ/HAMID: https://www.researchgate.net/publication/220337321_An_Efficient_and_Generic_Algorithm_for_Matrix_Inversion ", | |
Documentation.Category = " Xls.Math and trigonometry ", | |
Documentation.Source = " www.TheBIccountant.com ", | |
Documentation.Version = " 1.0 ", | |
Documentation.Author = " Imke Feldmann ", | |
Documentation.Examples = {[Description = " ", | |
Code = " ", | |
Result = " "]}] | |
in | |
Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment