-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSparseSingle.m
351 lines (300 loc) · 14.9 KB
/
SparseSingle.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
classdef SparseSingle
%< matlab.mixin.indexing.RedefinesParen -> New way of indexing
properties (SetAccess = private, Hidden = true)
objectHandle; % Handle to the underlying C++ class instance
cleanup;
end
properties (SetAccess = private)
nRows
nCols
end
methods
%% Constructor - Create a new C++ class instance
function this = SparseSingle(varargin)
if nargin == 1 && isa(varargin{1},'uint64')
this.objectHandle = varargin{1};
else
this.objectHandle = mexSparseSingle('new', varargin{:});
end
this.cleanup = onCleanup(@() delete(this));
end
%% Destructor - Destroy the C++ class instance
function delete(this)
mexSparseSingle('delete', this.objectHandle);
end
%% nnz
function nnz = nnz(this)
nnz = mexSparseSingle('nnz',this.objectHandle);
end
%% size
function sz = size(this)
sz = mexSparseSingle('size',this.objectHandle);
end
function ret = horzcat(varargin)
error('sparseSingle:missingImplementation','horzcat not yet implemented!');
end
%% Plus & Minus
function ret = plus(A,B)
%error('sparseSingle:missingImplementation','plus operator not yet implemented!');
if isempty(A) || isempty(B)
error('Arrays have incompatible sizes for this operation');
elseif isa(A, 'SparseSingle') && isnumeric(B) && ~issparse(B)
ret = mexSparseSingle('plus',A.objectHandle,B);
% matrix * vector
elseif isa(B, 'SparseSingle') && isnumeric(A) && ~issparse(A)
ret = mexSparseSingle('plus',B.objectHandle,A);
elseif isa(A, 'SparseSingle') && isa(B,'SparseSingle')
ret = SparseSingle(mexSparseSingle('plus',A.objectHandle,B.objectHandle));
else
error('Inputs %s & %s not supported', class(A),class(B));
end
end
function ret = times(A,B)
if isempty(A) || isempty(B)
error('Arrays have incompatible sizes for this operation');
elseif isa(A, 'SparseSingle') && isnumeric(B) && ~issparse(B)
ret = mexSparseSingle('times',A.objectHandle,B);
% matrix * vector
elseif isa(B, 'SparseSingle') && isnumeric(A) && ~issparse(A)
ret = mexSparseSingle('times',B.objectHandle,A);
elseif isa(A, 'SparseSingle') && isa(B,'SparseSingle')
ret = mexSparseSingle('times',A.objectHandle,B.objectHandle);
else
error('Inputs %s & %s not supported', class(A),class(B));
end
ret = SparseSingle(ret);
end
function ret = minus(A,B)
if isempty(A) || isempty(B)
error('Arrays have incompatible sizes for this operation');
elseif isa(A, 'SparseSingle') && isnumeric(B) && ~issparse(B)
ret = mexSparseSingle('minusAsMinuend',A.objectHandle,B);
% matrix * vector
elseif isa(B, 'SparseSingle') && isnumeric(A) && ~issparse(A)
ret = mexSparseSingle('minusAsSubtrahend',B.objectHandle,A);
elseif isa(A, 'SparseSingle') && isa(B,'SparseSingle')
ret = SparseSingle(mexSparseSingle('minusAsMinuend',A.objectHandle,B.objectHandle));
else
error('Inputs %s & %s not supported', class(A),class(B));
end
end
function ret = uminus(this)
ret = SparseSingle(mexSparseSingle('uminus',this.objectHandle));
end
function ret = ldivide(A,B)
warning('rdivide for sparseSingle behavior differs from its double counterpart, as only nnzs are affected!');
if isempty(A) || isempty(B)
error('Arrays have incompatible sizes for this operation');
elseif isa(A, 'SparseSingle') && isnumeric(B) && ~issparse(B)
retHandle = mexSparseSingle('ldivide',A.objectHandle,B);
% matrix * vector
elseif isa(B, 'SparseSingle') && isnumeric(A) && ~issparse(A)
retHandle = mexSparseSingle('rdivide',B.objectHandle,A);
elseif isa(A, 'SparseSingle') && isa(B,'SparseSingle')
retHandle = mexSparseSingle('ldivide',A.objectHandle,B.objectHandle);
else
error('Inputs %s & %s not supported', class(A),class(B));
end
ret = SparseSingle(retHandle);
end
function ret = rdivide(A,B)
warning('rdivide for sparseSingle behavior differs from its double counterpart, as only nnzs are affected!');
if isempty(A) || isempty(B)
error('Arrays have incompatible sizes for this operation');
elseif isa(A, 'SparseSingle') && isnumeric(B) && ~issparse(B)
retHandle = mexSparseSingle('rdivide',A.objectHandle,B);
% matrix * vector
elseif isa(B, 'SparseSingle') && isnumeric(A) && ~issparse(A)
retHandle = mexSparseSingle('ldivide',B.objectHandle,A);
elseif isa(A, 'SparseSingle') && isa(B,'SparseSingle')
retHandle = mexSparseSingle('rdivide',A.objectHandle,B.objectHandle);
else
error('Inputs %s & %s not supported', class(A),class(B));
end
ret = SparseSingle(retHandle);
end
function ret = mldivide(A,B)
if (issparse(A) && isnumeric(B) && ~issparse(B))
ret = mexSparseSingle('mldivide',A.objectHandle,B);
elseif issparse(A) && issparse(B)
ret = SparseSingle(mexSparseSingle('mldivide',A.objectHandle,B.objectHandle));
elseif isa(B, 'SparseSingle') && isnumeric(A) && ~issparse(A)
ret = A\full(B);
else
error('Inputs %s & %s not supported', class(A),class(B));
end
end
function ret = mrdivide(B,A)
%The mrdivide function curently only wraps mldivide
ret = transpose(mldivide(A',B'));
end
function ret = find(this)
ret = mexSparseSingle('find',this.objectHandle);
end
%% sparsity oprations
function ret = full(this)
ret = mexSparseSingle('full',this.objectHandle);
end
function ret = issparse(this)
ret = true;
end
function ret = isa(this,typeStr)
ret = false;
if strcmpi(typeStr,'numeric') || strcmpi(typeStr,'SparseSingle') || strcmpi(typeStr,'single')
ret = true;
end
end
function ret = isnumeric(this)
ret = true;
end
%% disp
function disp(this)
mexSparseSingle('disp',this.objectHandle);
end
%%mtimes
function ret = mtimes(A,B)
if isempty(A) || isempty(B)
error('One Input is empty');
% vector * matrix
% arg1: numeric vector
% arg2: SparseSingle obj
elseif isa(A, 'SparseSingle') && isnumeric(B) && ~issparse(B)
ret = mexSparseSingle('mtimesr',A.objectHandle,B);
% matrix * vector
elseif isa(A, 'SparseSingle') && isa(B,'SparseSingle')
ret = SparseSingle(mexSparseSingle('mtimesr',A.objectHandle,B.objectHandle));
elseif isa(B, 'SparseSingle') && isnumeric(A) && ~issparse(A)
ret = mexSparseSingle('mtimesl',B.objectHandle,A);
if isscalar(A)
ret = SparseSingle(ret);
end
else
error('mtimes for inputs %s & %s not supported', class(A),class(B));
end
end
function ret = transpose(this)
ret = SparseSingle(mexSparseSingle('transpose',this.objectHandle));
end
function ret = ctranspose(this)
%Todo: difference between conjugate and non-conjugate transpose
ret = SparseSingle(mexSparseSingle('transpose',this.objectHandle));
end
%%getter functions
function nr = get.nRows(this)
nr = size(this);
nr = nr(1);
end
function nr = get.nCols(this)
nr = size(this);
nr = nr(2);
end
%% Indexing
function values = subsref(this,s)
switch s(1).type
case '()'
if length(s) == 1
nSubs = length(s.subs);
%Linear Indexing
if nSubs == 1
subMatrixHandle = mexSparseSingle('linearIndexing',this.objectHandle,s.subs{1});
values = SparseSingle(subMatrixHandle);
%Submatrix indexing
elseif nSubs == 2
%Workaround for Colon at the moment
if isequal(s.subs{1},':')
warning('Row Colon indexing not efficient at the moment!');
s.subs{1} = 1:this.nRows;
end
if isequal(s.subs{2},':')
warning('Column Colon indexing not efficient at the moment!');
s.subs{2} = 1:this.nCols;
end
% Workaround for Logical indexing at the moment
if islogical(s.subs{1})
if ~isvector(s.subs{1}) || numel(s.subs{1}) ~= this.nRows
error('Wrong index dimension: Number of elements must be the same!');
end
warning('Logical indexing not efficient at the moment and will be converted to an index list!');
s.subs{1} = find(s.subs{1});
end
if islogical(s.subs{2})
if ~isvector(s.subs{2}) || numel(s.subs{1}) ~= this.nCols
error('Wrong index dimension: Number of elements must be the same!');
end
warning('Logical indexing not efficient at the moment and will be converted to an index list!');
s.subs{2} = find(s.subs{2});
end
subMatrixHandle = mexSparseSingle('subsrefRowCol',this.objectHandle,s.subs{1},s.subs{2});
values = SparseSingle(subMatrixHandle);
else
error('Requested Indexing Pattern is not supported!');
end
elseif length(s) >= 2 && strcmp(s(2).type,'.')
error('Dot indexing not supported for SparseSingle!');
else
error('Requested indexing pattern not supported!');
end
case '.'
values = builtin('subsref',this,s);
case '{}'
error('{} indexing not supported for SparseSingle!');
otherwise
error('Not a valid indexing expression');
end
end
function this = subsasgn(this,s,ix)
switch s(1).type
case '()'
if length(s) == 1
nSubs = length(s.subs);
%Linear Indexing
if nSubs == 1
error('Linear Index Assignment not implemented!');
subMatrixHandle = mexSparseSingle('linearIndexAssign',this.objectHandle,s.subs{1});
this = SparseSingle(subMatrixHandle);
%Submatrix indexing
elseif nSubs == 2
%error('subscript assignment not implemented!');
%Workaround for Colon at the moment
if isequal(s.subs{1},':')
warning('Row Colon indexing not efficient at the moment!');
s.subs{1} = 1:this.nRows;
end
if isequal(s.subs{2},':')
warning('Column Colon indexing not efficient at the moment!');
s.subs{2} = 1:this.nCols;
end
% Workaround for Logical indexing at the moment
if islogical(s.subs{1})
if ~isvector(s.subs{1}) || numel(s.subs{1}) ~= this.nRows
error('Wrong index dimension: Number of elements must be the same!');
end
warning('Logical indexing not efficient at the moment and will be converted to an index list!');
s.subs{1} = find(s.subs{1});
end
if islogical(s.subs{2})
if ~isvector(s.subs{2}) || numel(s.subs{1}) ~= this.nCols
error('Wrong index dimension: Number of elements must be the same!');
end
warning('Logical indexing not efficient at the moment and will be converted to an index list!');
s.subs{2} = find(s.subs{2});
end
this = SparseSingle(mexSparseSingle('subsasgnRowCol',this.objectHandle,s.subs{1},s.subs{2},ix));
else
error('Requested Indexing Pattern is not supported!');
end
elseif length(s) >= 2 && strcmp(s(2).type,'.')
error('Dot indexing not supported for SparseSingle!');
else
error('Requested indexing pattern not supported!');
end
case '.'
[varargout{1:nargout}] = builtin('subsref',this,s);
case '{}'
error('{} indexing not supported for SparseSingle!');
otherwise
error('Not a valid indexing expression');
end
end
end
end