[Question]
  [
The cofactor matrix is the transpose of the [Adjugate Matrix](https://en.wikipedia.org/wiki/Adjugate_matrix). The elements of this matrix are the [cofactors](https://en.wikipedia.org/wiki/Minor_%28linear_algebra%29) of the original matrix.
The cofactor [![enter image description here](https://i.stack.imgur.com/3QQ4e.png)](https://i.stack.imgur.com/3QQ4e.png) (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
[![enter image description here](https://i.stack.imgur.com/BDLIM.png)](https://i.stack.imgur.com/BDLIM.png)
The element of the cofactor matrix at row 1 and column 2 is:
[![enter image description here](https://i.stack.imgur.com/t40WC.png)](https://i.stack.imgur.com/t40WC.png)
You can find info on what the determinant of a matrix is and how to calculate them [here](https://en.wikipedia.org/wiki/Determinant).
### Challenge
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**.
### Input
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 `[[a,b],[c,d]]`.
You may replace the square brackets and commas with something else if it fits your language and is sensible (e.g. `((a;b);(c;d))`)
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).
### Output
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.
### Test cases
* Input: `[[1]]`
Output: `[[1]]`
* Input: `[[1,2],[3,4]]`
Output: `[[4,-3],[-2,1]]`
* Input: `[[-3,2,-5],[-1,0,-2],[3,-4,1]]`
Output: `[[-8,-5,4],[18,12,-6],[-4,-1,2]]`
* Input: `[[3,-2,7,5,0],[1,-1,42,12,-10],[7,7,7,7,7],[1,2,3,4,5],[-3,14,-1,5,-9]]`
Output: 
```
[[9044,-13580,-9709,23982,-9737],[-1981,1330,3689,-3444,406],[14727,7113,2715,-9792,414],[-28448,-2674,-707,16989,14840],[-2149,2569,-2380,5649,-3689]]
```
### Scoring
This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") so the shortest answer in bytes wins.
  
[Answer]
# Matlab, ~~42~~ 33 bytes
Using an anonymous function:
```
@(A)round(inv(A+eps)'*det(A+eps))
```
Input and output are matrices (2D numeric arrays).
`eps` is added in case the matrix is singular. It is "removed" using `round` (the true result is guaranteed to be an integer).
Example:
```
>> @(A)round(inv(A+eps)'*det(A+eps))
ans = 
    @(A)round(inv(A+eps)'*det(A+eps))
>> ans([-3,2,-5; -1,0,-2; 3,-4,1])
ans =
-8    -5     4
18    12    -6
-4    -1     2
```
Example with singular matrix:
```
>> @(A)round(inv(A+eps)'*det(A+eps))
ans = 
    @(A)round(inv(A+eps)*det(A+eps)')
>> ans([1,0 ; 0,0])
ans =
     0     0
     0     1
```
Or [try it online](http://ideone.com/NoIUmO) in Octave.
[Answer]
# Mathematica, ~~27~~ 35 bytes
```
Thread[Det[#+x]Inverse[#+x]]/.x->0&
```
[Answer]
# R, ~~121~~ 94 bytes
```
function(A)t(outer(1:(n=NROW(A)),1:n,Vectorize(function(i,j)(-1)^(i+j)*det(A[-i,-j,drop=F]))))
```
This is an absurdly long function that accepts an object of class `matrix` and returns another such object. To call it, assign it to a variable.
Ungolfed:
```
cofactor <- function(A) {
    # Get the number of rows (and columns, since A is guaranteed to
    # be square) of the input matrix A
    n <- NROW(A)
    # Define a function that accepts two indices i,j and returns the
    # i,j cofactor
    C <- function(i, j) {
        # Since R loves to drop things into lower dimensions whenever
        # possible, ensure that the minor obtained by column deletion
        # is still a matrix object by adding the drop = FALSE option
        a <- A[-i, -j, drop = FALSE]
        (-1)^(i+j) * det(a)
    }
    # Obtain the adjugate matrix by vectorizing the function C over
    # the indices of A
    adj <- outer(1:n, 1:n, Vectorize(C))
    # Transpose to obtain the cofactor matrix
    t(adj)
}
```
[Answer]
# [GAP](http://www.gap-system.org), 246 Bytes
You can tell this is good coding by the triple nested for-loops.
It's pretty straightforward. GAP doesn't really have the same tools to deal with matrices that other math oriented languages do. The only thing really used here is the built in determinant operator. 
```
f:=function(M)local A,B,i,j,v;A:=StructuralCopy(M);if not Size(M)=1 then for i in [1..Size(M)] do for j in [1..Size(M)] do B:=StructuralCopy(M);for v in B do Remove(v,j);od;Remove(B,i);A[i][j]:= (-1)^(i+j)*DeterminantMat(B);od;od;fi;Print(A);end;
```
ungolfed:
```
f:=function(M)
    local A,B,i,j,v;
    A:=StructuralCopy(M);
    if not Size(M)=1 then
        for i in [1..Size(M)] do
            for j in [1..Size(M)] do
                B:=StructuralCopy(M);
                for v in B do
                    Remove(v,j);
                od;
                Remove(B,i);
                 A[i][j]:= (-1)^(i+j)*DeterminantMat(B);
            od;
        od;
    fi;
    Print(A);
end;
```
[Answer]
# J, 29 bytes
```
3 :'<.0.5+|:(-/ .**%.)1e_9+y'
```
Same epsilon/inverse/determinant trick. From right to left:
* `1e_9+` adds an epsilon,
* `(-/ .**%.)` is *determinant* (`-/ .*`) times *inverse* (`%.`),
* `|:` transposes,
* `<.0.5+` rounds.
[Answer]
# [Verbosity v2](https://github.com/cairdcoinheringaahing/Verbosity-v2), 196 bytes
```
IncludeTypePackage<Matrix>
IncludeTypePackage<OutputSystem>
print=OutputSystem:NewOutput<DEFAULT>
input=Matrix:Adjugate<ARGV0>
input=Matrix:Transpose<input>
OutputSystem:DisplayAsText<print;input>
```
[Try it online!](https://tio.run/##bY3LDoIwFET3/QqWsFN0haVJE9SY@IpW9xVuSBVL04fSr68GNmpcnpnJmQfoS2uE9WkIK1k2rgLmFex5eeM14A23WnQE/al2zipnj95YuBOktJA2/8yyLTwHxsV8QU9rRpCQb8wHaUarq6u5BUwPy/Pop2SaS6NaA7iPCfpSF8KohntqGHQW99@zYRdCiONxlCZRPImmSfIC "Verbosity v2 – Try It Online")
NB: Doesn't currently work on TIO, awaiting a pull. Should work offline
Takes input in the form `((a b)(c d))` to represent
$$
\bigg[
\begin{matrix}
a \: b \\
c \: d
\end{matrix}
\bigg]
$$
Despite having a builtin for the adjugate, Verbosity's verbosity still cripples it. Fairly basic how it works, just transposes the adjugate of the input.
]