@@ -3,25 +3,41 @@ using LinearSolve
33using LinearAlgebra
44
55# 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+ ]
817end
918rowval = [1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 ]
1019colval = [1 , 2 , 1 , 2 , 3 , 4 , 5 , 6 ]
1120
1221# 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 ]
1524end
1625gradinds = [1 , 2 , 3 , 4 , 5 , 6 ]
1726
1827# 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+ ]
2036n = length (x0)
21- hess_mat = sparse (rowval, colval, hess_sparse (x0), n, n)
37+ hess_mat = sparse (rowval, colval, hess_sparse (x0), n, n)
2238grad_vec = sparsevec (gradinds, grad_sparse (x0), n)
2339
2440# # Converting grad_vec to dense succeds in solving
25- prob = LinearProblem (hess_mat, grad_vec)
41+ prob = LinearProblem (hess_mat, grad_vec)
2642linsolve = init (prob)
2743@test solve (linsolve). u ≈ hess_mat \ Array (grad_vec)
0 commit comments