[Question]
  [
## Background
A **[Jordan matrix](https://en.wikipedia.org/wiki/Jordan_matrix)** is a block-diagonal matrix where each block on the diagonal has the structure of
$$
\begin{bmatrix}
\lambda & 1 & 0 & \cdots & 0 \\
0 & \lambda & 1 & \cdots & 0 \\
\vdots & \vdots & \vdots & \ddots & \vdots \\
0 & 0 & 0 & \lambda & 1 \\
0 & 0 & 0 & 0 & \lambda
\end{bmatrix}
$$
where all values of \$\lambda\$ are identical inside each block. (A block diagonal matrix is one that can be divided into blocks, so that the blocks on the main diagonal are square and those out of the main diagonal are all zeros. See examples below.)
The following are some examples of a Jordan matrix:
$$
J\_1=\left[\begin{array}{c|c|cc}
2 & 0 & 0 & 0 \\ \hline
0 & 0 & 0 & 0 \\ \hline
0 & 0 & 2 & 1 \\
0 & 0 & 0 & 2 \end{array}\right]\\
J\_2=\left[\begin{array}{ccc|cc|cc|ccc}
0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ \hline
0 & 0 & 0 & 3 & 1 & 0 & 0 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 3 & 0 & 0 & 0 & 0 & 0 \\ \hline
0 & 0 & 0 & 0 & 0 & 3 & 1 & 0 & 0 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 3 & 0 & 0 & 0 \\ \hline
0 & 0 & 0 & 0 & 0 & 0 & 0 & 7 & 1 & 0 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 7 & 1 \\
0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 7 \end{array}\right]
$$
The following are more examples of Jordan matrices:
```
[[0]]  // 1x1 matrix is always Jordan
[[0, 0],
 [0, 0]]  // all-zero matrix is also always Jordan
[[3, 0, 0],
 [0, 2, 0],
 [0, 0, 2]]  // diagonal matrix is also Jordan, even if it has duplicates
[[3, 0, 0],
 [0, 2, 1],  // the one between the two 2s is optional
 [0, 0, 2]]  // (if it is a zero, the two are in separate blocks)
[[99,  1,  0,  0],
 [ 0, 99,  1,  0],
 [ 0,  0, 99,  0],
 [ 0,  0,  0,  1]]  // the matrix may contain numbers >= 10
[[1, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 3, 1, 0],  // same diagonal entries may appear separately
 [0, 0, 0, 3, 0],
 [0, 0, 0, 0, 1]]
```
The following are not Jordan:
```
[[0, 0, 0],
 [0, 0, 0],
 [1, 0, 0]]  // has a one where it should be zero
[[2, 0, 0],
 [1, 2, 0],
 [0, 0, 1]]  // 1 is at the wrong side of the diagonal
[[1, 1, 0],
 [0, 2, 0],  // the numbers on the left of and below the
 [0, 0, 2]]  // non-diagonal 1 are not equal
[[99, 99,  0,  0],
 [ 0, 99, 99,  0],
 [ 0,  0, 99,  0],
 [ 0,  0,  0,  1]]  // the matrix may contain numbers >= 10
[[1, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 3, 2, 0],  // all numbers right above the main diagonal
 [0, 0, 0, 3, 0],  // should be either 0 or 1
 [0, 0, 0, 0, 1]]
```
## Challenge
Given a square matrix filled with non-negative integers, determine if it is a Jordan matrix.
Input format is flexible. For output, you can choose to
1. output truthy/falsy using your language's convention (swapping is allowed), or
2. use two distinct, fixed values to represent true (affirmative) or false (negative) respectively.
Standard [code-golf](/questions/tagged/code-golf "show questions tagged 'code-golf'") rules apply. The shortest code in bytes wins.
  
[Answer]
# JavaScript, 58 bytes
```
a=>a.some((r,y)=>r.some((c,x)=>(r/(r=c)==a[x][x])/-y--<c))
```
[Try it online!](https://tio.run/##jVLN0pswDLznKXSEGUjifNeSWy99hQwHBURwayxqOyG002dPbZPw5a8zneGABNpd7eo7ntBWRvYu11zTpSkuWGxxabmjJDHZmBZbc62q7OyrxKwSU1RpUeDuXPonXeVjnn@p0vTioIDdYrdblyXAagXiLKBDZ@QZpAVUA44WvrGpUS@y8F8G6zJbwPQyzaBS@S8y/DBo@c30hx@6A9jcg/nyildLPLBG9Yw3AWVAJ9IgG5AOWrRQH3slK3Rk/8EhyiziupaANcGe3EAeItRuYNjYQMG9k4H1RU4yUQUVENbM5kE0BFKDpR6N54e94uqHTaMMMcl4FPO24wWL2IlsFjv6dIC0d4Csd2IE7HtCM7Op8R7z4x2LKMs5tKfv8f2q8bpn8BKjQUNLYTMHtuWjqr1hcfGItXmcf4pQ3M4o2uWiU4NhfQAra@9@Ezu39W5GiZebmPPSx25PxqczxaWocQEFdVCleAjdl8Q063y2UMSYNDugn8dPzv8P506Rv/RZkZGH1gHu@URRWof@FObNXqKZwp39JOlnDKyBDYg3sZULt2zYfMWqTTootvAbKtaWFS0VH5Im6dIU/qSXvw "JavaScript (Node.js) – Try It Online")
* When `c==0`
	+ `r/(r=c)` is `NaN` or `Infinity`;
	+ `(r/(r=c)==a[x][x])` is `false`
	+ `(r/(r=c)==a[x][x])/-y--` is `0` or `NaN`
	+ `(r/(r=c)==a[x][x])/-y--<c` is false
* When `y==0` (cells on main diagonal) and `c>=1`
	+ `(r/(r=c)==a[x][x])/-y--` is `NaN` or `Infinity`
	+ `(r/(r=c)==a[x][x])/-y--<c` is false
* When `y==-1` (cells on right side of main diagonal) and `c==1`
	+ `r/(r=c)` equals to `r` equals to left cell,
	+ `(r/(r=c)==a[x][x])` test if left cell equals to bottom cell
	+ `(r/(r=c)==a[x][x])/-y--` is `1` or `0` based on whether left cell equals to bottom cell
	+ `(r/(r=c)==a[x][x])/-y--<c` is `true` if left cell not equals to right cell
* When `y==-1` (cells on right side of main diagonal) and `c>=2`
	+ `(r/(r=c)==a[x][x])/-y--` is `1` or `0`
	+ `(r/(r=c)==a[x][x])/-y--<c` is `true`
* When (`y<-1` or `y>0`) and `c>=1`
	+ `(r/(r=c)==a[x][x])/-y--` is a number in range [-Inf, 1)
	+ `(r/(r=c)==a[x][x])/-y--<c` is `true`
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), ~~14~~ 13 bytes
```
ŒDµḢ=Ɲo@ƑḢ>FẠ
```
[Try it online!](https://tio.run/##y0rNyan8///oJJdDWx/uWGR7bG6@w7GJQJad28NdC/4fbteM/P8/OtogNlYHSOooGABpMAXiGwMZYCEuBZCgERIbxMWhxhBDjSFEDapKrCJA0wwx1Bhj02UIdzGaLJgNtRGsxghVHM0XhjAXGhLyKUm@gJiggOkNBQVMjwAA "Jelly – Try It Online")
*-1 because I actually thought to check if the input can contain negative integers*
```
ŒDµ              Consider the diagonals of the input matrix.
   Ḣ             Pop the main diagonal;
     Ɲ           for each pair of its adjacent elements
    =            are they equal?
                 (Especially note that this results in a list of ones and zeroes,
                 of the same length as the superdiagonal.)
         Ḣ       Pop the superdiagonal;
      o@         are the logical ORs of its elements and the neighbor equalities
        Ƒ        equal to the neighbor equalities?
           F     For every element in the flattened remaining diagonals,
          > Ạ    is the test result strictly greater?
```
Jelly's logical OR is Python's: if the first argument is truthy it is returned, else the second argument is returned. Testing the diagonal equalities' invariance under flipped logical OR with the superdiagonal determines if every element of the superdiagonal is either a 0 anywhere or a 1 "between" two equal elements on the diagonal, since a 0 on the superdiagonal leaves either equality value unchanged, but any non-zero overrides the equality value--passing only if both are 1.
[Answer]
# [Rattle](https://github.com/DRH001/Rattle), 111 bytes
```
|sI^>s[[PgI#1I#s2=#-#1[^0[^1g2[^0=q]][1g2[^0[^1=q]P=#4<s<=#3-sg0>I~<I~s_3P=#4+<s<=#3sg0>I~<I~<[^~=q]]]]g1]`]`=1
```
[Try it Online!](https://www.drh001.com/rattle/?flags=&code=%7CsI%5E%3Es%5B%5BPgI%231I%23s2%3D%23-%231%5B%5E0%5B%5E1g2%5B%5E0%3Dq%5D%5D%5B1g2%5B%5E0%5B%5E1%3Dq%5DP%3D%234%3Cs%3C%3D%233-sg0%3EI%7E%3CI%7Es_3P%3D%234%2B%3Cs%3C%3D%233sg0%3EI%7E%3CI%7E%3C%5B%5E%7E%3Dq%5D%5D%5D%5Dg1%5D%60%5D%60%3D1&inputs=%5B%5B1%2C%200%2C%200%2C%200%2C%200%5D%2C%20%5B0%2C%200%2C%200%2C%200%2C%200%5D%2C%20%5B0%2C%200%2C%203%2C%201%2C%200%5D%2C%20%5B0%2C%200%2C%200%2C%203%2C%200%5D%2C%20%5B0%2C%200%2C%200%2C%200%2C%201%5D%5D)
Needless to say, Rattle is not built to handle matrices and this approach is pretty brute-force. However, this code really shows off most of Rattle's features!
# Explanation
```
|                      takes input and parses automatically (Rattle's parser is very
                        powerful and automatically recognises nested lists)
 s                     saves the list to the first memory slot (slot 0)
  I^                   gets the length of the list
    >                  move pointer right
     s                 save length of list to slot 1
      [[ ... ]`]`      nested loop to iterate over array
                 
(inside nested loop)
I won't explain every single command here but the code inside the nested loop is split
 into two parts. 
First, the code checks to make sure all values that are not on the diagonal or off 
 the diagonal to the right are all zero and all values off the diagonal to the right
 are 1 ​or 0. If not, it prints a falsy value (0).
Next, it iterates over all values of 1 which appear in the off-diagonal. For each
 value of 1 in this off-diagonal, it ensures the value to the left and below the 1
 are equal. If not, it prints a falsy value (0).
Knowing these conditions have been checked for, a truthy value (1) is printed if a
 falsy value has not already been printed.
```
[Answer]
# [R](https://www.r-project.org/), ~~96~~ ~~93~~ ~~83~~ ~~75~~ 67 bytes
```
function(m,k,j=1:k^2%%(k+1),x=m[!j])any(m[j>1],diff(diag(m))&x,x>1)
```
[Try it online!](https://tio.run/##jZNfb5swFMXf8ynuVHXCqqvGQX2ZlH6APe@t6qRLMMGJsaltCuzLZzYw/iRpVilSMNH9neNzbswp256ySu2c0Coq6JEetuzH8ffm/j46PjBCm23x@u3wRlC1UfF6eGFvNBVZFqUC91FByPeGNi@MnLKoQGdEE@2iNaEsfO4Anp6ANQz6n0BYQFlja@GnNimq1Wwo/vLQfGpNYU1XEL4I3dCkNbre/vKPAwelfPzDjV7ArL5BjClM0M2E9wdC40khHhRCDlqhPFfo0RT4B1cgMhAOcrSQVqUUO3Tc3lRltKe7nINWHBLuau5B4exqDRsbhHQZWkN522HUqwdjELKgIwUNB6HA8hKNtwSJ1LujJQtnbHA283flHAfL/qFXtFjwKRiuPIpbH1ALWJYczago2xkwvhTwG/E8XeiZ3K0u218MrWDwezWJUAB2edY5D3d3YHNdydTn20WzoG/mzMUmsKt01kXsunRro9UerEh9fVn35l8c5@Gy5bJNtauqSLjxJfetS565wEIV3Epdh7efF@8hSqvHsQTWla20A/5eXbr4WsUzh/6PNTo0Yp87wER/8M5qgX6pxvueFTxsyBg7F37EwBq0Afaf9k9/AQ "R – Try It Online")
Takes input as matrix and it's size.
Outputs inverted `TRUE/FALSE` values.
[Answer]
# [Python 3](https://docs.python.org/3/), 69 bytes
```
lambda m,n:re.sub(f"(.)({10**~-n}\\1)*(0%r|$)"%{n},"",m)>""
import re
```
[Try it online!](https://tio.run/##XYzdSsQwEIXv8xRhcCFTakjau4X6IsaLLptipJ2ENF7Iur56zQ@CCvk555szJ3ykV0/jsUzmWOftcp351tM5Wrm/X8QCQqK4adV1X490N0ZjJ9Qpfj4gnG507wH6DZ8AmNuCj4lHeyw@8m1O3BH3wZJQKKOdrwLlHlaXBBgyBHhmJTXl2/jqyO4CGadpzVuZP6sXbCEA@eZdhZmE6CiJpbieEA/FmFLlsLH8Q9HDj9HNaKVqpr2jrnpsRJf9KkuMsaGI2pInWv@q/Ncy/Gn5Bg "Python 3 – Try It Online")
input is a flatened string of the matrix and its size
output `False` for Jordan and `True` for not jordan
#### Edit:
as it wasn't specified when I post this answer, my solution only works for matrix with single digits elements
## How it works:
* the main idea i to substitute every "jordan" sub patern by the empty string and check if the result is the empty string (every subpart of the matrix was jordan)
* `(.)({10**~-n}\\1)*(0%r|$)"%{n}` with for example `n=4` will give the patern `/(.)(1000\1)*(0{4}|$)/`
[Answer]
# [Wolfram Mathematica](http://www.wolfram.com), ~~150~~ ~~144~~ ~~137~~ 69 bytes
```
With[{m=#,l=Length@#,d=Diagonal},Union@@Join[m~d~#&/@2~Range~l~Join~-Range@l,{If[#2==1,#1,#2]}&~MapThread~{Differences@d@m,m~d~1}]=={0}]&
```
I know... I don't like it either! But this was the shortest code that I could think of, and I'm definitely not an expert. Please take it with a huge grain of salt and enlighten me with your valuable suggestions. [Try it online!](https://tio.run/##dY5NS8UwEEX3/RWFQFcRm7oOzOK5eKIgfuCidDG0SRNoU6nZhZm/Xl9EUGqFuzmHOx8zRmdmjL7HzertzUfXplkLOel7E8boQMhBnzyOS8CJ5GvwSwC4W3xoZx5YVNfQ8BOG0fDEWfPVF8Ek09m2QmmtpGikUB1V/IDvL241OHDK00qevLVmNaE3HzCAoE7rVFNXbY@rDxEs3PZuAVHxc4@BU5FSqWROTbIoU1nvIDNd6KDX/NM7WPENat/7ye/raqfqQ3XJzV@V/yIqaPsE)
**Edit later-** [Michael E2](https://mathematica.stackexchange.com/users/4999/michael-e2) proposed this elegant, much shorter solution: [Try it online](https://tio.run/##dY7BCoJAEIbvPoUgdPKgdRYmqMAoiHyCbR1zDu7G7hDFsr76phAUZvBfvp@Pf6YT3GInmKQITRHKGhUTP4@CDT3ggOrKLSRFccY7GouVNgzri4UdGcuw16YWaoNSdzdtiUkrSBbhZEgxNLCVrR64r6RQvYuci/N0TObTKHZxNoGR/UAz3vKPNzPxhnzqffJ9PZ9U2Ww1ZPVbjX95H/nwAg)
```
IdentityMatrix@Length@#==ReverseSort@Abs@First@JordanDecomposition@#&
```
Explanation for the initial code:
```
(* The Diagonal method extracts diagonal vectors from input matrix *)
With[{m=#, l=Length@#, d=Diagonal},
Union@@(
(* Except for the main diagonal and the one above it,
   all other diagonals must be zero: *)
d[m,#]& /@ Complement[Range[-l,l],{0,1}]
(* These diagonals are combined by Union method above *)
)=={0}
&&
(* The Difference of elements of the main diagonal is calculated.
   If an element in the secondary diagonal is 1,
   its corresponding element in the difference diagonal must be zero *)
And@@
MapThread[If[#1==1,#2==0,#1==0]&, {d[#,1], Differences@ d[#]}]]&
```
[Answer]
# [Desmos](https://desmos.com/calculator), 90 bytes
```
I=wy+x-w
f(L,w)=\prod_{x=1}^w\prod_{y=1}^w\{L[I]=0,x=y,x=y+1:\{L[I]=1\}\{L[I-1]=L[I+w]\}\}
```
Takes input as the flattened matrix and its width. Returns 1 for truthy and NaN for falsy.
[Try it on Desmos!](https://www.desmos.com/calculator/qquqiagcaj)
### Explanation
```
f(L,w)=
  \prod_{x=1}^w\prod_{y=1}^w   # iterate over all x,y in matrix
  \{ # piecewise (case statement)
    L[I]=0, # any 0 is OK; multiply by 1
    x=y,    # any values on main diagonal are OK; multiply by 1
    x=y+1:  # immediately above the main diagonal 
      \{L[I]=1\}           # must have value 1 (0s matched L[I]=0) and
      \{L[I-1]=L[I+w]\}    # left and bottom are same
    # fall through: multiply by NaN
  \}
```
[Answer]
# [Charcoal](https://github.com/somebody1234/Charcoal), 27 bytes
```
⬤θ⬤ι∨¬λ∨⁼κμ∧⁼⊕κμ⁼λ⁼§ικ§§θμμ
```
[Try it online!](https://tio.run/##bUy9DsIgEN59ihuPBJPa1YnBoYu6EwYCJDY9aErR@PZYRByMl0u@3ztz09HMmnK@xjEkFES4cCgwcrhEPM8Jib3pablrWnHi4DdDBNucIZjovAvJWZxYjT8RfZlIQ7DuWd6WTpMNl3rmWZtjzlLKnkNXV/EdgOz@6q11@Ml7pVTeP@gF "Charcoal – Try It Online") Link is to verbose version of code. Outputs a Charcoal boolean, i.e. `-` for Jordon, nothing if not. Explanation:
```
 θ                          Input array
⬤                           Each row satisfies
   ι                        Current row
  ⬤                         Each cell satifies
      λ                     Current cell
     ¬                      Logical Not i.e. is zero
    ∨                       Logical Or
         κ                  Current row index
        ⁼                   Equal to
          μ                 Current column index
       ∨                    Logical Or
              κ             Current row index
             ⊕              Incremented
            ⁼               Equal to
               μ            Current column index
           ∧                Logical And
                 λ          Current value
                ⁼           Equals
                   §ικ      Value to left
                  ⁼         Equals
                      §§θμμ Value below
```
Since the `Equals` operator returns `1` or `0`, the penultimate equality only holds for `1`s where the two adjacent values on the main diagonal are equal or `0`s where the values are unequal, but if the value was `0` we skipped it earlier anyway.
[Answer]
# [PARI/GP](https://pari.math.u-bordeaux.fr), 57 bytes
```
m->matrix(#m,,i,j,[1+k=i-j,1-a=m[i,j],m[i,i]-m[j,j]]*a*k)
```
[Attempt This Online!](https://ato.pxeger.com/run?1=jVBBCsIwEMSfRL2kusGmvVhK-wNfUILspZLWSCgR9C1eCiK-SV9j0kTtRfAQdmZ2Z3fI5a6xk9ud7q81KW5HU7P1I1OsVGg6eaJzBSChgYov20KyBjjDQlVWEuCKFExVjWVigYs28guekxVqvT9TJKwkupMHQxHIzLEZkGlNMYqAVBs0NHYghji3T1iYgieJlyBxYpYBD7qHQ8tCD2LgbmoYCWNjlH4cDn-7g2lkyIcFTkwCfYfgIYS7-E7xuf5_jORHDBH-re99fQE)
Outputs a zero matrix when the input is a Jordan matrix. In PARI/GP's convention, a matrix is truthy if and only if it contains at least one truthy entry.
[Answer]
# [Python 3](https://www.python.org), 92 bytes
```
def f(m,w,h):
 for i in range(h-1):i*=w;m[i:i+2]=(m[i]==m[i-~w])<m[i+1],
 return any(m[:-1])
```
[Attempt This Online!](https://ato.pxeger.com/run?1=nVIxbsMwDETH-BUcpYYGLGVp3PgljoYAtWoBtRKoNows-UiXLM0j8pP0NaWi2HKLtkMX6cg7ijxQb6fdvq239nh871qdPlzWT5UGzRrsseZ5AnrrwICx4Db2uWJ1Knhu7ov-sSlNbuZSFYyQKgo600Ov-IrAXChMwFVt5yxs7J4keSoUDz0-7s7-1ca_ypJZWWaK5P7GTOH1DPECh4wcGJQ_cOIbt1yiGOkQ3BQUDDCjsiC_akf9V7yYFPtoqhFx7JinO_QOnBxz0ULsK361R4P6WaOHyeT_MCH_NkGLnu2csS3T7LVraPtQKo7wUllaXTZCzm8rHL7LJw)
This is longer than the earlier Python solution, but it supports numbers larger than 9, as required by the challenge. It takes the matrix as a flat list/array of integers in row-major order, and returns False if it's Jordan, True if not.
The loop tests that each triple of diagonal and superdiagonal entries follows the rules, and the diagonals are overwritten with the results. Then `any` checks both that those tests passed and that the rest of the matrix is zero.
This is the first time in my life that I've wanted comparison operators to be left-associative. I almost rewrote the answer in C just for that reason, but I looked it up, and it turns out `<` and `==` have different precedence in C, and `<`'s precedence is higher, which is the opposite of what I need. So much for that.
[Answer]
# [Haskell](https://www.haskell.org/), 91 bytes
```
import Data.Array
j m=and[c==r+1&&v==1&&m!(r,c-1)==m!(r+1,c)|((r,c),v)<-assocs m,v/=0,c/=r]
```
[Try it online!](https://tio.run/##HY3BCoMwEETv/YoURLK4VqNX91DoX4iHJS001qhsglDov6cqDI95c5k3h89rmlJyfl0kqgdHvt1F@HsZlSeen70lksLk@Ua001@1oC0NEB21MGjhp48NcIOu5BAWG5THraIabUUyJM9uplXcHLMxm1yI54HWBg2gbrEF6FtU9ZEdDSqDp6hmSH8 "Haskell – Try It Online")
]