Skip to content

Commit 3225361

Browse files
committed
refactor: update main.c to align triangular MGF implementation with Julia
--- 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 ---
1 parent ce898d8 commit 3225361

File tree

1 file changed

+36
-23
lines changed
  • lib/node_modules/@stdlib/stats/base/dists/triangular/mgf/src

1 file changed

+36
-23
lines changed

lib/node_modules/@stdlib/stats/base/dists/triangular/mgf/src/main.c

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,23 @@
1616
* limitations under the License.
1717
*/
1818

19-
#include "stdlib/stats/base/dists/triangular/mgf.h"
2019
#include "stdlib/math/base/assert/is_nan.h"
2120
#include "stdlib/math/base/special/exp.h"
22-
#include "stdlib/math/base/special/pow.h"
21+
#include "stdlib/math/base/special/expm1.h"
22+
#include "stdlib/stats/base/dists/triangular/mgf.h"
23+
24+
/**
25+
* Helper function for repeated computation in the MGF formula.
26+
*
27+
* @param x input value
28+
* @return evaluated result
29+
*/
30+
static double phi2( const double x ) {
31+
if ( x == 0.0 ) {
32+
return 1.0;
33+
}
34+
return ( 2.0 * ( stdlib_base_expm1( x ) - x ) ) / ( x * x );
35+
}
2336

2437
/**
2538
* Evaluates the moment-generating function (MGF) for a triangular distribution with lower limit `a`, upper limit `b`, and mode `c` at a value `t`.
@@ -35,29 +48,29 @@
3548
* // returns ~1.021
3649
*/
3750
double stdlib_base_dists_triangular_mgf( const double t, const double a, const double b, const double c ) {
38-
double bmc;
39-
double bma;
40-
double cma;
41-
double ret;
42-
if (
43-
stdlib_base_is_nan( t ) ||
44-
stdlib_base_is_nan( a ) ||
45-
stdlib_base_is_nan( b ) ||
46-
stdlib_base_is_nan( c ) ||
47-
a > c ||
48-
c > b
49-
) {
50-
return 0.0/0.0; // NaN
51+
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 ) {
52+
return 0.0 / 0.0; // NaN
5153
}
5254
if ( t == 0.0 ) {
5355
return 1.0;
5456
}
55-
bmc = b - c;
56-
bma = b - a;
57-
cma = c - a;
58-
ret = ( bmc * stdlib_base_exp( a*t ) ) - ( bma * stdlib_base_exp( c*t ) );
59-
ret += cma * stdlib_base_exp( b*t );
60-
ret *= 2.0;
61-
ret /= bma * cma * bmc * stdlib_base_pow( t, 2.0 );
62-
return ret;
57+
double result = 0.0;
58+
double term1 = 0.0;
59+
double term2 = 0.0;
60+
if ( a < c ) {
61+
if ( c < b ) {
62+
term1 = ( c - a ) * phi2( ( a - c ) * t );
63+
term2 = ( b - c ) * phi2( ( b - c ) * t );
64+
result = stdlib_base_exp( c * t ) * ( term1 + term2 ) / ( b - a );
65+
} else {
66+
term1 = phi2( ( a - c ) * t );
67+
result = stdlib_base_exp( c * t ) * term1;
68+
}
69+
} else if ( c < b ) {
70+
term1 = phi2( ( b - c ) * t );
71+
result = stdlib_base_exp( c * t ) * term1;
72+
} else {
73+
result = stdlib_base_exp( c * t );
74+
}
75+
return result;
6376
}

0 commit comments

Comments
 (0)