Skip to content

Commit 4a2e205

Browse files
committed
handle H = []
handle case when H =[]
1 parent 22e2ca7 commit 4a2e205

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

matlab/sqsolve.m

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
function [x,fval,exitFlag,output,lambda] = sqsolve(Hx, f, varargin)
2-
% function [x,fval,exitFlag,output,lambda] = sqsolve(Hx, f, varargin)
1+
function [x,fval,exitFlag,output,lambda] = sqsolve(H, f, varargin)
2+
% function [x,fval,exitFlag,output,lambda] = sqsolve(H, f, varargin)
33
%
44
% This function interface is similar to the MATLAB function quadprog.
5-
% However, the Hessian is provided via a user-defined function Hx.
65
%
76
% Currently, the only field recognized in the 'option's arguments are
87
% options.name, options.start
@@ -11,18 +10,18 @@
1110
%
1211
%
1312
% Calling sequences:
14-
% x = sqsolve(Hx, f)
15-
% x = sqsolve(Hx, f, A, b)
16-
% x = sqsolve(Hx, f, A, b, Aeq, beq)
17-
% x = sqsolve(Hx, f, A, b, Aeq, beq, lb, ub)
18-
% x = sqsolve(Hx, f, A, b, Aeq, beq, lb, ub, x0)
19-
% x = sqsolve(Hx, f, A, b, Aeq, beq, lb, ub, x0, options)
20-
% x = sqsolve(Hx, f, A, b, Aeq, beq, lb, ub, x0, lambda, states, options)
13+
% x = sqsolve(H, f)
14+
% x = sqsolve(H, f, A, b)
15+
% x = sqsolve(H, f, A, b, Aeq, beq)
16+
% x = sqsolve(H, f, A, b, Aeq, beq, lb, ub)
17+
% x = sqsolve(H, f, A, b, Aeq, beq, lb, ub, x0)
18+
% x = sqsolve(H, f, A, b, Aeq, beq, lb, ub, x0, options)
19+
% x = sqsolve(H, f, A, b, Aeq, beq, lb, ub, x0, lambda, states, options)
2120
%
22-
% [x,fval] = sqsolve(Hx, f, ...)
23-
% [x,fval,exitflag] = sqsolve(Hx, f, ...)
24-
% [x,fval,exitflag,output] = sqsolve(Hx, f, ...)
25-
% [x,fval,exitflag,output,lambda] = sqsolve(Hx, f, ...)
21+
% [x,fval] = sqsolve(H, f, ...)
22+
% [x,fval,exitflag] = sqsolve(H, f, ...)
23+
% [x,fval,exitflag,output] = sqsolve(H, f, ...)
24+
% [x,fval,exitflag,output,lambda] = sqsolve(H, f, ...)
2625
%
2726
%
2827
% Solve the given quadratic problem:
@@ -32,8 +31,10 @@
3231
% Aeq*x = beq
3332
%
3433
% INPUT:
35-
% Hx is a user-defined function that returns the product of a
36-
% vector and the Hessian matrix of the objective
34+
% H is a Matlab function (either a function handle or string)
35+
% that computes H*x for a given x or a matrix (dense or sparse).
36+
% If the problem is an LP (H = 0), then set H = 0 or H = []
37+
% (or call lpopt).
3738
%
3839
% f is the linear term of the objective
3940
%
@@ -74,16 +75,25 @@
7475
probName = '';
7576
start = 'Cold';
7677

77-
if isnumeric(Hx) && Hx == 0,
78-
warning('No Hessian detected: the problem is an LP');
79-
userHx = 0;
78+
if isempty(H),
79+
warning('No Hessian detected: the problem is an LP');
80+
userHx = 0;
8081
else
81-
userHx = checkFun(Hx,'SQOPT','Hx');
82+
if isnumeric(H),
83+
if H == 0,
84+
warning('No Hessian detected: the problem is an LP');
85+
userHx = 0;
86+
else
87+
userHx = @(x)myHx(H,x);
88+
end
89+
else
90+
userHx = checkFun(H,'SQOPT','H');
91+
end
8292
end
8393

8494

8595
if nargin == 2,
86-
% sqsolve(Hx, f)
96+
% sqsolve(H, f)
8797
A = []; b = [];
8898
Aeq = []; beq = [];
8999
lb = []; ub = [];
@@ -93,7 +103,7 @@
93103
astate = []; amul = [];
94104

95105
elseif nargin == 4,
96-
% sqsolve(Hx, f, A, b)
106+
% sqsolve(H, f, A, b)
97107
A = varargin{1};
98108
b = varargin{2};
99109
Aeq = []; beq = [];
@@ -105,7 +115,7 @@
105115

106116

107117
elseif nargin == 6,
108-
% sqsolve(Hx, f, A, b, Aeq, beq)
118+
% sqsolve(H, f, A, b, Aeq, beq)
109119
A = varargin{1};
110120
b = varargin{2};
111121
Aeq = varargin{3};
@@ -117,7 +127,7 @@
117127
astate = []; amul = [];
118128

119129
elseif nargin == 8,
120-
% sqsolve(Hx, f, A, b, Aeq, beq, lb, ub)
130+
% sqsolve(H, f, A, b, Aeq, beq, lb, ub)
121131
A = varargin{1};
122132
b = varargin{2};
123133
Aeq = varargin{3};
@@ -130,7 +140,7 @@
130140
astate = []; amul = [];
131141

132142
elseif nargin == 9,
133-
% sqsolve(Hx, f, A, b, Aeq, beq, lb, ub, x0)
143+
% sqsolve(H, f, A, b, Aeq, beq, lb, ub, x0)
134144
A = varargin{1};
135145
b = varargin{2};
136146
Aeq = varargin{3};
@@ -143,8 +153,8 @@
143153
astate = []; amul = [];
144154

145155
elseif nargin == 10 || nargin == 12,
146-
% sqsolve(Hx, f, A, b, Aeq, beq, lb, ub, x0, options)
147-
% sqsolve(Hx, f, A, b, Aeq, beq, lb, ub, x0, lambda, states, options)
156+
% sqsolve(H, f, A, b, Aeq, beq, lb, ub, x0, options)
157+
% sqsolve(H, f, A, b, Aeq, beq, lb, ub, x0, lambda, states, options)
148158
A = varargin{1};
149159
b = varargin{2};
150160
Aeq = varargin{3};
@@ -214,3 +224,7 @@
214224
states.linear = state(n+1:n+m);
215225
lambda.linear = y(n+1:n+m);
216226
end
227+
228+
229+
function [Hx] = myHx(H,x)
230+
Hx = H*x;

0 commit comments

Comments
 (0)