LU decomposition: Difference between revisions

m (correction: there stood "columns" where rows was meant)
Line 1,098:
 
type matrix struct {
stride int
ele []float64
stride int
}
 
func matrixFromRows(rows [][]float64) *matrix {
if len(rows) == 0 {
return &matrix{nil, 0}
}
m := &matrix{make([]float64, len(rows)*len(rows[0])), len(rows[0])}
for rx, row := range rows {
copy(m.ele[rx*m.stride:(rx+1)*m.stride], row)
}
return m
}
 
Line 1,127 ⟶ 1,116:
return nil, false
}
m3 = &matrix{m2.stride, make([]float64, (len(m1.ele)/m1.stride)*m2.stride), m2.stride}
for m1c0, m3x := 0, 0; m1c0 < len(m1.ele); m1c0 += m1.stride {
for m2r0 := 0; m2r0 < m2.stride; m2r0++ {
Line 1,141 ⟶ 1,130:
 
func zero(rows, cols int) *matrix {
return &matrix{cols, make([]float64, rows*cols), cols}
}
 
Line 1,202 ⟶ 1,191:
}
l.ele[ixc0+j] = (a.ele[ixc0+j] - sum) / u.ele[jxc0+j]
}
jxc0 += a.stride
}
return
}
 
func main() {
showLU(matrixFromRows([]&matrix{3, []float64{
{1, 3, 5},
{2, 4, 7},
{1, 1, 0}}))
showLU(matrixFromRows([]&matrix{4, []float64{
{11, 9, 24, 2},
{1, 5, 2, 6},
{3, 17, 18, 1},
{2, 5, 7, 1}}))
}
 
func showLU(a *matrix) {
a.print("\na")
l, u, p := a.lu()
l.print("l")
u.print("u")
1,707

edits