@stdlib/math-base-special-cabs
Advanced tools
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2021 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #ifndef STDLIB_MATH_BASE_SPECIAL_CABS_H | ||
| #define STDLIB_MATH_BASE_SPECIAL_CABS_H | ||
| #include <complex.h> | ||
| /* | ||
| * If C++, prevent name mangling so that the compiler emits a binary file having undecorated names, thus mirroring the behavior of a C compiler. | ||
| */ | ||
| #ifdef __cplusplus | ||
| extern "C" { | ||
| #endif | ||
| /** | ||
| * Computes the absolute value of a double-precision complex floating-point number. | ||
| */ | ||
| double stdlib_base_cabs( const double complex z ); | ||
| #ifdef __cplusplus | ||
| } | ||
| #endif | ||
| #endif // !STDLIB_MATH_BASE_SPECIAL_CABS_H |
+50
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2018 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| 'use strict'; | ||
| // MODULES // | ||
| var hypot = require( '@stdlib/math-base-special-hypot' ); | ||
| var real = require( '@stdlib/complex-real' ); | ||
| var imag = require( '@stdlib/complex-imag' ); | ||
| // MAIN // | ||
| /** | ||
| * Computes the absolute value of a double-precision complex floating-point number. | ||
| * | ||
| * @param {Complex128} z - complex number | ||
| * @returns {number} absolute value | ||
| * | ||
| * @example | ||
| * var Complex128 = require( '@stdlib/complex-float64' ); | ||
| * | ||
| * var v = cabs( new Complex128( 5.0, 3.0 ) ); | ||
| * // returns ~5.83 | ||
| */ | ||
| function cabs( z ) { | ||
| // TODO: consider whether to use C99 rules for special cases involving infinities and nans (see https://github.com/python/cpython/blob/f4c03484da59049eb62a9bf7777b963e2267d187/Objects/complexobject.c#L191) | ||
| return hypot( real( z ), imag( z ) ); | ||
| } | ||
| // EXPORTS // | ||
| module.exports = cabs; |
| { | ||
| "options": {}, | ||
| "fields": [ | ||
| { | ||
| "field": "src", | ||
| "resolve": true, | ||
| "relative": true | ||
| }, | ||
| { | ||
| "field": "include", | ||
| "resolve": true, | ||
| "relative": true | ||
| }, | ||
| { | ||
| "field": "libraries", | ||
| "resolve": false, | ||
| "relative": false | ||
| }, | ||
| { | ||
| "field": "libpath", | ||
| "resolve": true, | ||
| "relative": false | ||
| } | ||
| ], | ||
| "confs": [ | ||
| { | ||
| "src": [ | ||
| "./src/main.c" | ||
| ], | ||
| "include": [ | ||
| "./include" | ||
| ], | ||
| "libraries": [], | ||
| "libpath": [], | ||
| "dependencies": [ | ||
| "@stdlib/math-base-special-hypot" | ||
| ] | ||
| } | ||
| ] | ||
| } |
+35
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2021 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| #include "stdlib/math/base/special/cabs.h" | ||
| #include "stdlib/math/base/special/hypot.h" | ||
| #include <complex.h> | ||
| /** | ||
| * Computes the absolute value of a double-precision complex floating-point number. | ||
| * | ||
| * @param z number | ||
| * @return result | ||
| * | ||
| * @example | ||
| * double y = stdlib_base_cabs( 5.0+3.0*I ); | ||
| * // returns ~5.83 | ||
| */ | ||
| double stdlib_base_cabs( const double complex z ) { | ||
| return stdlib_base_hypot( creal( z ), cimag( z ) ); | ||
| } |
+6
-8
| {{alias}}( re, im ) | ||
| Computes the absolute value of a complex number. | ||
| {{alias}}( z ) | ||
| Computes the absolute value of a double-precision complex floating-point | ||
| number. | ||
@@ -9,8 +10,5 @@ The absolute value of a complex number is its distance from zero. | ||
| ---------- | ||
| re: number | ||
| Real component. | ||
| z: Complex128 | ||
| Complex number. | ||
| im: number | ||
| Imaginary component. | ||
| Returns | ||
@@ -23,3 +21,3 @@ ------- | ||
| -------- | ||
| > var y = {{alias}}( 5.0, 3.0 ) | ||
| > var y = {{alias}}( new {{alias:@stdlib/complex/float64}}( 5.0, 3.0 ) ) | ||
| ~5.831 | ||
@@ -26,0 +24,0 @@ |
@@ -21,4 +21,8 @@ /* | ||
| /// <reference types="@stdlib/types"/> | ||
| import { Complex128 } from '@stdlib/types/object'; | ||
| /** | ||
| * Computes the absolute value of a complex number. | ||
| * Computes the absolute value of a double-precision complex floating-point number. | ||
| * | ||
@@ -29,11 +33,12 @@ * ## Notes | ||
| * | ||
| * @param re - real component | ||
| * @param im - imaginary component | ||
| * @param z - complex number | ||
| * @returns absolute value | ||
| * | ||
| * @example | ||
| * var v = cabs( 5.0, 3.0 ); | ||
| * var Complex128 = require( `@stdlib/complex/float64` ); | ||
| * | ||
| * var v = cabs( new Complex128( 5.0, 3.0 ) ); | ||
| * // returns ~5.83 | ||
| */ | ||
| declare function cabs( re: number, im: number ): number; | ||
| declare function cabs( z: Complex128 ): number; | ||
@@ -40,0 +45,0 @@ |
+11
-22
@@ -19,2 +19,3 @@ /* | ||
| import Complex128 = require( '@stdlib/complex-float64' ); | ||
| import cabs = require( './index' ); | ||
@@ -27,29 +28,17 @@ | ||
| { | ||
| cabs( 5, 3 ); // $ExpectType number | ||
| cabs( new Complex128( 5, 3 ) ); // $ExpectType number | ||
| } | ||
| // The compiler throws an error if the function is provided a first argument which is not a number... | ||
| // The compiler throws an error if the function is not provided a complex number... | ||
| { | ||
| cabs( true, 3 ); // $ExpectError | ||
| cabs( false, 3 ); // $ExpectError | ||
| cabs( null, 3 ); // $ExpectError | ||
| cabs( undefined, 3 ); // $ExpectError | ||
| cabs( '5', 3 ); // $ExpectError | ||
| cabs( [], 3 ); // $ExpectError | ||
| cabs( {}, 3 ); // $ExpectError | ||
| cabs( ( x: number ): number => x, 3 ); // $ExpectError | ||
| cabs( true ); // $ExpectError | ||
| cabs( false ); // $ExpectError | ||
| cabs( null ); // $ExpectError | ||
| cabs( undefined ); // $ExpectError | ||
| cabs( '5' ); // $ExpectError | ||
| cabs( [] ); // $ExpectError | ||
| cabs( {} ); // $ExpectError | ||
| cabs( ( x: number ): number => x ); // $ExpectError | ||
| } | ||
| // The compiler throws an error if the function is provided a second argument which is not a number... | ||
| { | ||
| cabs( 5, true ); // $ExpectError | ||
| cabs( 5, false ); // $ExpectError | ||
| cabs( 5, null ); // $ExpectError | ||
| cabs( 5, undefined ); // $ExpectError | ||
| cabs( 5, '5' ); // $ExpectError | ||
| cabs( 5, [] ); // $ExpectError | ||
| cabs( 5, {} ); // $ExpectError | ||
| cabs( 5, ( x: number ): number => x ); // $ExpectError | ||
| } | ||
| // The compiler throws an error if the function is provided insufficient arguments... | ||
@@ -56,0 +45,0 @@ { |
+5
-4
@@ -22,3 +22,3 @@ /** | ||
| /** | ||
| * Compute an absolute value of a complex number. | ||
| * Compute the absolute value of a double-precision complex floating-point number. | ||
| * | ||
@@ -28,5 +28,6 @@ * @module @stdlib/math-base-special-cabs | ||
| * @example | ||
| * var Complex128 = require( '@stdlib/complex-float64' ); | ||
| * var cabs = require( '@stdlib/math-base-special-cabs' ); | ||
| * | ||
| * var v = cabs( 5.0, 3.0 ); | ||
| * var v = cabs( new Complex128( 5.0, 3.0 ) ); | ||
| * // returns ~5.83 | ||
@@ -37,3 +38,3 @@ */ | ||
| var cabs = require( './cabs.js' ); | ||
| var main = require( './main.js' ); | ||
@@ -43,2 +44,2 @@ | ||
| module.exports = cabs; | ||
| module.exports = main; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| Copyright (c) 2016-2021 The Stdlib Authors. | ||
| Copyright (c) 2016-2022 The Stdlib Authors. |
+11
-7
| { | ||
| "name": "@stdlib/math-base-special-cabs", | ||
| "version": "0.0.6", | ||
| "description": "Compute an absolute value of a complex number.", | ||
| "version": "0.0.7", | ||
| "description": "Compute the absolute value of a double-precision complex floating-point number.", | ||
| "license": "Apache-2.0", | ||
@@ -21,3 +21,5 @@ "author": { | ||
| "example": "./examples", | ||
| "include": "./include", | ||
| "lib": "./lib", | ||
| "src": "./src", | ||
| "test": "./test" | ||
@@ -41,3 +43,7 @@ }, | ||
| "dependencies": { | ||
| "@stdlib/math-base-special-hypot": "^0.0.x" | ||
| "@stdlib/complex-imag": "^0.0.x", | ||
| "@stdlib/complex-real": "^0.0.x", | ||
| "@stdlib/math-base-special-hypot": "^0.0.x", | ||
| "@stdlib/types": "^0.0.x", | ||
| "@stdlib/utils-library-manifest": "^0.0.x" | ||
| }, | ||
@@ -47,9 +53,7 @@ "devDependencies": { | ||
| "@stdlib/complex-float64": "^0.0.x", | ||
| "@stdlib/complex-imag": "^0.0.x", | ||
| "@stdlib/complex-real": "^0.0.x", | ||
| "@stdlib/constants-float64-eps": "^0.0.x", | ||
| "@stdlib/math-base-assert-is-nan": "^0.0.x", | ||
| "@stdlib/math-base-special-abs": "^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", | ||
@@ -56,0 +60,0 @@ "istanbul": "^0.4.1", |
+143
-19
@@ -21,11 +21,11 @@ <!-- | ||
| # Absolute Value | ||
| # cabs | ||
| [![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] --> | ||
| > Compute an [absolute value][absolute-value] of a complex number. | ||
| > Compute the [absolute value][absolute-value] of a double-precision [complex][@stdlib/complex/float64] floating-point number. | ||
| <section class="intro"> | ||
| The [absolute value][absolute-value] of a complex number is defined as | ||
| The [absolute value][absolute-value] of a [complex][@stdlib/complex/float64] number is defined as | ||
@@ -65,8 +65,10 @@ <!-- <equation class="equation" label="eq:absolute_value_complex" align="center" raw="|a + bi| = \sqrt{a^2 + b^2}" alt="Absolute value"> --> | ||
| #### cabs( re, im ) | ||
| #### cabs( z ) | ||
| Computes an [absolute value][absolute-value] of a `complex` number comprised of a **real** component `re` and an **imaginary** component `im`. | ||
| Computes an [absolute value][absolute-value] of a double-precision [complex][@stdlib/complex/float64] floating-point number. | ||
| ```javascript | ||
| var y = cabs( 5.0, 3.0 ); | ||
| var Complex128 = require( '@stdlib/complex-float64' ); | ||
| var y = cabs( new Complex128( 5.0, 3.0 ) ); | ||
| // returns ~5.83 | ||
@@ -83,2 +85,4 @@ ``` | ||
| <!-- eslint-disable max-len --> | ||
| <!-- eslint no-undef: "error" --> | ||
@@ -88,18 +92,10 @@ | ||
| 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' ); | ||
| var cabs = require( '@stdlib/math-base-special-cabs' ); | ||
| var re; | ||
| var im; | ||
| var z; | ||
| var i; | ||
| for ( i = 0; i < 100; i++ ) { | ||
| re = round( randu()*100.0 ) - 50.0; | ||
| im = round( randu()*100.0 ) - 50.0; | ||
| z = new Complex128( re, im ); | ||
| console.log( 'cabs(%s) = %d', z.toString(), cabs( real(z), imag(z) ) ); | ||
| z = new Complex128( discreteUniform( -50, 50 ), discreteUniform( -50, 50 ) ); | ||
| console.log( 'cabs(%s) = %d', z.toString(), cabs( z ) ); | ||
| } | ||
@@ -112,3 +108,110 @@ ``` | ||
| <!-- 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/special/cabs.h" | ||
| ``` | ||
| #### stdlib_base_cabs( z ) | ||
| Computes the [absolute value][absolute-value] of a double-precision complex floating-point number. | ||
| ```c | ||
| #include <complex.h> | ||
| double y = stdlib_base_cabs( 5.0+3.0*I ); | ||
| // returns ~5.83 | ||
| ``` | ||
| The function accepts the following arguments: | ||
| - **z**: `[in] double complex` input value. | ||
| ```c | ||
| double stdlib_base_cabs( const double complex z ); | ||
| ``` | ||
| </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/special/cabs.h" | ||
| #include <stdio.h> | ||
| #include <complex.h> | ||
| int main() { | ||
| double complex x[] = { 3.14+1.0*I, -3.14-1.0*I, 0.0+0.0*I, 0.0/0.0+0.0/0.0*I }; | ||
| double complex v; | ||
| double y; | ||
| int i; | ||
| for ( i = 0; i < 4; i++ ) { | ||
| v = x[ i ]; | ||
| y = stdlib_base_cabs( v ); | ||
| printf( "f(%lf + %lf) = %lf\n", creal( v ), cimag( v ), y ); | ||
| } | ||
| } | ||
| ``` | ||
| </section> | ||
| <!-- /.examples --> | ||
| </section> | ||
| <!-- /.c --> | ||
| <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> | ||
| <section class="related"> | ||
| * * * | ||
| ## See Also | ||
| - <span class="package-name">[`@stdlib/math/base/special/cabs2`][@stdlib/math/base/special/cabs2]</span><span class="delimiter">: </span><span class="description">compute the squared absolute value of a complex number.</span> | ||
| - <span class="package-name">[`@stdlib/math/base/special/abs`][@stdlib/math/base/special/abs]</span><span class="delimiter">: </span><span class="description">compute the absolute value of a double-precision floating-point number.</span> | ||
| </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" > | ||
@@ -137,3 +240,3 @@ | ||
| Copyright © 2016-2021. The Stdlib [Authors][stdlib-authors]. | ||
| Copyright © 2016-2022. The Stdlib [Authors][stdlib-authors]. | ||
@@ -157,5 +260,16 @@ </section> | ||
| <!-- | ||
| [dependencies-image]: https://img.shields.io/david/stdlib-js/math-base-special-cabs.svg | ||
| [dependencies-url]: https://david-dm.org/stdlib-js/math-base-special-cabs/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-special-cabs/tree/deno | ||
| [umd-url]: https://github.com/stdlib-js/math-base-special-cabs/tree/umd | ||
| [esm-url]: https://github.com/stdlib-js/math-base-special-cabs/tree/esm | ||
| [chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg | ||
@@ -172,4 +286,14 @@ [chat-url]: https://gitter.im/stdlib-js/stdlib/ | ||
| [@stdlib/complex/float64]: https://www.npmjs.com/package/@stdlib/complex-float64 | ||
| <!-- <related-links> --> | ||
| [@stdlib/math/base/special/cabs2]: https://www.npmjs.com/package/@stdlib/math-base-special-cabs2 | ||
| [@stdlib/math/base/special/abs]: https://www.npmjs.com/package/@stdlib/math-base-special-abs | ||
| <!-- </related-links> --> | ||
| </section> | ||
| <!-- /.links --> |
-46
| /** | ||
| * @license Apache-2.0 | ||
| * | ||
| * Copyright (c) 2018 The Stdlib Authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| 'use strict'; | ||
| // MODULES // | ||
| var hypot = require( '@stdlib/math-base-special-hypot' ); | ||
| // MAIN // | ||
| /** | ||
| * Computes the absolute value of a complex number. | ||
| * | ||
| * @param {number} re - real component | ||
| * @param {number} im - imaginary component | ||
| * @returns {number} absolute value | ||
| * | ||
| * @example | ||
| * var v = cabs( 5.0, 3.0 ); | ||
| * // returns ~5.83 | ||
| */ | ||
| function cabs( re, im ) { | ||
| return hypot( re, im ); | ||
| } | ||
| // EXPORTS // | ||
| module.exports = cabs; |
46798
15.17%10
-16.67%13
30%193
24.52%292
73.81%5
400%+ Added
+ Added
+ Added
+ Added
+ Added