Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
115 changes: 115 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/rsqrt/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<!--

@license Apache-2.0

Copyright (c) 2019 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.

-->

# Reciprocal Square Root

> Compute the reciprocal of the principal [square root][square-root].

<section class="intro">

Reciprocal of the principal [square root][square-root] is defined as

<!-- <equation class="equation" label="eq:reciprocal_square_root" align="center" raw="\operatorname{rsqrt}(x)=\frac{1}{\sqrt{x}}" alt="Reciprocal square root"> -->

<div class="equation" align="center" data-raw-text="\operatorname{rsqrt}(x)=\frac{1}{\sqrt{x}}" data-equation="eq:reciprocal_square_root">
<img src="" alt="Reciprocal square root">
<br>
</div>

<!-- </equation> -->

</section>

<!-- /.intro -->

<section class="usage">

## Usage

```javascript
var rsqrt = require( '@stdlib/math/base/special/rsqrt' );
```

#### rsqrt( x )

Computes the reciprocal (inverse) square root.

```javascript
var v = rsqrt( 1.0 );
// returns 1.0

v = rsqrt( 4.0 );
// returns 0.5

v = rsqrt( 100.0 );
// returns 0.1

v = rsqrt( 0.0 );
// returns Infinity

v = rsqrt( NaN );
// returns NaN

v = rsqrt( Infinity );
// returns 0.0
```

For negative numbers, the reciprocal square root is **not** defined.

```javascript
var v = rsqrt( -4.0 );
// returns NaN
```

</section>

<!-- /.usage -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var rsqrt = require( '@stdlib/math/base/special/rsqrt' );

var x;
var i;

for ( i = 0; i < 100; i++ ) {
x = round( randu() * 100.0 );
console.log( 'rsqrt(%d) = %d', x, rsqrt( x ) );
}
```

</section>

<!-- /.examples -->

<section class="links">

[square-root]: https://en.wikipedia.org/wiki/Square_root

</section>

<!-- /.links -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2019 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 bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var pkg = require( './../package.json' ).name;
var rsqrt = require( './../lib' );


// MAIN //

bench( pkg, function benchmark( b ) {
var x;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100000.0 ) - 0.0;
y = rsqrt( x );
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});

bench( pkg+'::built-in', function benchmark( b ) {
var x;
var y;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = ( randu()*100000.0 ) - 0.0;
y = 1.0 / Math.sqrt( x ); // eslint-disable-line stdlib/no-builtin-math
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
}
b.toc();
if ( isnan( y ) ) {
b.fail( 'should not return NaN' );
}
b.pass( 'benchmark finished' );
b.end();
});
34 changes: 34 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/rsqrt/docs/repl.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

{{alias}}( x )
Computes the reciprocal square root.

For `x < 0`, the reciprocal square root is not defined.

Parameters
----------
x: number
Input value.

Returns
-------
y: number
Reciprocal square root.

Examples
--------
> var y = {{alias}}( 4.0 )
0.5
> y = {{alias}}( 100.0 )
0.1
> y = {{alias}}( 0.0 )
0.0
> y = {{alias}}( Infinity )
0.0
> y = {{alias}}( -4.0 )
NaN
> y = {{alias}}( NaN )
NaN

See Also
--------

Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2019 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.
*/

// TypeScript Version: 2.0

/**
* Computes the reciprocal square root.
*
* ## Notes
*
* - For `x < 0`, the reciprocal square root is not defined.
*
* @param x - input value
* @returns reciprocal square root of `x`
*
* @example
* var v = rsqrt( 4.0 );
* // returns 0.5
*
* @example
* var v = rsqrt( 100.0 );
* // returns 0.1
*
* @example
* var v = rsqrt( 0.0 );
* // returns 0.0
*
* @example
* var v = rsqrt( Infinity );
* // returns 0.0
*
* @example
* var v = rsqrt( -4.0 );
* // returns NaN
*
* @example
* var v = rsqrt( NaN );
* // returns NaN
*/
declare function rsqrt( x: number ): number;


// EXPORTS //

export = rsqrt;
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2019 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.
*/

import rsqrt = require( './index' );


// TESTS //

// The function returns a number...
{
rsqrt( 2 ); // $ExpectType number
}

// The function does not compile if provided a value other than a number...
{
rsqrt( true ); // $ExpectError
rsqrt( false ); // $ExpectError
rsqrt( null ); // $ExpectError
rsqrt( undefined ); // $ExpectError
rsqrt( '5' ); // $ExpectError
rsqrt( [] ); // $ExpectError
rsqrt( {} ); // $ExpectError
rsqrt( ( x: number ): number => x ); // $ExpectError
}

// The function does not compile if provided insufficient arguments...
{
rsqrt(); // $ExpectError
}
31 changes: 31 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/rsqrt/examples/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2019 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';

var randu = require( '@stdlib/random/base/randu' );
var round = require( '@stdlib/math/base/special/round' );
var rsqrt = require( './../lib' );

var x;
var i;

for ( i = 0; i < 100; i++ ) {
x = round( randu() * 100.0 );
console.log( 'rsqrt(%d) = %d', x, rsqrt( x ) );
}
Loading