Skip to content

Commit a023392

Browse files
headlessNodekgryte
andauthored
feat: add blas/ext/base/dcartesian-square
PR-URL: #10797 Co-authored-by: Athan Reines <kgryte@gmail.com> Reviewed-by: Athan Reines <kgryte@gmail.com> Closes: stdlib-js/metr-issue-tracker#172
1 parent 32f6869 commit a023392

33 files changed

Lines changed: 4488 additions & 0 deletions
Lines changed: 325 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,325 @@
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+
# dcartesianSquare
22+
23+
> Compute the Cartesian square for a double-precision floating-point strided array.
24+
25+
<section class="usage">
26+
27+
## Usage
28+
29+
```javascript
30+
var dcartesianSquare = require( '@stdlib/blas/ext/base/dcartesian-square' );
31+
```
32+
33+
#### dcartesianSquare( order, N, x, strideX, out, LDO )
34+
35+
Computes the Cartesian square for a double-precision floating-point strided array.
36+
37+
```javascript
38+
var Float64Array = require( '@stdlib/array/float64' );
39+
40+
var x = new Float64Array( [ 1.0, 2.0 ] );
41+
var out = new Float64Array( 8 );
42+
43+
dcartesianSquare( 'row-major', x.length, x, 1, out, 2 );
44+
// out => <Float64Array>[ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
45+
```
46+
47+
The function has the following parameters:
48+
49+
- **order**: storage layout. Must be either `'row-major'` or `'column-major'`.
50+
- **N**: number of indexed elements.
51+
- **x**: input [`Float64Array`][@stdlib/array/float64].
52+
- **strideX**: stride length for `x`.
53+
- **out**: output [`Float64Array`][@stdlib/array/float64].
54+
- **LDO**: stride length between successive contiguous vectors of the matrix `out` (a.k.a., leading dimension of `out`).
55+
56+
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:
57+
58+
```javascript
59+
var Float64Array = require( '@stdlib/array/float64' );
60+
61+
var x = new Float64Array( [ 1.0, 0.0, 2.0, 0.0 ] );
62+
var out = new Float64Array( 8 );
63+
64+
dcartesianSquare( 'row-major', 2, x, 2, out, 2 );
65+
// out => <Float64Array>[ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
66+
```
67+
68+
Note that indexing is relative to the first index. To introduce an offset, use [`typed array`][mdn-typed-array] views.
69+
70+
```javascript
71+
var Float64Array = require( '@stdlib/array/float64' );
72+
73+
// Initial array:
74+
var x0 = new Float64Array( [ 0.0, 1.0, 2.0, 3.0 ] );
75+
76+
// Create an offset view:
77+
var x1 = new Float64Array( x0.buffer, x0.BYTES_PER_ELEMENT*1 ); // start at 2nd element
78+
79+
// Output array:
80+
var out = new Float64Array( 8 );
81+
82+
dcartesianSquare( 'row-major', 2, x1, 1, out, 2 );
83+
// out => <Float64Array>[ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
84+
```
85+
86+
<!-- lint disable maximum-heading-length -->
87+
88+
#### dcartesianSquare.ndarray( N, x, strideX, offsetX, out, strideOut1, strideOut2, offsetOut )
89+
90+
<!-- lint enable maximum-heading-length -->
91+
92+
Computes the Cartesian square for a double-precision floating-point strided array using alternative indexing semantics.
93+
94+
```javascript
95+
var Float64Array = require( '@stdlib/array/float64' );
96+
97+
var x = new Float64Array( [ 1.0, 2.0 ] );
98+
var out = new Float64Array( 8 );
99+
100+
dcartesianSquare.ndarray( x.length, x, 1, 0, out, 2, 1, 0 );
101+
// out => <Float64Array>[ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
102+
```
103+
104+
The function has the following parameters:
105+
106+
- **N**: number of indexed elements.
107+
- **x**: input [`Float64Array`][@stdlib/array/float64].
108+
- **strideX**: stride length for `x`.
109+
- **offsetX**: starting index for `x`.
110+
- **out**: output [`Float64Array`][@stdlib/array/float64].
111+
- **strideOut1**: stride length of the first dimension of `out`.
112+
- **strideOut2**: stride length of the second dimension of `out`.
113+
- **offsetOut**: starting index for `out`.
114+
115+
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:
116+
117+
```javascript
118+
var Float64Array = require( '@stdlib/array/float64' );
119+
120+
var x = new Float64Array( [ 0.0, 0.0, 1.0, 2.0 ] );
121+
var out = new Float64Array( 8 );
122+
123+
dcartesianSquare.ndarray( 2, x, 1, 2, out, 2, 1, 0 );
124+
// out => <Float64Array>[ 1.0, 1.0, 1.0, 2.0, 2.0, 1.0, 2.0, 2.0 ]
125+
```
126+
127+
</section>
128+
129+
<!-- /.usage -->
130+
131+
<section class="notes">
132+
133+
## Notes
134+
135+
- 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.
136+
- For an input array of length `N`, the output array must contain at least `N * N * 2` indexed elements.
137+
- 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)`.
138+
- If `N <= 0`, both functions return `out` unchanged.
139+
140+
</section>
141+
142+
<!-- /.notes -->
143+
144+
<section class="examples">
145+
146+
## Examples
147+
148+
<!-- eslint no-undef: "error" -->
149+
150+
```javascript
151+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
152+
var Float64Array = require( '@stdlib/array/float64' );
153+
var dcartesianSquare = require( '@stdlib/blas/ext/base/dcartesian-square' );
154+
155+
var N = 2;
156+
var x = discreteUniform( N, 1, 10, {
157+
'dtype': 'float64'
158+
});
159+
console.log( x );
160+
161+
var out = new Float64Array( N * N * 2 );
162+
dcartesianSquare( 'row-major', N, x, 1, out, 2 );
163+
console.log( out );
164+
```
165+
166+
</section>
167+
168+
<!-- /.examples -->
169+
170+
<!-- C interface documentation. -->
171+
172+
* * *
173+
174+
<section class="c">
175+
176+
## C APIs
177+
178+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
179+
180+
<section class="intro">
181+
182+
</section>
183+
184+
<!-- /.intro -->
185+
186+
<!-- C usage documentation. -->
187+
188+
<section class="usage">
189+
190+
### Usage
191+
192+
```c
193+
#include "stdlib/blas/ext/base/dcartesiansquare.h"
194+
```
195+
196+
#### stdlib_strided_dcartesian_square( order, N, \*X, strideX, \*Out, LDO )
197+
198+
Computes the Cartesian square for a double-precision floating-point strided array.
199+
200+
```c
201+
#include "stdlib/blas/base/shared.h"
202+
203+
const double x[] = { 1.0, 2.0 };
204+
double out[ 8 ];
205+
206+
stdlib_strided_dcartesian_square( CblasRowMajor, 2, x, 1, out, 2 );
207+
```
208+
209+
The function accepts the following arguments:
210+
211+
- **order**: `[in] CBLAS_LAYOUT` storage layout.
212+
- **N**: `[in] CBLAS_INT` number of indexed elements.
213+
- **X**: `[in] double*` input array.
214+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
215+
- **Out**: `[out] double*` output array.
216+
- **LDO**: `[in] CBLAS_INT` stride length between successive contiguous vectors of the matrix `Out` (a.k.a., leading dimension of `Out`). For row-major order, must be greater than or equal to `2`. For column-major order, must be greater than or equal to `max(1,N*N)`.
217+
218+
```c
219+
void stdlib_strided_dcartesian_square( const CBLAS_LAYOUT order, const CBLAS_INT N, const double *X, const CBLAS_INT strideX, double *Out, const CBLAS_INT LDO );
220+
```
221+
222+
<!-- lint disable maximum-heading-length -->
223+
224+
#### stdlib_strided_dcartesian_square_ndarray( N, \*X, strideX, offsetX, \*Out, strideOut1, strideOut2, offsetOut )
225+
226+
<!-- lint enable maximum-heading-length -->
227+
228+
Computes the Cartesian square for a double-precision floating-point strided array using alternative indexing semantics.
229+
230+
```c
231+
const double x[] = { 1.0, 2.0 };
232+
double out[ 8 ];
233+
234+
stdlib_strided_dcartesian_square_ndarray( 2, x, 1, 0, out, 2, 1, 0 );
235+
```
236+
237+
The function accepts the following arguments:
238+
239+
- **N**: `[in] CBLAS_INT` number of indexed elements.
240+
- **X**: `[in] double*` input array.
241+
- **strideX**: `[in] CBLAS_INT` stride length for `X`.
242+
- **offsetX**: `[in] CBLAS_INT` starting index for `X`.
243+
- **Out**: `[out] double*` output array.
244+
- **strideOut1**: `[in] CBLAS_INT` stride length of the first dimension of `Out`.
245+
- **strideOut2**: `[in] CBLAS_INT` stride length of the second dimension of `Out`.
246+
- **offsetOut**: `[in] CBLAS_INT` starting index for `Out`.
247+
248+
```c
249+
void stdlib_strided_dcartesian_square_ndarray( const CBLAS_INT N, const double *X, const CBLAS_INT strideX, const CBLAS_INT offsetX, double *Out, const CBLAS_INT strideOut1, const CBLAS_INT strideOut2, const CBLAS_INT offsetOut );
250+
```
251+
252+
</section>
253+
254+
<!-- /.usage -->
255+
256+
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
257+
258+
<section class="notes">
259+
260+
</section>
261+
262+
<!-- /.notes -->
263+
264+
<!-- C API usage examples. -->
265+
266+
<section class="examples">
267+
268+
### Examples
269+
270+
```c
271+
#include "stdlib/blas/ext/base/dcartesiansquare.h"
272+
#include "stdlib/blas/base/shared.h"
273+
#include <stdio.h>
274+
275+
int main( void ) {
276+
// Create a strided input array:
277+
const double x[] = { 1.0, 2.0 };
278+
279+
// Specify the number of indexed elements:
280+
const int N = 2;
281+
282+
// Create an output array (N*N pairs, each pair has 2 elements):
283+
double out[ 8 ];
284+
285+
// Specify strides:
286+
const int strideX = 1;
287+
const int LDO = 2;
288+
289+
// Compute the Cartesian square:
290+
stdlib_strided_dcartesian_square( CblasRowMajor, N, x, strideX, out, LDO );
291+
292+
// Print the result:
293+
for ( int i = 0; i < N*N; i++ ) {
294+
printf( "out[ %i ] = ( %lf, %lf )\n", i, out[ i*2 ], out[ (i*2)+1 ] );
295+
}
296+
}
297+
```
298+
299+
</section>
300+
301+
<!-- /.examples -->
302+
303+
</section>
304+
305+
<!-- /.c -->
306+
307+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
308+
309+
<section class="related">
310+
311+
</section>
312+
313+
<!-- /.related -->
314+
315+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
316+
317+
<section class="links">
318+
319+
[@stdlib/array/float64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/float64
320+
321+
[mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
322+
323+
</section>
324+
325+
<!-- /.links -->

0 commit comments

Comments
 (0)