|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var isRowMajor = require( '@stdlib/ndarray/base/assert/is-row-major' ); |
| 24 | + |
| 25 | + |
| 26 | +// MAIN // |
| 27 | + |
| 28 | +/** |
| 29 | +* Finds the index of the last non zero column in a matrix `A`. |
| 30 | +* |
| 31 | +* @private |
| 32 | +* @param {PositiveInteger} M - number of rows in `A` |
| 33 | +* @param {PositiveInteger} N - number of columns in `A` |
| 34 | +* @param {Float64Array} A - input matrix |
| 35 | +* @param {integer} strideA1 - stride of the first dimension of `A` |
| 36 | +* @param {integer} strideA2 - stride of the second dimension of `A` |
| 37 | +* @param {NonNegativeInteger} offsetA - index offset for `A` |
| 38 | +* @returns {integer} index of the last column |
| 39 | +* |
| 40 | +* @example |
| 41 | +* var Float64array = require( '@stdlib/array/float64' ); |
| 42 | +* |
| 43 | +* var A = new Float64array( [ 1.0, 2.0, 0.0, 3.0, 4.0, 0.0 ] ); // => [ [ 1.0, 2.0, 0.0 ], [ 3.0, 4.0, 0.0 ] ] |
| 44 | +* |
| 45 | +* var out = iladlc( 3, 2, A, 2, 1, 0 ); |
| 46 | +* // returns 1 |
| 47 | +*/ |
| 48 | +function iladlc( M, N, A, strideA1, strideA2, offsetA ) { |
| 49 | + var ia1; |
| 50 | + var ia2; |
| 51 | + var i; |
| 52 | + var j; |
| 53 | + |
| 54 | + if ( M === 0 || N === 0 ) { |
| 55 | + return -1; // Invalid index |
| 56 | + } |
| 57 | + |
| 58 | + ia1 = offsetA + ( (N-1) * strideA2 ); // A( 1, N ) |
| 59 | + ia2 = ia1 + ( (M-1) * strideA2 ); // A( M, N ) |
| 60 | + |
| 61 | + // Quick test for common case where corners are non-zero: |
| 62 | + if ( A[ ia1 ] !== 0.0 || A[ ia2 ] !== 0.0 ) { |
| 63 | + return N - 1; |
| 64 | + } |
| 65 | + |
| 66 | + if ( isRowMajor( [ strideA1, strideA2 ] ) ) { |
| 67 | + ia1 = offsetA; |
| 68 | + for ( i = 0; i < M; i++ ) { |
| 69 | + ia2 = ( N - 1 ) * strideA2; |
| 70 | + for ( j = N-1; j >= 0; j-- ) { |
| 71 | + if ( A[ ia1 + ia2 ] !== 0.0 ) { |
| 72 | + return j; |
| 73 | + } |
| 74 | + ia2 -= strideA2; |
| 75 | + } |
| 76 | + ia1 += strideA1; |
| 77 | + } |
| 78 | + } else { |
| 79 | + ia1 = offsetA + ( (N-1) * strideA2 ); |
| 80 | + for ( i = N-1; i >= 0; i-- ) { |
| 81 | + ia1 = 0; |
| 82 | + for ( j = 0; j < M; j++ ) { |
| 83 | + if ( A[ ia1 + ia2 ] !== 0.0 ) { |
| 84 | + return i; |
| 85 | + } |
| 86 | + ia1 += strideA1; |
| 87 | + } |
| 88 | + ia1 -= strideA2; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return -1; |
| 93 | +} |
| 94 | + |
| 95 | + |
| 96 | +// EXPORTS // |
| 97 | + |
| 98 | +module.exports = iladlc; |
0 commit comments