Skip to content

Commit 01d9619

Browse files
aayush0325kgryte
andauthored
feat: add lapack/base/dlaset
PR-URL: #7467 Co-authored-by: Athan Reines <[email protected]> Reviewed-by: Athan Reines <[email protected]>
1 parent b82041b commit 01d9619

Some content is hidden

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

46 files changed

+3892
-0
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2025 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+
# dlaset
22+
23+
> Set the off-diagonal elements and the diagonal elements of a double-precision floating-point matrix to specified values.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dlaset = require( '@stdlib/lapack/base/dlaset' );
31+
```
32+
33+
#### dlaset( order, uplo, M, N, alpha, beta, A, LDA )
34+
35+
Sets the off-diagonal elements and the diagonal elements of a double-precision floating-point matrix to specified values.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var A = new Float64Array( 4 );
41+
42+
dlaset( 'row-major', 'all', 2, 2, 2.0, 1.0, A, 2 );
43+
// A => <Float64Array>[ 1.0, 2.0, 2.0, 1.0 ]
44+
```
45+
46+
The function has the following parameters:
47+
48+
- **order**: storage layout.
49+
- **uplo**: specifies whether to set the upper or lower triangular/trapezoidal part of a matrix `A`.
50+
- **M**: number of rows in `A`.
51+
- **N**: number of columns in `A`.
52+
- **alpha**: value assigned to off-diagonal elements.
53+
- **beta**: value assigned to diagonal elements.
54+
- **A**: input [`Float64Array`][mdn-float64array].
55+
- **LDA**: stride of the first dimension of `A` (a.k.a., leading dimension of the matrix `A`).
56+
57+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
58+
59+
<!-- eslint-disable stdlib/capitalized-comments -->
60+
61+
```javascript
62+
var Float64Array = require( '@stdlib/array/float64' );
63+
64+
// Initial array:
65+
var A0 = new Float64Array( 5 );
66+
67+
// Create offset view:
68+
var A1 = new Float64Array( A0.buffer, A0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
69+
70+
dlaset( 'row-major', 'all', 2, 2, 2.0, 1.0, A1, 2 );
71+
// A0 => <Float64Array>[ 0.0, 1.0, 2.0, 2.0, 1.0 ]
72+
```
73+
74+
#### dlaset.ndarray( uplo, M, N, alpha, beta, A, sa1, sa2, oa )
75+
76+
Sets the off-diagonal elements and the diagonal elements of a double-precision floating-point matrix to specified values using alternative indexing semantics.
77+
78+
```javascript
79+
var Float64Array = require( '@stdlib/array/float64' );
80+
81+
var A = new Float64Array( 4 );
82+
83+
dlaset.ndarray( 'all', 2, 2, 2.0, 1.0, A, 2, 1, 0 );
84+
// A => <Float64Array>[ 1.0, 2.0, 2.0, 1.0 ]
85+
```
86+
87+
The function has the following parameters:
88+
89+
- **uplo**: specifies whether to set the upper or lower triangular/trapezoidal part of a matrix `A`.
90+
- **M**: number of rows in `A`.
91+
- **N**: number of columns in `A`.
92+
- **alpha**: value assigned to off-diagonal elements.
93+
- **beta**: value assigned to diagonal elements.
94+
- **A**: input [`Float64Array`][mdn-float64array].
95+
- **sa1**: stride of the first dimension of `A`.
96+
- **sa2**: stride of the second dimension of `A`.
97+
- **oa**: starting index for `A`.
98+
99+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, the offset parameter supports indexing semantics based on a starting index. For example,
100+
101+
```javascript
102+
var Float64Array = require( '@stdlib/array/float64' );
103+
104+
var A = new Float64Array( 5 );
105+
106+
dlaset.ndarray( 'all', 2, 2, 2.0, 1.0, A, 2, 1, 1 );
107+
// A => <Float64Array>[ 0.0, 1.0, 2.0, 2.0, 1.0 ]
108+
```
109+
110+
</section>
111+
112+
<!-- /.usage -->
113+
114+
<section class="notes">
115+
116+
## Notes
117+
118+
- `dlaset()` corresponds to the [LAPACK][lapack] routine [`dlaset`][lapack-dlaset].
119+
120+
</section>
121+
122+
<!-- /.notes -->
123+
124+
<section class="examples">
125+
126+
## Examples
127+
128+
<!-- eslint no-undef: "error" -->
129+
130+
```javascript
131+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
132+
var uniform = require( '@stdlib/random/array/discrete-uniform' );
133+
var numel = require( '@stdlib/ndarray/base/numel' );
134+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
135+
var dlaset = require( '@stdlib/lapack/base/dlaset' );
136+
137+
var shape = [ 5, 8 ];
138+
var order = 'row-major';
139+
var strides = shape2strides( shape, order );
140+
141+
var N = numel( shape );
142+
143+
var A = uniform( N, -10, 10, {
144+
'dtype': 'float64'
145+
});
146+
console.log( ndarray2array( A, shape, strides, 0, order ) );
147+
148+
dlaset( order, 'all', shape[ 0 ], shape[ 1 ], 2.0, 3.0, A, strides[ 0 ] );
149+
console.log( ndarray2array( A, shape, strides, 0, order ) );
150+
```
151+
152+
</section>
153+
154+
<!-- /.examples -->
155+
156+
<!-- C interface documentation. -->
157+
158+
* * *
159+
160+
<section class="c">
161+
162+
## C APIs
163+
164+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
165+
166+
<section class="intro">
167+
168+
</section>
169+
170+
<!-- /.intro -->
171+
172+
<!-- C usage documentation. -->
173+
174+
<section class="usage">
175+
176+
### Usage
177+
178+
```c
179+
TODO
180+
```
181+
182+
#### TODO
183+
184+
TODO.
185+
186+
```c
187+
TODO
188+
```
189+
190+
TODO
191+
192+
```c
193+
TODO
194+
```
195+
196+
</section>
197+
198+
<!-- /.usage -->
199+
200+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
201+
202+
<section class="notes">
203+
204+
</section>
205+
206+
<!-- /.notes -->
207+
208+
<!-- C API usage examples. -->
209+
210+
<section class="examples">
211+
212+
### Examples
213+
214+
```c
215+
TODO
216+
```
217+
218+
</section>
219+
220+
<!-- /.examples -->
221+
222+
</section>
223+
224+
<!-- /.c -->
225+
226+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
227+
228+
<section class="related">
229+
230+
</section>
231+
232+
<!-- /.related -->
233+
234+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
235+
236+
<section class="links">
237+
238+
[lapack]: https://www.netlib.org/lapack/explore-html/
239+
240+
[lapack-dlaset]: https://www.netlib.org/lapack/explore-html-3.6.1/d7/d43/group__aux_o_t_h_e_rauxiliary_ga89e332374c7cd87e5db54bfe21550bc3.html
241+
242+
[mdn-float64array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array
243+
244+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
245+
246+
</section>
247+
248+
<!-- /.links -->
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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 isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var floor = require( '@stdlib/math/base/special/floor' );
28+
var pkg = require( './../package.json' ).name;
29+
var dlaset = require( './../lib/dlaset.js' );
30+
31+
32+
// VARIABLES //
33+
34+
var LAYOUTS = [
35+
'row-major',
36+
'column-major'
37+
];
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {string} order - storage layout
47+
* @param {PositiveInteger} N - number of elements along each dimension
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( order, N ) {
51+
var A;
52+
53+
A = uniform( N*N, -10.0, 10.0, {
54+
'dtype': 'float64'
55+
});
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var z;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
z = dlaset( order, 'all', N, N, i, i+1, A, N );
71+
if ( isnan( z[ i%z.length ] ) ) {
72+
b.fail( 'should not return NaN' );
73+
}
74+
}
75+
b.toc();
76+
if ( isnan( z[ i%z.length ] ) ) {
77+
b.fail( 'should not return NaN' );
78+
}
79+
b.pass( 'benchmark finished' );
80+
b.end();
81+
}
82+
}
83+
84+
85+
// MAIN //
86+
87+
/**
88+
* Main execution sequence.
89+
*
90+
* @private
91+
*/
92+
function main() {
93+
var min;
94+
var max;
95+
var ord;
96+
var N;
97+
var f;
98+
var i;
99+
var k;
100+
101+
min = 1; // 10^min
102+
max = 6; // 10^max
103+
104+
for ( k = 0; k < LAYOUTS.length; k++ ) {
105+
ord = LAYOUTS[ k ];
106+
for ( i = min; i <= max; i++ ) {
107+
N = floor( pow( pow( 10, i ), 1.0/2.0 ) );
108+
f = createBenchmark( ord, N );
109+
bench( pkg+'::equidimensional:order='+ord+',size='+(N*N), f );
110+
}
111+
}
112+
}
113+
114+
main();

0 commit comments

Comments
 (0)