-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvsdp_indexable.m
119 lines (113 loc) · 3.86 KB
/
vsdp_indexable.m
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
classdef vsdp_indexable < handle
% VSDP_INDEXABLE A quantity that can be separated into different cones.
%
% Example:
%
% obj = vsdp (...);
% n = obj.K.f + obj.K.l;
% lb = vsdp_indexable (zeros (n, 1), obj);
% lb.l = ...
%
% See also vsdp.
%
% Copyright 2004-2020 Christian Jansson (jansson@tuhh.de)
properties
value
vsdp_obj
end
methods
function obj = vsdp_indexable (value, vsdp_obj)
% VSDP_INDEXABLE Constructor for an indexable object.
%
% obj = vsdp_indexable(value, vsdp_obj) where 'value' is an arbitrary
% quantity, that should be indexed and 'vsdp_obj' is a
% reference to a VSDP object.
%
narginchk (2, 2);
if (~isa (vsdp_obj, 'vsdp'))
error ('VSDP_INDEXABLE:vsdp_indexable:badInput', ...
'vsdp_indexable: Second argument must be a VSDP object.');
end
obj.value = value;
obj.vsdp_obj = vsdp_obj;
end
function subval = subsref(obj, S)
% SUBSREF Redefine subscripted reference for objects.
%
switch (S(1).type)
case '.'
if (strcmp (S(1).subs, 'value'))
if (length (S) == 1)
subval = obj.value;
else
subval = subsref (obj.value, S(2:end));
end
return;
end
idx = getfield (obj.vsdp_obj.K.idx, S(1).subs);
if (length (S) == 1)
% Like 'obj.f'
subval = obj.value(idx(1):idx(end),:);
elseif ((length (S) == 2) && (isequal (S(2).type, '()')) ...
&& isscalar (S(2).subs))
cone_idx = S(2).subs{1};
if (isnumeric (cone_idx) && isscalar (cone_idx))
% Like 'obj.s(2)''
subval = obj.value(idx(cone_idx,1):idx(cone_idx,2),:);
else
error ('VSDP_INDEXABLE:subsref:unsupportet', ...
'subsref: Unsupported indexing.');
end
else
error ('VSDP_INDEXABLE:subsref:unsupportet', ...
'subsref: Unsupported indexing.');
end
case '()' % Like obj(1:end)
% Simply forward them to value
subval = subsref (obj.value, S);
otherwise
error ('VSDP_INDEXABLE:subsref:unsupportet', ...
'subsref: Unsupported indexing.');
end
end
function obj = subsasgn (obj, S, varargin)
% SUBASGN Redefine subscripted assignment for objects.
%
narginchk (3, 3);
val = varargin{1};
switch (S(1).type)
case '.'
idx = getfield (obj.vsdp_obj.K.idx, S(1).subs);
if (length (S) == 1)
% Like 'obj.f'
obj.value(idx(1):idx(end),:) = val;
elseif ((length (S) == 2) && (isequal (S(2).type, '()')) ...
&& isscalar (S(2).subs))
sidx = S(2).subs{:};
if (isnumeric (sidx) && isscalar (sidx))
% Like 'obj.s(2)'
obj.value(idx(sidx,1):idx(sidx,2),:) = val;
elseif (isnumeric (sidx) && isvector (sidx))
% Like 'obj.s([2,3])' or 'obj.s(2:4)'
obj.value(idx(sidx(1),1):idx(sidx(2),2),:) = val;
elseif (isequal (sidx, ':'))
% Like 'obj.s(:)'
obj.value(idx(1):idx(end),:) = val;
else
error ('VSDP_INDEXABLE:subsref:unsupportet', ...
'subsref: Unsupported indexing.');
end
else
error ('VSDP_INDEXABLE:subsref:unsupportet', ...
'subsref: Unsupported indexing.');
end
case '()' % Like obj(1:end)
% Simply forward them to value
obj.value = subsasgn (obj.value, S, val);
otherwise
error ('VSDP_INDEXABLE:subsref:unsupportet', ...
'subsref: Unsupported indexing.');
end
end
end
end