|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# flattenShapeFrom |
| 22 | + |
| 23 | +> Flatten a shape starting from a specified dimension. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### flattenShapeFrom( shape, dim ) |
| 44 | + |
| 45 | +Flattens a shape starting from a specified dimension. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var sh = flattenShapeFrom( [ 3, 3, 2 ], 1 ); |
| 49 | +// returns [ 3, 6 ] |
| 50 | +``` |
| 51 | + |
| 52 | +The function accepts the following parameters: |
| 53 | + |
| 54 | +- **shape**: array shape. |
| 55 | +- **dim**: dimension to start flattening from. |
| 56 | + |
| 57 | +#### flattenShapeFrom.assign( shape, dim, out ) |
| 58 | + |
| 59 | +Flattens a shape starting from a specified dimension and assigns results to a provided output array. |
| 60 | + |
| 61 | +```javascript |
| 62 | +var sh = [ 0, 0 ]; |
| 63 | + |
| 64 | +var out = flattenShapeFrom.assign( [ 3, 3, 2 ], 1, sh ); |
| 65 | +// returns [ 3, 6 ] |
| 66 | + |
| 67 | +var bool = ( sh === out ); |
| 68 | +// returns true |
| 69 | +``` |
| 70 | + |
| 71 | +The function accepts the following parameters: |
| 72 | + |
| 73 | +- **shape**: array shape. |
| 74 | +- **dim**: dimension to start flattening from. |
| 75 | +- **out**: output array. |
| 76 | + |
| 77 | +</section> |
| 78 | + |
| 79 | +<!-- /.usage --> |
| 80 | + |
| 81 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 82 | + |
| 83 | +<section class="notes"> |
| 84 | + |
| 85 | +</section> |
| 86 | + |
| 87 | +<!-- /.notes --> |
| 88 | + |
| 89 | +<!-- Package usage examples. --> |
| 90 | + |
| 91 | +<section class="examples"> |
| 92 | + |
| 93 | +## Examples |
| 94 | + |
| 95 | +<!-- eslint no-undef: "error" --> |
| 96 | + |
| 97 | +```javascript |
| 98 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 99 | +var zip = require( '@stdlib/array/base/zip' ); |
| 100 | +var logEachMap = require( '@stdlib/console/log-each-map' ); |
| 101 | +var flattenShapeFrom = require( '@stdlib/ndarray/base/flatten-shape-from' ); |
| 102 | + |
| 103 | +var opts = { |
| 104 | + 'dtype': 'int32' |
| 105 | +}; |
| 106 | +var d1 = discreteUniform( 100, 1, 10, opts ); |
| 107 | +var d2 = discreteUniform( d1.length, 1, 10, opts ); |
| 108 | +var d3 = discreteUniform( d1.length, 1, 10, opts ); |
| 109 | +var d4 = discreteUniform( d1.length, 1, 10, opts ); |
| 110 | + |
| 111 | +var dims = discreteUniform( d1.length, 0, 3, opts ); |
| 112 | +var shapes = zip( [ d1, d2, d3, d4 ] ); |
| 113 | + |
| 114 | +logEachMap( 'shape: (%s). dim: %d. flattened: (%s).', shapes, dims, flattenShapeFrom ); |
| 115 | +``` |
| 116 | + |
| 117 | +</section> |
| 118 | + |
| 119 | +<!-- /.examples --> |
| 120 | + |
| 121 | +<!-- C interface documentation. --> |
| 122 | + |
| 123 | +* * * |
| 124 | + |
| 125 | +<section class="c"> |
| 126 | + |
| 127 | +## C APIs |
| 128 | + |
| 129 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 130 | + |
| 131 | +<section class="intro"> |
| 132 | + |
| 133 | +</section> |
| 134 | + |
| 135 | +<!-- /.intro --> |
| 136 | + |
| 137 | +<!-- C usage documentation. --> |
| 138 | + |
| 139 | +<section class="usage"> |
| 140 | + |
| 141 | +### Usage |
| 142 | + |
| 143 | +```c |
| 144 | +#include "stdlib/ndarray/base/flatten_shape_from.h" |
| 145 | +``` |
| 146 | + |
| 147 | +#### stdlib_ndarray_flatten_shape_from( ndims, \*shape, dim, \*out ) |
| 148 | + |
| 149 | +Flattens a shape starting from a specified dimension. |
| 150 | + |
| 151 | +```c |
| 152 | +const int64_t ndims = 3; |
| 153 | +const int64_t shape[] = { 2, 3, 10 }; |
| 154 | +int64_t out[ 2 ]; |
| 155 | + |
| 156 | +stdlib_ndarray_flatten_shape( ndims, shape, 1, out ); |
| 157 | +``` |
| 158 | +
|
| 159 | +The function accepts the following arguments: |
| 160 | +
|
| 161 | +- **ndims**: `[in] int64_t` number of dimensions. |
| 162 | +- **shape**: `[in] int64_t*` array shape (dimensions). |
| 163 | +- **dim**: `[in] int64_t` dimension to start flattening from. |
| 164 | +- **out**: `[out] int64_t*` output array. |
| 165 | +
|
| 166 | +```c |
| 167 | +int8_t stdlib_ndarray_flatten_shape_from( const int64_t ndims, const int64_t *shape, const int64_t dim, int64_t *out ); |
| 168 | +``` |
| 169 | + |
| 170 | +</section> |
| 171 | + |
| 172 | +<!-- /.usage --> |
| 173 | + |
| 174 | +<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 175 | + |
| 176 | +<section class="notes"> |
| 177 | + |
| 178 | +</section> |
| 179 | + |
| 180 | +<!-- /.notes --> |
| 181 | + |
| 182 | +<!-- C API usage examples. --> |
| 183 | + |
| 184 | +<section class="examples"> |
| 185 | + |
| 186 | +### Examples |
| 187 | + |
| 188 | +```c |
| 189 | +#include "stdlib/ndarray/base/flatten_shape_from.h" |
| 190 | +#include <stdio.h> |
| 191 | +#include <inttypes.h> |
| 192 | + |
| 193 | +int main( void ) { |
| 194 | + const int64_t shape[] = { 2, 3, 4, 10 }; |
| 195 | + const int64_t ndims = 4; |
| 196 | + const int64_t dim = 2; |
| 197 | + int64_t out[ 3 ]; |
| 198 | + |
| 199 | + stdlib_ndarray_flatten_shape_from( ndims, shape, dim, out ); |
| 200 | + |
| 201 | + int i; |
| 202 | + printf( "shape = ( " ); |
| 203 | + for ( i = 0; i < ndims-dim+1; i++ ) { |
| 204 | + printf( "%"PRId64"", out[ i ] ); |
| 205 | + if ( i < ndims-dim ) { |
| 206 | + printf( ", " ); |
| 207 | + } |
| 208 | + } |
| 209 | + printf( " )\n" ); |
| 210 | +} |
| 211 | +``` |
| 212 | +
|
| 213 | +</section> |
| 214 | +
|
| 215 | +<!-- /.examples --> |
| 216 | +
|
| 217 | +</section> |
| 218 | +
|
| 219 | +<!-- /.c --> |
| 220 | +
|
| 221 | +<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 222 | +
|
| 223 | +<section class="references"> |
| 224 | +
|
| 225 | +</section> |
| 226 | +
|
| 227 | +<!-- /.references --> |
| 228 | +
|
| 229 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 230 | +
|
| 231 | +<section class="related"> |
| 232 | +
|
| 233 | +</section> |
| 234 | +
|
| 235 | +<!-- /.related --> |
| 236 | +
|
| 237 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 238 | +
|
| 239 | +<section class="links"> |
| 240 | +
|
| 241 | +</section> |
| 242 | +
|
| 243 | +<!-- /.links --> |
0 commit comments