
Security News
/Research
Popular node-ipc npm Package Infected with Credential Stealer
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.
@stdlib/array-complex64
Advanced tools
64-bit complex number array.
npm install @stdlib/array-complex64
var Complex64Array = require( '@stdlib/array-complex64' );
Creates a 64-bit complex number array.
var arr = new Complex64Array();
// returns <Complex64Array>
Creates a 64-bit complex number array having a specified length.
var arr = new Complex64Array( 10 );
// returns <Complex64Array>
var len = arr.length;
// returns 10
Creates a 64-bit complex number array from a typed array containing interleaves real and imaginary components.
var Float32Array = require( '@stdlib/array-float32' );
var buf = new Float32Array( [ 1.0, -1.0, 2.0, -2.0 ] ); // [ re, im, re, im ]
// returns <Float32Array>[ 1.0, -1.0, 2.0, -2.0 ]
var arr = new Complex64Array( buf );
// returns <Complex64Array>
var len = arr.length;
// returns 2
Creates a 64-bit complex number array from an array-like object or iterable.
var Complex64 = require( '@stdlib/complex-float32' );
// From an array of interleaved real and imaginary components:
var arr1 = new Complex64Array( [ 1.0, -1.0, 2.0, -2.0 ] );
// returns <Complex64Array>
var len = arr1.length;
// returns 2
// From an array containing complex numbers:
var buf = [ new Complex64( 1.0, -1.0 ), new Complex64( 2.0, -2.0 ) ];
var arr2 = new Complex64Array( buf );
len = arr2.length;
// returns 2
Returns a 64-bit complex number array view of an ArrayBuffer.
var ArrayBuffer = require( '@stdlib/array-buffer' );
var buf = new ArrayBuffer( 240 );
var arr1 = new Complex64Array( buf );
// returns <Complex64Array>
var len = arr1.length;
// returns 30
var arr2 = new Complex64Array( buf, 8 );
// returns <Complex64Array>
len = arr2.length;
// returns 29
var arr3 = new Complex64Array( buf, 8, 20 );
// returns <Complex64Array>
len = arr3.length;
// returns 20
Static property returning the size (in bytes) of each array element.
var nbytes = Complex64Array.BYTES_PER_ELEMENT;
// returns 8
Static property returning the constructor name.
var str = Complex64Array.name;
// returns 'Complex64Array'
Pointer to the underlying data buffer.
var arr = new Complex64Array( 2 );
// returns <Complex64Array>
var buf = arr.buffer;
// returns <ArrayBuffer>
Size (in bytes) of the array.
var arr = new Complex64Array( 10 );
// returns <Complex64Array>
var nbytes = arr.byteLength;
// returns 80
Offset (in bytes) of the array from the start of its underlying ArrayBuffer.
var ArrayBuffer = require( '@stdlib/array-buffer' );
var arr = new Complex64Array( 10 );
// returns <Complex64Array>
var offset = arr.byteOffset;
// returns 0
var buf = new ArrayBuffer( 240 );
arr = new Complex64Array( buf, 64 );
// returns <Complex64Array>
offset = arr.byteOffset;
// returns 64
Size (in bytes) of each array element.
var arr = new Complex64Array( 10 );
// returns <Complex64Array>
var nbytes = arr.BYTES_PER_ELEMENT;
// returns 8
Number of array elements.
var arr = new Complex64Array( 10 );
// returns <Complex64Array>
var len = arr.length;
// returns 10
Creates a new 64-bit complex number array from an array-like object or an iterable.
var Complex64 = require( '@stdlib/complex-float32' );
// Create an array from interleaved real and imaginary components:
var arr = Complex64Array.from( [ 1.0, -1.0 ] );
// returns <Complex64Array>
var len = arr.length;
// returns 1
// Create an array from an array of complex numbers:
arr = Complex64Array.from( [ new Complex64( 1.0, -1.0 ) ] );
// returns <Complex64Array>
len = arr.length;
// returns 1
The iterator returned by an iterable must return either a complex number or an array-like object containing a real and imaginary component.
var ITERATOR_SYMBOL = require( '@stdlib/symbol-iterator' );
var Float32Array = require( '@stdlib/array-float32' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
var iter;
var arr;
var len;
var re;
var im;
var z;
// Define a function which returns an iterator protocol-compliant object...
function iterable() {
var buf = new Float32Array( 2 );
var i = 0;
return {
'next': next
};
function next() {
i += 1;
if ( i < 3 ) {
// Reuse allocated memory...
buf[ 0 ] = i;
buf[ 1 ] = -i;
return {
'value': buf
};
}
return {
'done': true
};
}
}
if ( ITERATOR_SYMBOL === null ) {
console.error( 'Environment does not support iterables.' );
} else {
// Create an iterable:
iter = {};
iter[ ITERATOR_SYMBOL ] = iterable;
// Generate a complex number array:
arr = Complex64Array.from( iter );
// returns <Complex64Array>
len = arr.length;
// returns 2
z = arr.get( 0 );
// returns <Complex64>
re = real( z );
// returns 1.0
im = imag( z );
// returns -1.0
}
To invoke a function for each src value, provide a callback function. If src is an iterable or an array-like object containing complex numbers, the callback must return either a complex number
var Complex64 = require( '@stdlib/complex-float32' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
function map( z ) {
return new Complex64( real(z)*2.0, imag(z)*2.0 );
}
// Create a source array:
var src = [ new Complex64( 1.0, -1.0 ) ];
// Create a new complex number array by scaling the source array:
var arr = Complex64Array.from( src, map );
// returns <Complex64Array>
var len = arr.length;
// returns 1
var z = arr.get( 0 );
// returns <Complex64>
var re = real( z );
// returns 2.0
var im = imag( z );
// returns -2.0
or an array-like object containing real and imaginary components
var Float32Array = require( '@stdlib/array-float32' );
var Complex64 = require( '@stdlib/complex-float32' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
// Return a callback which reuses allocated memory...
function mapFcn() {
var buf = new Float32Array( 2 );
return map;
function map( z ) {
buf[ 0 ] = real( z ) * 2.0;
buf[ 1 ] = imag( z ) * 2.0;
return buf;
}
}
// Create a source array:
var src = [ new Complex64( 1.0, -1.0 ), new Complex64( 2.0, -2.0 ) ];
// Create a new complex number array by scaling the source array:
var arr = Complex64Array.from( src, mapFcn() );
// returns <Complex64Array>
var len = arr.length;
// returns 2
var z = arr.get( 0 );
// returns <Complex64>
var re = real( z );
// returns 2.0
var im = imag( z );
// returns -2.0
z = arr.get( 1 );
// returns <Complex64>
re = real( z );
// returns 4.0
im = imag( z );
// returns -4.0
If src is an array-like object containing interleaved real and imaginary components, the callback is invoked for each component and should return the transformed component value.
var Float32Array = require( '@stdlib/array-float32' );
var Complex64 = require( '@stdlib/complex-float32' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
function map( v ) {
return v * 2.0;
}
// Create a source array:
var src = new Float32Array( [ 1.0, -1.0 ] );
// Create a new complex number array by scaling the source array:
var arr = Complex64Array.from( src, map );
// returns <Complex64Array>
var len = arr.length;
// returns 1
var z = arr.get( 0 );
// returns <Complex64>
var re = real( z );
// returns 2.0
var im = imag( z );
// returns -2.0
A callback function is provided two arguments:
value: source valueindex: source indexTo set the callback execution context, provide a thisArg.
var Complex64 = require( '@stdlib/complex-float32' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
function map( z ) {
this.count += 1;
return new Complex64( real(z)*2.0, imag(z)*2.0 );
}
// Create a source array:
var src = [ new Complex64( 1.0, -1.0 ), new Complex64( 1.0, -1.0 ) ];
// Define an execution context:
var ctx = {
'count': 0
};
// Create a new complex number array by scaling the source array:
var arr = Complex64Array.from( src, map, ctx );
// returns <Complex64Array>
var len = arr.length;
// returns 2
var n = ctx.count;
// returns 2
Creates a new 64-bit complex number array from a variable number of arguments.
var Complex64 = require( '@stdlib/complex-float32' );
var arr = Complex64Array.of( 1.0, -1.0, 2.0, -2.0 );
// returns <Complex64Array>
var len = arr.length;
// returns 2
var z1 = new Complex64( 1.0, -1.0 );
var z2 = new Complex64( 2.0, -2.0 );
arr = Complex64Array.of( z1, z2 );
// returns <Complex64Array>
len = arr.length;
// returns 2
Copies a sequence of elements within the array starting at start and ending at end (non-inclusive) to the position starting at target.
var Complex64 = require( '@stdlib/complex-float32' );
var arr = new Complex64Array( 4 );
// Set the array elements:
arr.set( new Complex64( 1.0, -1.0 ), 0 );
arr.set( new Complex64( 2.0, -2.0 ), 1 );
arr.set( new Complex64( 3.0, -3.0 ), 2 );
arr.set( new Complex64( 4.0, -4.0 ), 3 );
// Get the first array element:
var z = arr.get( [ 0.0, 0.0 ], 0 );
// returns [ 1.0, -1.0 ]
// Get the second array element:
z = arr.get( [ 0.0, 0.0 ], 1 );
// returns [ 2.0, -2.0 ]
// Copy the last two elements to the first two elements:
arr.copyWithin( 0, 2 );
// Get the first array element:
z = arr.get( [ 0.0, 0.0 ], 0 );
// returns [ 3.0, -3.0 ]
// Get the second array element:
z = arr.get( [ 0.0, 0.0 ], 1 );
// returns [ 4.0, -4.0 ]
By default, end equals the number of array elements (i.e., one more than the last array index). To limit the sequence length, provide an end argument.
var Complex64 = require( '@stdlib/complex-float32' );
var arr = new Complex64Array( 4 );
// Set the array elements:
arr.set( new Complex64( 1.0, -1.0 ), 0 );
arr.set( new Complex64( 2.0, -2.0 ), 1 );
arr.set( new Complex64( 3.0, -3.0 ), 2 );
arr.set( new Complex64( 4.0, -4.0 ), 3 );
// Get the third array element:
var z = arr.get( [ 0.0, 0.0 ], 2 );
// returns [ 3.0, -3.0 ]
// Get the last array element:
z = arr.get( [ 0.0, 0.0 ], 3 );
// returns [ 4.0, -4.0 ]
// Copy the first two elements to the last two elements:
arr.copyWithin( 2, 0, 2 );
// Get the third array element:
z = arr.get( [ 0.0, 0.0 ], 2 );
// returns [ 1.0, -1.0 ]
// Get the last array element:
z = arr.get( [ 0.0, 0.0 ], 3 );
// returns [ 2.0, -2.0 ]
When a target, start, and/or end index is negative, the respective index is determined relative to the last array element. The following example achieves the same behavior as the previous example:
var Complex64 = require( '@stdlib/complex-float32' );
var arr = new Complex64Array( 4 );
// Set the array elements:
arr.set( new Complex64( 1.0, -1.0 ), 0 );
arr.set( new Complex64( 2.0, -2.0 ), 1 );
arr.set( new Complex64( 3.0, -3.0 ), 2 );
arr.set( new Complex64( 4.0, -4.0 ), 3 );
// Get the third array element:
var z = arr.get( [ 0.0, 0.0 ], 2 );
// returns [ 3.0, -3.0 ]
// Get the last array element:
z = arr.get( [ 0.0, 0.0 ], 3 );
// returns [ 4.0, -4.0 ]
// Copy the first two elements to the last two elements using negative indices:
arr.copyWithin( -2, -4, -2 );
// Get the third array element:
z = arr.get( [ 0.0, 0.0 ], 2 );
// returns [ 1.0, -1.0 ]
// Get the last array element:
z = arr.get( [ 0.0, 0.0 ], 3 );
// returns [ 2.0, -2.0 ]
Returns an iterator for iterating over array key-value pairs.
var Complex64 = require( '@stdlib/complex-float32' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
var arr = [
new Complex64( 1.0, -1.0 ),
new Complex64( 2.0, -2.0 ),
new Complex64( 3.0, -3.0 )
];
arr = new Complex64Array( arr );
// Create an iterator:
var it = arr.entries();
// Iterate over the key-value pairs...
var v = it.next().value;
// returns [ 0, <Complex64> ]
var re = real( v[ 1 ] );
// returns 1.0
var im = imag( v[ 1 ] );
// returns -1.0
v = it.next().value;
// returns [ 1, <Complex64> ]
re = real( v[ 1 ] );
// returns 2.0
im = imag( v[ 1 ] );
// returns -2.0
v = it.next().value;
// returns [ 2, <Complex64> ]
re = real( v[ 1 ] );
// returns 3.0
im = imag( v[ 1 ] );
// returns -3.0
var bool = it.next().done;
// returns true
Returns an array element located at position (index) i.
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
var arr = new Complex64Array( 10 );
// Set the first element:
arr.set( [ 1.0, -1.0 ], 0 );
// Get the first element:
var z = arr.get( 0 );
// returns <Complex64>
var re = real( z );
// returns 1.0
var im = imag( z );
// returns -1.0
By default, the method returns a 64-bit complex number. To return real and imaginary components separately, provide an array-like object as the first argument.
var arr = new Complex64Array( 10 );
// Set the first element:
arr.set( [ 1.0, -1.0 ], 0 );
// Define an output array:
var out = [ 0.0, 0.0 ];
// Get the first element:
var z = arr.get( out, 0 );
// returns [ 1.0, -1.0 ]
var bool = ( out === z );
// returns true
If provided an out-of-bounds index, the method returns undefined.
var arr = new Complex64Array( 10 );
var z = arr.get( 100 );
// returns undefined
var out = [ 0.0, 0.0 ];
z = arr.get( out, 100 );
// returns undefined
var bool = ( out === z );
// returns false
Sets one or more array elements.
var Complex64 = require( '@stdlib/complex-float32' );
var arr = new Complex64Array( 10 );
// Get the first element:
var z = arr.get( [ 0.0, 0.0 ], 0 );
// returns [ 0.0, 0.0 ]
// Set the first element:
arr.set( new Complex64( 1.0, -1.0 ) );
// Get the first element:
z = arr.get( [ 0.0, 0.0 ], 0 );
// returns [ 1.0, -1.0 ]
By default, the method sets array elements starting at position (index) i = 0. To set elements starting elsewhere in the array, provide an index argument i.
var Complex64 = require( '@stdlib/complex-float32' );
var arr = new Complex64Array( 10 );
// Get the fifth element:
var z = arr.get( [ 0.0, 0.0 ], 4 );
// returns [ 0.0, 0.0 ]
// Set the fifth element:
arr.set( new Complex64( 1.0, -1.0 ), 4 );
// Get the fifth element:
z = arr.get( [ 0.0, 0.0 ], 4 );
// returns [ 1.0, -1.0 ]
In addition to providing a complex number, to set one or more array elements, provide an array-like object containing either complex numbers
var Complex64 = require( '@stdlib/complex-float32' );
var arr = new Complex64Array( 10 );
// Define an array of complex numbers:
var buf = [
new Complex64( 1.0, -1.0 ),
new Complex64( 2.0, -2.0 ),
new Complex64( 3.0, -3.0 )
];
// Set the fifth, sixth, and seventh elements:
arr.set( buf, 4 );
// Get the sixth element:
var z = arr.get( [ 0.0, 0.0 ], 5 );
// returns [ 2.0, -2.0 ]
or interleaved real and imaginary components
var Float32Array = require( '@stdlib/array-float32' );
var arr = new Complex64Array( 10 );
// Define an interleaved array of real and imaginary components:
var buf = new Float32Array( [ 1.0, -1.0, 2.0, -2.0, 3.0, -3.0 ] );
// Set the fifth, sixth, and seventh elements:
arr.set( buf, 4 );
// Get the sixth element:
var z = arr.get( [ 0.0, 0.0 ], 5 );
// returns [ 2.0, -2.0 ]
A few notes:
i is out-of-bounds, the method throws an error.i exceeds the target array length), the method throws an error.ArrayBuffer with the target array, the method will intelligently copy the source range to the destination range.While a Complex64Array strives to maintain (but does not guarantee) consistency with typed arrays, significant deviations from ECMAScript-defined typed array behavior are as follows:
new operator.Z[i]) is not supported. Instead, one must use the .get() method which returns a value compatible with complex number output.set method has extended behavior in order to support complex numbers.var Complex64 = require( '@stdlib/complex-float32' );
var Float32Array = require( '@stdlib/array-float32' );
var Complex64Array = require( '@stdlib/array-complex64' );
var arr;
var out;
// Create a complex array by specifying a length:
out = new Complex64Array( 3 );
console.log( out );
// Create a complex array from an array of complex numbers:
arr = [
new Complex64( 1.0, -1.0 ),
new Complex64( -3.14, 3.14 ),
new Complex64( 0.5, 0.5 )
];
out = new Complex64Array( arr );
console.log( out );
// Create a complex array from an interleaved typed array:
arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] );
out = new Complex64Array( arr );
console.log( out );
// Create a complex array from an array buffer:
arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] );
out = new Complex64Array( arr.buffer );
console.log( out );
// Create a complex array from an array buffer view:
arr = new Float32Array( [ 1.0, -1.0, -3.14, 3.14, 0.5, 0.5 ] );
out = new Complex64Array( arr.buffer, 8, 2 );
console.log( out );
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
See LICENSE.
Copyright © 2016-2021. The Stdlib Authors.
FAQs
Complex64Array.
The npm package @stdlib/array-complex64 receives a total of 35,518 weekly downloads. As such, @stdlib/array-complex64 popularity was classified as popular.
We found that @stdlib/array-complex64 demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
/Research
Socket detected malicious node-ipc versions with obfuscated stealer/backdoor behavior in a developing npm supply chain attack.

Security News
TeamPCP and BreachForums are promoting a Shai-Hulud supply chain attack contest with a $1,000 prize for the biggest package compromise.

Security News
Packagist urges PHP projects to update Composer after a GitHub token format change exposed some GitHub Actions tokens in CI logs.