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
7 changes: 5 additions & 2 deletions lib/node_modules/@stdlib/math/base/special/cosm1/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

// MODULES //

var cos = require( '@stdlib/math/base/special/cos' );
var sin = require( '@stdlib/math/base/special/sin' );
var polyval = require( './polyval_p.js' );


Expand Down Expand Up @@ -73,8 +73,11 @@ var PIO4 = 7.85398163397448309616e-1; // 4/π
*/
function cosm1( x ) {
var x2;
var s;
if ( x < -PIO4 || x > PIO4 ) {
return cos( x ) - 1.0;
// Outside `[-π/4,π/4]`, `cos(x) - 1` cancels where `cos(x)` is close to one (near multiples of 2π). Use the half-angle identity `cos(x) - 1 = -2 sin^2(x/2)` instead, which has no cancellation:
s = sin( x / 2.0 );
return -2.0 * s * s;
}
x2 = x * x;
return ( -0.5*x2 ) + ( x2*x2*polyval( x2 ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/special/cos"
"@stdlib/math/base/special/sin"
]
},
{
Expand All @@ -51,7 +51,7 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/cos"
"@stdlib/math/base/special/sin"
]
},
{
Expand All @@ -65,7 +65,7 @@
"libraries": [],
"libpath": [],
"dependencies": [
"@stdlib/math/base/special/cos"
"@stdlib/math/base/special/sin"
]
}
]
Expand Down
7 changes: 5 additions & 2 deletions lib/node_modules/@stdlib/math/base/special/cosm1/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*/

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

static const double PIO4 = 7.85398163397448309616e-1; // 4/π

Expand Down Expand Up @@ -71,8 +71,11 @@ static double polyval_p( const double x ) {
*/
double stdlib_base_cosm1( const double x ) {
double x2;
double s;
if ( x < -PIO4 || x > PIO4 ) {
return stdlib_base_cos( x ) - 1.0;
// Outside `[-π/4,π/4]`, `cos(x) - 1` cancels where `cos(x)` is close to one (near multiples of 2π). Use the half-angle identity `cos(x) - 1 = -2 sin^2(x/2)` instead, which has no cancellation:
s = stdlib_base_sin( x / 2.0 );
return -2.0 * s * s;
}
x2 = x * x;
return ( -0.5 * x2 ) + ( x2 * x2 * polyval_p( x2 ) );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
julia 1.5
JSON 0.21

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
@@ -0,0 +1,82 @@
#!/usr/bin/env julia
#
# @license Apache-2.0
#
# Copyright (c) 2026 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 JSON

"""
gen( domain, name )

Generate fixture data and write to file.

The cosine minus one is evaluated in extended precision, as `cos(x) - 1` cancels near multiples of 2π in `Float64`.

# Arguments

* `domain`: domain
* `name::AbstractString`: output filename

# Examples

``` julia
julia> x = range( 1.0, stop = 10.0, length = 2001 );
julia> gen( x, "data.json" );
```
"""
function gen( domain, name )
x = collect( domain );
y = setprecision( BigFloat, 256 ) do
Float64.( cos.( BigFloat.( x ) ) .- 1 );
end

# Store data to be written to file as a collection:
data = Dict([
("x", x),
("expected", y)
]);

# Based on the script directory, create an output filepath:
filepath = joinpath( dir, name );

# Write the data to the output filepath as JSON:
outfile = open( filepath, "w" );
write( outfile, JSON.json(data) );
write( outfile, "\n" );
close( outfile );
end

# Get the filename:
file = @__FILE__;

# Extract the directory in which this file resides:
dir = dirname( file );

# Values outside `[-π/4,π/4]`:
x = range( pi/4, stop = 4*pi, length = 1000 );
gen( x, "positive.json" );

x = range( -4*pi, stop = -pi/4, length = 1000 );
gen( x, "negative.json" );

# Values close to multiples of 2π:
x = Float64[];
for k in 1:10
for d in exp10.( range( -8.0, stop = -1.0, length = 50 ) )
push!( x, 2*pi*k + d, 2*pi*k - d, -2*pi*k + d, -2*pi*k - d );
end
end
gen( x, "near_two_pi.json" );
48 changes: 31 additions & 17 deletions lib/node_modules/@stdlib/math/base/special/cosm1/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,19 @@
// MODULES //

var tape = require( 'tape' );
var linspace = require( '@stdlib/array/base/linspace' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var cos = require( '@stdlib/math/base/special/cos' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var PI = require( '@stdlib/constants/float64/pi' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );
var cosm1 = require( './../lib' );


// VARIABLES //

var NPIO4 = -7.85398163397448309616e-1; // -π/4
var PIO4 = 7.85398163397448309616e-1; // π/4


// FIXTURES //

var data = require( './fixtures/python/data.json' );
var positive = require( './fixtures/julia/positive.json' );
var negative = require( './fixtures/julia/negative.json' );
var nearTwoPi = require( './fixtures/julia/near_two_pi.json' );


// TESTS //
Expand Down Expand Up @@ -67,28 +61,48 @@ tape( 'the function computes the cosine minus one more accurately inside the int
t.end();
});

tape( 'the function computes `cos(x) - 1.0` outside the interval [-π/4,π/4]', function test( t ) {
tape( 'the function computes the cosine minus one outside the interval [-π/4,π/4] (positive values)', function test( t ) {
var expected;
var x;
var y;
var i;

x = linspace( PIO4, 4.0*PI, 100 );

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

x = linspace( -4.0*PI, NPIO4, 100 );
tape( 'the function computes the cosine minus one outside the interval [-π/4,π/4] (negative values)', function test( t ) {
var expected;
var x;
var y;
var i;

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

tape( 'the function computes the cosine minus one close to multiples of 2π', function test( t ) {
var expected;
var x;
var y;
var i;

x = nearTwoPi.x;
expected = nearTwoPi.expected;
for ( i = 0; i < x.length; i++ ) {
y = cosm1( 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 @@ -22,20 +22,15 @@

var resolve = require( 'path' ).resolve;
var tape = require( 'tape' );
var linspace = require( '@stdlib/array/base/linspace' );
var isnan = require( '@stdlib/math/base/assert/is-nan' );
var cos = require( '@stdlib/math/base/special/cos' );
var PINF = require( '@stdlib/constants/float64/pinf' );
var NINF = require( '@stdlib/constants/float64/ninf' );
var PI = require( '@stdlib/constants/float64/pi' );
var isAlmostSameValue = require( '@stdlib/assert/is-almost-same-value' );
var tryRequire = require( '@stdlib/utils/try-require' );


// VARIABLES //

var NPIO4 = -7.85398163397448309616e-1; // -π/4
var PIO4 = 7.85398163397448309616e-1; // π/4
var cosm1 = tryRequire( resolve( __dirname, './../lib/native.js' ) );
var opts = {
'skip': ( cosm1 instanceof Error )
Expand All @@ -45,6 +40,9 @@ var opts = {
// FIXTURES //

var data = require( './fixtures/python/data.json' );
var positive = require( './fixtures/julia/positive.json' );
var negative = require( './fixtures/julia/negative.json' );
var nearTwoPi = require( './fixtures/julia/near_two_pi.json' );


// TESTS //
Expand Down Expand Up @@ -72,28 +70,48 @@ tape( 'the function computes the cosine minus one more accurately inside the int
t.end();
});

tape( 'the function computes `cos(x) - 1.0` outside the interval [-π/4,π/4]', opts, function test( t ) {
tape( 'the function computes the cosine minus one outside the interval [-π/4,π/4] (positive values)', opts, function test( t ) {
var expected;
var x;
var y;
var i;

x = linspace( PIO4, 4.0*PI, 100 );

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

x = linspace( -4.0*PI, NPIO4, 100 );
tape( 'the function computes the cosine minus one outside the interval [-π/4,π/4] (negative values)', opts, function test( t ) {
var expected;
var x;
var y;
var i;

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

tape( 'the function computes the cosine minus one close to multiples of 2π', opts, function test( t ) {
var expected;
var x;
var y;
var i;

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

Expand Down
Loading