Skip to content

Commit

Permalink
test: add ndarray tests
Browse files Browse the repository at this point in the history
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: passed
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---

---
type: pre_push_report
description: Results of running various checks prior to pushing changes.
report:
  - task: run_javascript_examples
    status: na
  - task: run_c_examples
    status: na
  - task: run_cpp_examples
    status: na
  - task: run_javascript_readme_examples
    status: na
  - task: run_c_benchmarks
    status: na
  - task: run_cpp_benchmarks
    status: na
  - task: run_fortran_benchmarks
    status: na
  - task: run_javascript_benchmarks
    status: na
  - task: run_julia_benchmarks
    status: na
  - task: run_python_benchmarks
    status: na
  - task: run_r_benchmarks
    status: na
  - task: run_javascript_tests
    status: passed
---
  • Loading branch information
aayush0325 committed Mar 3, 2025
1 parent b1bed1b commit 6e908a9
Showing 1 changed file with 189 additions and 0 deletions.
189 changes: 189 additions & 0 deletions lib/node_modules/@stdlib/lapack/base/dgttrf/test/test.ndarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,42 @@
// MODULES //

var tape = require( 'tape' );
var Float64Array = require( '@stdlib/array/float64' );
var Int32Array = require( '@stdlib/array/int32' );
var EPS = require( '@stdlib/constants/float64/eps' );
var abs = require( '@stdlib/math/base/special/abs' );
var dgttrf = require( './../lib/ndarray.js' );


// FUNCTIONS //

/**
* Tests for element-wise approximate equality.
*
* @private
* @param {Object} t - test object
* @param {Collection} actual - actual values
* @param {Collection} expected - expected values
* @param {number} rtol - relative tolerance
*/
function isApprox( t, actual, expected, rtol ) {
var delta;
var tol;
var i;

t.strictEqual( actual.length, expected.length, 'returns expected value' );
for ( i = 0; i < expected.length; i++ ) {
if ( actual[ i ] === expected[ i ] ) {
t.strictEqual( actual[ i ], expected[ i ], 'returns expected value' );
} else {
delta = abs( actual[ i ] - expected[ i ] );
tol = rtol * EPS * abs( expected[ i ] );
t.ok( delta <= tol, 'within tolerance. actual: '+actual[ i ]+'. expected: '+expected[ i ]+'. delta: '+delta+'. tol: '+tol+'.' );
}
}
}


// TESTS //

tape( 'main export is a function', function test( t ) {
Expand All @@ -36,3 +69,159 @@ tape( 'the function has an arity of 16', function test( t ) {
t.strictEqual( dgttrf.length, 16, 'returns expected value' );
t.end();
});

tape( 'the function throws an error if provided a first argument which is less than zero', function test( t ) {
var values;
var IPIV;
var DU2;
var DU;
var DL;
var D;
var i;

DL = new Float64Array( [ 1.0, 1.0 ] );
D = new Float64Array( [ 2.0, 3.0, 1.0 ] );
DU = new Float64Array( [ 1.0, 1.0 ] );
DU2 = new Float64Array( 1 );
IPIV = new Int32Array( 3 );

values = [
-1,
-2,
-3
];

for ( i = 0; i < values.length; i++ ) {
t.throws( badValue( values[ i ] ), RangeError, 'throws an error when provided ' + values[ i ] );
}
t.end();

function badValue( value ) {
return function badValue() {
dgttrf( value, DL, 1, 0, D, 1, 0, DU, 1, 0, DU2, 1, 0, IPIV, 1, 0 );
};
}
});

tape( 'the function performs the `LU` factorization of a real tri diagonal matrix `A` using elimination with partial pivoting and row interchanges', function test( t ) {
var expectedIPIV;
var expectedDU2;
var expectedDU;
var expectedDL;
var expectedD;
var info;
var IPIV;
var DU2;
var DU;
var DL;
var D;
var N;

N = 3;

DL = new Float64Array( [ 1.0, 1.0 ] );
D = new Float64Array( [ 2.0, 3.0, 1.0 ] );
DU = new Float64Array( [ 1.0, 1.0 ] );
DU2 = new Float64Array( N-2 );
IPIV = new Int32Array( N );

expectedDL = new Float64Array( [ 0.5, 0.4 ] );
expectedD = new Float64Array( [ 2.0, 2.5, 0.6 ] );
expectedDU = new Float64Array( [ 1.0, 1.0 ] );
expectedDU2 = new Float64Array( [ 0.0 ] );
expectedIPIV = new Int32Array( [ 0, 1, 2 ] );

info = dgttrf( N, DL, 1, 0, D, 1, 0, DU, 1, 0, DU2, 1, 0, IPIV, 1, 0 );
t.strictEqual( info, 0, 'returns expected value' );
t.deepEqual( IPIV, expectedIPIV, 'returns expected value' );
isApprox( t, D, expectedD, 1.0 );
isApprox( t, DU, expectedDU, 1.0 );
isApprox( t, DU2, expectedDU2, 1.0 );
isApprox( t, DL, expectedDL, 1.0 );

N = 9;

DL = new Float64Array( [ 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0, 3.0 ] );
D = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
DU = new Float64Array( [ 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0, 4.0 ] );
DU2 = new Float64Array( N-2 );
IPIV = new Int32Array( N );

expectedDL = new Float64Array( [ 0.3333333333333333, 0.8181818181818182, 0.6969696969696969, 0.9082568807339449, 0.8493506493506494, -0.7991341991341993, 0.6251127548259066, -0.3327319742618318 ] ); // eslint-disable-line max-len
expectedD = new Float64Array( [ 3.0, 3.6666666666666665, 3.0, 3.3030303030303032, 3.5321100917431192, 3.0, 4.7991341991341994, 3.0, 4.3327319742618320 ] ); // eslint-disable-line max-len
expectedDU = new Float64Array( [ 1.0, -1.3333333333333333, 1.0, -2.7878787878787876, 4.0, 1.0, 3.1965367965367970, 1.0 ] ); // eslint-disable-line max-len
expectedDU2 = new Float64Array( [ 4.0, 0.0, 4.0, 0.0, 0.0, 4.0, 0.0 ] );
expectedIPIV = new Int32Array( [ 1, 1, 3, 3, 4, 6, 6, 8, 8 ] );

info = dgttrf( N, DL, 1, 0, D, 1, 0, DU, 1, 0, DU2, 1, 0, IPIV, 1, 0 );
t.strictEqual( info, 0, 'returns expected value' );
t.deepEqual( IPIV, expectedIPIV, 'returns expected value' );
isApprox( t, D, expectedD, 1.0 );
isApprox( t, DU, expectedDU, 1.0 );
isApprox( t, DU2, expectedDU2, 1.0 );
isApprox( t, DL, expectedDL, 1.0 );

t.end();
});

tape( 'the function leaves the input arrays unchanged when `N` is equal to zero', function test( t ) {
var expectedIPIV;
var expectedDU2;
var expectedDU;
var expectedDL;
var expectedD;
var info;
var IPIV;
var DU2;
var DU;
var DL;
var D;
var N;

N = 0;

DL = new Float64Array( [ 1.0, 1.0 ] );
D = new Float64Array( [ 2.0, 3.0, 1.0 ] );
DU = new Float64Array( [ 1.0, 1.0 ] );
DU2 = new Float64Array( 1 );
IPIV = new Int32Array( 3 );

expectedDL = new Float64Array( [ 1.0, 1.0 ] );
expectedD = new Float64Array( [ 2.0, 3.0, 1.0 ] );
expectedDU = new Float64Array( [ 1.0, 1.0 ] );
expectedDU2 = new Float64Array( [ 0.0 ] );
expectedIPIV = new Int32Array( [ 0, 0, 0 ] );

info = dgttrf( N, DL, 1, 0, D, 1, 0, DU, 1, 0, DU2, 1, 0, IPIV, 1, 0 );
t.strictEqual( info, 0, 'returns expected value' );
t.deepEqual( DU, expectedDU, 'returns expected value' );
t.deepEqual( DU2, expectedDU2, 'returns expected value' );
t.deepEqual( D, expectedD, 'returns expected value' );
t.deepEqual( DL, expectedDL, 'returns expected value' );
t.deepEqual( IPIV, expectedIPIV, 'returns expected value' );

t.end();
});

tape( 'the function returns a non zero status code when a diagonal element is equal to zero', function test( t ) {
var info;
var IPIV;
var DU2;
var DU;
var DL;
var D;
var N;

N = 3;

DL = new Float64Array( [ 0.0, 0.0 ] );
D = new Float64Array( [ 1.0, 1.0, 0.0 ] );
DU = new Float64Array( [ 2.0, 3.0 ] );
DU2 = new Float64Array( 1 );
IPIV = new Int32Array( 3 );

info = dgttrf( N, DL, 1, 0, D, 1, 0, DU, 1, 0, DU2, 1, 0, IPIV, 1, 0 );
t.strictEqual( info, 2, 'returns expected value' );

t.end();
});

0 comments on commit 6e908a9

Please sign in to comment.