Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #516 'ind = 2; ind(false)' logical extraction on scalar should r… #517

Merged
merged 1 commit into from
Sep 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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);
%=============================================================================