README.md (3127B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # abs2 22 23 > Compute the squared [absolute value][absolute-value] of a complex number. 24 25 <section class="intro"> 26 27 The [absolute value][absolute-value] of a complex number is defined as 28 29 <!-- <equation class="equation" label="eq:absolute_value_complex" align="center" raw="|a + bi| = \sqrt{a^2 + b^2}" alt="Absolute value"> --> 30 31 <div class="equation" align="center" data-raw-text="|a + bi| = \sqrt{a^2 + b^2}" data-equation="eq:absolute_value_complex"> 32 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@d4edb68b52a6c646be5683023c5a24890300727f/lib/node_modules/@stdlib/math/base/special/cabs2/docs/img/equation_absolute_value_complex.svg" alt="Absolute value"> 33 <br> 34 </div> 35 36 <!-- </equation> --> 37 38 which corresponds to the length of a vector from the origin to a complex value plotted in the complex plane. 39 40 </section> 41 42 <!-- /.intro --> 43 44 <section class="usage"> 45 46 ## Usage 47 48 ```javascript 49 var cabs2 = require( '@stdlib/math/base/special/cabs2' ); 50 ``` 51 52 #### cabs2( re, im ) 53 54 Computes the squared [absolute value][absolute-value] of a `complex` number comprised of a **real** component `re` and an **imaginary** component `im`. 55 56 ```javascript 57 var y = cabs2( 5.0, 3.0 ); 58 // returns 34.0 59 ``` 60 61 </section> 62 63 <!-- /.usage --> 64 65 <section class="notes"> 66 67 ## Notes 68 69 - Be careful to avoid overflow and underflow. 70 - Depending on the environment, this function _may_ have better performance than computing the [absolute value][absolute-value] of a `complex` number and then squaring. Hence, where appropriate, consider using `cabs2()` over [`cabs()`][@stdlib/math/base/special/cabs]. 71 72 </section> 73 74 <!-- /.notes --> 75 76 <section class="examples"> 77 78 ## Examples 79 80 <!-- eslint no-undef: "error" --> 81 82 ```javascript 83 var Complex128 = require( '@stdlib/complex/float64' ); 84 var randu = require( '@stdlib/random/base/randu' ); 85 var round = require( '@stdlib/math/base/special/round' ); 86 var real = require( '@stdlib/complex/real' ); 87 var imag = require( '@stdlib/complex/imag' ); 88 var cabs2 = require( '@stdlib/math/base/special/cabs2' ); 89 90 var re; 91 var im; 92 var z; 93 var i; 94 95 for ( i = 0; i < 100; i++ ) { 96 re = round( randu()*100.0 ) - 50.0; 97 im = round( randu()*100.0 ) - 50.0; 98 z = new Complex128( re, im ); 99 console.log( 'cabs2(%s) = %d', z.toString(), cabs2( real(z), imag(z) ) ); 100 } 101 ``` 102 103 </section> 104 105 <!-- /.examples --> 106 107 <section class="links"> 108 109 [absolute-value]: https://en.wikipedia.org/wiki/Absolute_value 110 111 [@stdlib/math/base/special/cabs]: https://www.npmjs.com/package/@stdlib/math/tree/main/base/special/cabs 112 113 </section> 114 115 <!-- /.links -->