|
| 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 | +# join |
| 22 | + |
| 23 | +> Return an [ndarray][@stdlib/ndarray/ctor] created by joining elements using a separator along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var join = require( '@stdlib/blas/ext/join' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### join( x\[, options] ) |
| 34 | + |
| 35 | +Returns an [ndarray][@stdlib/ndarray/ctor] created by joining elements using a separator along one or more [ndarray][@stdlib/ndarray/ctor] dimensions. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var array = require( '@stdlib/ndarray/array' ); |
| 39 | + |
| 40 | +// Create an input ndarray: |
| 41 | +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 42 | +// returns <ndarray> |
| 43 | + |
| 44 | +// Perform operation: |
| 45 | +var out = join( x ); |
| 46 | +// returns <ndarray>[ '1,2,3,4,5,6' ] |
| 47 | +``` |
| 48 | + |
| 49 | +The function has the following parameters: |
| 50 | + |
| 51 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 52 | +- **options**: function options (_optional_). |
| 53 | + |
| 54 | +The function accepts the following options: |
| 55 | + |
| 56 | +- **sep**: separator. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] separator value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] separator value must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. Default: `,`. |
| 57 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 58 | +- **keepdims**: boolean indicating whether the reduced dimensions should be included in the returned [ndarray][@stdlib/ndarray/ctor] as singleton dimensions. Default: `false`. |
| 59 | + |
| 60 | +By default, the function joins [ndarray][@stdlib/ndarray/ctor] elements by using `,` as a separator. To perform the operation with a different separator, provide a `sep` option. |
| 61 | + |
| 62 | +```javascript |
| 63 | +var array = require( '@stdlib/ndarray/array' ); |
| 64 | + |
| 65 | +var x = array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] ); |
| 66 | + |
| 67 | +var out = join( x, { |
| 68 | + 'sep': '|' |
| 69 | +}); |
| 70 | +// returns <ndarray>[ '1|2|3|4|5|6' ] |
| 71 | +``` |
| 72 | + |
| 73 | +By default, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. To perform the operation over specific dimensions, provide a `dims` option. |
| 74 | + |
| 75 | +```javascript |
| 76 | +var array = require( '@stdlib/ndarray/array' ); |
| 77 | + |
| 78 | +var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); |
| 79 | + |
| 80 | +var out = join( x, { |
| 81 | + 'dims': [ 0 ] |
| 82 | +}); |
| 83 | +// returns <ndarray>[ '1,3', '2,4' ] |
| 84 | + |
| 85 | +out = join( x, { |
| 86 | + 'dims': [ 1 ] |
| 87 | +}); |
| 88 | +// returns <ndarray>[ '1,2', '3,4' ] |
| 89 | + |
| 90 | +out = join( x, { |
| 91 | + 'dims': [ 0, 1 ] |
| 92 | +}); |
| 93 | +// returns <ndarray>[ '1,2,3,4' ] |
| 94 | +``` |
| 95 | + |
| 96 | +By default, the function excludes reduced dimensions from the output [ndarray][@stdlib/ndarray/ctor]. To include the reduced dimensions as singleton dimensions, set the `keepdims` option to `true`. |
| 97 | + |
| 98 | +```javascript |
| 99 | +var array = require( '@stdlib/ndarray/array' ); |
| 100 | + |
| 101 | +var x = array( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); |
| 102 | + |
| 103 | +var opts = { |
| 104 | + 'dims': [ 0 ], |
| 105 | + 'keepdims': true |
| 106 | +}; |
| 107 | + |
| 108 | +var out = join( x, opts ); |
| 109 | +// returns <ndarray>[ [ '1,3', '2,4' ] ] |
| 110 | +``` |
| 111 | + |
| 112 | +#### join.assign( x, out\[, options] ) |
| 113 | + |
| 114 | +Joins elements of an input [ndarray][@stdlib/ndarray/ctor] using a separator along one or more [ndarray][@stdlib/ndarray/ctor] dimensions and assigns results to a provided output [ndarray][@stdlib/ndarray/ctor]. |
| 115 | + |
| 116 | +```javascript |
| 117 | +var array = require( '@stdlib/ndarray/array' ); |
| 118 | +var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' ); |
| 119 | + |
| 120 | +var x = array( [ 1.0, 2.0, 3.0, 4.0 ] ); |
| 121 | +var y = scalar2ndarray( '', { |
| 122 | + 'dtype': 'generic' |
| 123 | +}); |
| 124 | + |
| 125 | +var out = join.assign( x, y ); |
| 126 | +// returns <ndarray>[ '1,2,3,4' ] |
| 127 | + |
| 128 | +var bool = ( out === y ); |
| 129 | +// returns true |
| 130 | +``` |
| 131 | + |
| 132 | +The method has the following parameters: |
| 133 | + |
| 134 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 135 | +- **out**: output [ndarray][@stdlib/ndarray/ctor]. |
| 136 | +- **options**: function options (_optional_). |
| 137 | + |
| 138 | +The method accepts the following options: |
| 139 | + |
| 140 | +- **sep**: separator. May be either a scalar value or an [ndarray][@stdlib/ndarray/ctor] having a "generic" [data type][@stdlib/ndarray/dtypes]. If provided an [ndarray][@stdlib/ndarray/ctor], the value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the complement of the shape defined by `options.dims`. For example, given the input shape `[2, 3, 4]` and `options.dims=[0]`, an [ndarray][@stdlib/ndarray/ctor] separator value must have a shape which is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with the shape `[3, 4]`. Similarly, when performing the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor], an [ndarray][@stdlib/ndarray/ctor] separator value must be a zero-dimensional [ndarray][@stdlib/ndarray/ctor]. Default: `,`. |
| 141 | +- **dims**: list of dimensions over which to perform operation. If not provided, the function performs the operation over all elements in a provided input [ndarray][@stdlib/ndarray/ctor]. |
| 142 | + |
| 143 | +</section> |
| 144 | + |
| 145 | +<!-- /.usage --> |
| 146 | + |
| 147 | +<section class="notes"> |
| 148 | + |
| 149 | +## Notes |
| 150 | + |
| 151 | +- Setting the `keepdims` option to `true` can be useful when wanting to ensure that the output [ndarray][@stdlib/ndarray/ctor] is [broadcast-compatible][@stdlib/ndarray/base/broadcast-shapes] with ndarrays having the same shape as the input [ndarray][@stdlib/ndarray/ctor]. |
| 152 | + |
| 153 | +</section> |
| 154 | + |
| 155 | +<!-- /.notes --> |
| 156 | + |
| 157 | +<section class="examples"> |
| 158 | + |
| 159 | +## Examples |
| 160 | + |
| 161 | +<!-- eslint no-undef: "error" --> |
| 162 | + |
| 163 | +```javascript |
| 164 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 165 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 166 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 167 | +var join = require( '@stdlib/blas/ext/join' ); |
| 168 | + |
| 169 | +// Generate an array of random numbers: |
| 170 | +var xbuf = discreteUniform( 10, 0, 20, { |
| 171 | + 'dtype': 'float64' |
| 172 | +}); |
| 173 | + |
| 174 | +// Wrap in an ndarray: |
| 175 | +var x = new ndarray( 'float64', xbuf, [ 5, 2 ], [ 2, 1 ], 0, 'row-major' ); |
| 176 | +console.log( ndarray2array( x ) ); |
| 177 | + |
| 178 | +// Perform operation: |
| 179 | +var out = join( x, { |
| 180 | + 'dims': [ -1 ] |
| 181 | +}); |
| 182 | + |
| 183 | +// Print the results: |
| 184 | +console.log( ndarray2array( out ) ); |
| 185 | +``` |
| 186 | + |
| 187 | +</section> |
| 188 | + |
| 189 | +<!-- /.examples --> |
| 190 | + |
| 191 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 192 | + |
| 193 | +<section class="related"> |
| 194 | + |
| 195 | +</section> |
| 196 | + |
| 197 | +<!-- /.related --> |
| 198 | + |
| 199 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 200 | + |
| 201 | +<section class="links"> |
| 202 | + |
| 203 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 204 | + |
| 205 | +[@stdlib/ndarray/dtypes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/dtypes |
| 206 | + |
| 207 | +[@stdlib/ndarray/base/broadcast-shapes]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/base/broadcast-shapes |
| 208 | + |
| 209 | +</section> |
| 210 | + |
| 211 | +<!-- /.links --> |
0 commit comments