|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2024 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 | +var max = require( '@stdlib/math/base/special/fast/max' ); |
| 25 | + |
| 26 | + |
| 27 | +// MAIN // |
| 28 | + |
| 29 | +/** |
| 30 | +* Returns last non-zero row of matrix `A`. |
| 31 | +* |
| 32 | +* @private |
| 33 | +* @param {NonNegativeInteger} M - number of rows |
| 34 | +* @param {NonNegativeInteger} N - number of columns |
| 35 | +* @param {Float64Array} A - input matrix |
| 36 | +* @param {integer} strideA1 - stride of the first dimension of `A` |
| 37 | +* @param {integer} strideA2 - stride of the second dimension of `A` |
| 38 | +* @param {NonNegativeInteger} offsetA - starting index for `A` |
| 39 | +* @returns {integer} index of last non-zero row |
| 40 | +* |
| 41 | +* @example |
| 42 | +* var Float64Array = require( '@stdlib/array/float64' ); |
| 43 | +* |
| 44 | +* var out; |
| 45 | +* var A = new Float64Array( [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 46 | +* |
| 47 | +* out = iladlr( 2, 2, A, 2, 1, 0 ); |
| 48 | +* // returns 1 |
| 49 | +*/ |
| 50 | +function iladlr( M, N, A, strideA1, strideA2, offsetA ) { |
| 51 | + var out; |
| 52 | + var i; |
| 53 | + var j; |
| 54 | + |
| 55 | + if ( M === 0 ) { |
| 56 | + return 0; |
| 57 | + } |
| 58 | + if ( isRowMajor( [ strideA1, strideA2 ] ) ) { |
| 59 | + if ( A[ offsetA + ( ( M - 1 ) * strideA1 ) ] !== 0.0 || A[ offsetA + ( ( M - 1 ) * strideA1 ) + ( ( N - 1 ) * strideA2 ) ] ) { |
| 60 | + return M - 1; |
| 61 | + } |
| 62 | + // Scan up each column tracking the last zero row seen. |
| 63 | + out = -1; |
| 64 | + for ( j = 0; j < N; j++ ) { |
| 65 | + i = M - 1; |
| 66 | + while ( i >= 0 && A[ offsetA + ( i * strideA1 ) + ( j * strideA2 ) ] === 0.0 ) { |
| 67 | + i -= 1; |
| 68 | + } |
| 69 | + out = max( out, i ); |
| 70 | + } |
| 71 | + return out; |
| 72 | + } |
| 73 | + // column-major |
| 74 | + if ( A[ offsetA + ( ( M - 1 ) * strideA2 ) ] !== 0.0 || A[ offsetA + ( ( M - 1 ) * strideA2 ) + ( ( N - 1 ) * strideA1 ) ] ) { |
| 75 | + return M - 1; |
| 76 | + } |
| 77 | + // Scan up each column tracking the last zero row seen. |
| 78 | + out = -1; |
| 79 | + for ( j = 0; j < N; j++ ) { |
| 80 | + i = M - 1; |
| 81 | + while ( i >= 0 && A[ offsetA + ( i * strideA2 ) + ( j * strideA1 ) ] === 0.0 ) { |
| 82 | + i -= 1; |
| 83 | + } |
| 84 | + out = max( out, i ); |
| 85 | + } |
| 86 | + return out; |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +// EXPORTS // |
| 91 | + |
| 92 | +module.exports = iladlr; |
0 commit comments