For loop: InexactError: Bool

Hello everyone!

im really new to Julia and have some problems regarding my code.
Im trying to do a qr decomposition.

function qr_zerlegung(A)

    m,n = size(A)
	
	j=1
	
	
	minNM=min(n-1, m);
	
	Q=Matrix(I,m,n)
	
	for j in 1:minNM
		
		
		alpha=1^(sign(A[j,j]))*norm(A[j:n,j],2)
		
		
		
		v=A[j:n,j]+alpha*Matrix(I,n+1-j,1)
		
		
		H=Matrix(I,n,n)
		
		H[j:n,j:n]=Matrix(I,n-j+1,n-j+1)-(2*(v*v')/norm(v,2)^2)
		
		A=H*A
		
		A[j+1:n,j]=0
		
		Q=Q*H
			
	end
	
	R=A

    return Q, R

end

I think that the line with

H[j:n,j:n]=Matrix(I,n-j+1,n-j+1)-(v*v')/norm(v,2)^2

produced the InexactError: Bool because of the assignment. Or i think it does :see_no_evil:
Does anyone know how to fix it?

Have a great day!

Shrox

You’re updating H in place, but it has an element type of Bool — so it can only store Bools:

julia> H=Matrix(I,3,3)
3×3 Matrix{Bool}:
 1  0  0
 0  1  0
 0  0  1

julia> H[1, 1] = rand()
ERROR: InexactError: Bool(0.7071995471955543)

You probably want something like H=Matrix{eltype(A)}(I,3,3).

3 Likes

hey!
thanks for the fast reply.

Oh ok. So H=Matrix{eltype(A)}(I,n,n) is producing a Int64

Matrix(I,n-j+1,n-j+1)-(2*(v*v')/norm(v,2)^2)
3×3 Array{Float64,2}:
  0.57735  -0.57735   0.57735
 -0.57735   0.211325  0.788675
  0.57735   0.788675  0.211325

is producing a Float64. So in order to work i need H to be Float64 as well. Is that how it generally works?

I really appreciate the help!

edit:
with H=Matrix{eltype(A)}(I,n,n)
i get an InexactError: Int64

Im a bit lost with these datatypes :see_no_evil:

H=Matrix{eltype(A)}(I,n,n) would make a matrix of eltype(A)

If you eltype(A) is Int64, it would have the similar inexact problem; i.e. making a matrix H of Int64.
Meaning, if you make a matrix like this:

julia> A = [1 2;3 4]
2×2 Array{Int64,2}:
 1  2
 3  4

The element would be Int64, therefore H would also be element of type Int64

julia> Matrix{eltype(A)}(I,n,n)
3×3 Array{Int64,2}:
 1  0  0
 0  1  0
 0  0  1
1 Like