|
| 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 --> |
0 commit comments