Skip to content

Commit 0c3e5bd

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/gcartesian-square
PR-URL: #11828 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#491
1 parent 50ffb8e commit 0c3e5bd

15 files changed

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