我正在尝试对 Julia 中的矩阵执行高斯消元法,然后以分数形式而不是小数形式提取解。例如,我想要 3/4 而不是 0.75 作为解决方案。
我当前用于行减少矩阵并将解表示为小数的代码如下:
using RowEchelon
A = [1 2 3;
4 5 6;
7 8 10]
A_reduced rref(A)
print(A_reduced)
有没有办法修改此代码以获得分数而不是浮点解?
也许您正在寻找
Rational{Int}.(rand())
在你的数据上要么投射初始矩阵
a = Rational{Int}.(A)
3×3 Matrix{Rational{Int64}}:
1 2 3
4 5 6
7 8 10
a[1]
1//1
rref(a)
3×3 Matrix{Rational{Int64}}:
1 0 0
0 1 0
0 0 1
或者只是结果
Rational{Int}.(rref(A))
3×3 Matrix{Rational{Int64}}:
1 0 0
0 1 0
0 0 1