[Question]
  [
Consider a linear system of equations, in \$n\$ unknowns, expressed as
$$A \textbf x = \textbf b$$
where \$A \in M\_{n,n}(\mathbb Z)\$ is an \$n \times n\$ matrix of integers, \$\textbf x\$ is a column vector of unknowns \$(x\_1, x\_2, \dots, x\_n)\$ and \$\textbf b = (b\_1, b\_2, \dots, b\_n)\$ is a column vector of integers.
We can consider the "augmented" matrix of this system as
$$A' = 
\begin{pmatrix}
a\_{11} & \cdots & a\_{1n} & b\_1 \\
a\_{21} & \cdots & a\_{2n} & b\_2 \\
\vdots & \ddots & \vdots & \vdots \\
a\_{n1} & \cdots & a\_{nn} & b\_n
\end{pmatrix}
$$
That is, the matrix \$A\$ but with an additional column vector \$\textbf b\$ as its final column. Let the \$i\$th row of \$A'\$ be represented as \$R\_i\$
There are three elementary row operations we can do to the rows of \$A'\$ that leave the solution set \$\textbf x\$ unchanged:
* Swapping the order of two rows (\$R\_i \leftrightarrow R\_j\$)
* Multiplying a row by a constant \$\lambda\$ (\$R\_i \to \lambda R\_i\$)
* Adding a multiple of row to a given row (\$R\_i \to R\_i + \lambda R\_j\$)
For example, we can say that the two following matrices are equivalent under these row operations:
$$
\begin{pmatrix}
 7 & 0 & 0 \\
-3 & -1 & 7 \\
 8 & 0 & -9 \\
\end{pmatrix}
\equiv
\begin{pmatrix}
8 & 0 & -9 \\
5 & -1 & -2 \\
1 & 0 & 0 \\
\end{pmatrix}
$$
(The specific row operations here are \$R\_1 \to \frac 1 7 R\_1\$, \$R\_1 \leftrightarrow R\_3\$, \$R\_2 \to R\_2 + R\_1\$)
---
Consider the \$n\times n\$ identity matrices:
$$
I\_1 = \begin{pmatrix} 1 \end{pmatrix} \\
I\_2 = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} \\
I\_3 = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix} \\
\vdots
$$
i.e. a given cell \$(I\_n)\_{ij}\$ is \$1\$ if \$i = j\$ and \$0\$ otherwise.
We'll say that an integer matrix is an "elementary matrix" if it is **exactly one** elementary row operation away from the identity matrix. For example, the following are all elementary matrices (with the row operation shown)
$$\begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix} \tag{$R\_1 \leftrightarrow R\_2$}$$
$$\begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 3 \\ 0 & 0 & 1 \end{pmatrix} \tag{$R\_2 \to R\_2 + 3R\_3$}$$
$$\begin{pmatrix} -5 \end{pmatrix} \tag{$R\_1 \to -5R\_1$}$$
$$\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & -1 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix} \tag{$R\_2 \to R\_2 - R\_4$}$$
$$\begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix} \tag{$R\_1 \to R\_1$}$$
However, none of these matrices are elementary matrices, as they all require 2 or more row operations from an identity matrix (some row operations shown):
$$\begin{pmatrix} 0 & 2 \\ 1 & 0 \end{pmatrix} \tag{$R\_1 \leftrightarrow R\_2$, $R\_1 \to 2R\_1$}$$
$$\begin{pmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{pmatrix}$$
$$\begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 1 & 0 & 0 \end{pmatrix} \tag{$R\_2 \leftrightarrow R\_4$, $R\_3 \leftrightarrow R\_4$}$$
---
You are to take a square \$n \times n\$ integer matrix and indicate whether it is an elementary matrix or not. This indication can either be:
* Two consistent distinct values to indicate whether the input is or isn't an elementary matrix (e.g. `True`/`False`, `1`/`0`, `"a"`/`7`, etc.)
* Two classes of values, which are naturally interpreted as truthy and falsey values in your language (e.g. `0` and non-zero integers, and empty vs non-empty list etc.)
You may take input in any [standard manner or format](https://codegolf.meta.stackexchange.com/q/2447/66833), including taking \$n\$ as an optional input. This is [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'"), so the shortest code in bytes wins.
## Test cases
| Input | Output |
| --- | --- |
| 
```
0 11 0
```
 | `True` |
| 
```
1 0 00 1 30 0 1
```
 | `True` |
| 
```
-5
```
 | `True` |
| 
```
1 0 0 00 1 0 -10 0 1 00 0 0 1
```
 | `True` |
| 
```
1 00 1
```
 | `True` |
| 
```
0 0 00 1 00 0 1
```
 | `True` |
| 
```
0 21 0
```
 | `False` |
| 
```
1 2 34 5 67 8 9
```
 | `False` |
| 
```
1 0 0 00 0 1 00 0 0 10 1 0 0
```
 | `False` |
| 
```
2 0 00 0 10 1 0
```
 | `False` |
| 
```
1 1 10 1 00 0 1
```
 | `False` |
 
  
[Answer]
# [Python 3](https://docs.python.org/3/) + numpy, 84 bytes
```
lambda m:sum(m!=eye(len(m)))-3*all((m*m==m)*(m@m==eye(len(m))))<2
from numpy import*
```
[Try it online!](https://tio.run/##XU/JbsMgED2Xr5hyAje27KTpEhWpvfQLfMyFtFhBYgBhfPDXu3hrnV5g5m08fB@vzh6GRpwHI/HyLQFPbYcM74XqFTPKMuSc54dMGsMYZigE8ozhexq2Cv62J01wCLZD34NG70LMhvleQNmC9aQWlFJSQkUqKAm5q0OnyDinLaFwSOfIrkx@vBUtshLyalZOwK1nxv72je1fegn7pcenNO1k3acKj3CEJ/IML/C6pdac21fnOr8Z6XtF642OjKqzpZw0LkAEbaE@EQC9cyAgFm0M2jO@Ss920gI44VZOCJpa0tEkrC9QRqZXjhcfRVDtVXrF8mqniy/X2SmH8odqDPJBJ0DvGqZTkuOEDD8 "Python 3 – Try It Online")
I'll get things started. No idea how good this is.
Criterion: If the matrix differs from the identity in one or zero places return True. Otherwise allow up to four if the matrix has only ones and zeroes and is its own inverse.
Not 100% sure this is tight.
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), ~~41~~ 31 bytes
```
⊙θ⊙θ¬‹⁼κμΣＥθΣＥν¬⁼π⁼ξ⎇⁼ρκμ⎇⁼ρμκρ
```
[Try it online!](https://tio.run/##S85ILErOT8z5/z@gKDOvRMMxr1KjUEcBSvnll2j4pBYXa7gWlibmFGtk6yjkauooBJfmavgmFoBUwJh5EMVQdQU6ClBWhY5CSGpRXmJRJUyuSEchG2hGLjYJkOFAO4o0UYD1///R0dGGOgoGQBSrw6WgEA1kAfnGcA6IHxsb@1@3LAcA "Charcoal – Try It Online") Link is to verbose version of code. Outputs a Charcoal boolean, i.e. `-` if elementary, nothing if not. Explanation: There must be a pair of rows such that the number of differences between the input and the identity matrix with the pair of rows swapped must not exceed zero if the rows are different or one if the rows are the same (i.e. there can be one difference between the input and the identity matrix).
```
⊙θ⊙θ                            Any two rows satisfy
      ⁼κμ                       Same rows
    ¬‹                          Is not less than
          Ｅθ Ｅι                 Map over input matrix
                 π              Current element
               ¬⁼               Does not equal
                  ⁼ξ            Identity matrix
                    ⎇⁼ρκμ⎇⁼ρμκρ With rows swapped
         Σ  Σ                   Take the sum
                                Implicitly print
```
[Answer]
# JavaScript (ES6), 92 bytes
Returns a Boolean value.
```
m=>m.some((P,p)=>m.some((Q,q)=>m.map((r,y)=>(y-p?y-q?r:P:Q).map(v=>e+=v!=!y--),e=p!=q)|e<2))
```
[Try it online!](https://tio.run/##VY1Lb4MwEITv/IqFS7wKJkCaPtKanNpjlSjXXCgxKRXGYBMk1Pa3U4eHkl7Wnm9mZ7/iJtaJysqaFvLIu5R1gkXC01JwQrZuiVe1c6teibgkRLmtEaSl5aal1Uatt@sd9lbDIj5njc3sllJ0OSttVuEPfwkRO8Wrc6Y4maV6hp7i8fEty/m@LRLio1fLfa2y4kTQ02We1cQ5FIfCQS@V6jVOPokGFsG3BZDzGgQw0NegM5zvIxNeHPR8MfD3s/jgCvHZbCey0DLnXi5PJCUC0frFzofACsC3LsM8RsLSzAu26GrEo@EDDQavB2NqEOZzE5wafAin9tD03sEK7q0HeISn2@b/hcMl/w8 "JavaScript (Node.js) – Try It Online")
### Commented
```
m =>                // m[] = input matrix
m.some((P, p) =>    // for each row P[] at position p in m[]:
  m.some((Q, q) =>  //   for each row Q[] at position q in m[]:
    m.map((r, y) => //     for each row r[] at position y in m[]:
      (             //       select the relevant row:
        y - p ?     //         if y is not equal to p:
          y - q ?   //           if y is not equal to q:
            r       //             use r[]
          :         //           else:
            P       //             use P[] (swapped with Q[])
        :           //         else:
          Q         //           use Q[] (swapped with P[])
      ).map(v =>    //       for each value v in this row:
        e +=        //         increment e if ...
          v != !y-- //           ... v is not equal to the expected
                    //           value in the identity matrix
      ),            //       end of inner map()
      e = p != q    //       start with e = 1 if two rows were
                    //       actually swapped, or 0 otherwise
    )               //     end of outer map()
    | e < 2         //     yield true if e is less than 2
  )                 //   end of inner some()
)                   // end of outer some()
```
[Answer]
# [Core Maude](http://maude.cs.illinois.edu/w/index.php/The_Maude_System), ~~315~~ ~~302~~ ~~286~~ ~~277~~ ~~276~~ ~~273~~ 268 bytes
```
mod E is pr LIST{Int}. ops e i : Int ~> Int . var A B N X Y Z :[Int]. ceq e(N
X A Y B Z)= 1 if(size(X)rem N + size(Y)rem N)size(A)size(B)i(N 0)X B Y A Z =
0 N N i(N(N ^ 2)). ceq e(N X A Y)= 1 if i(N 0)X I:Int Y := i(N(N ^ 2)). eq
i(N s A)= i(N A)(0 ^(A rem s N)). endm
```
The result is obtained by reducing the `e` function with the width \$n\$ prepended to the matrix as a flattened list. The output is either the constant `1` for true, or an error term of sort `[List{Int}]` for false.
### Example Session
```
Maude> --- True
> red e(
>     2
>     0 1
>     1 0
> ) .
result NzNat: 1
Maude> 
> --- True
> red e(
>     3
>     1 0 0
>     0 1 3
>     0 0 1
> ) .
result NzNat: 1
Maude> 
> --- True
> red e(
>     1
>     -5
> ) .
result NzNat: 1
Maude> 
> --- True
> red e(
>     4
>     1 0 0 0
>     0 1 0 -1
>     0 0 1 0
>     0 0 0 1
> ) .
result NzNat: 1
Maude> 
> --- True
> red e(
>     2
>     1 0
>     0 1
> ) .
result NzNat: 1
Maude> 
> --- True
> red e(
>     3
>     0 0 0
>     0 1 0
>     0 0 1
> ) .
result NzNat: 1
Maude> 
> --- False
> red e(
>     2
>     0 2
>     1 0
> ) .
result [List{Int}]: e(2 0 2 1 0)
Maude> 
> --- False
> red e(
>     3
>     1 2 3
>     4 5 6
>     7 8 9
> ) .
result [List{Int}]: e(3 1 2 3 4 5 6 7 8 9)
Maude> 
> --- False
> red e(
>     4
>     1 0 0 0
>     0 0 1 0
>     0 0 0 1
>     0 1 0 0
> ) .
result [List{Int}]: e(4 1 0 0 0 0 0 1 0 0 0 0 1 0 1 0 0)
```
### Ungolfed
```
mod E is
    pr LIST{Int} .
    ops e i : Int ~> Int .
    var A B N X Y Z : [Int] .
    ceq e(N X A Y B Z) = 1
        if (size(X) rem N + size(Y) rem N) size(A) size(B) i(N 0) X B Y A Z
            = 0 N N i(N (N ^ 2)) .
    ceq e(N X A Y) = 1
        if i(N 0) X I:Int Y := i(N (N ^ 2)) .
    eq i(N s A) = i(N A) (0 ^ (A rem s N)) .
endm
```
We guess a partition of the input list into three or five (possibly empty) sublists, and then attempt to perform the inverse of one of the three row operations.
* Swapping is undone by picking two rows and swapping them.
* Multiplying and adding rows is undone by picking a single element and ignoring it — it either corresponds to the single \$1\$ in the row that was multiplied by \$\lambda\$, or the single \$1\$ in a *different* row that was added.
---
Saved 13 bytes by simplifying the identity matrix function, by recognizing that the \$1\$ occurs every \$(n + 1)\$ entries.
Saved 16 more bytes by collapsing multiplying and adding into a single case and changing the signature to only one list argument with length prepended.
Saved ~~9~~ 10 more bytes by combining conditions into one list equality.
Saved 3 more bytes using `0 ^ X` to do the equivalent of `!x` in C — i.e., convert non-zero to zero and zero to one. Maude's convention is that \$x ^ 0 = 1\$ even for \$x = 0\$.
Saved 5 more bytes by removing the base case for `i` and handling it in the pattern matches.
[Answer]
# [Desmos](https://desmos.com/calculator), 117 bytes
```
K=[0...W^2]
J=K[(L-0^{mod(K,W+1)})^2>0]
n(L)=L.length
f(L,W)=\{n(J)<2,32=n(\mod(J,W).\unique)^23^{L[J+1]^2}.\total\}
```
This turned out pretty well, considering Desmos's limitations.
The blank line indicates that the subsequent line (`f(L,W)=...`) should be pasted including a leading newline.
Takes input as a flattened list and the width of the matrix.
Returns 1 if the matrix is elementary, otherwise NaN (shown as "undefined")
[Try it on Desmos!](https://www.desmos.com/calculator/pdinuqpqr5)
### How it works
It's not clear that this golfy approach works, so I'll start with a line-by-line explanation of what the code does, then give a proof of its correctness in the next section.
```
# K: indices of the list L, 0-indexed
#    this has one element more than L, but it gets ignored because
#    operations applied element-wise use the smallest length
K=[0...W^2]
# J: 0-indexed positions in L whose entries differ from the W×W identity matrix
J = K[ # filter K for elements where:
  (    # the corresponding entries differ from the identity matrix:
    L -            # value of L minus
    0^{mod(K,W+1)} # 1 if on main diagonal (mod(K,W+1)=0) else 0
  )^2>0 # this difference must be nonzero; (a-b)^2 > 0 ⟺ a ≠ b
]
# n: helper function to give the length
n(L)=L.length
# f: return 1 if the matrix is elementary, otherwise NaN
f(L,W) = \{
  # If 0 or 1 positions differ from the identity matrix, return 1
  n(J)<2,
  # If the following is true, return 1:
  32 = # 32 is equal to
    n(\mod(J,W).\unique)^2 * # the number of columns containing at least one entry
                             # differing from the identity matrix, squared
    3^{L[J+1]^2}.\total # the total of 3^{e_j} for each entry e_j differing from identity
\}
```
### Proof of correctness
As observed in the other answers, "multiplying a row by a constant" and "adding a multiple of a row to another row" give either the same identity matrix (multiplying by 1 or adding 0 times a row to another row) or the identity matrix with exactly one entry changed. This is handled by `n(J)<2`.
The only condition left to check is swapping two rows.
**Claim:** `32 = n(\mod(J,W).\unique)^2 * 3^{L[J+1]^2}.\total` if and only if L differs from an identity matrix by swapping two rows
**Proof (backward direction):** Assume L differs from an identity matrix by swapping two rows. Then exactly two columns contain entries different from the identity, so `n(\mod(J,W).\unique) = 2`. Additionally, the entries in `L` that are different than the identity must be exactly `0,0,1,1`, so `3^{L[J+1]^2}.\total = 3^0 + 3^0 + 3^1 + 3^1 = 8`. Hence `n(\mod(J,W).\unique)^2 * 3^{L[J+1]^2}.\total = 2^2 * 8 = 32` as desired.
**Proof (forwards direction):** Assume `32 = n(\mod(J,W).\unique)^2 * 3^{L[J+1]^2}.\total`, and assume for sake of contradiction that L does *not* differ from an identity matrix by swapping two rows.
Since each factor is a non-negative integer, we must have that `n(\mod(J,W).\unique)^2` is a factor of 32, so `n(\mod(J,W).\unique)` is a factor of 4.
*Case 1:* Assume `n(\mod(J,W).\unique)=1`, so all incorrect entries are in a single column. Then we must have `3^{L[J+1]^2}.\total = 32/1^2 = 32`. Since all incorrect entries are in a single column, there could be at most 1 incorrect value of `0`. Hence we must have `32` written as a sum of powers of `3` with at most 1 copy of `3^0`; this is impossible since 32 is 2 mod 3, contradiction.
*Case 2:* Assume `n(\mod(J,W).\unique)=2`, so all incorrect entries are in two columns. Then we must have `3^{L[J+1]^2}.\total = 32/2^2 = 8`. Since all incorrect entries are in a single column, there could be at most 2 incorrect values of `0`. Hence we must have `8` written as a sum of powers of `3` with at most 2 copies of `3^0`: the only way to have this is `3^0 + 3^0 + 3^1 + 3^1`, which can only occur when L differs from an identity matrix by swapping two rows, contradiction.
*Case 3:* Assume `n(\mod(J,W).\unique)=4`, so there are at least 4 incorrect entries. We must have `3^{L[J+1]^2}.\total = 32/4^2 = 2`, but this can't happen because `3^{L[J+1]^2}.\total` is the sum of at least 4 terms which are each at least 1, contradiction.
In all cases, there is a contradiction, so L must differ from an identity matrix by swapping two rows, as desired.
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~21~~ 19 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
δQαDΘO0K22SQsĀ˜O!Θ~
```
Can probably be a golfed a bit more.. Not too happy about the `ΘO0K22SQ`.
[Try it online](https://tio.run/##yy9OTMpM/f//3JbAcxtdzs3wN/A2MgoOLD7ScHqOv@K5GXX//0dHG@kY6BjE6kQDSR1DMG0I5McCAA) or [verify all test cases](https://tio.run/##yy9OTMpM/V9Waa@k8KhtkoKSfeXhCf/PbQk8t9Hl3Ax/A28jo@DA4iMNp@f4K56bUfdf5390dLSBjmGsTrShjkFsrI5CNIgBZOqAhHWMwTRIAVhK1xRJCVyRgY6uIUwdVBBJD0wIykXRiGo61FRkIxBKUOxDVoNkshGqN4zAzjfRMdUxA9LmOhY6lpjOR3cz3AqIUiNkhXBnx8YCAA).
**Explanation:**
```
δQ         # Push an identity matrix of the same size as the input-matrix:
δ          #  Apply on the rows of the (implicit) input-matrix double-vectorized:
 Q         #   Equality check
α          # Take the absolute difference of the values of this identity matrix
           # with the values of the input-matrix at the same positions
 D         # Duplicate this
  ΘO0K22SQ # Check that there are exactly two rows containing exactly two 1s:
  Θ        #  Check for each value if it's equal to 1 (1 if 1; 0 otherwise)
   O       #  Sum each row to get the amount of 1s per row
    0K     #  Remove all 0s from the sums
      22SQ #  And check if this is equal to [2,2]
 s         # Swap so the duplicated list is at the top again
  Ā˜O!Θ    # Check that there are either 0 or 1 cell values changed:
  Ā        #  Check for each whether it's NOT 0 (0 if 0; 1 otherwise)
   ˜       #  Flatten the matrix
    O      #  Get the sum of this list
     !     #  Get the factorial of it (0 to 1; 1 remains 1; >=2 just becomes larger)
      Θ    #  Check if this is equal to 1
~          # Bitwise-OR to check if either of the two checks is 1
           # (after which this is output implicitly as result)
```
]