Skip to content

Commit 042102b

Browse files
committed
Auto-generated commit
1 parent 2167c41 commit 042102b

File tree

8 files changed

+502
-10
lines changed

8 files changed

+502
-10
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1955,6 +1955,36 @@ var str = arr.toString();
19551955
// returns '1 + 1i,2 - 2i,3 + 3i'
19561956
```
19571957

1958+
<a name="method-with"></a>
1959+
1960+
#### Complex128Array.prototype.with( index, value )
1961+
1962+
Returns a new typed array with the element at a provided index replaced with a provided value.
1963+
1964+
```javascript
1965+
var real = require( '@stdlib/complex-real' );
1966+
var imag = require( '@stdlib/complex-imag' );
1967+
var Complex128 = require( '@stdlib/complex-float64' );
1968+
1969+
var arr = new Complex128Array( 3 );
1970+
1971+
arr.set( [ 1.0, 1.0 ], 0 );
1972+
arr.set( [ 2.0, 2.0 ], 1 );
1973+
arr.set( [ 3.0, 3.0 ], 1 );
1974+
1975+
var out = arr.with( 0, new Complex128( 4.0, 4.0 ) );
1976+
// returns <Complex128Array>
1977+
1978+
var z = out.get( 0 );
1979+
// returns <Complex128>
1980+
1981+
var re = real( z );
1982+
// returns 4.0
1983+
1984+
var im = imag( z );
1985+
// returns 4.0
1986+
```
1987+
19581988
</section>
19591989

19601990
<!-- /.usage -->

benchmark/benchmark.with.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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-harness' );
24+
var Complex128 = require( '@stdlib/complex-float64' );
25+
var isComplex128Array = require( '@stdlib/assert-is-complex128array' );
26+
var pkg = require( './../package.json' ).name;
27+
var Complex128Array = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg+':with', function benchmark( b ) {
33+
var values;
34+
var arr;
35+
var out;
36+
var i;
37+
38+
values = [
39+
new Complex128( 1.0, 1.0 ),
40+
new Complex128( 2.0, 2.0 ),
41+
new Complex128( 3.0, 3.0 )
42+
];
43+
arr = new Complex128Array( 5 );
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
out = arr.with( i%arr.length, values[ i%values.length ] );
48+
if ( typeof out !== 'object' ) {
49+
b.fail( 'should return an object' );
50+
}
51+
}
52+
b.toc();
53+
if ( !isComplex128Array( out ) ) {
54+
b.fail( 'should return a Complex128Array' );
55+
}
56+
b.pass( 'benchmark finished' );
57+
b.end();
58+
});

benchmark/benchmark.with.length.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 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-harness' );
24+
var pow = require( '@stdlib/math-base-special-pow' );
25+
var Complex128 = require( '@stdlib/complex-float64' );
26+
var isComplex128Array = require('@stdlib/assert-is-complex128array' );
27+
var pkg = require( './../package.json' ).name;
28+
var Complex128Array = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
var arr = new Complex128Array( len );
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var values;
52+
var out;
53+
var i;
54+
55+
values = [
56+
new Complex128( 1.0, 1.0 ),
57+
new Complex128( 2.0, 2.0 ),
58+
new Complex128( 3.0, 3.0 )
59+
];
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
out = arr.with( i%len, values[ i%values.length ] );
64+
if ( typeof out !== 'object' ) {
65+
b.fail( 'should return an object' );
66+
}
67+
}
68+
b.toc();
69+
if ( !isComplex128Array( out ) ) {
70+
b.fail( 'should return a Complex128Array' );
71+
}
72+
b.pass( 'benchmark finished' );
73+
b.end();
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+':with:len='+len, f );
99+
}
100+
}
101+
102+
main();

dist/index.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/types/index.d.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,41 @@ declare class Complex128Array implements Complex128ArrayInterface {
981981
* // returns '1 + 1i,2 + 2i'
982982
*/
983983
toString(): string;
984+
985+
/**
986+
* Returns a new typed array with the element at a provided index replaced with a provided value.
987+
*
988+
* @param index - element index
989+
* @param value - new value
990+
* @throws first argument must be an integer
991+
* @throws second argument must be a complex number
992+
* @throws index argument is out-of-bounds
993+
* @returns modified typed array
994+
*
995+
* @example
996+
* var real = require( '@stdlib/complex-real' );
997+
* var imag = require( '@stdlib/complex-imag' );
998+
* var Complex128 = require( '@stdlib/complex-float64' );
999+
*
1000+
* var arr = new Complex128Array( 3 );
1001+
*
1002+
* arr.set( [ 1.0, 1.0 ], 0 );
1003+
* arr.set( [ 2.0, 2.0 ], 1 );
1004+
* arr.set( [ 3.0, 3.0 ], 2 );
1005+
*
1006+
* var out = arr.with( 0, new Complex128( 4.0, 4.0 ) );
1007+
* // returns <Complex128Array>
1008+
*
1009+
* var z = out.get( 0 );
1010+
* // returns <Complex128>
1011+
*
1012+
* var re = real( z );
1013+
* // returns 4.0
1014+
*
1015+
* var im = imag( z );
1016+
* // returns 4.0
1017+
*/
1018+
with( index: number, value: ComplexLike ): Complex128Array;
9841019
}
9851020

9861021
/**

lib/main.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2120,6 +2120,70 @@ setReadOnly( Complex128Array.prototype, 'toString', function toString() {
21202120
return out.join( ',' );
21212121
});
21222122

2123+
/**
2124+
* Returns a new typed array with the element at a provided index replaced with a provided value.
2125+
*
2126+
* @name with
2127+
* @memberof Complex128Array.prototype
2128+
* @type {Function}
2129+
* @param {integer} index - element index
2130+
* @param {ComplexLike} value - new value
2131+
* @throws {TypeError} `this` must be a complex number array
2132+
* @throws {TypeError} first argument must be an integer
2133+
* @throws {RangeError} index argument is out-of-bounds
2134+
* @throws {TypeError} second argument must be a complex number
2135+
* @returns {Complex128Array} new typed array
2136+
*
2137+
* @example
2138+
* var real = require( '@stdlib/complex-real' );
2139+
* var imag = require( '@stdlib/complex-imag' );
2140+
* var Complex128 = require( '@stdlib/complex-float64' );
2141+
*
2142+
* var arr = new Complex128Array( 3 );
2143+
*
2144+
* arr.set( [ 1.0, 1.0 ], 0 );
2145+
* arr.set( [ 2.0, 2.0 ], 1 );
2146+
* arr.set( [ 3.0, 3.0 ], 2 );
2147+
*
2148+
* var out = arr.with( 0, new Complex128( 4.0, 4.0 ) );
2149+
* // returns <Complex128Array>
2150+
*
2151+
* var z = out.get( 0 );
2152+
* // returns <Complex128>
2153+
*
2154+
* var re = real( z );
2155+
* // returns 4.0
2156+
*
2157+
* var im = imag( z );
2158+
* // returns 4.0
2159+
*/
2160+
setReadOnly( Complex128Array.prototype, 'with', function copyWith( index, value ) {
2161+
var buf;
2162+
var out;
2163+
var len;
2164+
if ( !isComplexArray( this ) ) {
2165+
throw new TypeError( 'invalid invocation. `this` is not a complex number array.' );
2166+
}
2167+
if ( !isInteger( index ) ) {
2168+
throw new TypeError( format( 'invalid argument. First argument must be an integer. Value: `%s`.', index ) );
2169+
}
2170+
len = this._length;
2171+
if ( index < 0 ) {
2172+
index += len;
2173+
}
2174+
if ( index < 0 || index >= len ) {
2175+
throw new RangeError( format( 'invalid argument. Index argument is out-of-bounds. Value: `%s`.', index ) );
2176+
}
2177+
if ( !isComplexLike( value ) ) {
2178+
throw new TypeError( format( 'invalid argument. Second argument must be a complex number. Value: `%s`.', value ) );
2179+
}
2180+
out = new this.constructor( this._buffer );
2181+
buf = out._buffer; // eslint-disable-line no-underscore-dangle
2182+
buf[ 2*index ] = real( value );
2183+
buf[ (2*index)+1 ] = imag( value );
2184+
return out;
2185+
});
2186+
21232187

21242188
// EXPORTS //
21252189

0 commit comments

Comments
 (0)