The following code returns whether a matrix is symmetric.
bool is_symmetric(void) const
{
bool sym = true;
for(int i = 2; i <= M_.get_m(); i++)
for(int j = 1; j <= i - 1; j++)
if (M_.get(i,j) != M_.get(j,i))
sym = false;
return sym;
}
Why does it i
start at row 2 ( i=2
) and j
, as usual, start at column 1? are there any restrictions?
the completion condition of the second for
, j <= i - 1
, why does j
it always have to be less than i-1
?
Math
Because, when
i = j
,M_.get(i,j)
is equal toM_get(j,i)
, always.Which also explains the first question, since for
i = 1
;j
would not have any possible value (it starts with1
, but the condition is that it is less than or equal toi - 1 = 1 - 1 = 0
)And if,
j > i
, that value is already checked (by checking thatM_.get(2,4) == M_.get(4,2)
, you also check thatM_.get(4,2) == M.get(2,4)
.