forked from cpmech/gosl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathla_sparseReal01.go
More file actions
56 lines (45 loc) · 1.22 KB
/
la_sparseReal01.go
File metadata and controls
56 lines (45 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Copyright 2016 The Gosl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build ignore
package main
import (
"github.com/cpmech/gosl/io"
"github.com/cpmech/gosl/la"
)
func main() {
// input matrix in Triplet format
// including repeated positions. e.g. (0,0)
A := new(la.Triplet)
A.Init(5, 5, 13) // 5 x 5 matrix with 13 non-zero entries
A.Put(0, 0, +1.0) // 0 << repeated
A.Put(0, 0, +1.0) // 1 << repeated
A.Put(1, 0, +3.0) // 2
A.Put(0, 1, +3.0) // 3
A.Put(2, 1, -1.0) // 4
A.Put(4, 1, +4.0) // 5
A.Put(1, 2, +4.0) // 6
A.Put(2, 2, -3.0) // 7
A.Put(3, 2, +1.0) // 8
A.Put(4, 2, +2.0) // 9
A.Put(2, 3, +2.0) // 10
A.Put(1, 4, +6.0) // 11
A.Put(4, 4, +1.0) // 12
// right-hand-side
b := []float64{8.0, 45.0, -3.0, 3.0, 19.0}
// allocate solver
o := la.NewSparseSolver("umfpack")
defer o.Free()
// initialise solver
symmetric, verbose := false, false
o.Init(A, symmetric, verbose, "", "", nil)
// factorise
o.Fact()
// solve
x := la.NewVector(len(b))
o.Solve(x, b, false) // x := inv(A) * b
// print solution
xCorrect := []float64{1, 2, 3, 4, 5}
io.Pf("x = %v\n\n", x)
io.Pf("xCorrect = %v\n", xCorrect)
}