In Julia, matrix division of two rational-valued matrices returns a floating-point matrix. How can I obtain a matrix of rational numbers instead?
I can't just use convert(Array{Rational}, A \ b)
because of the loss of precision associated with floating-point numbers.
You'd have to implement a pivoted QR factorization algorithm for rational matrices, which is a pretty non-trivial undertaking, although certainly not impossible. Julia uses the LAPACK DGEQP3 function to do this for 64-bit floating-point matrices. Even if you did manage to get a rational QR factorization working, it would be nowhere near as fast as the LAPACK algorithm. So I guess I would ask what you need exact rational matrix division for.
Aside: You may find it more fruitful to ask questions like this on the julia-users list since you will be able to have a conversation about it – this isn't really an "asked and answered" kind of problem.
Update: This now "just works" because a generic pivoted QR now exists in Julia:
julia> A = [rand(1:10)//rand(1:10) for i=1:4, j=1:4]
4x4 Array{Rational{Int64},2}:
5//3 1//2 10//1 8//9
1//1 3//2 6//7 2//3
4//1 8//9 6//7 10//3
7//2 5//2 1//2 5//1
julia> b = collect(1:4)
4-element Array{Int64,1}:
1
2
3
4
julia> c = A\b
4-element Array{Rational{Int64},1}:
42055//62497
61344//62497
-2954//62497
-19635//124994
julia> A*c
4-element Array{Rational{Int64},1}:
1//1
2//1
3//1
4//1
Beware, however, that Rational{Int}
are fairly prone to overflow, so you may need to use Rational{Int128}
or Rational{BigInt}
to avoid overflows. This algorithm is thoroughly generic and works for even more exotic numerical types as well:
julia> using Quaternions # install with Pkg.add("Quaternions")
julia> A = [Quaternion(rand(1:10), rand(1:10), rand(1:10), rand(1:10)) for i=1:4, j=1:4]
4x4 Array{Quaternions.Quaternion{Int64},2}:
4 + 3im + 5jm + 8km 9 + 7im + 10jm + 3km 9 + 3im + 1jm + 7km 8 + 4im + 8jm + 5km
1 + 4im + 2jm + 1km 5 + 4im + 1jm + 4km 1 + 5im + 8jm + 2km 7 + 2im + 5jm + 3km
10 + 1im + 4jm + 4km 2 + 4im + 9jm + 2km 2 + 10im + 4jm + 10km 2 + 3im + 4jm + 8km
7 + 4im + 3jm + 7km 8 + 3im + 5jm + 9km 6 + 5im + 1jm + 3km 10 + 10im + 3jm + 1km
julia> b = collect(1:4)
4-element Array{Int64,1}:
1
2
3
4
julia> c = A\b
4-element Array{Quaternions.Quaternion{Float64},1}:
0.18112 + 0.019288355350921868im - 0.002638049486498678jm + 0.11047233503816825km
0.000578119 + 0.08073035854610844im - 0.023278758601757744jm - 0.16761078955242836km
-0.0501257 - 0.02428891715971441im - 0.11059096793480247jm - 0.059017235311989824km
0.0394953 - 0.16771397199827004im - 0.019823106776170954jm + 0.05251791790026253km
julia> A*c
4-element Array{Quaternions.Quaternion{Float64},1}:
1.0 + 1.1102230246251565e-16im + 0.0jm + 0.0km
2.0 + 2.220446049250313e-16im + 0.0jm + 0.0km
3.0 + 4.440892098500626e-16im - 2.220446049250313e-16jm + 8.881784197001252e-16km
4.0 + 2.220446049250313e-16im - 2.220446049250313e-16jm + 6.661338147750939e-16km
julia> norm(b - A*c)
1.680072297996111e-15
Note that quaternion multiplication is not commutative, which makes this rather interesting.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With