Generate matrices that satisfy multiple conditions


I am trying to verify the fact that an orthogonal upper (lower) triangular matrix has to be diagonal. Though it is pretty straightforward in maths, I could not correctly verify it using Julia.

The way I could think of is to directly generate orthogonal upper (lower) triangular matrix \mathbf{M} and show that M == Diagonal(M) or inv(M) == M'. However, the difficulty is I do not know how to generate a matrix that satisfies both triangularity and orthogonality. I now just have a brute force way

while true
    Q = qr(randn(n, n)).Q
    if isapprox(Q, LowerTriangular(Q))

but there is no hope.

Could someone help me, thank you in advance.

This is very similar in spirit to your other question, trying to numerically “verify” something that you can show using math.

This is a very unusual application, and it is not possible to help you more without some context.

Specifically, is this a homework problem?

As you noted, if you randomly generate an orthonormal matrix, it has a 0 (theoretically, maybe near-0 numerically) chance of being triangular. Your approach is conceptually similar to

while true
    r = randn()
    if iszero(r)
        @info r^2 == r # verify that 0*0 == 0 numerically

I realized sometime that my method is not possible to get result since the matrices that are both triangular and orthogonal could only be diagonal matrices (which is hard to attain if I just randomly generate matrices).