Last active
February 24, 2020 12:32
-
-
Save lucainnocenti/e8aae76b02d629b1b65aac96385ef93b to your computer and use it in GitHub Desktop.
A bunch of utility functions
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
zeros[0] = 0; | |
zeros[dim_] := ConstantArray[0, {dim, dim}]; | |
MF[expr_] := MatrixForm@expr; | |
(* take small matrix and embed it into a larger matrix, padding with zeros *) | |
injectIntoLargerMatrix[mat_, largerMatDim_, position_Integer: 1] := Module[{outMat}, | |
(* put zeros on bottom right *) | |
If[largerMatDim - Length@mat - position + 1 > 0, | |
outMat = ArrayFlatten[{ | |
{mat, 0}, {0, zeros[largerMatDim - Length@mat - position + 1]} | |
}], | |
outMat = mat | |
]; | |
(* put zeros of upper left *) | |
If[position > 1, | |
outMat = ArrayFlatten[{ | |
{zeros[position - 1], 0}, {0, outMat} | |
}] | |
]; | |
outMat | |
]; | |
(* if `indicesToReplace` is a list, and the second argument a matrix, then we use the third | |
argument to know which elements/indices of `largerMat` should be replaced with `mat` *) | |
injectIntoLargerMatrix[mat_, largerMat_, indicesToReplace_List] := Module[{outMat}, | |
outMat = largerMat; | |
outMat[[Sequence @@ indicesToReplace]] = mat; | |
outMat | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment