Skip to content

Commit eaddeda

Browse files
committed
feat: add blas/base/strsm
1 parent 63756c5 commit eaddeda

File tree

14 files changed

+1861
-0
lines changed

14 files changed

+1861
-0
lines changed
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
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+
# strsm
22+
23+
> Solve matrix equation `op(A) * X = alpha * B` or `X * op(A) = alpha * B` where `alpha` is a scalar, `X` and `B` are `m` by `n` matrices, `A` is a unit, or non-unit, upper or lower triangular matrix and `op(A)` is one of `op(A) = A` or `op(A) = A^T`. The matrix `X` is overwritten on `B`.
24+
25+
<section class = "usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var strsm = require( '@stdlib/blas/base/strsm' );
31+
```
32+
33+
#### strsm( order, side, uplo, transa, diag, m, n, alpha, A, LDA, B, LDB )
34+
35+
Solves matrix equation `op(A) * X = alpha * B` or `X * op(A) = alpha * B` where `alpha` is a scalar, `X` and `B` are `m` by `n` matrices, `A` is a unit, or non-unit, upper or lower triangular matrix and `op(A)` is one of `op(A) = A` or `op(A) = A^T`. The matrix `X` is overwritten on `B`.
36+
37+
```javascript
38+
var Float32Array = require( '@stdlib/array/loat32' );
39+
40+
A = new Float32Array( [ 1.0, 3.0, 0.0, 4.0 ] );
41+
B = new Float32Array( [ 5.0, 7.0, 0.0, 8.0 ] );
42+
43+
strsm( 'row-major', 'left', 'upper', 'no-transpose', 'non-unit', 2, 2, 6.0, A, 2, B, 2 );
44+
// B => <Float32Array>[ 30.0, 6.0, 0.0, 12.0 ]
45+
```
46+
47+
The function has the following parameters:
48+
49+
- **order**: storage layout of `A` and `B`.
50+
- **side**: specifies whether `op( A )` appears on the left or right side of `X`.
51+
- **uplo**: specifies whether the upper or lower triangular part of the matrix `A` is supplied.
52+
- **transa**: specifies the form of `op( A )` to be used in the matrix multiplication.
53+
- **diag**: specifies whether or not `A` is unit triangular.
54+
- **m**: number of rows in `B`.
55+
- **n**: number of columns in `B`.
56+
- **alpha**: scalar constant.
57+
- **A**: input matrix `A`.
58+
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
59+
- **B**: input matrix `B`.
60+
- **LDB**: stride of the first dimension of `B` (a.k.a., leading dimension of the matrix `B`).
61+
62+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
63+
64+
<!-- eslint-disable stdlib/capitalized-comments -->
65+
66+
```javascript
67+
var Float32Array = require( '@stdlib/array/loat32' );
68+
69+
// Initial arrays...
70+
var A0 = new Float32Array( [ 0.0, 1.0, 3.0, 0.0, 4.0 ] );
71+
var B0 = new Float32Array( [ 0.0, 5.0, 7.0, 0.0, 8.0 ] );
72+
73+
// Create offset views...
74+
var A1 = new Float32Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
75+
var B1 = new Float32Array( B0.buffer, B0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
76+
77+
strsm( 'row-major', 'left', 'upper', 'no-transpose', 'non-unit', 2, 2, 6.0, A1, 2, B1, 2 );
78+
// B0 => <Float32Array>[ 0.0, 30.0, 6.0, 0.0, 12.0 ]
79+
```
80+
81+
#### strsm.ndarray( s, ul, t, d, m, n, α, A, sa1, sa2, oa, B, sb1, sb2, ob )
82+
83+
Solves matrix equation `op(A) * X = alpha * B` or `X * op(A) = alpha * B` using alternative indexing semantics and where `alpha` is a scalar, `X` and `B` are `m` by `n` matrices, `A` is a unit, or non-unit, upper or lower triangular matrix and `op(A)` is one of `op(A) = A` or `op(A) = A^T`. The matrix `X` is overwritten on `B`.
84+
85+
```javascript
86+
var Float32Array = require( '@stdlib/array/loat32' );
87+
88+
A = new Float32Array( [ 0.0, 0.0, 1.0, 3.0, 0.0, 4.0 ] );
89+
B = new Float32Array( [ 0.0, 5.0, 7.0, 0.0, 8.0 ] );
90+
91+
strsm.ndarray( 'left', 'upper', 'no-transpose', 'non-unit', 2, 2, 6.0, A, 2, 1, 2, B, 2, 1, 1 );
92+
// B => <Float32Array>[ 0.0, 30.0, 6.0, 0.0, 12.0 ]
93+
```
94+
95+
The function has the following parameters:
96+
97+
- **side**: specifies whether `op( A )` appears on the left or right side of `X`.
98+
- **uplo**: specifies whether the upper or lower triangular part of the matrix `A` is supplied.
99+
- **transa**: specifies the form of `op( A )` to be used in the matrix multiplication.
100+
- **diag**: specifies whether or not `A` is unit triangular.
101+
- **m**: number of rows in `B`.
102+
- **n**: number of columns in `B`.
103+
- **alpha**: scalar constant.
104+
- **A**: input matrix `A`.
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+
- **B**: input matrix `B`.
109+
- **sb1**: stride of the first dimension of `B`.
110+
- **sb2**: stride of the second dimension of `B`.
111+
- **ob**: starting index for `B`.
112+
113+
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,
114+
115+
```javascript
116+
var Float32Array = require( '@stdlib/array/loat32' );
117+
118+
A = new Float32Array( [ 0.0, 0.0, 1.0, 3.0, 0.0, 4.0 ] );
119+
B = new Float32Array( [ 0.0, 5.0, 7.0, 0.0, 8.0 ] );
120+
121+
strsm.ndarray( 'left', 'upper', 'no-transpose', 'non-unit', 2, 2, 6.0, A, 2, 1, 2, B, 2, 1, 1 );
122+
// B => <Float32Array>[ 0.0, 30.0, 6.0, 0.0, 12.0 ]
123+
```
124+
125+
</section>
126+
127+
<!-- /.usage -->
128+
129+
<section class="notes">
130+
131+
## Notes
132+
133+
- `strsm()` corresponds to the [BLAS][blas] level 3 function [`strsm`][strsm].
134+
135+
</section>
136+
137+
<!-- /.notes -->
138+
139+
<section class="examples">
140+
141+
## Examples
142+
143+
<!-- eslint no-undef: "error" -->
144+
145+
```javascript
146+
var Float32Array = require( '@stdlib/array/loat32' );
147+
var strsm = require( '@stdlib/blas/base/strsm' );
148+
149+
var A = new Float32Array( [ 1.0, 0.0, 3.0, 4.0 ] );
150+
var B = new Float32Array( [ 5.0, 0.0, 7.0, 8.0 ] );
151+
152+
strsm( 'row-major', 'left', 'lower', 'no-transpose', 'non-unit', 2, 2, 6.0, A, 2, B, 2 );
153+
console.log( B );
154+
```
155+
156+
</section>
157+
158+
<!-- /.examples -->
159+
160+
<!-- C interface documentation. -->
161+
162+
* * *
163+
164+
<section class="c">
165+
166+
## C APIs
167+
168+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
169+
170+
<section class="intro">
171+
172+
</section>
173+
174+
<!-- /.intro -->
175+
176+
<!-- C usage documentation. -->
177+
178+
<section class="usage">
179+
180+
### Usage
181+
182+
```c
183+
TODO
184+
```
185+
186+
#### TODO
187+
188+
TODO.
189+
190+
```c
191+
TODO
192+
```
193+
194+
TODO
195+
196+
```c
197+
TODO
198+
```
199+
200+
</section>
201+
202+
<!-- /.usage -->
203+
204+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
205+
206+
<section class="notes">
207+
208+
</section>
209+
210+
<!-- /.notes -->
211+
212+
<!-- C API usage examples. -->
213+
214+
<section class="examples">
215+
216+
### Examples
217+
218+
```c
219+
TODO
220+
```
221+
222+
</section>
223+
224+
<!-- /.examples -->
225+
226+
</section>
227+
228+
<!-- /.c -->
229+
230+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
231+
232+
<section class="related">
233+
234+
</section>
235+
236+
<!-- /.related -->
237+
238+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
239+
240+
<section class="links">
241+
242+
[blas]: http://www.netlib.org/blas
243+
244+
[strsm]: https://www.netlib.org/lapack/explore-html/d2/d8b/strsm_8f.html
245+
246+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
247+
248+
</section>
249+
250+
<!-- /.links -->
Lines changed: 105 additions & 0 deletions
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 uniform = require( '@stdlib/random/array/uniform' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
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 strsm = require( './../lib/ndarray.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'float32'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} N - array dimension size
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( N ) {
50+
var A = uniform( N*N, -10.0, 10.0, options );
51+
var B = zeros( N*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 = strsm( 'left', 'lower', 'no-transpose', 'non-unit', N, N, 1.0, A, 1, N, 0, B, 1, N, 0 );
67+
if ( isnanf( z[ i%z.length ] ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnanf( 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 len;
90+
var min;
91+
var max;
92+
var f;
93+
var i;
94+
95+
min = 1; // 10^min
96+
max = 4; // 10^max
97+
98+
for ( i = min; i <= max; i++ ) {
99+
len = floor( pow( pow( 10, i ), 1.0/2.0 ) );
100+
f = createBenchmark( len );
101+
bench( pkg+':ndarray:order(A)=column-major,order(B)=column-major,side=left,uplo=lower,trans=false,diag=non-unit,size='+(len*len), f );
102+
}
103+
}
104+
105+
main();

0 commit comments

Comments
 (0)