Skip to content

Commit 181b4cc

Browse files
authored
feat: add blas/ext/base/gcartesian-power
PR-URL: #11926 Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#507
1 parent afe8812 commit 181b4cc

15 files changed

Lines changed: 2655 additions & 0 deletions

File tree

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
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+
# gcartesianPower
22+
23+
> Compute the Cartesian power for a strided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var gcartesianPower = require( '@stdlib/blas/ext/base/gcartesian-power' );
31+
```
32+
33+
#### gcartesianPower( order, N, k, x, strideX, out, LDO )
34+
35+
Computes the Cartesian power for a strided array.
36+
37+
```javascript
38+
var x = [ 1.0, 2.0 ];
39+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
40+
41+
gcartesianPower( 'row-major', x.length, 2, x, 1, out, 2 );
42+
// out => [ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
43+
```
44+
45+
The function has the following parameters:
46+
47+
- **order**: storage layout. Must be either `'row-major'` or `'column-major'`.
48+
- **N**: number of indexed elements.
49+
- **k**: power.
50+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
51+
- **strideX**: stride length for `x`.
52+
- **out**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
53+
- **LDO**: stride length between successive contiguous vectors of the matrix `out` (a.k.a., leading dimension of `out`).
54+
55+
The `N`, `k`, and stride parameters determine which elements in the strided arrays are accessed at runtime. For example, to compute the Cartesian power of every other element:
56+
57+
```javascript
58+
var x = [ 1.0, 0.0, 2.0, 0.0 ];
59+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
60+
61+
gcartesianPower( 'row-major', 2, 2, x, 2, out, 2 );
62+
// out => [ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
63+
```
64+
65+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
66+
67+
```javascript
68+
var Float64Array = require( '@stdlib/array/float64' );
69+
70+
// Initial array:
71+
var x0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
72+
73+
// Create an offset view:
74+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
75+
76+
// Output array:
77+
var out = new Float64Array( 8 );
78+
79+
gcartesianPower( 'row-major', 2, 2, x1, 1, out, 2 );
80+
// out => <Float64Array>[ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
81+
```
82+
83+
<!-- lint disable maximum-heading-length -->
84+
85+
#### gcartesianPower.ndarray( N, k, x, strideX, offsetX, out, strideOut1, strideOut2, offsetOut )
86+
87+
<!-- lint enable maximum-heading-length -->
88+
89+
Computes the Cartesian power for a strided array using alternative indexing semantics.
90+
91+
```javascript
92+
var x = [ 1.0, 2.0 ];
93+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
94+
95+
gcartesianPower.ndarray( x.length, 2, x, 1, 0, out, 2, 1, 0 );
96+
// out => [ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
97+
```
98+
99+
The function has the following parameters:
100+
101+
- **N**: number of indexed elements.
102+
- **k**: power.
103+
- **x**: input [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
104+
- **strideX**: stride length for `x`.
105+
- **offsetX**: starting index for `x`.
106+
- **out**: output [`Array`][mdn-array] or [`typed array`][mdn-typed-array].
107+
- **strideOut1**: stride length for the first dimension of `out`.
108+
- **strideOut2**: stride length for the second dimension of `out`.
109+
- **offsetOut**: starting index for `out`.
110+
111+
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, to access only the last two elements:
112+
113+
```javascript
114+
var x = [ 0.0, 0.0, 1.0, 2.0 ];
115+
var out = [ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 ];
116+
117+
gcartesianPower.ndarray( 2, 2, x, 1, 2, out, 2, 1, 0 );
118+
// out => [ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
119+
```
120+
121+
</section>
122+
123+
<!-- /.usage -->
124+
125+
<section class="notes">
126+
127+
## Notes
128+
129+
- `k`-tuples are stored as rows in the output matrix, where the `j`-th column contains the `j`-th element of each tuple.
130+
- For an input array of length `N`, the output array must contain at least `N^k * k` indexed elements.
131+
- For row-major order, the `LDO` parameter must be greater than or equal to `max(1,k)`. For column-major order, the `LDO` parameter must be greater than or equal to `max(1,N^k)`.
132+
- If `N <= 0` or `k <= 0`, both functions return `out` unchanged.
133+
- 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]).
134+
- Depending on the environment, the typed versions ([`dcartesianPower`][@stdlib/blas/ext/base/dcartesian-power], [`scartesianPower`][@stdlib/blas/ext/base/scartesian-power], etc.) are likely to be significantly more performant.
135+
136+
</section>
137+
138+
<!-- /.notes -->
139+
140+
<section class="examples">
141+
142+
## Examples
143+
144+
<!-- eslint no-undef: "error" -->
145+
146+
```javascript
147+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
148+
var zeros = require( '@stdlib/array/zeros' );
149+
var pow = require( '@stdlib/math/base/special/pow' );
150+
var gcartesianPower = require( '@stdlib/blas/ext/base/gcartesian-power' );
151+
152+
var N = 2;
153+
var k = 3;
154+
var x = discreteUniform( N, 1, 10, {
155+
'dtype': 'generic'
156+
});
157+
console.log( x );
158+
159+
var out = zeros( pow( N, k ) * k, 'generic' );
160+
gcartesianPower( 'row-major', N, k, x, 1, out, k );
161+
console.log( out );
162+
```
163+
164+
</section>
165+
166+
<!-- /.examples -->
167+
168+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
169+
170+
<section class="related">
171+
172+
</section>
173+
174+
<!-- /.related -->
175+
176+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
177+
178+
<section class="links">
179+
180+
[mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
181+
182+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
183+
184+
[@stdlib/array/base/accessor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/base/accessor
185+
186+
[@stdlib/blas/ext/base/dcartesian-power]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/dcartesian-power
187+
188+
[@stdlib/blas/ext/base/scartesian-power]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/blas/ext/base/scartesian-power
189+
190+
</section>
191+
192+
<!-- /.links -->
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 zeros = require( '@stdlib/array/zeros' );
26+
var isArray = require( '@stdlib/assert/is-array' );
27+
var pow = require( '@stdlib/math/base/special/pow' );
28+
var format = require( '@stdlib/string/format' );
29+
var pkg = require( './../package.json' ).name;
30+
var gcartesianPower = require( './../lib/main.js' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
var K = 2;
39+
40+
41+
// FUNCTIONS //
42+
43+
/**
44+
* Creates a benchmark function.
45+
*
46+
* @private
47+
* @param {PositiveInteger} len - array length
48+
* @returns {Function} benchmark function
49+
*/
50+
function createBenchmark( len ) {
51+
var out;
52+
var x;
53+
54+
x = uniform( len, -10.0, 10.0, options );
55+
out = zeros( pow( len, K ) * K, options.dtype );
56+
return benchmark;
57+
58+
/**
59+
* Benchmark function.
60+
*
61+
* @private
62+
* @param {Benchmark} b - benchmark instance
63+
*/
64+
function benchmark( b ) {
65+
var v;
66+
var i;
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
v = gcartesianPower( 'row-major', x.length, K, x, 1, out, K );
71+
if ( !isArray( v ) ) {
72+
b.fail( 'should return an array' );
73+
}
74+
}
75+
b.toc();
76+
if ( !isArray( v ) ) {
77+
b.fail( 'should return an array' );
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 len;
94+
var min;
95+
var max;
96+
var f;
97+
var i;
98+
99+
min = 1; // 10^min
100+
max = 3; // 10^max
101+
102+
for ( i = min; i <= max; i++ ) {
103+
len = pow( 10, i );
104+
f = createBenchmark( len );
105+
bench( format( '%s:len=%d', pkg, len ), f );
106+
}
107+
}
108+
109+
main();

0 commit comments

Comments
 (0)