|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2023 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 | +# slice |
| 22 | + |
| 23 | +> Return a read-only view of an input ndarray. |
| 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 slice = require( '@stdlib/ndarray/base/slice' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### slice( x, slice, strict ) |
| 44 | + |
| 45 | +Returns a **read-only** view of an input ndarray. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var Slice = require( '@stdlib/slice/ctor' ); |
| 49 | +var MultiSlice = require( '@stdlib/slice/multi' ); |
| 50 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 51 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 52 | + |
| 53 | +var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ]; |
| 54 | +var shape = [ 3, 2 ]; |
| 55 | +var strides = [ 2, 1 ]; |
| 56 | +var offset = 0; |
| 57 | + |
| 58 | +var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); |
| 59 | +// returns <ndarray> |
| 60 | + |
| 61 | +var sh = x.shape; |
| 62 | +// returns [ 3, 2 ] |
| 63 | + |
| 64 | +var arr = ndarray2array( x ); |
| 65 | +// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ] |
| 66 | + |
| 67 | +var s0 = new Slice( null, null, -2 ); |
| 68 | +var s1 = new Slice( null, null, -1 ); |
| 69 | +var s = new MultiSlice( s0, s1 ); |
| 70 | +// returns <MultiSlice> |
| 71 | + |
| 72 | +var y = slice( x, s, false ); |
| 73 | +// returns <ndarray> |
| 74 | + |
| 75 | +sh = y.shape; |
| 76 | +// returns [ 2, 2 ] |
| 77 | + |
| 78 | +arr = ndarray2array( y ); |
| 79 | +// returns [ [ 6.0, 5.0 ], [ 2.0, 1.0 ] ] |
| 80 | +``` |
| 81 | + |
| 82 | +The function accepts the following arguments: |
| 83 | + |
| 84 | +- **x**: input ndarray. |
| 85 | +- **slice**: a [`MultiSlice`][@stdlib/slice/multi] instance. |
| 86 | +- **strict**: boolean indicating whether to enforce strict bounds checking. |
| 87 | + |
| 88 | +</section> |
| 89 | + |
| 90 | +<!-- /.usage --> |
| 91 | + |
| 92 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 93 | + |
| 94 | +<section class="notes"> |
| 95 | + |
| 96 | +</section> |
| 97 | + |
| 98 | +<!-- /.notes --> |
| 99 | + |
| 100 | +<!-- Package usage examples. --> |
| 101 | + |
| 102 | +<section class="examples"> |
| 103 | + |
| 104 | +## Examples |
| 105 | + |
| 106 | +<!-- eslint no-undef: "error" --> |
| 107 | + |
| 108 | +<!-- eslint-disable new-cap --> |
| 109 | + |
| 110 | +```javascript |
| 111 | +var S = require( '@stdlib/slice/ctor' ); |
| 112 | +var E = require( '@stdlib/slice/multi' ); |
| 113 | +var array = require( '@stdlib/ndarray/array' ); |
| 114 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 115 | +var zeroTo = require( '@stdlib/array/base/zero-to' ); |
| 116 | +var slice = require( '@stdlib/ndarray/base/slice' ); |
| 117 | + |
| 118 | +// Alias `null` to allow for more compact indexing expressions: |
| 119 | +var _ = null; |
| 120 | + |
| 121 | +// Create a linear ndarray buffer: |
| 122 | +var buf = zeroTo( 27 ); |
| 123 | + |
| 124 | +// Create an ndarray: |
| 125 | +var x = array( buf, { |
| 126 | + 'shape': [ 3, 3, 3 ] |
| 127 | +}); |
| 128 | + |
| 129 | +// Get each matrix... |
| 130 | +var s1 = E( 0, _, _ ); |
| 131 | +var y1 = slice( x, s1, false ); |
| 132 | +// returns <ndarray> |
| 133 | + |
| 134 | +var a1 = ndarray2array( y1 ); |
| 135 | +// returns [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ] ] |
| 136 | + |
| 137 | +var s2 = E( 1, _, _ ); |
| 138 | +var y2 = slice( x, s2, false ); |
| 139 | +// returns <ndarray> |
| 140 | + |
| 141 | +var a2 = ndarray2array( y2 ); |
| 142 | +// returns [ [ 9, 10, 11 ], [ 12, 13, 14 ], [ 15, 16, 17 ] ] |
| 143 | + |
| 144 | +var s3 = E( 2, _, _ ); |
| 145 | +var y3 = slice( x, s3, false ); |
| 146 | +// returns <ndarray> |
| 147 | + |
| 148 | +var a3 = ndarray2array( y3 ); |
| 149 | +// returns [ [ 18, 19, 20 ], [ 21, 22, 23 ], [ 24, 25, 26 ] ] |
| 150 | + |
| 151 | +// Reverse all elements: |
| 152 | +var s = S( _, _, -1 ); |
| 153 | +var s4 = E( s, s, s ); |
| 154 | +var y4 = slice( x, s4, false ); |
| 155 | +// returns <ndarray> |
| 156 | + |
| 157 | +var a4 = ndarray2array( y4 ); |
| 158 | +// returns [...] |
| 159 | + |
| 160 | +// Get the second rows from each matrix: |
| 161 | +var s5 = E( _, 1, _ ); |
| 162 | +var y5 = slice( x, s5, false ); |
| 163 | +// returns <ndarray> |
| 164 | + |
| 165 | +var a5 = ndarray2array( y5 ); |
| 166 | +// returns [ [ 3, 4, 5 ], [ 12, 13, 14 ], [ 21, 22, 23 ] ] |
| 167 | + |
| 168 | +// Get the second columns from each matrix: |
| 169 | +var s6 = E( _, _, 1 ); |
| 170 | +var y6 = slice( x, s6, false ); |
| 171 | +// returns <ndarray> |
| 172 | + |
| 173 | +var a6 = ndarray2array( y6 ); |
| 174 | +// returns [ [ 1, 4, 7 ], [ 10, 13, 16 ], [ 19, 22, 25 ] ] |
| 175 | +``` |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.examples --> |
| 180 | + |
| 181 | +<!-- 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. --> |
| 182 | + |
| 183 | +<section class="references"> |
| 184 | + |
| 185 | +</section> |
| 186 | + |
| 187 | +<!-- /.references --> |
| 188 | + |
| 189 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 190 | + |
| 191 | +<section class="related"> |
| 192 | + |
| 193 | +</section> |
| 194 | + |
| 195 | +<!-- /.related --> |
| 196 | + |
| 197 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 198 | + |
| 199 | +<section class="links"> |
| 200 | + |
| 201 | +[@stdlib/slice/multi]: https://github.com/stdlib-js/stdlib |
| 202 | + |
| 203 | +</section> |
| 204 | + |
| 205 | +<!-- /.links --> |
0 commit comments