Anonymous user
LU decomposition: Difference between revisions
→{{header|Ruby}}
(Updated D entry) |
|||
Line 1,964:
if j >= i
# upper
u[i][j] = tmp[i,j] - (0 ... i
else
# lower
l[i][j] = (tmp[i,j] - (0 ... j
end
end
Line 1,973:
[ Matrix[*l], Matrix[*u], p ]
end
def get_pivot
raise ArgumentError, "must be square" unless square?
Line 1,990:
Matrix[*id]
end
def pretty_print(format, head=nil)
puts head if head
puts each_slice(column_size).map{|row|
end▼
end
end
puts "Example 1:"
a = Matrix[[1, 3, 5],
[2, 4, 7],
[1, 1, 0]]
l, u, p = a.lu_decomposition
puts "\nExample 2:"
a = Matrix[[11, 9,24,2],
[ 1, 5, 2,6],
[ 3,17,18,1],
[ 2, 5, 7,1]]
l, u, p = a.lu_decomposition
{{out}}
<pre style="height: 40ex; overflow: scroll">
Example 1:
1 3 5 ▼
A
U▼
2.00000 4.00000 7.00000 ▼
0.00000 1.00000 1.50000 ▼
0.00000 0.00000 -2.00000 ▼
L
▲U
P
0 1 0
1 0 0
0 0 1
Example 2:
A
11 9 24 2
U▼
11.00000 9.00000 24.00000 2.00000 ▼
0.00000 14.54545 11.45455 0.45455 ▼
0.00000 0.00000 -3.47500 5.68750 ▼
0.00000 0.00000 0.00000 0.51079 ▼
L
▲U
P
1 0 0 0
0 0 1 0
0 1 0 0
0 0 0 1
</pre>
Matrix has a <code>lup_decomposition</code> built-in method.
<lang ruby>l, u, p = a.lup_decomposition
l.pretty_print(" %8.5f", "L")
u.pretty_print(" %8.5f", "U")
p.pretty_print(" %d", "P")</lang>
Output is the same.
=={{header|Tcl}}==
|