Skip to content

Commit

Permalink
refactor: update main.c to align triangular MGF implementation with J…
Browse files Browse the repository at this point in the history
…ulia

---
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: na
  - 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: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - 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: na
  - task: run_cpp_examples
    status: na
  - task: run_javascript_readme_examples
    status: na
  - task: run_c_benchmarks
    status: na
  - 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
---
  • Loading branch information
anandkaranubc committed Jan 15, 2025
1 parent ce898d8 commit 3225361
Showing 1 changed file with 36 additions and 23 deletions.
59 changes: 36 additions & 23 deletions lib/node_modules/@stdlib/stats/base/dists/triangular/mgf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,23 @@
* limitations under the License.
*/

#include "stdlib/stats/base/dists/triangular/mgf.h"
#include "stdlib/math/base/assert/is_nan.h"
#include "stdlib/math/base/special/exp.h"
#include "stdlib/math/base/special/pow.h"
#include "stdlib/math/base/special/expm1.h"
#include "stdlib/stats/base/dists/triangular/mgf.h"

/**
* Helper function for repeated computation in the MGF formula.
*
* @param x input value
* @return evaluated result
*/
static double phi2( const double x ) {
if ( x == 0.0 ) {
return 1.0;
}
return ( 2.0 * ( stdlib_base_expm1( x ) - x ) ) / ( x * x );
}

/**
* Evaluates the moment-generating function (MGF) for a triangular distribution with lower limit `a`, upper limit `b`, and mode `c` at a value `t`.
Expand All @@ -35,29 +48,29 @@
* // returns ~1.021
*/
double stdlib_base_dists_triangular_mgf( const double t, const double a, const double b, const double c ) {
double bmc;
double bma;
double cma;
double ret;
if (
stdlib_base_is_nan( t ) ||
stdlib_base_is_nan( a ) ||
stdlib_base_is_nan( b ) ||
stdlib_base_is_nan( c ) ||
a > c ||
c > b
) {
return 0.0/0.0; // NaN
if ( stdlib_base_is_nan( t ) || stdlib_base_is_nan( a ) || stdlib_base_is_nan( b ) || stdlib_base_is_nan( c ) || a > c || c > b ) {
return 0.0 / 0.0; // NaN
}
if ( t == 0.0 ) {
return 1.0;
}
bmc = b - c;
bma = b - a;
cma = c - a;
ret = ( bmc * stdlib_base_exp( a*t ) ) - ( bma * stdlib_base_exp( c*t ) );
ret += cma * stdlib_base_exp( b*t );
ret *= 2.0;
ret /= bma * cma * bmc * stdlib_base_pow( t, 2.0 );
return ret;
double result = 0.0;
double term1 = 0.0;
double term2 = 0.0;
if ( a < c ) {
if ( c < b ) {
term1 = ( c - a ) * phi2( ( a - c ) * t );
term2 = ( b - c ) * phi2( ( b - c ) * t );
result = stdlib_base_exp( c * t ) * ( term1 + term2 ) / ( b - a );
} else {
term1 = phi2( ( a - c ) * t );
result = stdlib_base_exp( c * t ) * term1;
}
} else if ( c < b ) {
term1 = phi2( ( b - c ) * t );
result = stdlib_base_exp( c * t ) * term1;
} else {
result = stdlib_base_exp( c * t );
}
return result;
}

0 comments on commit 3225361

Please sign in to comment.