Skip to content

refactor: update math/base/special/cabsf to follow latest project conventions #3358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 21 commits into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 25 additions & 32 deletions lib/node_modules/@stdlib/math/base/special/cabsf/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

@license Apache-2.0

Copyright (c) 2021 The Stdlib Authors.
Copyright (c) 2024 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.
Expand Down Expand Up @@ -33,7 +33,7 @@ The [absolute value][absolute-value] of a [complex][@stdlib/complex/float32/ctor
```

<!-- <div class="equation" align="center" data-raw-text="|a + bi| = \sqrt{a^2 + b^2}" data-equation="eq:absolute_value_complex">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@462b40597988f604d5c05a46279cbb16105a24d3/lib/node_modules/@stdlib/math/base/special/cabsf/docs/img/equation_absolute_value_complex.svg" alt="Absolute value">
<img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@d4edb68b52a6c646be5683023c5a24890300727f/lib/node_modules/@stdlib/math/base/special/cabsf/docs/img/equation_absolute_value_complex.svg" alt="Absolute value">
<br>
</div> -->

Expand Down Expand Up @@ -78,17 +78,13 @@ var y = cabsf( new Complex64( 5.0, 3.0 ) );

```javascript
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' );
var cabsf = require( '@stdlib/math/base/special/cabsf' );

// Create a PRNG to generate uniformly distributed pseudorandom integers:
var rand = discreteUniform( -50, 50 );

// Compute the absolute value for a set of random numbers...
var z;
var i;
for ( i = 0; i < 100; i++ ) {
z = new Complex64( rand(), rand() );
z = new Complex64( discreteUniform( -50, 50 ), discreteUniform( -50, 50 ) );
console.log( 'cabsf(%s) = %d', z.toString(), cabsf( z ) );
}
```
Expand Down Expand Up @@ -125,21 +121,23 @@ for ( i = 0; i < 100; i++ ) {

#### stdlib_base_cabsf( z )

Computes an [absolute value][absolute-value] of a single-precision complex floating-point number.
Computes the [absolute value][absolute-value] of a single-precision complex floating-point number.

```c
#include <complex.h>
#include "stdlib/complex/float32/ctor.h"

stdlib_complex64_t z = stdlib_complex64( 5.0, 3.0 );

float y = stdlib_base_cabsf( 5.0+3.0*I );
float y = stdlib_base_cabsf( z );
// returns ~5.83f
```

The function accepts the following arguments:

- **z**: `[in] float complex` input value.
- **z**: `[in] stdlib_complex64_t` input value.

```c
float stdlib_base_cabsf( const float complex z );
float stdlib_base_cabsf( const stdlib_complex64_t complex z );
```

</section>
Expand All @@ -162,19 +160,28 @@ float stdlib_base_cabsf( const float complex z );

```c
#include "stdlib/math/base/special/cabsf.h"
#include "stdlib/complex/float32/ctor.h"
#include "stdlib/complex/float32/reim.h"
#include <stdio.h>
#include <complex.h>

int main( void ) {
const float complex x[] = { 3.14f+1.0f*I, -3.14f-1.0f*I, 0.0f+0.0f*I, 0.0f/0.0f+0.0f/0.0f*I };

float complex v;
const stdlib_complex64_t x[] = {
stdlib_complex64( 3.14f, 1.0f ),
stdlib_complex64( -3.14f, -1.0f ),
stdlib_complex64( 0.0f, 0.0f ),
stdlib_complex64( 0.0f / 0.0f, 0.0f / 0.0f )
};

stdlib_complex64_t v;
float re;
float im;
float y;
int i;
for ( i = 0; i < 4; i++ ) {
v = x[ i ];
y = stdlib_base_cabsf( v );
printf( "f(%f + %f) = %f\n", crealf( v ), cimagf( v ), y );
stdlib_complex64_reim( v, &re, &im );
printf( "f(%f + %f) = %f\n", re, im, y );
}
}
```
Expand All @@ -191,14 +198,6 @@ int main( void ) {

<section class="related">

* * *

## See Also

- <span class="package-name">[`@stdlib/math/base/special/cabs`][@stdlib/math/base/special/cabs]</span><span class="delimiter">: </span><span class="description">compute the absolute value of a double-precision complex floating-point number.</span>
- <span class="package-name">[`@stdlib/math/base/special/cabs2f`][@stdlib/math/base/special/cabs2f]</span><span class="delimiter">: </span><span class="description">compute the squared absolute value of a single-precision complex floating-point number.</span>
- <span class="package-name">[`@stdlib/math/base/special/absf`][@stdlib/math/base/special/absf]</span><span class="delimiter">: </span><span class="description">compute the absolute value of a single-precision floating-point number.</span>

</section>

<!-- /.related -->
Expand All @@ -213,12 +212,6 @@ int main( void ) {

<!-- <related-links> -->

[@stdlib/math/base/special/cabs]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cabs

[@stdlib/math/base/special/cabs2f]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/cabs2f

[@stdlib/math/base/special/absf]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/math/base/special/absf

<!-- </related-links> -->

</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ bench( pkg, function benchmark( b ) {

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = cabsf( values[ i%values.length ] );
y = cabsf( values[ i % values.length ] );
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2024 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 resolve = require( 'path' ).resolve;
var bench = require( '@stdlib/bench' );
var uniform = require( '@stdlib/random/base/uniform' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var Complex64 = require( '@stdlib/complex/float32/ctor' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

var cabsf = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( cabsf instanceof Error )
};


// MAIN //

bench( pkg+'::native', opts, function benchmark( b ) {
var values;
var y;
var i;

values = [
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) ),
new Complex64( uniform( -500.0, 500.0 ), uniform( -500.0, 500.0 ) )
];

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
y = cabsf( values[ i % values.length ] );
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnanf( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand Down Expand Up @@ -78,7 +78,7 @@ static double tic( void ) {
*
* @return random number
*/
static float rand_float( void ) {
static double rand_float( void ) {
int r = rand();
return (float)r / ( (float)RAND_MAX + 1.0f );
}
Expand All @@ -91,16 +91,16 @@ static float rand_float( void ) {
static double benchmark( void ) {
float complex z;
double elapsed;
double t;
float re;
float im;
float y;
double t;
int i;

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
re = ( 1000.0f*rand_float() ) - 500.0f;
im = ( 1000.0f*rand_float() ) - 500.0f;
re = ( 1000.0f * rand_float() ) - 500.0f;
im = ( 1000.0f * rand_float() ) - 500.0f;
z = re + im*I;
y = cabsf( z );
if ( y != y ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2021 The Stdlib Authors.
* Copyright (c) 2024 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.
Expand All @@ -17,7 +17,8 @@
*/

#include "stdlib/math/base/special/cabsf.h"
#include <complex.h>
#include "stdlib/complex/float32/ctor.h"
#include "stdlib/complex/float32/reim.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
Expand Down Expand Up @@ -91,19 +92,23 @@ static float rand_float( void ) {
* @return elapsed time in seconds
*/
static double benchmark( void ) {
float complex z;
double elapsed;
double t;
float re;
float im;
float re[ 100 ];
float im[ 100 ];
float y;
double t;
int i;

for ( i = 0; i < 100; i++ ) {
re[ i ] = ( 1000.0f * rand_float() ) - 500.0f;
im[ i ] = ( 1000.0f * rand_float() ) - 500.0f;
}

stdlib_complex64_t z;

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
re = ( 1000.0f*rand_float() ) - 500.0f;
im = ( 1000.0f*rand_float() ) - 500.0f;
z = re + im*I;
z = stdlib_complex64( re[ i % 100 ], im[ i % 100 ] );
y = stdlib_base_cabsf( z );
if ( y != y ) {
printf( "should not return NaN\n" );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# @license Apache-2.0
#
# Copyright (c) 2021 The Stdlib Authors.
# Copyright (c) 2024 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.
Expand Down
Loading
Loading