-
-
Notifications
You must be signed in to change notification settings - Fork 905
test: validate implementation against Julia test fixtures #5847
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
Changes from all commits
926417e
2274f73
54d4772
ab62d84
aac699f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
#!/usr/bin/env julia | ||
# | ||
# @license Apache-2.0 | ||
# | ||
# Copyright (c) 2025 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. | ||
# Arguments | ||
* `domain`: domain | ||
* `name::AbstractString`: output filename | ||
# Examples | ||
``` julia | ||
julia> x = range( -1000.0, stop = 1000.0, length = 2001 ); | ||
julia> gen( x, \"data.json\" ); | ||
``` | ||
""" | ||
function gen( domain, name ) | ||
x = collect( domain ); | ||
y = cos.( x ); | ||
|
||
# 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 within the defined domain: | ||
x = range( -pi/4.0, stop = pi/4.0, length = 1000 ) | ||
gen( x, "small_range.json" ); | ||
|
||
# Positive values outside the defined domain: | ||
x = range( 40.0*pi/4.0, stop = 200*pi/4.0, length = 1000 ) | ||
gen( x, "large_positive.json" ); | ||
|
||
# Negative values outside the defined domain: | ||
x = range( -200*pi/4.0, stop = -40*pi/4.0, length = 1000 ) | ||
gen( x, "large_negative.json" ); | ||
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,10 +23,9 @@ | |
var resolve = require( 'path' ).resolve; | ||
var tape = require( 'tape' ); | ||
var isnan = require( '@stdlib/math/base/assert/is-nan' ); | ||
var linspace = require( '@stdlib/array/base/linspace' ); | ||
var abs = require( '@stdlib/math/base/special/abs' ); | ||
var EPS = require( '@stdlib/constants/float64/eps' ); | ||
var rempio2 = require( '@stdlib/math/base/special/rempio2' ); | ||
var PI = require( '@stdlib/constants/float64/pi' ); | ||
var cos = require( '@stdlib/math/base/special/cos' ); | ||
var tryRequire = require( '@stdlib/utils/try-require' ); | ||
|
||
|
||
|
@@ -38,6 +37,13 @@ var opts = { | |
}; | ||
|
||
|
||
// FIXTURES // | ||
|
||
var smallRange = require( './fixtures/julia/small_range.json' ); | ||
var largePositive = require( './fixtures/julia/large_positive.json' ); | ||
var largeNegative = require( './fixtures/julia/large_negative.json' ); | ||
|
||
|
||
// TESTS // | ||
|
||
tape( 'main export is a function', opts, function test( t ) { | ||
|
@@ -48,52 +54,66 @@ tape( 'main export is a function', opts, function test( t ) { | |
|
||
tape( 'the function returns `NaN` if provided `NaN` for either parameter', opts, function test( t ) { | ||
var v = kernelCos( NaN, 0.0 ); | ||
t.equal( isnan( v ), true, 'returns NaN' ); | ||
t.equal( isnan( v ), true, 'returns expected value' ); | ||
|
||
v = kernelCos( 4.0, NaN ); | ||
t.equal( isnan( v ), true, 'returns NaN' ); | ||
t.equal( isnan( v ), true, 'returns expected value' ); | ||
|
||
v = kernelCos( NaN, NaN ); | ||
t.equal( isnan( v ), true, 'returns NaN' ); | ||
t.equal( isnan( v ), true, 'returns expected value' ); | ||
t.end(); | ||
}); | ||
|
||
tape( 'the function evaluates the cosine for input values on the interval `[-pi/4, pi/4]`', opts, function test( t ) { | ||
var expected; | ||
var values; | ||
var out; | ||
var x; | ||
var i; | ||
|
||
values = linspace( -PI/4.0, PI/4.0, 1000 ); | ||
values = smallRange.x; | ||
expected = smallRange.expected; | ||
for ( i = 0; i < values.length; i++ ) { | ||
x = values[ i ]; | ||
out = kernelCos( x, 0.0 ); | ||
t.strictEqual( out, cos( x ), 'returns expected value' ); | ||
t.strictEqual( out, expected[ i ], 'returns expected value' ); | ||
} | ||
t.end(); | ||
}); | ||
|
||
tape( 'the function can be used to compute the cosine for input values outside of `[-pi/4, pi/4]` after argument reduction via `rempio2` (positive)', opts, function test( t ) { | ||
var expected; | ||
var values; | ||
var delta; | ||
var tol; | ||
var out; | ||
var x; | ||
var y; | ||
var n; | ||
var i; | ||
|
||
values = linspace( 40.0*PI/4.0, 200*PI/4.0, 1000 ); | ||
y = new Array( 2 ); | ||
values = largePositive.x; | ||
expected = largePositive.expected; | ||
y = [ 0.0, 0.0 ]; | ||
for ( i = 0; i < values.length; i++ ) { | ||
x = values[ i ]; | ||
n = rempio2( x, y ); | ||
switch ( n & 3 ) { | ||
case 0: | ||
out = kernelCos( y[ 0 ], y[ 1 ] ); | ||
t.strictEqual( out, cos( x ), 'returns expected value' ); | ||
t.strictEqual( out, expected[ i ], 'returns expected value' ); | ||
break; | ||
case 2: | ||
out = -kernelCos( y[ 0 ], y[ 1 ] ); | ||
t.strictEqual( out, cos( x ), 'returns expected value' ); | ||
if ( out === expected[ i ] ) { | ||
t.strictEqual( out, expected[ i ], 'returns expected value' ); | ||
} else { | ||
delta = abs( out - expected[ i ] ); | ||
|
||
// NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205 | ||
tol = EPS * abs( expected[ i ] ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
t.ok( delta <= tol, 'within tolerance. x: '+x+'. out: '+out+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' ); | ||
} | ||
break; | ||
default: | ||
break; | ||
|
@@ -103,26 +123,38 @@ tape( 'the function can be used to compute the cosine for input values outside o | |
}); | ||
|
||
tape( 'the function can be used to compute the cosine for input values outside of `[-pi/4, pi/4]` after argument reduction via `rempio2` (negative)', opts, function test( t ) { | ||
var expected; | ||
var values; | ||
var delta; | ||
var tol; | ||
var out; | ||
var x; | ||
var y; | ||
var n; | ||
var i; | ||
|
||
values = linspace( -200.0*PI/4.0, -40.0*PI/4.0, 1000 ); | ||
y = new Array( 2 ); | ||
values = largeNegative.x; | ||
expected = largeNegative.expected; | ||
y = [ 0.0, 0.0 ]; | ||
for ( i = 0; i < values.length; i++ ) { | ||
x = values[ i ]; | ||
n = rempio2( x, y ); | ||
switch ( n & 3 ) { | ||
case 0: | ||
out = kernelCos( y[ 0 ], y[ 1 ] ); | ||
t.strictEqual( out, cos( x ), 'returns expected value' ); | ||
t.strictEqual( out, expected[ i ], 'returns expected value' ); | ||
break; | ||
case 2: | ||
out = -kernelCos( y[ 0 ], y[ 1 ] ); | ||
t.strictEqual( out, cos( x ), 'returns expected value' ); | ||
if ( out === expected[ i ] ) { | ||
t.strictEqual( out, expected[ i ], 'returns expected value' ); | ||
} else { | ||
delta = abs( out - expected[ i ] ); | ||
|
||
// NOTE: the tolerance here is larger than for the JavaScript implementation due to compiler optimizations which may be performed resulting in result divergence. For discussion, see https://github.com/stdlib-js/stdlib/pull/2298#discussion_r1624765205 | ||
tol = EPS * abs( expected[ i ] ); | ||
t.ok( delta <= tol, 'within tolerance. x: '+x+'. out: '+out+'. E: '+expected[i]+'. tol: '+tol+'. Δ: '+delta+'.' ); | ||
} | ||
break; | ||
default: | ||
break; | ||
|
Uh oh!
There was an error while loading. Please reload this page.