From 22c7006d4990a121f3957e516947768a9cbdda1a Mon Sep 17 00:00:00 2001 From: kaustubh Date: Mon, 22 Jun 2026 23:50:33 +0530 Subject: [PATCH] feat: add blas/base/ndarray/dtrsv --- 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_pkg_readmes status: passed - task: lint_markdown_docs status: na - task: lint_markdown status: na - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - 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: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed --- --- .../@stdlib/blas/base/ndarray/dtrsv/README.md | 118 +++++++++++ .../base/ndarray/dtrsv/benchmark/benchmark.js | 109 ++++++++++ .../blas/base/ndarray/dtrsv/docs/repl.txt | 34 +++ .../base/ndarray/dtrsv/docs/types/index.d.ts | 57 +++++ .../base/ndarray/dtrsv/docs/types/test.ts | 63 ++++++ .../blas/base/ndarray/dtrsv/examples/index.js | 36 ++++ .../blas/base/ndarray/dtrsv/lib/index.js | 49 +++++ .../blas/base/ndarray/dtrsv/lib/main.js | 69 ++++++ .../blas/base/ndarray/dtrsv/package.json | 72 +++++++ .../blas/base/ndarray/dtrsv/test/test.js | 198 ++++++++++++++++++ 10 files changed, 805 insertions(+) create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/README.md create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/examples/index.js create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/lib/index.js create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/lib/main.js create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/package.json create mode 100644 lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/test/test.js diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/README.md b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/README.md new file mode 100644 index 000000000000..4170610febca --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/README.md @@ -0,0 +1,118 @@ + + +# dtrsv + +> Solve one of the systems of equations `A*x = b` or `A^T*x = b`, where `b` and `x` are `N` element ndarrays and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix. + +
+ +
+ + + +
+ +## Usage + +```javascript +var dtrsv = require( '@stdlib/blas/base/ndarray/dtrsv' ); +``` + +#### dtrsv( arrays ) + +Solves one of the systems of equations `A*x = b` or `A^T*x = b`, where `b` and `x` are `N` element ndarrays and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix. + +```javascript +var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); + +var x = new Float64Vector( [ 1.0, 2.0, 3.0 ] ); +var A = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 4.0, 5.0, 0.0, 0.0, 6.0 ] ), [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); + +var out = dtrsv( [ x, A ] ); +// x => [ -0.25, -0.125, 0.5 ] + +var bool = ( out === x ); +// returns true +``` + +The function has the following parameters: + +- **arrays**: array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a two-dimensional input ndarray. + +
+ + + +
+ +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dtrsv = require( '@stdlib/blas/base/ndarray/dtrsv' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = new Float64Vector( discreteUniform( 3, 0, 10, opts ) ); +var A = new ndarray( 'float64', new Float64Array( discreteUniform( 9, 0, 10, opts ) ), [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); + +var out = dtrsv( [ x, A ] ); +console.log( ndarray2array( out ) ); +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/benchmark/benchmark.js b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/benchmark/benchmark.js new file mode 100644 index 000000000000..98f90a2bacde --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/benchmark/benchmark.js @@ -0,0 +1,109 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var bench = require( '@stdlib/bench' ); +var uniform = require( '@stdlib/random/uniform' ); +var zeros = require( '@stdlib/ndarray/zeros' ); +var isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var format = require( '@stdlib/string/format' ); +var pkg = require( './../package.json' ).name; +var dtrsv = require( './../lib' ); + + +// VARIABLES // + +var options = { + 'dtype': 'float64' +}; + + +// FUNCTIONS // + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @returns {Function} benchmark function +*/ +function createBenchmark( len ) { + var x; + var A; + + x = zeros( [ len ], options ); + A = uniform( [ len, len ], -100.0, 100.0, options ); + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var z; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + z = dtrsv( [ x, A ] ); + if ( typeof z !== 'object' ) { + b.fail( 'should return an ndarray' ); + } + } + b.toc(); + if ( isnan( z.get( i%len ) ) ) { + b.fail( 'should not return NaN' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var f; + var i; + + min = 1; // 10^min + max = 3; // 10^max + + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + f = createBenchmark( len ); + bench( format( '%s:len=%d', pkg, len ), f ); + } +} + +main(); diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/repl.txt b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/repl.txt new file mode 100644 index 000000000000..594af90a3adf --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/repl.txt @@ -0,0 +1,34 @@ + +{{alias}}( arrays ) + Solves one of the systems of equations `A*x = b` or `A^T*x = b`, where `b` + and `x` are `N` element ndarrays and `A` is an `N` by `N` unit, or + non-unit, upper or lower triangular matrix. + + Parameters + ---------- + arrays: ArrayLikeObject + Array-like object containing the following ndarrays: + + - a one-dimensional input ndarray. + - a two-dimensional input ndarray. + + Returns + ------- + out: ndarray + Output ndarray. + + Examples + -------- + > var x = new {{alias:@stdlib/ndarray/vector/float64}}( [ 1.0, 2.0 ] ); + > var buf = new {{alias:@stdlib/array/float64}}( [ 1.0, 2.0, 0.0, 1.0 ] ); + > var sh = [ 2, 2 ]; + > var st = [ 2, 1 ]; + > var A = new {{alias:@stdlib/ndarray/base/ctor}}( 'float64', buf, sh, st, 0, 'row-major' ); + + > {{alias}}( [ x, A ] ); + > x + [ -3.0, 2.0 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/types/index.d.ts b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/types/index.d.ts new file mode 100644 index 000000000000..41ffbb2d8239 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/types/index.d.ts @@ -0,0 +1,57 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// TypeScript Version: 4.1 + +/// + +import { float64ndarray } from '@stdlib/types/ndarray'; + +/** +* Solves one of the systems of equations `A*x = b` or `A^T*x = b`, where `b` and `x` are `N` element ndarrays and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a two-dimensional input ndarray. +* +* @param arrays - array-like object containing ndarrays +* @returns output ndarray +* +* @example +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var x = new Float64Vector( [ 1.0, 2.0, 3.0 ] ); +* var A = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 4.0, 5.0, 0.0, 0.0, 6.0 ] ), [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); +* +* var out = dtrsv( [ x, A ] ); +* // returns [ -0.25, -0.125, 0.5 ] +* +* var bool = ( out === x ); +* // returns true +*/ +declare function dtrsv( arrays: [ float64ndarray, float64ndarray ] ): float64ndarray; + + +// EXPORTS // + +export = dtrsv; diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/types/test.ts b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/types/test.ts new file mode 100644 index 000000000000..92db5f03a4de --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/docs/types/test.ts @@ -0,0 +1,63 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +/* eslint-disable space-in-parens */ + +import zeros = require( '@stdlib/ndarray/zeros' ); +import dtrsv = require( '@stdlib/blas/base/ndarray/dtrsv' ); + + +// TESTS // + +// The function returns an ndarray... +{ + const x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + const A = zeros( [ 3, 3 ], { + 'dtype': 'float64' + }); + + dtrsv( [ x, A ] ); // $ExpectType float64ndarray +} + +// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays... +{ + dtrsv( '10' ); // $ExpectError + dtrsv( 10 ); // $ExpectError + dtrsv( true ); // $ExpectError + dtrsv( false ); // $ExpectError + dtrsv( null ); // $ExpectError + dtrsv( undefined ); // $ExpectError + dtrsv( [] ); // $ExpectError + dtrsv( {} ); // $ExpectError + dtrsv( ( x: number ): number => x ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = zeros( [ 3 ], { + 'dtype': 'float64' + }); + const A = zeros( [ 3, 3 ], { + 'dtype': 'float64' + }); + + dtrsv(); // $ExpectError + dtrsv( [ x, A ], {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/examples/index.js b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/examples/index.js new file mode 100644 index 000000000000..e3f859685afa --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/examples/index.js @@ -0,0 +1,36 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var dtrsv = require( './../lib' ); + +var opts = { + 'dtype': 'float64' +}; + +var x = new Float64Vector( discreteUniform( 3, 0, 10, opts ) ); +var A = new ndarray( 'float64', new Float64Array( discreteUniform( 9, 0, 10, opts ) ), [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); + +var out = dtrsv( [ x, A ] ); +console.log( ndarray2array( out ) ); diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/lib/index.js b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/lib/index.js new file mode 100644 index 000000000000..bf98a5d0cd2c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/lib/index.js @@ -0,0 +1,49 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +/** +* BLAS level 2 routine to solve one of the systems of equations `A*x = b` or `A^T*x = b` where `b` and `x` are `N` element ndarrays and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix. +* +* @module @stdlib/blas/base/ndarray/dtrsv +* +* @example +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* var dtrsv = require( '@stdlib/blas/base/ndarray/dtrsv' ); +* +* var x = new Float64Vector( [ 1.0, 2.0, 3.0 ] ); +* var A = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 4.0, 5.0, 0.0, 0.0, 6.0 ] ), [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); +* +* var out = dtrsv( [ x, A ] ); +* // returns [ -0.25, -0.125, 0.5 ] +* +* var bool = ( out === x ); +* // returns true +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/lib/main.js b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/lib/main.js new file mode 100644 index 000000000000..5229631643f3 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/lib/main.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var numelDimension = require( '@stdlib/ndarray/base/numel-dimension' ); +var getStride = require( '@stdlib/ndarray/base/stride' ); +var getOffset = require( '@stdlib/ndarray/base/offset' ); +var getData = require( '@stdlib/ndarray/base/data-buffer' ); +var strided = require( '@stdlib/blas/base/dtrsv' ).ndarray; + + +// MAIN // + +/** +* Solves one of the systems of equations `A*x = b` or `A^T*x = b` where `b` and `x` are `N` element ndarrays and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix. +* +* ## Notes +* +* - The function expects the following ndarrays: +* +* - a one-dimensional input ndarray. +* - a two-dimensional input ndarray. +* +* @param {ArrayLikeObject} arrays - array-like object containing ndarrays +* @returns {Object} output ndarray +* +* @example +* var Float64Vector = require( '@stdlib/ndarray/vector/float64' ); +* var ndarray = require( '@stdlib/ndarray/base/ctor' ); +* var Float64Array = require( '@stdlib/array/float64' ); +* +* var x = new Float64Vector( [ 1.0, 2.0, 3.0 ] ); +* var A = new ndarray( 'float64', new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 4.0, 5.0, 0.0, 0.0, 6.0 ] ), [ 3, 3 ], [ 3, 1 ], 0, 'row-major' ); +* +* var out = dtrsv( [ x, A ] ); +* // returns [ -0.25, -0.125, 0.5 ] +* +* var bool = ( out === x ); +* // returns true +*/ +function dtrsv( arrays ) { + var x = arrays[ 0 ]; + var A = arrays[ 1 ]; + strided( 'upper', 'no-transpose', 'non-unit', numelDimension( A, 0 ), getData( A ), getStride( A, 0 ), getStride( A, 1 ), getOffset( A ), getData( x ), getStride( x, 0 ), getOffset( x ) ); + return x; +} + + +// EXPORTS // + +module.exports = dtrsv; diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/package.json b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/package.json new file mode 100644 index 000000000000..42c2de1d233c --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/package.json @@ -0,0 +1,72 @@ +{ + "name": "@stdlib/blas/base/ndarray/dtrsv", + "version": "0.0.0", + "description": "Solve one of the systems of equations `A*x = b` or `A^T*x = b` where `b` and `x` are `N` element ndarrays and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "stdmath", + "mathematics", + "math", + "blas", + "level 2", + "dtrsv", + "linear", + "algebra", + "subroutines", + "triangular", + "solve", + "vector", + "matrix", + "array", + "ndarray", + "float64", + "double", + "float64array" + ] +} diff --git a/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/test/test.js b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/test/test.js new file mode 100644 index 000000000000..e641ac0a7ee4 --- /dev/null +++ b/lib/node_modules/@stdlib/blas/base/ndarray/dtrsv/test/test.js @@ -0,0 +1,198 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2026 The Stdlib Authors. +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +'use strict'; + +// MODULES // + +var tape = require( 'tape' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/base/ctor' ); +var getData = require( '@stdlib/ndarray/data-buffer' ); +var dtrsv = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Returns a one-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} length - number of indexed elements +* @param {integer} stride - stride length +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} one-dimensional ndarray +*/ +function vector( buffer, length, stride, offset ) { + return new ndarray( 'float64', buffer, [ length ], [ stride ], offset, 'row-major' ); +} + +/** +* Returns a two-dimensional ndarray. +* +* @private +* @param {Collection} buffer - underlying data buffer +* @param {NonNegativeInteger} N - number of rows and columns +* @param {integer} stride0 - stride of the first dimension +* @param {integer} stride1 - stride of the second dimension +* @param {NonNegativeInteger} offset - index offset +* @returns {ndarray} two-dimensional ndarray +*/ +function matrix( buffer, N, stride0, stride1, offset ) { + return new ndarray( 'float64', buffer, [ N, N ], [ stride0, stride1 ], offset, 'row-major' ); +} + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof dtrsv, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function has an arity of 1', function test( t ) { + t.strictEqual( dtrsv.length, 1, 'has expected arity' ); + t.end(); +}); + +tape( 'the function solves the system of equations `A*x = b`', function test( t ) { + var expected; + var xbuf; + var Abuf; + var x; + var A; + var v; + + xbuf = new Float64Array( [ 1.0, 2.0, 3.0 ] ); + Abuf = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 4.0, 5.0, 0.0, 0.0, 6.0 ] ); + x = vector( xbuf, 3, 1, 0 ); + A = matrix( Abuf, 3, 3, 1, 0 ); + + v = dtrsv( [ x, A ] ); + + expected = new Float64Array( [ -0.25, -0.125, 0.5 ] ); + t.strictEqual( v, x, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports ndarrays having non-unit strides', function test( t ) { + var expected; + var xbuf; + var Abuf; + var x; + var A; + var v; + + xbuf = new Float64Array([ + 1.0, // 0 + 0.0, + 2.0, // 1 + 0.0, + 3.0 // 2 + ]); + x = vector( xbuf, 3, 2, 0 ); + + Abuf = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 4.0, 5.0, 0.0, 0.0, 6.0 ] ); + A = matrix( Abuf, 3, 3, 1, 0 ); + + v = dtrsv( [ x, A ] ); + + expected = new Float64Array( [ -0.25, 0.0, -0.125, 0.0, 0.5 ] ); + t.strictEqual( v, x, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports ndarrays having negative strides', function test( t ) { + var expected; + var xbuf; + var Abuf; + var x; + var A; + var v; + + xbuf = new Float64Array([ + 3.0, // 2 + 2.0, // 1 + 1.0 // 0 + ]); + x = vector( xbuf, 3, -1, 2 ); + + Abuf = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 4.0, 5.0, 0.0, 0.0, 6.0 ] ); + A = matrix( Abuf, 3, 3, 1, 0 ); + + v = dtrsv( [ x, A ] ); + + expected = new Float64Array( [ 0.5, -0.125, -0.25 ] ); + t.strictEqual( v, x, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +}); + +tape( 'the function supports ndarrays having non-zero offsets', function test( t ) { + var expected; + var xbuf; + var Abuf; + var x; + var A; + var v; + + xbuf = new Float64Array([ + 0.0, + 1.0, // 0 + 0.0, + 2.0, // 1 + 0.0, + 3.0 // 2 + ]); + x = vector( xbuf, 3, 2, 1 ); + + Abuf = new Float64Array([ + 999.0, + 999.0, + 1.0, + 2.0, + 3.0, + 0.0, + 4.0, + 5.0, + 0.0, + 0.0, + 6.0 + ]); + A = matrix( Abuf, 3, 3, 1, 2 ); + + v = dtrsv( [ x, A ] ); + + expected = new Float64Array([ + 0.0, + -0.25, + 0.0, + -0.125, + 0.0, + 0.5 + ]); + t.strictEqual( v, x, 'returns expected value' ); + t.strictEqual( isSameFloat64Array( getData( v ), expected ), true, 'returns expected value' ); + t.end(); +});