What mistake did I make in this matrix multiplication in Julia? -
in julia:
in [1]: m1 = [1 3 4; 45 64 33; 456 3 454;] out [1]: 3x3 array{int64,2}: 1 3 4 45 64 33 456 3 454 in [2]: m1 * inv(m1) out [2]: 3x3 array{float64,2}: 1.0 6.93889e-18 -8.67362e-19 0.0 1.0 -2.08167e-17 -1.42109e-14 -8.88178e-16 1.0
m1 * inv(m1) supposed identity matrix definition. what's wrong?
i tried same thing in matlab:
>> m1 = [1 3 4; 45 64 33; 456 3 454;] m1 = 1 3 4 45 64 33 456 3 454 >> inv(m1) ans = -0.280088987764182 0.013057987135465 0.001518595540939 0.052057842046719 0.013251438796731 -0.001421869710306 0.280978865406007 -0.013203075881414 0.000686753397495 >> m1 * inv(m1) ans = 1.000000000000000 0.000000000000000 -0.000000000000000 0 1.000000000000000 -0.000000000000000 -0.000000000000014 -0.000000000000001 1.000000000000000 >>
matlab returns right result here. guess julia not make mistake here. what's wrong calculation / notation?
edit
the problem caused number of digits in floating point result. should have asked, how set result digits precision in julia?
julia , matlab give same result (for instance, bottom-left element -1.4e-14 in both cases): not identity matrix because floating point arithmetic not exact.
you can explicitly round result before displaying it.
m1 = [ 1 3 4; 45 64 33; 456 3 454 ] round( m1 * inv(m1), 6 ) # 3x3 array{float64,2}: # 1.0 0.0 -0.0 # 0.0 1.0 -0.0 # 0.0 -0.0 1.0
if want exact result, can use rationals.
m1 = [ 1//1 3 4; 45 64 33; 456 3 454 ] m1 * inv(m1) # 3x3 array{rational{int64},2}: # 1//1 0//1 0//1 # 0//1 1//1 0//1 # 0//1 0//1 1//1
Comments
Post a Comment