-
Notifications
You must be signed in to change notification settings - Fork 21
/
tt_dimscheck.m
78 lines (68 loc) · 2.55 KB
/
tt_dimscheck.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
function [sdims,vidx] = tt_dimscheck(dims,N,M)
%TT_DIMSCHECK Used to preprocess dimensions tensor dimensions.
%
% NEWDIMS = TT_DIMCHECK(DIMS,N) checks that the specified dimensions
% are valid for a tensor of order N. If DIMS is empty, then
% NEWDIMS=1:N. If DIMS is negative, then NEWDIMS is everything
% but the dimensions specified by -DIMS. Finally, NEWDIMS is
% returned in sorted order.
%
% [NEWDIMS,IDX] = TT_DIMCHECK(DIMS,N,M) does all of the above but
% also returns an index for M muliplicands.
%
%MATLAB Tensor Toolbox.
%Copyright 2015, Sandia Corporation.
% This is the MATLAB Tensor Toolbox by T. Kolda, B. Bader, and others.
% http://www.sandia.gov/~tgkolda/TensorToolbox.
% Copyright (2015) Sandia Corporation. Under the terms of Contract
% DE-AC04-94AL85000, there is a non-exclusive license for use of this
% work by or on behalf of the U.S. Government. Export of this data may
% require a license from the United States Government.
% The full license terms can be found in the file LICENSE.txt
% Fix empty case
if isempty(dims)
dims = 1:N;
end
% Fix "minus" case
if (max(dims) < 0)
% Check that every member of dims is in 1:N
tf = ismember(-dims,1:N);
if min(tf) == 0
error('Invalid dimensions specified');
end
dims = setdiff(1:N, -dims);
end
% Check that every member of dims is in 1:N
tf = ismember(dims,1:N);
if min(tf) == 0
error('Invalid dimensions specified');
end
% Save the number of dimensions in dims
P = length(dims);
% Reorder dims from smallest to largest (this matters in particular
% for the vector multiplicand case, where the order affects the
% result)
[sdims,sidx] = sort(dims,'ascend');
if (nargout == 2)
% Can't have more multiplicands them dimensions
if (M > N)
error('Cannot have more multiplcands than dimensions');
end
% Check that the number of mutliplicands must either be
% full-dimensional (i.e., M==N) or equal to the number of specified
% dimensions (i.e., M==P).
if (M ~= N) && (M ~= P)
error('Invalid number of multiplicands');
end
% Check sizes to determine how to index multiplicands
if (P == M)
% Case 1: Number of items in dims and number of multiplicands
% are equal; therefore, index in order of how sdims was sorted.
vidx = sidx;
else
% Case 2: Number of multiplicands is equal to the number of
% dimensions in the tensor; therefore, index multiplicands by
% dimensions specified in dims argument.
vidx = sdims; % index multiplicands by (sorted) dimension
end
end