Skip to content

Commit 8147ab4

Browse files
committed
feat: add examples and benchmarks
--- 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: passed - task: lint_repl_help status: na - task: lint_javascript_src status: na - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: na - task: lint_javascript_benchmarks status: passed - 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 ---
1 parent 6dbee4c commit 8147ab4

File tree

4 files changed

+413
-0
lines changed

4 files changed

+413
-0
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var floor = require( '@stdlib/math/base/special/floor' );
29+
var pkg = require( './../package.json' ).name;
30+
var dlatrs = require( './../lib/dlatrs.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var LAYOUTS = [
36+
'row-major',
37+
'column-major'
38+
];
39+
var MATRIX_TRIANGLES = [
40+
'upper',
41+
'lower'
42+
];
43+
var TRANSPOSE_OPS = [
44+
'transpose',
45+
'no-transpose'
46+
];
47+
48+
49+
// FUNCTIONS //
50+
51+
/**
52+
* Creates a benchmark function.
53+
*
54+
* @private
55+
* @param {string} order - storage layout
56+
* @param {PositiveInteger} N - number of elements along each dimension
57+
* @param {string} uplo - specifies whether `A` is an upper or lower triangular matrix
58+
* @param {string} trans - specifies whether `A` should be transposed or not transposed
59+
* @returns {Function} benchmark function
60+
*/
61+
function createBenchmark( order, N, uplo, trans ) {
62+
var CNORM;
63+
var X;
64+
var A;
65+
66+
A = uniform( N*N, 2.0, 10.0, {
67+
'dtype': 'float64'
68+
});
69+
X = uniform( N, 1.0, 10.0, {
70+
'dtype': 'float64'
71+
});
72+
CNORM = new Float64Array( N );
73+
return benchmark;
74+
75+
/**
76+
* Benchmark function.
77+
*
78+
* @private
79+
* @param {Benchmark} b - benchmark instance
80+
*/
81+
function benchmark( b ) {
82+
var z;
83+
var i;
84+
85+
b.tic();
86+
for ( i = 0; i < b.iterations; i++ ) {
87+
z = dlatrs( order, uplo, trans, 'non-unit', 'no', N, A, N, X, CNORM );
88+
if ( isnan( z ) ) {
89+
b.fail( 'should not return NaN' );
90+
}
91+
}
92+
b.toc();
93+
if ( isnan( z ) ) {
94+
b.fail( 'should not return NaN' );
95+
}
96+
b.pass( 'benchmark finished' );
97+
b.end();
98+
}
99+
}
100+
101+
102+
// MAIN //
103+
104+
/**
105+
* Main execution sequence.
106+
*
107+
* @private
108+
*/
109+
function main() {
110+
var trans;
111+
var uplo;
112+
var min;
113+
var max;
114+
var ord;
115+
var N;
116+
var f;
117+
var i;
118+
var j;
119+
var k;
120+
var l;
121+
122+
min = 1; // 10^min
123+
max = 6; // 10^max
124+
125+
for ( k = 0; k < LAYOUTS.length; k++ ) {
126+
ord = LAYOUTS[ k ];
127+
for ( i = min; i <= max; i++ ) {
128+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
129+
for ( j = 0; j < MATRIX_TRIANGLES.length; j++ ) {
130+
uplo = MATRIX_TRIANGLES[ j ];
131+
for ( l = 0; l < TRANSPOSE_OPS.length; l++ ) {
132+
trans = TRANSPOSE_OPS[ l ];
133+
f = createBenchmark( ord, N, uplo, trans );
134+
bench( pkg+'::square_matrix:order='+ord+',uplo='+uplo+',trans='+trans+',size='+(N*N), f );
135+
}
136+
}
137+
}
138+
}
139+
}
140+
141+
main();
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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 bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var Float64Array = require( '@stdlib/array/float64' );
26+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var isColumnMajor = require( '@stdlib/ndarray/base/assert/is-column-major-string' );
29+
var floor = require( '@stdlib/math/base/special/floor' );
30+
var pkg = require( './../package.json' ).name;
31+
var dlatrs = require( './../lib/ndarray.js' );
32+
33+
34+
// VARIABLES //
35+
36+
var LAYOUTS = [
37+
'row-major',
38+
'column-major'
39+
];
40+
var MATRIX_TRIANGLES = [
41+
'upper',
42+
'lower'
43+
];
44+
var TRANSPOSE_OPS = [
45+
'transpose',
46+
'no-transpose'
47+
];
48+
49+
50+
// FUNCTIONS //
51+
52+
/**
53+
* Creates a benchmark function.
54+
*
55+
* @private
56+
* @param {string} order - storage layout
57+
* @param {PositiveInteger} N - number of elements along each dimension
58+
* @param {string} uplo - specifies whether `A` is an upper or lower triangular matrix
59+
* @param {string} trans - specifies whether `A` should be transposed or not transposed
60+
* @returns {Function} benchmark function
61+
*/
62+
function createBenchmark( order, N, uplo, trans ) {
63+
var CNORM;
64+
var sa1;
65+
var sa2;
66+
var X;
67+
var A;
68+
69+
A = uniform( N*N, 2.0, 10.0, {
70+
'dtype': 'float64'
71+
});
72+
X = uniform( N, 1.0, 10.0, {
73+
'dtype': 'float64'
74+
});
75+
if ( isColumnMajor( order ) ) {
76+
sa1 = 1;
77+
sa2 = N;
78+
} else { // order === 'row-major'
79+
sa1 = N;
80+
sa2 = 1;
81+
}
82+
CNORM = new Float64Array( N );
83+
return benchmark;
84+
85+
/**
86+
* Benchmark function.
87+
*
88+
* @private
89+
* @param {Benchmark} b - benchmark instance
90+
*/
91+
function benchmark( b ) {
92+
var z;
93+
var i;
94+
95+
b.tic();
96+
for ( i = 0; i < b.iterations; i++ ) {
97+
z = dlatrs( uplo, trans, 'non-unit', 'no', N, A, sa1, sa2, 0, X, 1, 0, CNORM, 1, 0 );
98+
if ( isnan( z ) ) {
99+
b.fail( 'should not return NaN' );
100+
}
101+
}
102+
b.toc();
103+
if ( isnan( z ) ) {
104+
b.fail( 'should not return NaN' );
105+
}
106+
b.pass( 'benchmark finished' );
107+
b.end();
108+
}
109+
}
110+
111+
112+
// MAIN //
113+
114+
/**
115+
* Main execution sequence.
116+
*
117+
* @private
118+
*/
119+
function main() {
120+
var trans;
121+
var uplo;
122+
var min;
123+
var max;
124+
var ord;
125+
var N;
126+
var f;
127+
var i;
128+
var j;
129+
var k;
130+
var l;
131+
132+
min = 1; // 10^min
133+
max = 6; // 10^max
134+
135+
for ( k = 0; k < LAYOUTS.length; k++ ) {
136+
ord = LAYOUTS[ k ];
137+
for ( i = min; i <= max; i++ ) {
138+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
139+
for ( j = 0; j < MATRIX_TRIANGLES.length; j++ ) {
140+
uplo = MATRIX_TRIANGLES[ j ];
141+
for ( l = 0; l < TRANSPOSE_OPS.length; l++ ) {
142+
trans = TRANSPOSE_OPS[ l ];
143+
f = createBenchmark( ord, N, uplo, trans );
144+
bench( pkg+'::square_matrix:order='+ord+',uplo='+uplo+',trans='+trans+',size='+(N*N), f );
145+
}
146+
}
147+
}
148+
}
149+
}
150+
151+
main();
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
var Float64Array = require( '@stdlib/array/float64' );
22+
var numel = require( '@stdlib/ndarray/base/numel' );
23+
var uniform = require( '@stdlib/random/array/uniform' );
24+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
25+
var dlatrs = require( './../lib' );
26+
27+
// Specify matrix meta data:
28+
var shape = [ 3, 3 ];
29+
var strides = [ 3, 1 ];
30+
var offset = 0;
31+
var N = numel( shape );
32+
var order = 'row-major';
33+
34+
// Create a matrix stored in linear memory:
35+
var A = uniform( N, 2.0, 10.0, {
36+
'dtype': 'float64'
37+
});
38+
39+
console.log( ndarray2array( A, shape, strides, offset, order ) );
40+
41+
var X = uniform( shape[ 0 ], 0.0, 10.0, {
42+
'dtype': 'float64'
43+
});
44+
45+
var CNORM = new Float64Array( shape[ 0 ] );
46+
47+
var scale = dlatrs( order, 'upper', 'no-transpose', 'non-unit', 'no', shape[ 0 ], A, strides[ 0 ], X, CNORM );
48+
console.log( ndarray2array( A, shape, strides, offset, order ) );
49+
console.log( 'scaling factor: ', scale );

0 commit comments

Comments
 (0)