1 | function varargout = nnz(varargin) |
---|
2 | %NNZ (overloaded) |
---|
3 | % |
---|
4 | % n = nnz(X) |
---|
5 | % |
---|
6 | % The NNZ operator is implemented using the concept of nonlinear operators |
---|
7 | % in YALMIP. NNZ(X) creates a new so called derived variable that can be |
---|
8 | % treated as any other variable in YALMIP. When SOLVESDP is issued, |
---|
9 | % logic constraints are added to the problem to model the NNZ operator. |
---|
10 | |
---|
11 | % Author Johan Löfberg |
---|
12 | % $Id: nnz.m,v 1.1 2006/08/10 18:00:21 joloef Exp $ |
---|
13 | |
---|
14 | switch class(varargin{1}) |
---|
15 | |
---|
16 | case 'sdpvar' |
---|
17 | % Simple binary vector? |
---|
18 | if all(ismember(getvariables(varargin{1}),yalmip('binvariables'))) |
---|
19 | if is(varargin{1},'lpcone') |
---|
20 | varargout{1} = sum(reshape(varargin{1},prod(size(varargin{1})),1)); |
---|
21 | return |
---|
22 | end |
---|
23 | end |
---|
24 | % Nope, more advanced variable. |
---|
25 | varargout{1} = yalmip('addextendedvariable','nnz',varargin{:}); |
---|
26 | |
---|
27 | case 'char' |
---|
28 | z = varargin{2}; |
---|
29 | x = varargin{3}; |
---|
30 | switch varargin{1} |
---|
31 | case 'graph' |
---|
32 | F = nnz_internal(z,x,0); |
---|
33 | properties = struct('convexity','convex','monotonicity','none','definiteness','none'); |
---|
34 | case 'milp' |
---|
35 | F = nnz_internal(z,x,1); |
---|
36 | properties = struct('convexity','milp','monotonicity','milp','definiteness','milp'); |
---|
37 | otherwise |
---|
38 | error('Unexpected call to NNZ OPERATOR') |
---|
39 | end |
---|
40 | varargout{1} = F; |
---|
41 | varargout{2} = properties ; |
---|
42 | varargout{3} = x; |
---|
43 | otherwise |
---|
44 | end |
---|