Skip to content
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

feat(stats): add C implementation for stats/base/dists/degenerate/median #4579

Merged
merged 12 commits into from
Jan 18, 2025
Merged
Prev Previous commit
Next Next commit
refactor: fixed the formatting issues
---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: passed
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: na
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: missing_dependencies
  - task: lint_c_examples
    status: missing_dependencies
  - task: lint_c_benchmarks
    status: missing_dependencies
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---

---
type: pre_push_report
description: Results of running various checks prior to pushing changes.
report:
  - task: run_javascript_examples
    status: na
  - task: run_c_examples
    status: passed
  - task: run_cpp_examples
    status: na
  - task: run_javascript_readme_examples
    status: passed
  - task: run_c_benchmarks
    status: passed
  - task: run_cpp_benchmarks
    status: na
  - task: run_fortran_benchmarks
    status: na
  - task: run_javascript_benchmarks
    status: na
  - task: run_julia_benchmarks
    status: na
  - task: run_python_benchmarks
    status: na
  - task: run_r_benchmarks
    status: na
  - task: run_javascript_tests
    status: na
---
anandkaranubc committed Jan 8, 2025
commit 938c7a3c08323f54523748488fcd0cd48d83e153
Original file line number Diff line number Diff line change
@@ -185,10 +185,10 @@ int main( void ) {
int i;
for ( i = 0; i < 10; i++ ) {
mu = random_uniform( -20.0, 20.0 );
mu = random_uniform( 0.0, 20.0 );
m = stdlib_base_dists_degenerate_median( mu );
printf( "mu: %lf, Median: %lf\n", mu, m );
printf( "µ: %lf, Median(X;µ): %lf\n", mu, m );
}
return 0;
Original file line number Diff line number Diff line change
@@ -16,12 +16,12 @@
* limitations under the License.
*/

#include <sys/time.h>
#include "stdlib/stats/base/dists/degenerate/median.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>

#define NAME "degenerate-median"
#define ITERATIONS 1000000
@@ -71,7 +71,7 @@ static void print_results( double elapsed ) {
static double tic( void ) {
struct timeval now;
gettimeofday( &now, NULL );
return (double)now.tv_sec + (double)now.tv_usec/1.0e6;
return (double)now.tv_sec + (double)now.tv_usec / 1.0e6;
}

/**
@@ -83,7 +83,7 @@ static double tic( void ) {
*/
static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v*(max-min) );
return min + ( v * ( max - min ) );
}

/**
@@ -104,7 +104,7 @@ static double benchmark( void ) {

t = tic();
for ( i = 0; i < ITERATIONS; i++ ) {
y = stdlib_base_dists_degenerate_median( mu[ i%100 ] );
y = stdlib_base_dists_degenerate_median( mu[ i % 100 ] );
if ( y != y ) {
printf( "should not return NaN\n" );
break;
@@ -124,15 +124,15 @@ int main( void ) {
double elapsed;
int i;

// Use the current time to seed the random number generator:
// Use the current time to seed the random number generator:
srand( time( NULL ) );

print_version();
for ( i = 0; i < REPEATS; i++ ) {
printf( "# c::%s\n", NAME );
elapsed = benchmark();
print_results( elapsed );
printf( "ok %d benchmark finished\n", i+1 );
printf( "ok %d benchmark finished\n", i + 1 );
}
print_summary( REPEATS, REPEATS );
}
267 changes: 144 additions & 123 deletions lib/node_modules/@stdlib/stats/base/dists/degenerate/median/binding.gyp
Original file line number Diff line number Diff line change
@@ -19,131 +19,152 @@
# [1]: https://gyp.gsrc.io/docs/InputFormatReference.md
# [2]: https://gyp.gsrc.io/docs/UserDocumentation.md
{
# List of files to include in this file:
"includes": [
"./include.gypi",
],
# Define variables to be used throughout the configuration for all targets:
"variables": {
# Target name should match the add-on export name:
"addon_target_name%": "addon",
# Set variables based on the host OS:
"conditions": [
[
'OS=="win"',
{
# Define the object file suffix:
"obj": "obj",
},
{
# Define the object file suffix:
"obj": "o",
},
], # end condition (OS=="win")
], # end conditions
}, # end variables
# Define compile targets:
"targets": [
# Target to generate an add-on:
# List of files to include in this file:
'includes': [
'./include.gypi',
],

# Define variables to be used throughout the configuration for all targets:
'variables': {
# Target name should match the add-on export name:
'addon_target_name%': 'addon',

# Set variables based on the host OS:
'conditions': [
[
'OS=="win"',
{
# The target name should match the add-on export name:
"target_name": "<(addon_target_name)",
# Define dependencies:
"dependencies": [],
# Define directories which contain relevant include headers:
"include_dirs": [
# Local include directory:
"<@(include_dirs)",
],
# List of source files:
"sources": [
"<@(src_files)",
"src/addon.c",
],
# Settings which should be applied when a target's object files are used as linker input:
"link_settings": {
# Define libraries:
"libraries": [
"<@(libraries)",
],
# Define library directories:
"library_dirs": [
"<@(library_dirs)",
],
},
# C/C++ compiler flags:
"cflags": [
# Enable commonly used warning options:
"-Wall",
# Aggressive optimization:
"-O3",
],
# C specific compiler flags:
"cflags_c": [
# Specify the C standard to which a program is expected to conform:
"-std=c99",
# Define the object file suffix:
'obj': 'obj',
},
{
# Define the object file suffix:
'obj': 'o',
}
], # end condition (OS=="win")
], # end conditions
}, # end variables

# Define compile targets:
'targets': [

# Target to generate an add-on:
{
# The target name should match the add-on export name:
'target_name': '<(addon_target_name)',

# Define dependencies:
'dependencies': [],

# Define directories which contain relevant include headers:
'include_dirs': [
# Local include directory:
'<@(include_dirs)',
],

# List of source files:
'sources': [
'<@(src_files)',
],

# Settings which should be applied when a target's object files are used as linker input:
'link_settings': {
# Define libraries:
'libraries': [
'<@(libraries)',
],

# Define library directories:
'library_dirs': [
'<@(library_dirs)',
],
},

# C/C++ compiler flags:
'cflags': [
# Enable commonly used warning options:
'-Wall',

# Aggressive optimization:
'-O3',
],

# C specific compiler flags:
'cflags_c': [
# Specify the C standard to which a program is expected to conform:
'-std=c99',
],

# C++ specific compiler flags:
'cflags_cpp': [
# Specify the C++ standard to which a program is expected to conform:
'-std=c++11',
],

# Linker flags:
'ldflags': [],

# Apply conditions based on the host OS:
'conditions': [
[
'OS=="mac"',
{
# Linker flags:
'ldflags': [
'-undefined dynamic_lookup',
'-Wl,-no-pie',
'-Wl,-search_paths_first',
],
# C++ specific compiler flags:
"cflags_cpp": [
# Specify the C++ standard to which a program is expected to conform:
"-std=c++11",
},
], # end condition (OS=="mac")
[
'OS!="win"',
{
# C/C++ flags:
'cflags': [
# Generate platform-independent code:
'-fPIC',
],
# Linker flags:
"ldflags": [],
# Apply conditions based on the host OS:
"conditions": [
[
'OS=="mac"',
{
# Linker flags:
"ldflags": [
"-undefined dynamic_lookup",
"-Wl,-no-pie",
"-Wl,-search_paths_first",
],
},
], # end condition (OS=="mac")
[
'OS!="win"',
{
# C/C++ flags:
"cflags": [
# Generate platform-independent code:
"-fPIC",
],
},
], # end condition (OS!="win")
], # end conditions
}, # end target <(addon_target_name)
# Target to copy a generated add-on to a standard location:
},
], # end condition (OS!="win")
], # end conditions
}, # end target <(addon_target_name)

# Target to copy a generated add-on to a standard location:
{
'target_name': 'copy_addon',

# Declare that the output of this target is not linked:
'type': 'none',

# Define dependencies:
'dependencies': [
# Require that the add-on be generated before building this target:
'<(addon_target_name)',
],

# Define a list of actions:
'actions': [
{
"target_name": "copy_addon",
# Declare that the output of this target is not linked:
"type": "none",
# Define dependencies:
"dependencies": [
# Require that the add-on be generated before building this target:
"<(addon_target_name)",
],
# Define a list of actions:
"actions": [
{
"action_name": "copy_addon",
"message": "Copying addon...",
# Explicitly list the inputs in the command-line invocation below:
"inputs": [],
# Declare the expected outputs:
"outputs": [
"<(addon_output_dir)/<(addon_target_name).node",
],
# Define the command-line invocation:
"action": [
"cp",
"<(PRODUCT_DIR)/<(addon_target_name).node",
"<(addon_output_dir)/<(addon_target_name).node",
],
},
], # end actions
}, # end target copy_addon
], # end targets
'action_name': 'copy_addon',
'message': 'Copying addon...',

# Explicitly list the inputs in the command-line invocation below:
'inputs': [],

# Declare the expected outputs:
'outputs': [
'<(addon_output_dir)/<(addon_target_name).node',
],

# Define the command-line invocation:
'action': [
'cp',
'<(PRODUCT_DIR)/<(addon_target_name).node',
'<(addon_output_dir)/<(addon_target_name).node',
],
},
], # end actions
}, # end target copy_addon
], # end targets
}
Original file line number Diff line number Diff line change
@@ -17,25 +17,25 @@
*/

#include "stdlib/stats/base/dists/degenerate/median.h"
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>

static double random_uniform( const double min, const double max ) {
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v * ( max - min ) );
double v = (double)rand() / ( (double)RAND_MAX + 1.0 );
return min + ( v * ( max - min ) );
}

int main( void ) {
double mu;
double m;
int i;
double mu;
double m;
int i;

for ( i = 0; i < 10; i++ ) {
mu = random_uniform( -20.0, 20.0 );
m = stdlib_base_dists_degenerate_median( mu );
for ( i = 0; i < 10; i++ ) {
mu = random_uniform( 0.0, 20.0 );
m = stdlib_base_dists_degenerate_median( mu );

printf( "mu: %lf, Median: %lf\n", mu, m );
}
printf( "µ: %lf, Median(X;µ): %lf\n", mu, m );
}

return 0;
return 0;
}
Loading