Skip to content
Draft
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
12 changes: 11 additions & 1 deletion lib/node_modules/@stdlib/math/base/special/covercos/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// MODULES //

var sin = require( '@stdlib/math/base/special/sin' );
var cos = require( '@stdlib/math/base/special/cos' );


// MAIN //
Expand Down Expand Up @@ -48,7 +49,16 @@ var sin = require( '@stdlib/math/base/special/sin' );
* // returns NaN
*/
function covercos( x ) {
return 1.0 + sin( x );
var s;
var c;

s = sin( x );
if ( s < 0.0 ) {
// Where `sin(x)` is close to minus one, `1 + sin(x)` cancels. Use `1 + sin(x) = cos(x)^2 / (1 - sin(x))` where `sin(x) < 0`, since `cos(x)` is computed with full relative accuracy near its zeros and the denominator does not cancel:
c = cos( x );
return ( c * c ) / ( 1.0 - s );
}
return 1.0 + s;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/special/sin"
"@stdlib/math/base/special/sin",
"@stdlib/math/base/special/cos"
]
},
{
Expand All @@ -51,7 +52,8 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/sin"
"@stdlib/math/base/special/sin",
"@stdlib/math/base/special/cos"
]
},
{
Expand All @@ -65,7 +67,8 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/sin"
"@stdlib/math/base/special/sin",
"@stdlib/math/base/special/cos"
]
}
]
Expand Down
12 changes: 11 additions & 1 deletion lib/node_modules/@stdlib/math/base/special/covercos/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "stdlib/math/base/special/covercos.h"
#include "stdlib/math/base/special/sin.h"
#include "stdlib/math/base/special/cos.h"

/**
* Computes the coversed cosine.
Expand All @@ -30,5 +31,14 @@
* // returns 2.0
*/
double stdlib_base_covercos( const double x ) {
return 1.0 + stdlib_base_sin( x );
double s;
double c;

s = stdlib_base_sin( x );
if ( s < 0.0 ) {
// Where `sin(x)` is close to minus one, `1 + sin(x)` cancels. Use `1 + sin(x) = cos(x)^2 / (1 - sin(x))` where `sin(x) < 0`, since `cos(x)` is computed with full relative accuracy near its zeros and the denominator does not cancel:
c = stdlib_base_cos( x );
return ( c * c ) / ( 1.0 - s );
}
return 1.0 + s;
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import JSON

"""
gen( domain, filepath )

Check failure on line 22 in lib/node_modules/@stdlib/math/base/special/covercos/test/fixtures/julia/runner.jl

View workflow job for this annotation

GitHub Actions / Lint Changed Files

wrong indentation type (spaces instead of tabs)

Generate fixture data and write to file.

Expand All @@ -37,7 +37,9 @@
"""
function gen( domain, filepath )
x = collect( domain );
y = 1.0 .+ sin.( x );
y = setprecision( BigFloat, 256 ) do
Float64.( 1 .+ sin.( BigFloat.( x ) ) );
end
data = Dict([
("x", x),
("expected", y)
Expand Down Expand Up @@ -83,3 +85,14 @@
x = range( 2.0^60*(pi/2.0), stop = 2.0^1000*(pi/2.0), length = 4000 )
out = joinpath( dir, "huge_positive.json" );
gen( x, out );

# Values close to where `sin(x)` is one or minus one, where the textbook formulas cancel:
x = Float64[];
for k in -10:10
for d in exp10.( range( -8.0, stop = -1.0, length = 50 ) )
push!( x, (2*k+0.5)*pi + d, (2*k+0.5)*pi - d, (2*k-0.5)*pi + d, (2*k-0.5)*pi - d );
end
end
out = joinpath( dir, "near_extrema.json" );
gen( x, out );

29 changes: 23 additions & 6 deletions lib/node_modules/@stdlib/math/base/special/covercos/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ var largeNegative = require( './fixtures/julia/large_negative.json' );
var largePositive = require( './fixtures/julia/large_positive.json' );
var hugeNegative = require( './fixtures/julia/huge_negative.json' );
var hugePositive = require( './fixtures/julia/huge_positive.json' );
var nearExtrema = require( './fixtures/julia/near_extrema.json' );


// TESTS //
Expand All @@ -57,7 +58,7 @@ tape( 'the function computes the coversed cosine (for -256*pi < x < 0)', functio

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -73,7 +74,7 @@ tape( 'the function computes the coversed cosine (for 0 < x < 256*pi)', function

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -89,7 +90,7 @@ tape( 'the function computes the coversed cosine (for -2**60 (pi/2) < x < -2**20

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -105,7 +106,7 @@ tape( 'the function computes the coversed cosine (for 2**20 (pi/2) < x < 2**60 (

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -121,7 +122,7 @@ tape( 'the function computes the coversed cosine (for x <= -2**60 (PI/2))', func

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -137,7 +138,23 @@ tape( 'the function computes the coversed cosine (for x >= 2**60 (PI/2))', funct

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the function computes the coversed cosine close to where `sin(x)` is one or minus one', function test( t ) {
var expected;
var x;
var y;
var i;

x = nearExtrema.x;
expected = nearExtrema.expected;

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ var largeNegative = require( './fixtures/julia/large_negative.json' );
var largePositive = require( './fixtures/julia/large_positive.json' );
var hugeNegative = require( './fixtures/julia/huge_negative.json' );
var hugePositive = require( './fixtures/julia/huge_positive.json' );
var nearExtrema = require( './fixtures/julia/near_extrema.json' );


// TESTS //
Expand All @@ -66,7 +67,7 @@ tape( 'the function computes the coversed cosine (for -256*pi < x < 0)', opts, f

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -82,7 +83,7 @@ tape( 'the function computes the coversed cosine (for 0 < x < 256*pi)', opts, fu

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -98,7 +99,7 @@ tape( 'the function computes the coversed cosine (for -2**60 (pi/2) < x < -2**20

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -114,7 +115,7 @@ tape( 'the function computes the coversed cosine (for 2**20 (pi/2) < x < 2**60 (

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -130,7 +131,7 @@ tape( 'the function computes the coversed cosine (for x <= -2**60 (PI/2))', opts

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 3 ), true, 'returns expected value' );
}
t.end();
});
Expand All @@ -146,7 +147,23 @@ tape( 'the function computes the coversed cosine (for x >= 2**60 (PI/2))', opts,

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 0 ), true, 'returns expected value' );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});

tape( 'the function computes the coversed cosine close to where `sin(x)` is one or minus one', opts, function test( t ) {
var expected;
var x;
var y;
var i;

x = nearExtrema.x;
expected = nearExtrema.expected;

for ( i = 0; i < x.length; i++ ) {
y = covercos( x[i] );
t.strictEqual( isAlmostSameValue( y, expected[ i ], 2 ), true, 'returns expected value' );
}
t.end();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
// MODULES //

var sin = require( '@stdlib/math/base/special/sin' );
var cos = require( '@stdlib/math/base/special/cos' );


// MAIN //
Expand Down Expand Up @@ -48,7 +49,16 @@ var sin = require( '@stdlib/math/base/special/sin' );
* // returns NaN
*/
function hacovercos( x ) {
return (1.0 + sin( x )) / 2.0;
var s;
var c;

s = sin( x );
if ( s < 0.0 ) {
// Where `sin(x)` is close to minus one, `1 + sin(x)` cancels. Use `1 + sin(x) = cos(x)^2 / (1 - sin(x))` where `sin(x) < 0`, since `cos(x)` is computed with full relative accuracy near its zeros and the denominator does not cancel:
c = cos( x );
return ( c * c ) / ( 2.0 * ( 1.0 - s ) );
}
return ( 1.0 + s ) / 2.0;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/special/sin"
"@stdlib/math/base/special/sin",
"@stdlib/math/base/special/cos"
]
},
{
Expand All @@ -51,7 +52,8 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/sin"
"@stdlib/math/base/special/sin",
"@stdlib/math/base/special/cos"
]
},
{
Expand All @@ -65,7 +67,8 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/sin"
"@stdlib/math/base/special/sin",
"@stdlib/math/base/special/cos"
]
}
]
Expand Down
12 changes: 11 additions & 1 deletion lib/node_modules/@stdlib/math/base/special/hacovercos/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "stdlib/math/base/special/hacovercos.h"
#include "stdlib/math/base/special/sin.h"
#include "stdlib/math/base/special/cos.h"

/**
* Computes the half-value coversed cosine.
Expand All @@ -30,5 +31,14 @@
* // returns 1.0
*/
double stdlib_base_hacovercos( const double x ) {
return ( 1.0 + stdlib_base_sin( x ) ) / 2.0;
double s;
double c;

s = stdlib_base_sin( x );
if ( s < 0.0 ) {
// Where `sin(x)` is close to minus one, `1 + sin(x)` cancels. Use `1 + sin(x) = cos(x)^2 / (1 - sin(x))` where `sin(x) < 0`, since `cos(x)` is computed with full relative accuracy near its zeros and the denominator does not cancel:
c = stdlib_base_cos( x );
return ( c * c ) / ( 2.0 * ( 1.0 - s ) );
}
return ( 1.0 + s ) / 2.0;
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import JSON

"""
gen( x, filepath )

Check failure on line 22 in lib/node_modules/@stdlib/math/base/special/hacovercos/test/fixtures/julia/runner.jl

View workflow job for this annotation

GitHub Actions / Lint Changed Files

wrong indentation type (spaces instead of tabs)

Generate fixture data and write to file.

Expand All @@ -36,7 +36,9 @@
```
"""
function gen( x, filepath )
y = (1.0 .+ sin.( x )) ./ 2.0;
y = setprecision( BigFloat, 256 ) do
Float64.( (1 .+ sin.( BigFloat.( x ) )) ./ 2 );
end
data = Dict([
("x", x),
("expected", y)
Expand Down Expand Up @@ -82,3 +84,14 @@
x = range( 2.0^60*(pi/2.0), stop = 2.0^1000*(pi/2.0), length = 4000 )
out = joinpath( dir, "huge_positive.json" );
gen( x, out );

# Values close to where `sin(x)` is one or minus one, where the textbook formulas cancel:
x = Float64[];
for k in -10:10
for d in exp10.( range( -8.0, stop = -1.0, length = 50 ) )
push!( x, (2*k+0.5)*pi + d, (2*k+0.5)*pi - d, (2*k-0.5)*pi + d, (2*k-0.5)*pi - d );
end
end
out = joinpath( dir, "near_extrema.json" );
gen( x, out );

Loading
Loading