|
| 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 | +# shape |
| 22 | + |
| 23 | +> Return the shape of a provided [ndarray][@stdlib/ndarray/base/ctor]. |
| 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 shape = require( '@stdlib/ndarray/base/shape' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### shape( x, copy ) |
| 44 | + |
| 45 | +Returns the shape of a provided [ndarray][@stdlib/ndarray/base/ctor]. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 49 | + |
| 50 | +var x = zeros( [ 3, 2, 3 ] ); |
| 51 | +// returns <ndarray> |
| 52 | + |
| 53 | +var sh = shape( x, false ); |
| 54 | +// returns [ 3, 2, 3 ] |
| 55 | +``` |
| 56 | + |
| 57 | +When `copy` is `false`, changes to the returned shape array may mutate the input [ndarray][@stdlib/ndarray/base/ctor] shape. If there is a chance that the returned shape will be mutated (either directly or by downstream consumers), set `copy` to `true` to prevent unintended side effects. |
| 58 | + |
| 59 | +```javascript |
| 60 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 61 | + |
| 62 | +var x = zeros( [ 3, 2, 3 ] ); |
| 63 | +// returns <ndarray> |
| 64 | + |
| 65 | +var sh = shape( x, true ); |
| 66 | +// returns [ 3, 2, 3 ] |
| 67 | + |
| 68 | +var bool = ( x.shape === sh ); |
| 69 | +// returns false |
| 70 | +``` |
| 71 | + |
| 72 | +</section> |
| 73 | + |
| 74 | +<!-- /.usage --> |
| 75 | + |
| 76 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 77 | + |
| 78 | +<section class="notes"> |
| 79 | + |
| 80 | +</section> |
| 81 | + |
| 82 | +<!-- /.notes --> |
| 83 | + |
| 84 | +<!-- Package usage examples. --> |
| 85 | + |
| 86 | +<section class="examples"> |
| 87 | + |
| 88 | +## Examples |
| 89 | + |
| 90 | +<!-- eslint no-undef: "error" --> |
| 91 | + |
| 92 | +<!-- eslint-disable new-cap --> |
| 93 | + |
| 94 | +```javascript |
| 95 | +var zeros = require( '@stdlib/ndarray/zeros' ); |
| 96 | +var slice = require( '@stdlib/ndarray/slice' ); |
| 97 | +var E = require( '@stdlib/slice/multi' ); |
| 98 | +var S = require( '@stdlib/slice/ctor' ); |
| 99 | +var shape = require( '@stdlib/ndarray/base/shape' ); |
| 100 | + |
| 101 | +// Create an array: |
| 102 | +var x = zeros( [ 10, 10, 10, 10 ] ); |
| 103 | +// returns <ndarray> |
| 104 | + |
| 105 | +// Define some slices: |
| 106 | +var slices = [ |
| 107 | + // :,:,:,: |
| 108 | + E( null, null, null, null ), |
| 109 | + |
| 110 | + // 5:10,4,2:4,::-1 |
| 111 | + E( S( 5, 10 ), 4, S( 2, 4 ), S( null, null, -1 ) ), |
| 112 | + |
| 113 | + // :,:,2,: |
| 114 | + E( null, null, 2, null ), |
| 115 | + |
| 116 | + // 1,2,3,: |
| 117 | + E( 1, 2, 3, null ), |
| 118 | + |
| 119 | + // 1,3,::2,4::2 |
| 120 | + E( 1, 3, S( null, null, 2 ), S( 4, null, 2 ) ) |
| 121 | +]; |
| 122 | + |
| 123 | +// Determine the shape of each slice... |
| 124 | +var s; |
| 125 | +var i; |
| 126 | +for ( i = 0; i < slices.length; i++ ) { |
| 127 | + s = slice( x, slices[ i ] ); |
| 128 | + console.log( 'slice(%s) => %s', x.shape.join( 'x' ), shape( s, false ).join( 'x' ) ); |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +</section> |
| 133 | + |
| 134 | +<!-- /.examples --> |
| 135 | + |
| 136 | +<!-- 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. --> |
| 137 | + |
| 138 | +<section class="references"> |
| 139 | + |
| 140 | +</section> |
| 141 | + |
| 142 | +<!-- /.references --> |
| 143 | + |
| 144 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 145 | + |
| 146 | +<section class="related"> |
| 147 | + |
| 148 | +</section> |
| 149 | + |
| 150 | +<!-- /.related --> |
| 151 | + |
| 152 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 153 | + |
| 154 | +<section class="links"> |
| 155 | + |
| 156 | +[@stdlib/ndarray/base/ctor]: https://github.com/stdlib-js/stdlib |
| 157 | + |
| 158 | +</section> |
| 159 | + |
| 160 | +<!-- /.links --> |
0 commit comments