The cofactor (i.e. the element of the cofactor matrix at row i and column j) is the determinant of the submatrix formed by deleting the ith row and jth column from the original matrix, multiplied by (-1)^(i+j).
For example, for the matrix
The element of the cofactor matrix at row 1 and column 2 is:
You can find info on what the determinant of a matrix is and how to calculate them here.
Your goal is to output the cofactor matrix of an input matrix.
Note: Built-ins that evaluate cofactor matrices, or adjugate matrices, or determinants or anything similar are allowed.
The matrix may be inputed as a command line argument, as a function parameter, in
STDIN or in any way that is most appropriate for the language you use.
The matrix will be formatted as a list of lists, each sublist corresponding to one row, which contains factors ordered from left to right. Rows are ordered from top to bottom in the list.
For example, the matrix
a b c d
will be represented by
You may replace the square brackets and commas with something else if it fits your language and is sensible (e.g.
Matrices will only contain integers (which can be negative).
Matrices will always be square (i.e. same number of rows and columns).
You may assume that the input will always be correct (i.e. no formatting problem, nothing other than integers, no empty matrix).
The resulting cofactor matrix may be outputed to
STDOUT, returned from a function, written to a file, or anything similar that naturally suits the language you use.
The cofactor matrix must be formatted in the exact same way the input matrices are given, e.g.
[[d,-c],[-b,a]]. If you read a string, then you must return/output a string in which the matrix is formatted exactly like in the input. If you use something like e.g. a list of lists as input, then you must return a list of lists too.
This is code-golf so the shortest answer in bytes wins.