|
| 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 | +# zip2views1d |
| 22 | + |
| 23 | +> Zip one or more one-dimensional ndarrays to an array of composite views. |
| 24 | +
|
| 25 | +<section class="usage"> |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +```javascript |
| 30 | +var zip2views1d = require( '@stdlib/ndarray/base/zip2views1d' ); |
| 31 | +``` |
| 32 | + |
| 33 | +#### zip2views1d( arrays, labels ) |
| 34 | + |
| 35 | +Zips one or more one-dimensional ndarrays to an array of composite views. |
| 36 | + |
| 37 | +```javascript |
| 38 | +var array2ndarray = require( '@stdlib/ndarray/base/from-array' ); |
| 39 | + |
| 40 | +var x = array2ndarray( [ 1, 2, 3 ], 'row-major' ); |
| 41 | +var y = array2ndarray( [ 'a', 'b', 'c' ], 'row-major' ); |
| 42 | + |
| 43 | +var labels = [ 'x', 'y' ]; |
| 44 | + |
| 45 | +var z = zip2views1d( [ x, y ], labels ); |
| 46 | +// returns [ <Object>, <Object>, <Object> ] |
| 47 | + |
| 48 | +var v0 = z[ 0 ].toJSON(); |
| 49 | +// returns { 'x': 1, 'y': 'a' } |
| 50 | + |
| 51 | +var v1 = z[ 1 ].toJSON(); |
| 52 | +// returns { 'x': 2, 'y': 'b' } |
| 53 | + |
| 54 | +var v2 = z[ 2 ].toJSON(); |
| 55 | +// returns { 'x': 3, 'y': 'c' } |
| 56 | + |
| 57 | +// Mutate one of the input arrays: |
| 58 | +x.set( 0, 5 ); |
| 59 | + |
| 60 | +v0 = z[ 0 ].toJSON(); |
| 61 | +// returns { 'x': 5, 'y': 'a' } |
| 62 | + |
| 63 | +// Set a view property: |
| 64 | +z[ 1 ].y = 'beep'; |
| 65 | + |
| 66 | +v1 = z[ 1 ].toJSON(); |
| 67 | +// returns { 'x': 2, 'y': 'beep' } |
| 68 | + |
| 69 | +var v = y.get( 1 ); |
| 70 | +// returns 'beep' |
| 71 | +``` |
| 72 | + |
| 73 | +The function supports the following parameters: |
| 74 | + |
| 75 | +- **arrays**: list of ndarrays to zip. |
| 76 | +- **labels**: list of labels. |
| 77 | + |
| 78 | +Each element in the returned array is a class instance having prototype properties corresponding to the list of labels. As demonstrated in the example above, to convert an element to a regular object, invoke an element's `toJSON` method. Note, however, that the object returned by an element's `toJSON` method no longer shares the same memory as the provided input ndarrays. |
| 79 | + |
| 80 | +</section> |
| 81 | + |
| 82 | +<!-- /.usage --> |
| 83 | + |
| 84 | +<section class="notes"> |
| 85 | + |
| 86 | +- The function assumes that the list of ndarrays to be zipped all have the same length. |
| 87 | +- The list of provided labels should equal the number of ndarrays to be zipped. |
| 88 | +- Each view in the returned array shares the same memory as the corresponding elements in the input ndarrays. Accordingly, mutation of either an input ndarray or a view will mutate the other. |
| 89 | + |
| 90 | +</section> |
| 91 | + |
| 92 | +<!-- /.notes --> |
| 93 | + |
| 94 | +<section class="examples"> |
| 95 | + |
| 96 | +## Examples |
| 97 | + |
| 98 | +<!-- eslint no-undef: "error" --> |
| 99 | + |
| 100 | +```javascript |
| 101 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 102 | +var zeroTo = require( '@stdlib/array/base/zero-to' ); |
| 103 | +var array2ndarray = require( '@stdlib/ndarray/base/from-array' ); |
| 104 | +var zip2views1d = require( '@stdlib/ndarray/base/zip2views1d' ); |
| 105 | + |
| 106 | +var x = array2ndarray( zeroTo( 10 ), 'row-major' ); |
| 107 | +var y = array2ndarray( discreteUniform( x.length, -100, 100 ), 'row-major' ); |
| 108 | + |
| 109 | +var labels = [ 'x', 'y' ]; |
| 110 | + |
| 111 | +var out = zip2views1d( [ x, y ], labels ); |
| 112 | +// returns [...] |
| 113 | +``` |
| 114 | + |
| 115 | +</section> |
| 116 | + |
| 117 | +<!-- /.examples --> |
| 118 | + |
| 119 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 120 | + |
| 121 | +<section class="related"> |
| 122 | + |
| 123 | +</section> |
| 124 | + |
| 125 | +<!-- /.related --> |
| 126 | + |
| 127 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 128 | + |
| 129 | +<section class="links"> |
| 130 | + |
| 131 | +</section> |
| 132 | + |
| 133 | +<!-- /.links --> |
0 commit comments