# QR-like factorization preserving type?

**URL:** <https://discourse.julialang.org/t/qr-like-factorization-preserving-type/38129>\
**Category:** General Usage\
**Created:** [April 24, 2020, 8:38am UTC](https://discourse.julialang.org/t/qr-like-factorization-preserving-type/38129 "2020-04-24T08:38:55Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![chrisvwx](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisvwx/32/45289_2.png) [@chrisvwx](https://discourse.julialang.org/u/chrisvwx)\
**Post date:** [April 25, 2020, 3:27am UTC](https://discourse.julialang.org/t/qr-like-factorization-preserving-type/38129/2 "2020-04-25T03:27:24Z")

</div>

I don’t think the LinearAlgebra package has such a function; perhaps the following will work:

```julia
julia> function gs(H::Matrix{Td}) where {Td}
           Q = copy(H)
           n = size(H,2)
           R = Matrix{Td}(I,n,n)
           for i=1:n
               for j=1:i-1
                   R[j,i] = H[:,i]'*Q[:,j]
                   Q[:,i]-= R[j,i]*Q[:,j]
               end
           end
           return Q,R
       end
gs (generic function with 1 method)

julia> N=2; a=rand(0:10,N,N); Q,R = gs(a); Q*R-a
2×2 Array{Int64,2}:
 0 0
 0 0

julia> N=2; a=rand(0:10,N,N).//rand(1:10,N,N); Q,R = gs(a); Q*R-a
2×2 Array{Rational{Int64},2}:
 0//1 0//1
 0//1 0//1

```

---

_[View the full topic](https://discourse.julialang.org/t/qr-like-factorization-preserving-type/38129)._
