Skip to content

Commit

Permalink
fix #1205 - diag may return wrong sub- or superdiagonals
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Jun 3, 2024
1 parent 9c989da commit 9a25cbe
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## 1.6.0 (UNRELEASED)

### Fixed

- [#1205](http://github.com/nelson-lang/nelson/issues/1205) `diag` may return wrong sub-diagonals.

## 1.5.0 (2024-05-31)

### Added
Expand Down
75 changes: 75 additions & 0 deletions modules/constructors_functions/tests/bug_github_issue_#1205.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
%=============================================================================
% Copyright (c) 2017 Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
% <-- Issue URL -->
% https://github.com/nelson-lang/nelson/issues/1205
% <-- Short Description -->
% `diag` may return wrong sub- or superdiagonals
%=============================================================================
A = vander(1:3);
R = diag(A)';
REF = [1 2 1];
assert_isequal(R, REF);
%=============================================================================
A = vander(1:3);
R = diag(A, 1)';
REF = [1 1];
assert_isequal(R, REF);
%=============================================================================
R = diag(A, 2)';
REF = 1;
assert_isequal(R, REF);
%=============================================================================
R = diag(A, 3)';
REF = zeros(1, 0);
assert_isequal(R, REF);
%=============================================================================
R = diag(A, 4)';
REF = zeros(1, 0);
assert_isequal(R, REF);
%=============================================================================
R = diag(A, 5)';
REF = zeros(1, 0);
assert_isequal(R, REF);
%=============================================================================
A = vander(1:4);
R = diag(A, 4)';
REF = zeros(1, 0);
assert_isequal(R, REF);
%=============================================================================
A = vander(1:4);
R = diag(A, 5)';
REF = zeros(1, 0);
assert_isequal(R, REF);
%=============================================================================
A = vander(1:4);
R = diag(A, 45)';
REF = zeros(1, 0);
assert_isequal(R, REF);
%=============================================================================
A = vander(1:3);
R = diag(A, -1)';
REF = [4 3];
assert_isequal(R, REF);
%=============================================================================
A = vander(1:3);
R = diag(A, -2)';
REF = 9;
assert_isequal(R, REF);
%=============================================================================
A = vander(1:3);
R = diag(A, -3)';
REF = zeros(1, 0);
assert_isequal(R, REF);
%=============================================================================
A = vander(1:3);
R = diag(A, -4)';
REF = zeros(1, 0);
assert_isequal(R, REF);
%=============================================================================
7 changes: 7 additions & 0 deletions modules/types/src/cpp/ArrayOf_Constructors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ ArrayOf::getDiagonal(int64 diagonalOrder)
int64 srcIndex;
void* qp;
if (diagonalOrder < 0) {
if (diagonalOrder < -(int64)(rows)) {
diagonalOrder = -(int64)(rows);
}

outLen = (rows + diagonalOrder) < cols ? (rows + diagonalOrder) : cols;
outLen = (outLen < 0) ? 0 : outLen;
if (outLen == 0) {
Expand All @@ -57,6 +61,9 @@ ArrayOf::getDiagonal(int64 diagonalOrder)
copyElements(srcIndex, qp, i, 1);
}
} else {
if (diagonalOrder > (int64)cols) {
diagonalOrder = (int64)cols;
}
outLen = rows < (cols - diagonalOrder) ? rows : (cols - diagonalOrder);
outLen = (outLen < 0) ? 0 : outLen;
if (outLen == 0) {
Expand Down

0 comments on commit 9a25cbe

Please sign in to comment.