Skip to content

Commit 3cfc9ba

Browse files
committed
Auto-generated commit
1 parent 93afd84 commit 3cfc9ba

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+16954
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
### Features
1212

13+
- [`c6e2443`](https://github.com/stdlib-js/stdlib/commit/c6e24436d222d609c3ea153fd81f31e1d16451bc) - add `ndarray/base/ternary` [(#9566)](https://github.com/stdlib-js/stdlib/pull/9566)
1314
- [`1aff763`](https://github.com/stdlib-js/stdlib/commit/1aff763c61863b7d737a699db89729d2bba0e1bc) - add `ndarray/spread-dimensions` [(#9424)](https://github.com/stdlib-js/stdlib/pull/9424)
1415
- [`3c567b6`](https://github.com/stdlib-js/stdlib/commit/3c567b634cf5e5fa5e31b64f94206db5b4a88133) - update `ndarray/base` TypeScript declarations (#9640) [(#9640)](https://github.com/stdlib-js/stdlib/pull/9640)
1516
- [`483acba`](https://github.com/stdlib-js/stdlib/commit/483acba3dbcea263aa0014a2db41e1c073f8338b) - add `ndarray/find-last` [(#8724)](https://github.com/stdlib-js/stdlib/pull/8724)
@@ -702,6 +703,7 @@ A total of 40 issues were closed in this release:
702703

703704
<details>
704705

706+
- [`c6e2443`](https://github.com/stdlib-js/stdlib/commit/c6e24436d222d609c3ea153fd81f31e1d16451bc) - **feat:** add `ndarray/base/ternary` [(#9566)](https://github.com/stdlib-js/stdlib/pull/9566) _(by Muhammad Haris, Athan Reines)_
705707
- [`0e64df0`](https://github.com/stdlib-js/stdlib/commit/0e64df09f90ee261c1bf2dc56ae5ad3c1705b120) - **docs:** update examples [(#9688)](https://github.com/stdlib-js/stdlib/pull/9688) _(by stdlib-bot)_
706708
- [`f477420`](https://github.com/stdlib-js/stdlib/commit/f477420675c7c2b4c00729e0ed7ef75d4e1cdb11) - **test:** add tests to `ndarray/count-if` [(#9671)](https://github.com/stdlib-js/stdlib/pull/9671) _(by Muhammad Haris, Athan Reines)_
707709
- [`892be43`](https://github.com/stdlib-js/stdlib/commit/892be43dfec83129f37877bae20fff27f73f8cf2) - **test:** fix description _(by Athan Reines)_

base/ternary/README.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2026 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+
# Ternary
22+
23+
> Apply a ternary callback to elements in input ndarrays and assign results to elements in an output 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 ternary = require( '@stdlib/ndarray/base/ternary' );
41+
```
42+
43+
#### ternary( arrays, fcn )
44+
45+
Applies a ternary callback to elements in input ndarrays and assigns results to elements in an output ndarray.
46+
47+
```javascript
48+
var Float64Array = require( '@stdlib/array/float64' );
49+
var add3 = require( '@stdlib/number/float64/base/add3' );
50+
51+
// Create data buffers:
52+
var xbuf = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0 ] );
53+
var ybuf = new Float64Array( [ 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 ] );
54+
var zbuf = new Float64Array( [ 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 ] );
55+
var wbuf = new Float64Array( 6 );
56+
57+
// Define the shape of the input and output arrays:
58+
var shape = [ 3, 1, 2 ];
59+
60+
// Define the array strides:
61+
var sx = [ 2, 2, 1 ];
62+
var sy = [ 2, 2, 1 ];
63+
var sz = [ 2, 2, 1 ];
64+
var sw = [ 2, 2, 1 ];
65+
66+
// Define the index offsets:
67+
var ox = 0;
68+
var oy = 0;
69+
var oz = 0;
70+
var ow = 0;
71+
72+
// Create the input and output ndarray-like objects:
73+
var x = {
74+
'dtype': 'float64',
75+
'data': xbuf,
76+
'shape': shape,
77+
'strides': sx,
78+
'offset': ox,
79+
'order': 'row-major'
80+
};
81+
var y = {
82+
'dtype': 'float64',
83+
'data': ybuf,
84+
'shape': shape,
85+
'strides': sy,
86+
'offset': oy,
87+
'order': 'row-major'
88+
};
89+
var z = {
90+
'dtype': 'float64',
91+
'data': zbuf,
92+
'shape': shape,
93+
'strides': sz,
94+
'offset': oz,
95+
'order': 'row-major'
96+
};
97+
var w = {
98+
'dtype': 'float64',
99+
'data': wbuf,
100+
'shape': shape,
101+
'strides': sw,
102+
'offset': ow,
103+
'order': 'row-major'
104+
};
105+
106+
// Apply the ternary function:
107+
ternary( [ x, y, z, w ], add3 );
108+
109+
console.log( w.data );
110+
// => <Float64Array>[ 2.5, 3.5, 4.5, 5.5, 6.5, 7.5 ]
111+
```
112+
113+
The function accepts the following arguments:
114+
115+
- **arrays**: array-like object containing three input ndarrays and one output ndarray.
116+
- **fcn**: ternary function to apply.
117+
118+
</section>
119+
120+
<!-- /.usage -->
121+
122+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="notes">
125+
126+
## Notes
127+
128+
129+
- Each provided ndarray should be an object with the following properties:
130+
131+
- **dtype**: data type.
132+
- **data**: data buffer.
133+
- **shape**: dimensions.
134+
- **strides**: stride lengths.
135+
- **offset**: index offset.
136+
- **order**: specifies whether an ndarray is row-major (C-style) or column major (Fortran-style).
137+
138+
- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a ternary function in order to achieve better performance.
139+
140+
</section>
141+
142+
<!-- /.notes -->
143+
144+
<!-- Package usage examples. -->
145+
146+
<section class="examples">
147+
148+
## Examples
149+
150+
<!-- eslint no-undef: "error" -->
151+
152+
```javascript
153+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
154+
var filledarray = require( '@stdlib/array/filled' );
155+
var filledarrayBy = require( '@stdlib/array/filled-by' );
156+
var add3 = require( '@stdlib/number/float64/base/add3' );
157+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
158+
var ndarray2array = require( '@stdlib/ndarray/base/to-array' );
159+
var ternary = require( '@stdlib/ndarray/base/ternary' );
160+
161+
var N = 10;
162+
var x = {
163+
'dtype': 'generic',
164+
'data': filledarrayBy( N, 'generic', discreteUniform( -100, 100 ) ),
165+
'shape': [ 5, 2 ],
166+
'strides': [ 2, 1 ],
167+
'offset': 0,
168+
'order': 'row-major'
169+
};
170+
console.log( ndarray2array( x.data, x.shape, x.strides, x.offset, x.order ) );
171+
172+
var y = {
173+
'dtype': 'generic',
174+
'data': filledarrayBy( N, 'generic', discreteUniform( -100, 100 ) ),
175+
'shape': x.shape.slice(),
176+
'strides': shape2strides( x.shape, 'column-major' ),
177+
'offset': 0,
178+
'order': 'column-major'
179+
};
180+
console.log( ndarray2array( y.data, y.shape, y.strides, y.offset, y.order ) );
181+
182+
var z = {
183+
'dtype': 'generic',
184+
'data': filledarrayBy( N, 'generic', discreteUniform( -100, 100 ) ),
185+
'shape': x.shape.slice(),
186+
'strides': shape2strides( x.shape, 'row-major' ),
187+
'offset': 0,
188+
'order': 'row-major'
189+
};
190+
console.log( ndarray2array( z.data, z.shape, z.strides, z.offset, z.order ) );
191+
192+
var w = {
193+
'dtype': 'generic',
194+
'data': filledarray( 0, N, 'generic' ),
195+
'shape': x.shape.slice(),
196+
'strides': shape2strides( x.shape, 'column-major' ),
197+
'offset': 0,
198+
'order': 'column-major'
199+
};
200+
201+
ternary( [ x, y, z, w ], add3 );
202+
console.log( ndarray2array( w.data, w.shape, w.strides, w.offset, w.order ) );
203+
```
204+
205+
</section>
206+
207+
<!-- /.examples -->
208+
209+
<!-- 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. -->
210+
211+
<section class="references">
212+
213+
</section>
214+
215+
<!-- /.references -->
216+
217+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
218+
219+
<section class="related">
220+
221+
</section>
222+
223+
<!-- /.related -->
224+
225+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
226+
227+
<section class="links">
228+
229+
</section>
230+
231+
<!-- /.links -->

0 commit comments

Comments
 (0)