Skip to content

Commit

Permalink
feat: add math/base/special/lucasf
Browse files Browse the repository at this point in the history
  • Loading branch information
aayush0325 committed Dec 5, 2024
1 parent 26c72a1 commit 12a3f66
Show file tree
Hide file tree
Showing 14 changed files with 1,423 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,282 @@
/**
* @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 bench = require( '@stdlib/bench' );
var randu = require( '@stdlib/random/base/randu' );
var floorf = require( '@stdlib/math/base/special/floorf' );
var roundf = require( '@stdlib/math/base/special/roundf' );
var pow = require( '@stdlib/math/base/special/pow' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var PHI = require( '@stdlib/constants/float32/phi' );
var pkg = require( './../package.json' ).name;
var LUCAS = require( './../lib/lucas.json' );
var lucasf = require( './../lib' );


// MAIN //

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floorf( randu() * 35.0 );
y = lucasf( x );
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();
});

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

function lucasf( n ) {
return roundf( pow( PHI, n ) );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floorf( randu() * 35.0 );
y = lucasf( x );
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();
});

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floorf( randu() * 35.0 );
y = LUCAS[ x ];
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();
});

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

function lucasf( n ) {
if ( n === 0 ) {
return 2;
}
if ( n === 1 ) {
return 1;
}
return lucasf( n-1 ) + lucasf( n-2 );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floorf( randu() * 40.0 ); // limit upper bound
y = lucasf( x );
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();
});

bench( pkg+'::recursion_memoized', function benchmark( b ) {
var arr;
var N;
var x;
var y;
var i;

arr = new Array( 35 );
arr[ 0 ] = 2;
arr[ 1 ] = 1;
N = 1;

function lucasf( n ) {
if ( n <= N ) {
return arr[ n ];
}
arr[ n ] = lucasf( n-1 ) + lucasf( n-2 );
return arr[ n ];
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floorf( randu() * 40.0 ); // limit upper bound
y = lucasf( x );
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();
});

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

function lucasf( n ) {
var arr;
var i;

arr = new Array( n+1 );
arr[ 0 ] = 2;
arr[ 1 ] = 1;
for ( i = 2; i <= n; i++ ) {
arr[ i ] = arr[ i-1 ] + arr[ i-2 ];
}
return arr[ n ];
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floorf( randu() * 35.0 );
y = lucasf( x );
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();
});

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

function lucasf( n ) {
var a;
var b;
var c;
var i;

a = 2;
if ( n === 0 ) {
return a;
}
b = 1;
for ( i = 2; i <= n; i++ ) {
c = a + b;
a = b;
b = c;
}
return b;
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floorf( randu() * 35.0 );
y = lucasf( x );
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();
});

bench( pkg+'::iterative_memoized', function benchmark( b ) {
var arr;
var N;
var x;
var y;
var i;

arr = new Array( 35 );
arr[ 0 ] = 2;
arr[ 1 ] = 1;
N = 1;

function lucasf( n ) {
var i;
if ( n > N ) {
for ( i = N+1; i <= n; i++ ) {
arr[ i ] = arr[ i-1 ] + arr[ i-2 ];
}
N = n;
}
return arr[ n ];
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floorf( randu() * 35.0 );
y = lucasf( x );
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
@@ -0,0 +1,61 @@
/**
* @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 randu = require( '@stdlib/random/base/randu' );
var floorf = require( '@stdlib/math/base/special/floorf' );
var isnanf = require( '@stdlib/math/base/assert/is-nanf' );
var tryRequire = require( '@stdlib/utils/try-require' );
var pkg = require( './../package.json' ).name;


// VARIABLES //

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


// MAIN //

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

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
x = floorf( randu() * 34.0 );
y = lucasf( x );
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();
});
Loading

0 comments on commit 12a3f66

Please sign in to comment.