Socket
Socket
Sign inDemoInstall

@stdlib/math-base-ops-cmul

Package Overview
Dependencies
71
Maintainers
4
Versions
12
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.0.6 to 0.0.7

include/stdlib/math/base/ops/cmul.h

44

docs/repl.txt
{{alias}}( [out,] re1, im1, re2, im2 )
Multiplies two complex numbers.
{{alias}}( z1, z2 )
Multiplies two double-precision complex floating-point numbers.
Parameters
----------
out: Array|TypedArray|Object (optional)
Output array.
z1: Complex128
Complex number.
re1: number
Real component.
z2: Complex128
Complex number.
im1: number
Imaginary component.
re2: number
Real component.
im2: number
Imaginary component.
Returns
-------
out: Array|TypedArray|Object
Array containing the real and imaginary components of the result.
out: Complex128
Result.
Examples
--------
> var out = {{alias}}( 5.0, 3.0, -2.0, 1.0 )
[ -13.0, -1.0 ]
> var z1 = new {{alias:@stdlib/complex/float64}}( 5.0, 3.0 )
<Complex128>
> var z2 = new {{alias:@stdlib/complex/float64}}( -2.0, 1.0 )
<Complex128>
> var out = {{alias}}( z1, z2 )
<Complex128>
> var re = {{alias:@stdlib/complex/real}}( out )
-13.0
> var im = {{alias:@stdlib/complex/imag}}( out )
-1.0
// Provide an output array:
> var out = new {{alias:@stdlib/array/float64}}( 2 );
> var v = {{alias}}( out, 5.0, 3.0, -2.0, 1.0 )
<Float64Array>[ -13.0, -1.0 ]
> var bool = ( v === out )
true
See Also
--------

@@ -23,36 +23,32 @@ /*

import { ArrayLike } from '@stdlib/types/array';
import { Complex128 } from '@stdlib/types/object';
/**
* Multiplies two complex numbers.
* Multiplies two double-precision complex floating-point numbers.
*
* @param out - output array
* @param re1 - real component
* @param im1 - imaginary component
* @param re2 - real component
* @param im2 - imaginary component
* @returns real and imaginary components
* @param z1 - complex number
* @param z2 - complex number
* @returns result
*
* @example
* var Float64Array = require( `@stdlib/array/float64` );
* var out = new Float64Array( 2 );
* var v = cmul( out, 5.0, 3.0, -2.0, 1.0 );
* // returns <Float64Array>[ -13.0, -1.0 ]
*/
declare function cmul( out: ArrayLike<number>, re1: number, im1: number, re2: number, im2: number ): ArrayLike<number>; // tslint-disable-line max-line-length
/**
* Multiplies two complex numbers.
* var Complex128 = require( `@stdlib/complex/float64` );
* var real = require( `@stdlib/complex/real` );
* var imag = require( `@stdlib/complex/imag` );
*
* @param re1 - real component
* @param im1 - imaginary component
* @param re2 - real component
* @param im2 - imaginary component
* @returns real and imaginary components
* var z1 = new Complex128( 5.0, 3.0 );
* // returns <Complex128>
*
* @example
var v = cmul( 5.0, 3.0, -2.0, 1.0 );
// returns [ -13.0, -1.0 ]
* var z2 = new Complex128( -2.0, 1.0 );
* // returns <Complex128>
*
* var out = cmul( z1, z2 );
* // returns <Complex128>
*
* var re = real( out );
* // returns -13.0
*
* var im = imag( out );
* // returns -1.0
*/
declare function cmul( re1: number, im1: number, re2: number, im2: number ): ArrayLike<number>; // tslint-disable-line max-line-length
declare function cmul( z1: Complex128, z2: Complex128 ): Complex128;

@@ -59,0 +55,0 @@

@@ -19,2 +19,3 @@ /*

import Complex128 = require( '@stdlib/complex-float64' );
import cmul = require( './index' );

@@ -25,72 +26,44 @@

// The function returns an array of numbers...
// The function returns a complex number...
{
cmul( 5, 3, -2, 1 ); // $ExpectType ArrayLike<number>
cmul( [], 5, 3, -2, 1 ); // $ExpectType ArrayLike<number>
const z = new Complex128( 1.0, 1.0 );
cmul( z, z ); // $ExpectType Complex128
}
// The compiler throws an error if the function is provided a first real component which is not a number...
// The compiler throws an error if the function is provided a first argument which is not a complex number...
{
cmul( true, 3, -2, 1 ); // $ExpectError
cmul( false, 3, -2, 1 ); // $ExpectError
cmul( null, 3, -2, 1 ); // $ExpectError
cmul( undefined, 3, -2, 1 ); // $ExpectError
cmul( '5', 3, -2, 1 ); // $ExpectError
cmul( [], 3, -2, 1 ); // $ExpectError
cmul( {}, 3, -2, 1 ); // $ExpectError
cmul( ( x: number ): number => x, 3, -2, 1 ); // $ExpectError
}
const z = new Complex128( 1.0, 1.0 );
// The compiler throws an error if the function is provided a first imaginary component which is not a number...
{
cmul( 5, true, -2, 1 ); // $ExpectError
cmul( 5, false, -2, 1 ); // $ExpectError
cmul( 5, null, -2, 1 ); // $ExpectError
cmul( 5, undefined, -2, 1 ); // $ExpectError
cmul( 5, '5', -2, 1 ); // $ExpectError
cmul( 5, [], -2, 1 ); // $ExpectError
cmul( 5, {}, -2, 1 ); // $ExpectError
cmul( 5, ( x: number ): number => x, -2, 1 ); // $ExpectError
cmul( true, z ); // $ExpectError
cmul( false, z ); // $ExpectError
cmul( null, z ); // $ExpectError
cmul( undefined, z ); // $ExpectError
cmul( '5', z ); // $ExpectError
cmul( [], z ); // $ExpectError
cmul( {}, z ); // $ExpectError
cmul( ( x: number ): number => x, z ); // $ExpectError
}
// The compiler throws an error if the function is provided a second real component which is not a number...
// The compiler throws an error if the function is provided a second argument which is not a complex number...
{
cmul( 5, 3, true, 1 ); // $ExpectError
cmul( 5, 3, false, 1 ); // $ExpectError
cmul( 5, 3, null, 1 ); // $ExpectError
cmul( 5, 3, undefined, 1 ); // $ExpectError
cmul( 5, 3, '5', 1 ); // $ExpectError
cmul( 5, 3, [], 1 ); // $ExpectError
cmul( 5, 3, {}, 1 ); // $ExpectError
cmul( 5, 3, ( x: number ): number => x, 1 ); // $ExpectError
}
const z = new Complex128( 1.0, 1.0 );
// The compiler throws an error if the function is provided a second imaginary component which is not a number...
{
cmul( 5, 3, -2, true ); // $ExpectError
cmul( 5, 3, -2, false ); // $ExpectError
cmul( 5, 3, -2, null ); // $ExpectError
cmul( 5, 3, -2, undefined ); // $ExpectError
cmul( 5, 3, -2, '5' ); // $ExpectError
cmul( 5, 3, -2, [] ); // $ExpectError
cmul( 5, 3, -2, {} ); // $ExpectError
cmul( 5, 3, -2, ( x: number ): number => x ); // $ExpectError
cmul( z, true ); // $ExpectError
cmul( z, false ); // $ExpectError
cmul( z, null ); // $ExpectError
cmul( z, undefined ); // $ExpectError
cmul( z, '5' ); // $ExpectError
cmul( z, [] ); // $ExpectError
cmul( z, {} ); // $ExpectError
cmul( z, ( x: number ): number => x ); // $ExpectError
}
// The compiler throws an error if the function is provided an output array which is not array-like...
// The compiler throws an error if the function is provided an unsupported number of arguments...
{
cmul( true, 5, 3, -2, 1 ); // $ExpectError
cmul( false, 5, 3, -2, 1 ); // $ExpectError
cmul( 'abc', 5, 3, -2, 1 ); // $ExpectError
cmul( {}, 5, 3, -2, 1 ); // $ExpectError
cmul( ( x: number ): number => x, 5, 3, -2, 1 ); // $ExpectError
cmul( 123, 5, 3, -2, 1 ); // $ExpectError
}
const z = new Complex128( 1.0, 1.0 );
// The compiler throws an error if the function is provided insufficient arguments...
{
cmul(); // $ExpectError
cmul( 2 ); // $ExpectError
cmul( 2, 3 ); // $ExpectError
cmul( 2, 3, 4 ); // $ExpectError
cmul( z ); // $ExpectError
cmul( z, z, z ); // $ExpectError
}

@@ -22,3 +22,3 @@ /**

/**
* Multiply two complex numbers.
* Multiply two double-precision complex floating-point numbers.
*

@@ -28,17 +28,21 @@ * @module @stdlib/math-base-ops-cmul

* @example
* var Complex128 = require( '@stdlib/complex-float64' );
* var real = require( '@stdlib/complex-real' );
* var imag = require( '@stdlib/complex-imag' );
* var cmul = require( '@stdlib/math-base-ops-cmul' );
*
* var v = cmul( 5.0, 3.0, -2.0, 1.0 );
* // returns [ -13.0, -1.0 ]
* var z1 = new Complex128( 5.0, 3.0 );
* // returns <Complex128>
*
* @example
* var cmul = require( '@stdlib/math-base-ops-cmul' );
* var z2 = new Complex128( -2.0, 1.0 );
* // returns <Complex128>
*
* var out = new Array( 2 );
* var out = cmul( z1, z2 );
* // returns <Complex128>
*
* var v = cmul( out, 5.0, 3.0, -2.0, 1.0 );
* // returns [ -13.0, -1.0 ]
* var re = real( out );
* // returns -13.0
*
* var bool = ( v === out );
* // returns true
* var im = imag( out );
* // returns -1.0
*/

@@ -48,3 +52,3 @@

var cmul = require( './main.js' );
var main = require( './main.js' );

@@ -54,2 +58,2 @@

module.exports = cmul;
module.exports = main;

@@ -23,3 +23,4 @@ /**

var mul = require( './cmul.js' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );

@@ -30,31 +31,36 @@

/**
* Multiplies two complex numbers.
* Multiplies two double-precision complex floating-point numbers.
*
* @param {(Array|TypedArray|Object)} [out] - output array
* @param {number} re1 - real component
* @param {number} im1 - imaginary component
* @param {number} re2 - real component
* @param {number} im2 - imaginary component
* @returns {(Array|TypedArray|Object)} real and imaginary components
* @param {Complex128} z1 - complex number
* @param {Complex128} z2 - complex number
* @returns {Complex128} result
*
* @example
* var out = new Array( 2 );
* var Complex128 = require( '@stdlib/complex-float64' );
* var real = require( '@stdlib/complex-real' );
* var imag = require( '@stdlib/complex-imag' );
*
* var v = cmul( out, 5.0, 3.0, -2.0, 1.0 );
* // returns [ -13.0, -1.0 ]
* var z1 = new Complex128( 5.0, 3.0 );
* // returns <Complex128>
*
* @example
* var out = new Array( 2 );
* var z2 = new Complex128( -2.0, 1.0 );
* // returns <Complex128>
*
* var v = cmul( out, 5.0, 3.0, -2.0, 1.0 );
* // returns [ -13.0, -1.0 ]
* var out = cmul( z1, z2 );
* // returns <Complex128>
*
* var bool = ( v === out );
* // returns true
* var re = real( out );
* // returns -13.0
*
* var im = imag( out );
* // returns -1.0
*/
function cmul( out, re1, im1, re2, im2 ) {
if ( arguments.length === 4 ) {
return mul( [ 0.0, 0.0 ], out, re1, im1, re2 );
}
return mul( out, re1, im1, re2, im2 );
function cmul( z1, z2 ) {
var re1 = real( z1 );
var re2 = real( z2 );
var im1 = imag( z1 );
var im2 = imag( z2 );
var re = (re1*re2) - (im1*im2);
var im = (re1*im2) + (im1*re2);
return new z1.constructor( re, im );
}

@@ -61,0 +67,0 @@

{
"name": "@stdlib/math-base-ops-cmul",
"version": "0.0.6",
"description": "Multiply two complex numbers.",
"version": "0.0.7",
"description": "Multiply two double-precision complex floating-point numbers.",
"license": "Apache-2.0",

@@ -40,14 +40,13 @@ "author": {

"dependencies": {
"@stdlib/types": "^0.0.x"
"@stdlib/complex-imag": "^0.0.x",
"@stdlib/complex-real": "^0.0.x",
"@stdlib/types": "^0.0.x",
"@stdlib/utils-library-manifest": "^0.0.x"
},
"devDependencies": {
"@stdlib/array-float64": "^0.0.x",
"@stdlib/assert-is-array": "^0.0.x",
"@stdlib/bench": "^0.0.x",
"@stdlib/complex-float64": "^0.0.x",
"@stdlib/complex-imag": "^0.0.x",
"@stdlib/complex-real": "^0.0.x",
"@stdlib/math-base-assert-is-nan": "^0.0.x",
"@stdlib/math-base-special-round": "^0.0.x",
"@stdlib/random-base-randu": "^0.0.x",
"@stdlib/random-base-discrete-uniform": "^0.0.x",
"@stdlib/random-base-uniform": "^0.0.x",
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",

@@ -54,0 +53,0 @@ "istanbul": "^0.4.1",

@@ -21,7 +21,7 @@ <!--

# multiply
# cmul
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] [![dependencies][dependencies-image]][dependencies-url]
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->
> Multiply two complex numbers.
> Multiply two double-precision complex floating-point numbers.

@@ -52,23 +52,22 @@ <section class="intro">

#### cmul( \[out,] re1, im1, re2, im2 )
#### cmul( z1, z2 )
Multiplies two `complex` numbers where each `complex` number is comprised of a **real** component `re` and an **imaginary** component `im`.
Multiplies two double-precision complex floating-point numbers.
```javascript
var v = cmul( 5.0, 3.0, -2.0, 1.0 );
// returns [ -13.0, -1.0 ]
```
var Complex128 = require( '@stdlib/complex-float64' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
By default, the function returns real and imaginary components as a two-element `array`. To avoid unnecessary memory allocation, the function supports providing an output (destination) object.
var z1 = new Complex128( 5.0, 3.0 );
var z2 = new Complex128( -2.0, 1.0 );
```javascript
var Float64Array = require( '@stdlib/array-float64' );
var v = cmul( z1, z2 );
// returns <Complex128>
var out = new Float64Array( 2 );
var re = real( v );
// returns -13.0
var v = cmul( out, 5.0, 3.0, -2.0, 1.0 );
// returns <Float64Array>[ -13.0, -1.0 ]
var bool = ( v === out );
// returns true
var im = imag( v );
// returns -1.0
```

@@ -88,29 +87,107 @@

var Complex128 = require( '@stdlib/complex-float64' );
var randu = require( '@stdlib/random-base-randu' );
var round = require( '@stdlib/math-base-special-round' );
var real = require( '@stdlib/complex-real' );
var imag = require( '@stdlib/complex-imag' );
var discreteUniform = require( '@stdlib/random-base-discrete-uniform' ).factory;
var cmul = require( '@stdlib/math-base-ops-cmul' );
var re;
var im;
var rand;
var z1;
var z2;
var z3;
var o;
var i;
rand = discreteUniform( -50, 50 );
for ( i = 0; i < 100; i++ ) {
re = round( randu()*100.0 ) - 50.0;
im = round( randu()*100.0 ) - 50.0;
z1 = new Complex128( re, im );
z1 = new Complex128( rand(), rand() );
z2 = new Complex128( rand(), rand() );
z3 = cmul( z1, z2 );
console.log( '(%s) * (%s) = %s', z1.toString(), z2.toString(), z3.toString() );
}
```
re = round( randu()*100.0 ) - 50.0;
im = round( randu()*100.0 ) - 50.0;
z2 = new Complex128( re, im );
</section>
o = cmul( real(z1), imag(z1), real(z2), imag(z2) );
z3 = new Complex128( o[ 0 ], o[ 1 ] );
<!-- /.examples -->
console.log( '(%s) * (%s) = %s', z1.toString(), z2.toString(), z3.toString() );
<!-- C interface documentation. -->
* * *
<section class="c">
## C APIs
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
<section class="intro">
</section>
<!-- /.intro -->
<!-- C usage documentation. -->
<section class="usage">
### Usage
```c
#include "stdlib/math/base/ops/cmul.h"
```
#### stdlib_base_cmul( z1, z2 )
Multiplies two double-precision complex floating-point numbers.
```c
#include <complex.h>
double complex z1 = 5.0 + 3.0*I;
double complex z2 = -2.0 + 1.0*I;
double complex out = stdlib_base_cmul( z1, z2 );
// returns -13.0-1.0*I
```
The function accepts the following arguments:
- **z1**: `[in] double complex` input value.
- **z2**: `[in] double complex` input value.
```c
double complex stdlib_base_cmul( const double complex z1, const double complex z2 );
```
</section>
<!-- /.usage -->
<!-- C API usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="notes">
</section>
<!-- /.notes -->
<!-- C API usage examples. -->
<section class="examples">
### Examples
```c
#include "stdlib/math/base/ops/cmul.h"
#include <stdio.h>
#include <complex.h>
int main() {
double complex x[] = { 3.14+1.5*I, -3.14-1.5*I, 0.0+0.0*I, 0.0/0.0+0.0/0.0*I };
double complex v;
double complex y;
int i;
for ( i = 0; i < 4; i++ ) {
v = x[ i ];
y = stdlib_base_cmul( v, v );
printf( "z = %lf + %lfi\ncmul(z, z) = %lf + %lfi\n", creal( v ), cimag( v ), creal( y ), cimag( y ) );
}
}

@@ -123,3 +200,17 @@ ```

</section>
<!-- /.c -->
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
<section class="related">
</section>
<!-- /.related -->
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="main-repo" >

@@ -148,3 +239,3 @@

Copyright &copy; 2016-2021. The Stdlib [Authors][stdlib-authors].
Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].

@@ -168,5 +259,16 @@ </section>

<!--
[dependencies-image]: https://img.shields.io/david/stdlib-js/math-base-ops-cmul.svg
[dependencies-url]: https://david-dm.org/stdlib-js/math-base-ops-cmul/main
-->
[umd]: https://github.com/umdjs/umd
[es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
[deno-url]: https://github.com/stdlib-js/math-base-ops-cmul/tree/deno
[umd-url]: https://github.com/stdlib-js/math-base-ops-cmul/tree/umd
[esm-url]: https://github.com/stdlib-js/math-base-ops-cmul/tree/esm
[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg

@@ -173,0 +275,0 @@ [chat-url]: https://gitter.im/stdlib-js/stdlib/

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc