SquareMatrixQ
Details
- A matrix m is square if it has the same number of rows and columns, in which case Dimensions[m]{n,n}. »
- SquareMatrixQ works for symbolic as well as numerical matrices.
Examples
open all close allBasic Examples (3)
Test if an object is a square matrix:
SquareMatrixQ[(| | |
| - | - |
| 1 | 2 |
| 3 | 4 |)]The following matrix is not square:
SquareMatrixQ[(| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |)]SquareMatrixQ[{1, 2, 3}]SquareMatrixQ[{{1}, {2, 3}}]Scope (9)
Basic Uses (5)
Test if a real machine-precision matrix is square:
m = {{1.2, -2.3}, {2.6, 3.4}};SquareMatrixQ[m]Test if a complex matrix is square:
m = {{I, 2 - 3I}, {-2 - 3I, -I}};SquareMatrixQ[m]Test if an exact matrix is square:
m = (| | | | |
| -- | - | -- | -- |
| 2 | 4 | -1 | -5 |
| -5 | 1 | 3 | -3 |
| -4 | 3 | -2 | 2 |);SquareMatrixQ[m]SquareMatrixQ[m.m^]Use SquareMatrixQ with a symbolic matrix:
SquareMatrixQ[{{a, b}, {c, d}}]SquareMatrixQ works efficiently with large numerical matrices:
m = RandomReal[1, RandomInteger[{1000, 2000}, 2]];AbsoluteTiming[SquareMatrixQ[m]]a = m.m^;
AbsoluteTiming[SquareMatrixQ[a]]Special Matrices (4)
Use SquareMatrixQ with sparse matrices:
SparseArray[{{1, 1} -> 1, {1, 2} -> 2, {2, 2} -> 3, {2, 1} -> 4}, {2, 2}]SquareMatrixQ[%]SparseArray[{i_, j_} -> i - j + (i + j)I, {5, 4}]SquareMatrixQ[%]Use SquareMatrixQ with structured matrices:
SymmetrizedArray[{{1, 2} -> 4, {3, 2} -> 5}, {3, 3}, Antisymmetric[All]]SquareMatrixQ[%]Use with a QuantityArray structured matrix:
QuantityArray[{{1, 2}, {2, 5}}, {"Meters", "Seconds"}]SquareMatrixQ[%]Use SquareMatrixQ with an identity matrix:
SquareMatrixQ[IdentityMatrix[5]]SquareMatrixQ[IdentityMatrix[{5, 3}]]Use SquareMatrixQ with HilbertMatrix:
SquareMatrixQ[HilbertMatrix[5]]SquareMatrixQ[HilbertMatrix[{5, 6}]]Applications (1)
Define a function that only evaluates for explicit square matrices:
hpart[m_ ? SquareMatrixQ] := (m + ConjugateTranspose[m]) / 2This represents the Hermitian part of a matrix explicitly:
hpart[{{1, 2I}, {3I, 4}}]HermitianMatrixQ[%]This does not evaluate because the matrix is rectangular:
hpart[{{1, 2I, -3}, {3I, 4, 0}}]This does not evaluate because a is not an explicit matrix:
hpart[a]Properties & Relations (5)
For a square matrix m, Dimensions[m] gives {n,n}:
Dimensions[(| | | |
| - | - | - |
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |)]SquareMatrixQ[expr] returns False for expressions that are not matrices:
SquareMatrixQ["string"]SquareMatrixQ[1]The empty list is not considered a square matrix:
SquareMatrixQ[{}]A square matrix is made up of
vectors of length
:
m = RandomReal[1, {3, 3}];Make sure it is made up of vectors:
And@@Map[VectorQ, m]And@@Map[VectorQ, {}]Verify that all vectors have the same length and the number of vectors equals their length:
Equal@@Append[Map[Length, m], Length[m]]SquareMatrixQ[m]For lists, SquareMatrixQ[a] is equivalent to MatchQ[TensorDimensions[a],{n,n}]:
MatrixForm[m = RandomReal[1, RandomInteger[{2, 4}, {2}]]]SquareMatrixQ[m] == MatchQ[TensorDimensions[m], {n_, n_}]Tech Notes
Related Guides
History
Text
Wolfram Research (2014), SquareMatrixQ, Wolfram Language function, https://reference.wolfram.com/language/ref/SquareMatrixQ.html.
CMS
Wolfram Language. 2014. "SquareMatrixQ." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SquareMatrixQ.html.
APA
Wolfram Language. (2014). SquareMatrixQ. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SquareMatrixQ.html
BibTeX
@misc{reference.wolfram_2026_squarematrixq, author="Wolfram Research", title="{SquareMatrixQ}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/SquareMatrixQ.html}", note=[Accessed: 15-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_squarematrixq, organization={Wolfram Research}, title={SquareMatrixQ}, year={2014}, url={https://reference.wolfram.com/language/ref/SquareMatrixQ.html}, note=[Accessed: 15-June-2026]}