Skip to content

Commit

Permalink
fix #516 'ind = 2; ind(false)' logical extraction on scalar should re…
Browse files Browse the repository at this point in the history
…turn empty matrix. (#517)
  • Loading branch information
Nelson-numerical-software committed Sep 26, 2021
1 parent c7f9f1d commit dd3e3c1
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# 0.5.9 (UNRELEASED)

- [#516](http://github.com/Nelson-numerical-software/nelson/issues/516): `ind = 2; ind(false)` logical extraction on scalar should return empty matrix.

- [#514](http://github.com/Nelson-numerical-software/nelson/issues/514): `C{3} = 4` should create a cell with good dimensions.

- [#512](http://github.com/Nelson-numerical-software/nelson/issues/512): Assign must not change left assign type when it is possible.
Expand Down
9 changes: 7 additions & 2 deletions modules/types/src/cpp/ArrayOf.cpp
Expand Up @@ -187,8 +187,13 @@ ArrayOf::toOrdinalType()
}
// Reset our data pointer to the new vector.
Dimensions dimensions;
dimensions[1] = 1;
dimensions[0] = indexCount;
if (isRowVector()) {
dimensions[1] = indexCount;
dimensions[0] = 1;
} else {
dimensions[1] = 1;
dimensions[0] = indexCount;
}
// Change the class to an NLS_UINT32.
#ifdef NLS_INDEX_TYPE_64
dp = dp->putData(NLS_UINT64, dimensions, lp);
Expand Down
12 changes: 11 additions & 1 deletion modules/types/src/cpp/ArrayOf_Extract.cpp
Expand Up @@ -89,7 +89,17 @@ ArrayOf::getVectorSubset(ArrayOf& index)
= ArrayOf::integerRangeConstructor(1, 1, dp->getElementCount(), true);
return getVectorSubset(newIndex);
}
double idx = index.getContentAsDoubleScalar();
if (index.isLogical()) {
logical idx = index.getContentAsLogicalScalar();
if (idx) {
return getValueAtIndex(0);
} else {
Dimensions dims(0, 0);
return ArrayOf(
dp->dataClass, dims, nullptr, isSparse(), dp->fieldNames);
}
}
double idx = index.getContentAsInteger64Scalar();
auto iidx = static_cast<int64>(idx);
if (idx != static_cast<double>(iidx) || idx < 0) {
Error(_W("index must either be real positive integers or logicals."));
Expand Down
52 changes: 52 additions & 0 deletions modules/types/tests/bug_github_issue_#516.m
@@ -0,0 +1,52 @@
%=============================================================================
% Copyright (c) 2017 Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% This program is free software; you can redistribute it and/or
% modify it under the terms of the GNU Lesser General Public
% License as published by the Free Software Foundation; either
% version 2.1 of the License, or (at your option) any later version.
%
% Alternatively, you can redistribute it and/or
% modify it under the terms of the GNU General Public License as
% published by the Free Software Foundation; either version 2 of
% the License, or (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public
% License along with this program. If not, see <http://www.gnu.org/licenses/>.
% LICENCE_BLOCK_END
%=============================================================================
% <-- Issue URL -->
% https://github.com/Nelson-numerical-software/nelson/issues/516
% <-- Short Description -->
% 'ind = 2; ind(false)' logical extraction on scalar should return empty matrix
%=============================================================================
ind = 2;
R = ind(true);
REF = 2;
assert_isequal(R, REF);
%=============================================================================
R = ind(false);
REF = [];
assert_isequal(R, REF);
%=============================================================================
R = ind(logical([0 0 0 ]'));
REF = zeros(0, 1);
assert_isequal(R, REF);
%=============================================================================
R =ind(logical([0 0 0 ]));
REF = zeros(1, 0);
assert_isequal(R, REF);
%=============================================================================
A = eye(3, 3);
R = A(logical(ones(3,3)));
REF = [ 1; 0; 0; 0; 1; 0; 0; 0; 1];
assert_isequal(R, REF);
%=============================================================================

0 comments on commit dd3e3c1

Please sign in to comment.