Skip to content

Commit 75860d1

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/gvander
PR-URL: #10934 Closes: stdlib-js/metr-issue-tracker#197 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com>
1 parent cda31f5 commit 75860d1

16 files changed

Lines changed: 3147 additions & 0 deletions

File tree

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# gvander
22+
23+
> Generate a Vandermonde matrix.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gvander = require( '@stdlib/blas/ext/base/gvander' );
37+
```
38+
39+
#### gvander( order, mode, M, N, x, strideX, out, ldo )
40+
41+
Generates a Vandermonde matrix.
42+
43+
```javascript
44+
var x = [ 1.0, 2.0, 3.0 ];
45+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
46+
47+
gvander( 'row-major', 1, 3, 3, x, 1, out, 3 );
48+
// out => [ 1.0, 1.0, 1.0, 1.0, 2.0, 4.0, 1.0, 3.0, 9.0 ]
49+
```
50+
51+
The function has the following parameters:
52+
53+
- **order**: row-major (C-style) or column-major (Fortran-style) order.
54+
- **mode**: mode. If `mode < 0`, the function generates decreasing powers. If `mode > 0`, the function generates increasing powers.
55+
- **M**: number of rows in `out` and number of indexed elements in `x`.
56+
- **N**: number of columns in `out`.
57+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
58+
- **strideX**: stride length for `x`.
59+
- **out**: output matrix.
60+
- **ldo**: stride of the first dimension of `out` (a.k.a., leading dimension of the matrix `out`).
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, max-len -->
65+
66+
```javascript
67+
var Float64Array = require( '@stdlib/array/float64' );
68+
69+
// Initial arrays:
70+
var x0 = new Float64Array( [ 999.0, 1.0, 2.0, 3.0 ] );
71+
var out0 = new Float64Array( [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ] );
72+
73+
// Create offset views:
74+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
75+
var out1 = new Float64Array( out0.buffer, out0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
76+
77+
gvander( 'row-major', 1, 3, 3, x1, 1, out1, 3 );
78+
// out0 => <Float64Array>[ 0.0, 1.0, 1.0, 1.0, 1.0, 2.0, 4.0, 1.0, 3.0, 9.0 ]
79+
```
80+
81+
When the mode is positive, the matrix is generated such that
82+
83+
```text
84+
[
85+
1 x_0^1 x_0^2 ... x_0^(N-1)
86+
1 x_1^1 x_1^2 ... x_1^(N-1)
87+
...
88+
]
89+
```
90+
91+
with increasing powers along the rows.
92+
93+
When the mode is negative, the matrix is generated such that
94+
95+
```text
96+
[
97+
x_0^(N-1) ... x_0^2 x_0^1 1
98+
x_1^(N-1) ... x_1^2 x_1^1 1
99+
...
100+
]
101+
```
102+
103+
with decreasing powers along the rows.
104+
105+
<!-- lint disable maximum-heading-length -->
106+
107+
#### gvander.ndarray( mode, M, N, x, strideX, offsetX, out, strideOut1, strideOut2, offsetOut )
108+
109+
<!-- lint enable maximum-heading-length -->
110+
111+
Generates a Vandermonde matrix using alternative indexing semantics.
112+
113+
```javascript
114+
var x = [ 1.0, 2.0, 3.0 ];
115+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
116+
117+
gvander.ndarray( 1, 3, 3, x, 1, 0, out, 3, 1, 0 );
118+
// out => [ 1.0, 1.0, 1.0, 1.0, 2.0, 4.0, 1.0, 3.0, 9.0 ]
119+
```
120+
121+
The function has the following additional parameters:
122+
123+
- **offsetX**: starting index for `x`.
124+
- **strideOut1**: stride length for the first dimension of `out`.
125+
- **strideOut2**: stride length for the second dimension of `out`.
126+
- **offsetOut**: starting index for `out`.
127+
128+
While [`typed array`][mdn-typed-array] views mandate a view offset based on the underlying buffer, offset parameters support indexing semantics based on starting indices. For example, to use every other element from the input array starting from the second element:
129+
130+
```javascript
131+
var x = [ 0.0, 1.0, 0.0, 2.0, 0.0, 3.0 ];
132+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
133+
134+
gvander.ndarray( 1, 3, 3, x, 2, 1, out, 3, 1, 0 );
135+
// out => [ 1.0, 1.0, 1.0, 1.0, 2.0, 4.0, 1.0, 3.0, 9.0 ]
136+
```
137+
138+
</section>
139+
140+
<!-- /.usage -->
141+
142+
<section class="notes">
143+
144+
## Notes
145+
146+
- If `M <= 0` or `N <= 0`, both functions return `out` unchanged.
147+
- Both functions support array-like objects having getter and setter accessors for array element access (e.g., [`@stdlib/array/base/accessor`][@stdlib/array/base/accessor]).
148+
- Depending on the environment, the typed versions ([`dvander`][@stdlib/blas/ext/base/dvander], [`svander`][@stdlib/blas/ext/base/svander], etc.) are likely to be significantly more performant.
149+
150+
</section>
151+
152+
<!-- /.notes -->
153+
154+
<section class="examples">
155+
156+
## Examples
157+
158+
<!-- eslint no-undef: "error" -->
159+
160+
```javascript
161+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
162+
var zeros = require( '@stdlib/array/zeros' );
163+
var gvander = require( '@stdlib/blas/ext/base/gvander' );
164+
165+
var M = 3;
166+
var N = 4;
167+
168+
var x = discreteUniform( M, 0, 10, {
169+
'dtype': 'generic'
170+
});
171+
var out = zeros( M*N, 'generic' );
172+
console.log( x );
173+
174+
gvander( 'row-major', -1, M, N, x, 1, out, N );
175+
console.log( out );
176+
```
177+
178+
</section>
179+
180+
<!-- /.examples -->
181+
182+
<section class="references">
183+
184+
</section>
185+
186+
<!-- /.references -->
187+
188+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
189+
190+
<section class="related">
191+
192+
</section>
193+
194+
<!-- /.related -->
195+
196+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
197+
198+
<section class="links">
199+
200+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
201+
202+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
203+
204+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
205+
206+
[@stdlib/blas/ext/base/dvander]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dvander
207+
208+
[@stdlib/blas/ext/base/svander]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/svander
209+
210+
<!-- <related-links> -->
211+
212+
<!-- </related-links> -->
213+
214+
</section>
215+
216+
<!-- /.links -->
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2026 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 zeros = require( '@stdlib/array/zeros' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gvander = require( './../lib/main.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var out = zeros( len * len, options.dtype );
51+
var x = uniform( len, -10, 10, options );
52+
return benchmark;
53+
54+
/**
55+
* Benchmark function.
56+
*
57+
* @private
58+
* @param {Benchmark} b - benchmark instance
59+
*/
60+
function benchmark( b ) {
61+
var v;
62+
var i;
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
x[ 0 ] += 0.1;
67+
v = gvander( 'row-major', 1, len, len, x, 1, out, len );
68+
if ( isnan( v[ i%v.length ] ) ) {
69+
b.fail( 'should not return NaN' );
70+
}
71+
}
72+
b.toc();
73+
if ( isnan( v[ i%v.length ] ) ) {
74+
b.fail( 'should not return NaN' );
75+
}
76+
b.pass( 'benchmark finished' );
77+
b.end();
78+
}
79+
}
80+
81+
82+
// MAIN //
83+
84+
/**
85+
* Main execution sequence.
86+
*
87+
* @private
88+
*/
89+
function main() {
90+
var len;
91+
var min;
92+
var max;
93+
var f;
94+
var i;
95+
96+
min = 1; // 10^min
97+
max = 3; // 10^max
98+
99+
for ( i = min; i <= max; i++ ) {
100+
len = pow( 10, i );
101+
f = createBenchmark( len );
102+
bench( format( '%s:len=%d', pkg, len ), f );
103+
}
104+
}
105+
106+
main();

0 commit comments

Comments
 (0)