Skip to content

Commit 3eb7c44

Browse files
committed
feat: add ndarray/base/slice-dimension-to
1 parent 4ee7e66 commit 3eb7c44

File tree

10 files changed

+2498
-0
lines changed

10 files changed

+2498
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
# sliceDimensionTo
22+
23+
> Return a truncated view of an input ndarray along 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 sliceDimensionTo = require( '@stdlib/ndarray/base/slice-dimension-to' );
41+
```
42+
43+
#### sliceDimensionTo( x, dim, stop, strict, writable )
44+
45+
Returns a truncated view of an input ndarray along a specified dimension.
46+
47+
```javascript
48+
var ndarray = require( '@stdlib/ndarray/ctor' );
49+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
50+
51+
var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ];
52+
var shape = [ 3, 2 ];
53+
var strides = [ 2, 1 ];
54+
var offset = 0;
55+
56+
var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' );
57+
// returns <ndarray>
58+
59+
var sh = x.shape;
60+
// returns [ 3, 2 ]
61+
62+
var arr = ndarray2array( x );
63+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ], [ 5.0, 6.0 ] ]
64+
65+
var y = sliceDimensionTo( x, 0, 2, false, false );
66+
// returns <ndarray>
67+
68+
sh = y.shape;
69+
// returns [ 2, 2 ]
70+
71+
arr = ndarray2array( y );
72+
// returns [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ]
73+
```
74+
75+
The function accepts the following arguments:
76+
77+
- **x**: input ndarray.
78+
- **dim**: index of dimension along which to slice. If provided an integer less than zero, the dimension index is resolved relative to the last dimension, with the last dimension corresponding to the value `-1`.
79+
- **stop**: ending index (exclusive). If provided an integer less than zero, the corresponding element along the specified dimension is resolved relative to the last element along that dimension. For negative integers, the last element corresponds to the value `-1`.
80+
- **strict**: boolean indicating whether to enforce strict bounds checking.
81+
- **writable**: boolean indicating whether a returned ndarray should be writable.
82+
83+
</section>
84+
85+
<!-- /.usage -->
86+
87+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
88+
89+
<section class="notes">
90+
91+
## Notes
92+
93+
- The `writable` parameter **only** applies to ndarray constructors supporting **read-only** instances.
94+
95+
</section>
96+
97+
<!-- /.notes -->
98+
99+
<!-- Package usage examples. -->
100+
101+
<section class="examples">
102+
103+
## Examples
104+
105+
<!-- eslint no-undef: "error" -->
106+
107+
```javascript
108+
var array = require( '@stdlib/ndarray/array' );
109+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
110+
var zeroTo = require( '@stdlib/array/base/zero-to' );
111+
var sliceDimensionTo = require( '@stdlib/ndarray/base/slice-dimension-to' );
112+
113+
// Create a linear ndarray buffer:
114+
var buf = zeroTo( 27 );
115+
116+
// Create an ndarray:
117+
var x = array( buf, {
118+
'shape': [ 3, 3, 3 ]
119+
});
120+
121+
// Get the first two rows of each matrix:
122+
var y1 = sliceDimensionTo( x, 1, 2, false, false );
123+
// returns <ndarray>
124+
125+
var a1 = ndarray2array( y1 );
126+
// returns [ [ [ 0, 1, 2 ], [ 3, 4, 5 ] ], [ [ 9, 10, 11 ], [ 12, 13, 14 ] ], [ [ 18, 19, 20 ], [ 21, 22, 23 ] ] ]
127+
128+
// Get the first columns of each matrix:
129+
var y2 = sliceDimensionTo( x, 2, 2, false, false );
130+
// returns <ndarray>
131+
132+
var a2 = ndarray2array( y2 );
133+
// returns [ [ [ 0, 1 ], [ 3, 4 ], [ 6, 7 ] ], [ [ 9, 10 ], [ 12, 13 ], [ 15, 16 ] ], [ [ 18, 19 ], [ 21, 22 ], [ 24, 25 ] ] ]
134+
135+
// Get the first two matrices:
136+
var y3 = sliceDimensionTo( x, 0, 2, false, false );
137+
// returns <ndarray>
138+
139+
var a3 = ndarray2array( y3 );
140+
// returns [ [ [ 0, 1, 2 ], [ 3, 4, 5 ], [ 6, 7, 8 ] ], [ [ 9, 10, 11 ], [ 12, 13, 14 ], [ 15, 16, 17 ] ] ]
141+
```
142+
143+
</section>
144+
145+
<!-- /.examples -->
146+
147+
<!-- 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. -->
148+
149+
<section class="references">
150+
151+
</section>
152+
153+
<!-- /.references -->
154+
155+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
156+
157+
<section class="related">
158+
159+
</section>
160+
161+
<!-- /.related -->
162+
163+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
164+
165+
<section class="links">
166+
167+
</section>
168+
169+
<!-- /.links -->

0 commit comments

Comments
 (0)