@@ -3,25 +3,41 @@ using LinearSolve
3
3
using LinearAlgebra
4
4
5
5
# Constructing sparse array
6
- function hess_sparse (x:: Vector{T} ) where T
7
- return [- sin (x[1 ] + x[2 ]) + 1 , - sin (x[1 ] + x[2 ]), - sin (x[1 ] + x[2 ]), - sin (x[1 ] + x[2 ]) + 1.0 , 1.0 , 1.0 , 12.0 * x[5 ]^ 2 + 1.0 , 1.0 ]
6
+ function hess_sparse (x:: Vector{T} ) where {T}
7
+ return [
8
+ - sin (x[1 ] + x[2 ]) + 1 ,
9
+ - sin (x[1 ] + x[2 ]),
10
+ - sin (x[1 ] + x[2 ]),
11
+ - sin (x[1 ] + x[2 ]) + 1.0 ,
12
+ 1.0 ,
13
+ 1.0 ,
14
+ 12.0 * x[5 ]^ 2 + 1.0 ,
15
+ 1.0 ,
16
+ ]
8
17
end
9
18
rowval = [1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 ]
10
19
colval = [1 , 2 , 1 , 2 , 3 , 4 , 5 , 6 ]
11
20
12
21
# Constructing sparse vec
13
- function grad_sparse (x:: Vector{T} ) where T <: Number
14
- return [cos (x[1 ] + x[2 ]), cos (x[1 ] + x[2 ]), 2 * x[3 ], 1 / 2 , 4 * x[5 ]^ 3 , 1 / 2 ]
22
+ function grad_sparse (x:: Vector{T} ) where {T <: Number }
23
+ return [cos (x[1 ] + x[2 ]), cos (x[1 ] + x[2 ]), 2 * x[3 ], 1 / 2 , 4 * x[5 ]^ 3 , 1 / 2 ]
15
24
end
16
25
gradinds = [1 , 2 , 3 , 4 , 5 , 6 ]
17
26
18
27
# Forming the matrix and vector
19
- x0 = [0.7853981648713337 , 0.7853981693418342 , 1.023999999999997e-7 , - 1.0 , 0.33141395338218227 , - 1.0 ]
28
+ x0 = [
29
+ 0.7853981648713337 ,
30
+ 0.7853981693418342 ,
31
+ 1.023999999999997e-7 ,
32
+ - 1.0 ,
33
+ 0.33141395338218227 ,
34
+ - 1.0 ,
35
+ ]
20
36
n = length (x0)
21
- hess_mat = sparse (rowval, colval, hess_sparse (x0), n, n)
37
+ hess_mat = sparse (rowval, colval, hess_sparse (x0), n, n)
22
38
grad_vec = sparsevec (gradinds, grad_sparse (x0), n)
23
39
24
40
# # Converting grad_vec to dense succeds in solving
25
- prob = LinearProblem (hess_mat, grad_vec)
41
+ prob = LinearProblem (hess_mat, grad_vec)
26
42
linsolve = init (prob)
27
43
@test solve (linsolve). u ≈ hess_mat \ Array (grad_vec)
0 commit comments