Skip to content

Instantly share code, notes, and snippets.

@Scriddie
Last active July 24, 2019 12:55
Show Gist options
  • Save Scriddie/329631444533f22292cabf95d47de88f to your computer and use it in GitHub Desktop.
Save Scriddie/329631444533f22292cabf95d47de88f to your computer and use it in GitHub Desktop.
inverse_matrix
# @author https://github.com/agemcipe
# Calculate inverse of X * X
create table XtX_inverse (row_num int, col_num int, value float);
insert into XtX_inverse
select mat_pos.col_num as row_num # transponse
, mat_pos.row_num as col_num
, (1 / @determinant) * power(-1, mat_pos.row_num + mat_pos.col_num) *
(mat_1.value * mat_4.value - mat_2.value
* mat_3.value)
value
from XtX mat_1
left join XtX mat_2
on mat_1.row_num = mat_2.row_num
and mat_1.col_num < mat_2.col_num
left join XtX mat_3
on mat_3.col_num = mat_1.col_num
and mat_3.row_num > mat_1.row_num
left join XtX mat_4
on mat_4.col_num = mat_2.col_num
and mat_4.row_num > mat_2.row_num
left join XtX mat_pos
on mat_pos.row_num not in (mat_1.row_num, mat_4.row_num)
and mat_pos.col_num not in (mat_1.col_num, mat_4.col_num)
where
(mat_1.col_num + mat_4.col_num + mat_1.row_num + mat_4.row_num)
= (mat_2.col_num + mat_3.col_num + mat_2.row_num + mat_3.row_num)
order by mat_pos.row_num, mat_pos.col_num;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment