Skip to content

Commit 09afa7c

Browse files
authored
feat: add blas/base/dtrsv
PR-URL: #2753 Ref: #2039 Reviewed-by: Athan Reines <[email protected]>
1 parent 1745404 commit 09afa7c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3948
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,259 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2024 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# dtrsv
22+
23+
> Solve one of the systems of equations `A*x = b` or `A^T*x = b`.
24+
25+
<section class = "usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dtrsv = require( '@stdlib/blas/base/dtrsv' );
31+
```
32+
33+
#### dtrsv( order, uplo, trans, diag, N, A, LDA, x, sx )
34+
35+
Solves one of the systems of equations `A*x = b` or `A^T*x = b` where `b` and `x` are `N` element vectors and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0 ] );
41+
var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
42+
43+
dtrsv( 'row-major', 'upper', 'no-transpose', 'unit', 3, A, 3, x, 1 );
44+
// x => <Float64Array>[ 0.0, -4.0, 3.0 ]
45+
```
46+
47+
The function has the following parameters:
48+
49+
- **order**: storage layout.
50+
- **uplo**: specifies whether `A` is an upper or lower triangular matrix.
51+
- **trans**: specifies whether `A` should be transposed, conjugate-transposed, or not transposed.
52+
- **diag**: specifies whether `A` has a unit diagonal.
53+
- **N**: number of elements along each dimension of `A`.
54+
- **A**: input matrix stored in linear memory as a [`Float64Array`][mdn-float64array].
55+
- **lda**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
56+
- **x**: input vector [`Float64Array`][mdn-float64array].
57+
- **sx**: `x` stride length.
58+
59+
The stride parameters determine how elements in the input arrays are accessed at runtime. For example, to iterate over the elements of `x` in reverse order,
60+
61+
```javascript
62+
var Float64Array = require( '@stdlib/array/float64' );
63+
64+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0 ] );
65+
var x = new Float64Array( [ 3.0, 2.0, 1.0 ] );
66+
67+
dtrsv( 'row-major', 'upper', 'no-transpose', 'unit', 3, A, 3, x, -1 );
68+
// x => <Float64Array>[ 3.0, -4.0, 0.0 ]
69+
```
70+
71+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
72+
73+
<!-- eslint-disable stdlib/capitalized-comments -->
74+
75+
```javascript
76+
var Float64Array = require( '@stdlib/array/float64' );
77+
78+
// Initial arrays...
79+
var x0 = new Float64Array( [ 1.0, 1.0, 1.0, 1.0 ] );
80+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0 ] );
81+
82+
// Create offset views...
83+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
84+
85+
dtrsv( 'row-major', 'upper', 'no-transpose', 'unit', 3, A, 3, x1, 1 );
86+
// x0 => <Float64Array>[ 1.0, 0.0, -1.0, 1.0 ]
87+
```
88+
89+
#### dtrsv.ndarray( uplo, trans, diag, N, A, sa1, sa2, oa, x, sx, ox )
90+
91+
Solves one of the systems of equations `A*x = b` or `A^T*x = b`, using alternative indexing semantics and where `b` and `x` are `N` element vectors and `A` is an `N` by `N` unit, or non-unit, upper or lower triangular matrix.
92+
93+
```javascript
94+
var Float64Array = require( '@stdlib/array/float64' );
95+
96+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0 ] );
97+
var x = new Float64Array( [ 1.0, 2.0, 3.0 ] );
98+
99+
dtrsv.ndarray( 'upper', 'no-transpose', 'unit', 3, A, 3, 1, 0, x, 1, 0 );
100+
// x => <Float64Array>[ 0.0, -4.0, 3.0 ]
101+
```
102+
103+
The function has the following additional parameters:
104+
105+
- **sa1**: stride of the first dimension of `A`.
106+
- **sa2**: stride of the second dimension of `A`.
107+
- **oa**: starting index for `A`.
108+
- **ox**: starting index for `x`.
109+
110+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameters support indexing semantics based on starting indices. For example,
111+
112+
```javascript
113+
var Float64Array = require( '@stdlib/array/float64' );
114+
115+
var A = new Float64Array( [ 1.0, 2.0, 3.0, 0.0, 1.0, 2.0, 0.0, 0.0, 1.0 ] );
116+
var x = new Float64Array( [ 3.0, 2.0, 1.0 ] );
117+
118+
dtrsv.ndarray( 'upper', 'no-transpose', 'unit', 3, A, 3, 1, 0, x, -1, 2 );
119+
// x => <Float64Array>[ 3.0, -4.0, 0.0 ]
120+
```
121+
122+
</section>
123+
124+
<!-- /.usage -->
125+
126+
<section class="notes">
127+
128+
## Notes
129+
130+
- `dtrsv()` corresponds to the [BLAS][blas] level 2 function [`dtrsv`][blas-dtrsv].
131+
- Neither routine tests for singularity or near-singularity. Such tests must be performed before calling the routines.
132+
133+
</section>
134+
135+
<!-- /.notes -->
136+
137+
<section class="examples">
138+
139+
## Examples
140+
141+
<!-- eslint no-undef: "error" -->
142+
143+
```javascript
144+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
145+
var dtrsv = require( '@stdlib/blas/base/dtrsv' );
146+
147+
var opts = {
148+
'dtype': 'float64'
149+
};
150+
151+
var N = 5;
152+
153+
var A = discreteUniform( N*N, -10.0, 10.0, opts );
154+
var x = discreteUniform( N, -10.0, 10.0, opts );
155+
156+
dtrsv( 'column-major', 'upper', 'no-transpose', 'unit', N, A, N, x, 1 );
157+
console.log( x );
158+
159+
dtrsv.ndarray( 'upper', 'no-transpose', 'unit', N, A, 1, N, 0, x, 1, 0 );
160+
console.log( x );
161+
```
162+
163+
</section>
164+
165+
<!-- /.examples -->
166+
167+
<!-- C interface documentation. -->
168+
169+
* * *
170+
171+
<section class="c">
172+
173+
## C APIs
174+
175+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
176+
177+
<section class="intro">
178+
179+
</section>
180+
181+
<!-- /.intro -->
182+
183+
<!-- C usage documentation. -->
184+
185+
<section class="usage">
186+
187+
### Usage
188+
189+
```c
190+
TODO
191+
```
192+
193+
#### TODO
194+
195+
TODO.
196+
197+
```c
198+
TODO
199+
```
200+
201+
TODO
202+
203+
```c
204+
TODO
205+
```
206+
207+
</section>
208+
209+
<!-- /.usage -->
210+
211+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
212+
213+
<section class="notes">
214+
215+
</section>
216+
217+
<!-- /.notes -->
218+
219+
<!-- C API usage examples. -->
220+
221+
<section class="examples">
222+
223+
### Examples
224+
225+
```c
226+
TODO
227+
```
228+
229+
</section>
230+
231+
<!-- /.examples -->
232+
233+
</section>
234+
235+
<!-- /.c -->
236+
237+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
238+
239+
<section class="related">
240+
241+
</section>
242+
243+
<!-- /.related -->
244+
245+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
246+
247+
<section class="links">
248+
249+
[blas]: http://www.netlib.org/blas
250+
251+
[blas-dtrsv]: https://www.netlib.org/lapack/explore-html/dd/dc3/group__trsv_ga7a7dcbb8745b4776ce13063ab031141f.html#ga7a7dcbb8745b4776ce13063ab031141f
252+
253+
[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
254+
255+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
256+
257+
</section>
258+
259+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 bench = require( '@stdlib/bench' );
24+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
26+
var zeros = require( '@stdlib/array/zeros' );
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 dtrsv = require( './../lib/dtrsv.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float64'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} N - number of elements along each dimension
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( N ) {
50+
var A = discreteUniform( N*N, -10.0, 10.0, options );
51+
var x = zeros( N, options.dtype );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var z;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
z = dtrsv( 'row-major', 'upper', 'transpose', 'non-unit', N, A, N, x, 1 );
67+
if ( isnan( z[ i%z.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( z[ i%z.length ] ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
}
78+
}
79+
80+
81+
// MAIN //
82+
83+
/**
84+
* Main execution sequence.
85+
*
86+
* @private
87+
*/
88+
function main() {
89+
var min;
90+
var max;
91+
var N;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 6; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
100+
f = createBenchmark( N );
101+
bench( pkg+':size='+(N*N), f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)