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