Skip to content

Commit 434062a

Browse files
Merge branch 'stdlib-js:develop' into constant/pi-squared
2 parents a743ef0 + 4b9e370 commit 434062a

File tree

52 files changed

+2581
-54
lines changed

Some content is hidden

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

52 files changed

+2581
-54
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
# gjoin
22+
23+
> Return a string created by joining one-dimensional ndarray elements using a specified separator.
24+
25+
<section class="intro">
26+
27+
</section>
28+
29+
<!-- /.intro -->
30+
31+
<section class="usage">
32+
33+
## Usage
34+
35+
```javascript
36+
var gjoin = require( '@stdlib/blas/ext/base/ndarray/gjoin' );
37+
```
38+
39+
#### gjoin( arrays )
40+
41+
Returns a string created by joining one-dimensional ndarray elements using a specified separator.
42+
43+
```javascript
44+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
45+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
46+
47+
var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
48+
var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
49+
50+
var separator = scalar2ndarray( ',', {
51+
'dtype': 'generic'
52+
});
53+
54+
var out = gjoin( [ x, separator ] );
55+
// returns '1,3,4,2'
56+
```
57+
58+
The function has the following parameters:
59+
60+
- **arrays**: array-like object containing the following ndarrays:
61+
62+
- a one-dimensional input ndarray.
63+
- a zero-dimensional ndarray containing the separator.
64+
65+
</section>
66+
67+
<!-- /.usage -->
68+
69+
<section class="notes">
70+
71+
</section>
72+
73+
<!-- /.notes -->
74+
75+
<section class="examples">
76+
77+
## Examples
78+
79+
<!-- eslint no-undef: "error" -->
80+
81+
```javascript
82+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
83+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
84+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
85+
var ndarray2array = require( '@stdlib/ndarray/to-array' );
86+
var ndarraylike2scalar = require( '@stdlib/ndarray/base/ndarraylike2scalar' );
87+
var gjoin = require( '@stdlib/blas/ext/base/ndarray/gjoin' );
88+
89+
var xbuf = discreteUniform( 10, -100, 100, {
90+
'dtype': 'generic'
91+
});
92+
var x = new ndarray( 'generic', xbuf, [ xbuf.length ], [ 1 ], 0, 'row-major' );
93+
console.log( ndarray2array( x ) );
94+
95+
var separator = scalar2ndarray( ',', {
96+
'dtype': 'generic'
97+
});
98+
console.log( 'Separator:', ndarraylike2scalar( separator ) );
99+
100+
var out = gjoin( [ x, separator ] );
101+
console.log( out );
102+
```
103+
104+
</section>
105+
106+
<!-- /.examples -->
107+
108+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
109+
110+
<section class="related">
111+
112+
</section>
113+
114+
<!-- /.related -->
115+
116+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
117+
118+
<section class="links">
119+
120+
</section>
121+
122+
<!-- /.links -->
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var uniform = require( '@stdlib/random/array/uniform' );
25+
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var ndarray = require( '@stdlib/ndarray/base/ctor' );
28+
var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
29+
var pkg = require( './../package.json' ).name;
30+
var gjoin = require( './../lib' );
31+
32+
33+
// VARIABLES //
34+
35+
var options = {
36+
'dtype': 'generic'
37+
};
38+
39+
40+
// FUNCTIONS //
41+
42+
/**
43+
* Creates a benchmark function.
44+
*
45+
* @private
46+
* @param {PositiveInteger} len - array length
47+
* @returns {Function} benchmark function
48+
*/
49+
function createBenchmark( len ) {
50+
var separator;
51+
var xbuf;
52+
var x;
53+
54+
xbuf = uniform( len, 0.0, 100.0, options );
55+
x = new ndarray( options.dtype, xbuf, [ len ], [ 1 ], 0, 'row-major' );
56+
57+
separator = scalar2ndarray( ',', {
58+
'dtype': 'generic'
59+
});
60+
61+
return benchmark;
62+
63+
function benchmark( b ) {
64+
var out;
65+
var i;
66+
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
out = gjoin( [ x, separator ] );
70+
if ( out !== out ) {
71+
b.fail( 'should return a string' );
72+
}
73+
}
74+
b.toc();
75+
if ( !isString( out ) ) {
76+
b.fail( 'should return a string' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
}
81+
}
82+
83+
84+
// MAIN //
85+
86+
/**
87+
* Main execution sequence.
88+
*
89+
* @private
90+
*/
91+
function main() {
92+
var len;
93+
var min;
94+
var max;
95+
var f;
96+
var i;
97+
98+
min = 1; // 10^min
99+
max = 6; // 10^max
100+
101+
for ( i = min; i <= max; i++ ) {
102+
len = pow( 10, i );
103+
f = createBenchmark( len );
104+
bench( pkg+':len='+len, f );
105+
}
106+
}
107+
108+
main();
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
2+
{{alias}}( arrays )
3+
Returns a string created by joining one-dimensional ndarray elements using a
4+
specified separator.
5+
6+
Parameters
7+
----------
8+
arrays: ArrayLikeObject<ndarray>
9+
Array-like object containing the following ndarrays:
10+
11+
- a one-dimensional input ndarray.
12+
- a zero-dimensional ndarray containing the separator.
13+
14+
Returns
15+
-------
16+
out: string
17+
Joined string.
18+
19+
Examples
20+
--------
21+
> var xbuf = [ 1.0, 2.0, 3.0 ];
22+
> var dt = 'generic';
23+
> var sh = [ xbuf.length ];
24+
> var sx = [ 1 ];
25+
> var ox = 0;
26+
> var ord = 'row-major';
27+
> var x = new {{alias:@stdlib/ndarray/ctor}}( dt, xbuf, sh, sx, ox, ord );
28+
> var v = {{alias:@stdlib/ndarray/from-scalar}}( ',', { 'dtype': dt } );
29+
> {{alias}}( [ x, v ] )
30+
'1,2,3'
31+
32+
See Also
33+
--------
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { typedndarray } from '@stdlib/types/ndarray';
24+
25+
/**
26+
* Returns a string created by joining one-dimensional ndarray elements using a specified separator.
27+
*
28+
* @param arrays - array-like object containing a one-dimensional input ndarray and a zero-dimensional ndarray containing a separator
29+
* @returns joined string
30+
*
31+
* @example
32+
* var ndarray = require( '@stdlib/ndarray/base/ctor' );
33+
* var scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
34+
*
35+
* var xbuf = [ 1.0, 3.0, 4.0, 2.0 ];
36+
* var x = new ndarray( 'generic', xbuf, [ 4 ], [ 1 ], 0, 'row-major' );
37+
*
38+
* var separator = scalar2ndarray( ',', {
39+
* 'dtype': 'generic'
40+
* });
41+
*
42+
* var v = gjoin( [ x, separator ] );
43+
* // returns '1,3,4,2'
44+
*/
45+
declare function gjoin<T = unknown, U = unknown>( arrays: [ typedndarray<T>, typedndarray<U> ] ): string;
46+
47+
48+
// EXPORTS //
49+
50+
export = gjoin;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
/* eslint-disable space-in-parens */
20+
21+
import zeros = require( '@stdlib/ndarray/zeros' );
22+
import scalar2ndarray = require( '@stdlib/ndarray/from-scalar' );
23+
import gjoin = require( './index' );
24+
25+
26+
// TESTS //
27+
28+
// The function returns a number...
29+
{
30+
const x = zeros( [ 10 ], {
31+
'dtype': 'generic'
32+
});
33+
const separator = scalar2ndarray( ',', {
34+
'dtype': 'generic'
35+
});
36+
37+
gjoin( [ x, separator ] ); // $ExpectType string
38+
}
39+
40+
// The compiler throws an error if the function is provided a first argument which is not an array of ndarrays...
41+
{
42+
gjoin( '10' ); // $ExpectError
43+
gjoin( 10 ); // $ExpectError
44+
gjoin( true ); // $ExpectError
45+
gjoin( false ); // $ExpectError
46+
gjoin( null ); // $ExpectError
47+
gjoin( undefined ); // $ExpectError
48+
gjoin( [] ); // $ExpectError
49+
gjoin( {} ); // $ExpectError
50+
gjoin( ( x: number ): number => x ); // $ExpectError
51+
}
52+
53+
// The compiler throws an error if the function is provided an unsupported number of arguments...
54+
{
55+
const x = zeros( [ 10 ], {
56+
'dtype': 'generic'
57+
});
58+
const separator = scalar2ndarray( 0.0, {
59+
'dtype': 'generic'
60+
});
61+
62+
gjoin(); // $ExpectError
63+
gjoin( [ x, separator ], {} ); // $ExpectError
64+
}

0 commit comments

Comments
 (0)