Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/factorialln/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,24 @@
// MODULES //

var isNegativeInteger = require( '@stdlib/math/base/assert/is-negative-integer' );
var gamma1pm1 = require( '@stdlib/math/base/special/gamma1pm1' );
var gammaln = require( '@stdlib/math/base/special/gammaln' );
var log1p = require( '@stdlib/math/base/special/log1p' );


// MAIN //

/**
* Evaluates the natural logarithm of the factorial of `x`.
*
* ## Method
*
* For \\(-1 < x < 2\\), forming \\(x+1\\) can round away low-order bits of \\(x\\), which results in large relative errors near the zeros of \\(\ln \Gamma(x+1)\\) at \\(x = 0\\) and \\(x = 1\\). To avoid forming \\(x+1\\), we use the identity
*
* ```tex
* \ln \Gamma(x+1) = \operatorname{log1p}(\Gamma(x+1) - 1) = \operatorname{log1p}(\operatorname{gamma1pm1}(x))
* ```
*
* @param {number} x - input value
* @returns {number} natural logarithm of factorial of `x`
*
Expand Down Expand Up @@ -56,6 +66,9 @@ function factorialln( x ) {
if ( isNegativeInteger( x ) ) {
return NaN;
}
if ( x > -1.0 && x < 2.0 ) {
return log1p( gamma1pm1( x ) );
}
return gammaln( x + 1.0 );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
"dependencies": [
"@stdlib/math/base/napi/unary",
"@stdlib/math/base/assert/is-negative-integer",
"@stdlib/math/base/special/gamma1pm1",
"@stdlib/math/base/special/gammaln",
"@stdlib/math/base/special/log1p",
"@stdlib/constants/float64/nan"
]
},
Expand All @@ -54,7 +56,9 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-negative-integer",
"@stdlib/math/base/special/gamma1pm1",
"@stdlib/math/base/special/gammaln",
"@stdlib/math/base/special/log1p",
"@stdlib/constants/float64/nan"
]
},
Expand All @@ -70,7 +74,9 @@
"libpath": [],
"dependencies": [
"@stdlib/math/base/assert/is-negative-integer",
"@stdlib/math/base/special/gamma1pm1",
"@stdlib/math/base/special/gammaln",
"@stdlib/math/base/special/log1p",
"@stdlib/constants/float64/nan"
]
}
Expand Down
13 changes: 13 additions & 0 deletions lib/node_modules/@stdlib/math/base/special/factorialln/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,21 @@
#include "stdlib/math/base/special/factorialln.h"
#include "stdlib/constants/float64/nan.h"
#include "stdlib/math/base/assert/is_negative_integer.h"
#include "stdlib/math/base/special/gamma1pm1.h"
#include "stdlib/math/base/special/gammaln.h"
#include "stdlib/math/base/special/log1p.h"

/**
* Evaluates the natural logarithm of the factorial of `x`.
*
* ## Method
*
* For \\(-1 < x < 2\\), forming \\(x+1\\) can round away low-order bits of \\(x\\), which results in large relative errors near the zeros of \\(\ln \Gamma(x+1)\\) at \\(x = 0\\) and \\(x = 1\\). To avoid forming \\(x+1\\), we use the identity
*
* ```tex
* \ln \Gamma(x+1) = \operatorname{log1p}(\Gamma(x+1) - 1) = \operatorname{log1p}(\operatorname{gamma1pm1}(x))
* ```
*
* @param x input value
* @return natural logarithm of factorial of `x`
*
Expand All @@ -35,5 +45,8 @@ double stdlib_base_factorialln( const double x ) {
if ( stdlib_base_is_negative_integer( x ) ) {
return STDLIB_CONSTANT_FLOAT64_NAN;
}
if ( x > -1.0 && x < 2.0 ) {
return stdlib_base_log1p( stdlib_base_gamma1pm1( x ) );
}
return stdlib_base_gammaln( x + 1.0 );
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import JSON
using SpecialFunctions

"""
gen( x, name )
gen( x, name )

Generate fixture data and write to file.

Expand All @@ -37,13 +37,9 @@ julia> gen( x, \"data.json\" );
```
"""
function gen( x, name )
y = Array{Float64}( undef, length( x ) );
for i in eachindex(x)
if isinteger( x[i] )
y[i] = lfactorial( Int64( x[i] ) );
else
y[i] = lgamma( x[i]+1 );
end
# Evaluate in extended precision, as forming `x+1` in double precision can round away the low-order bits of `x`:
y = setprecision( BigFloat, 2048 ) do
Float64.( loggamma.( BigFloat.( x ) .+ 1 ) )
end

# Store data to be written to file as a collection:
Expand All @@ -68,8 +64,20 @@ file = @__FILE__;
# Extract the directory in which this file resides:
dir = dirname( file );

# Negative tiny values:
x = range( -1.0e-300, stop = -1.0e-308, length = 500 );
gen( x, "tiny_negative.json" );

# Positive tiny values:
x = range( 1.0e-300, stop = 1.0e-308, length = 500 );
gen( x, "tiny_positive.json" );

# Negative small values:
x = range( -0.999, stop = -0.001, length = 500 );
gen( x, "small_negative.json" );

# Positive small values:
x = range( 1.0, stop = 3.0, length = 1001 );
x = range( 0.002, stop = 3.0, length = 1500 );
gen( x, "small_positive.json" );

# Positive medium values:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"expected":[6.9071788853838525,5.807418734725977,5.295451799982127,4.957844784368176,4.7054020901548075,4.503609622996786,4.335440242151057,4.191230559217485,4.064965015335488,3.952643399235571,3.851470361297991,3.759415387088238,3.674956947385164,3.5969254226836873,3.524402304388072,3.456653086974839,3.3930811807180192,3.3331954073912837,3.276586541328727,3.222910033847072,3.1718730627935647,3.1232246695384775,3.076748140193624,3.032255044856693,2.9895805198908048,2.9485794945917863,2.909123644093329,2.871098906977536,2.834403446481069,2.7989459634470717,2.76464429062277,2.731424213811968,2.6992184773202705,2.667965940165458,2.6376108564325826,2.608102258480727,2.579393425851028,2.551441425971994,2.5242067153207235,2.4976527917352254,2.4717458902021603,2.446454715755348,2.42175020818161,2.3976053340942145,2.3739949026407587,2.350895401693265,2.3282848518481,2.30614267596149,2.2844495822781474,2.263187459488125,2.2423392822801476,2.2218890261562483,2.2018215904388594,2.1821227285426956,2.1627789847040684,2.143777636463035,2.1251066422818927,2.106754593759239,2.088710671964119,2.07096460747118,2.0535066437266893,2.036327503417712,2.0194183575537963,2.002770797002779,1.986376806250606,1.9702287391798505,1.954319296683377,1.938641505948793,1.9231887012662465,1.9079545062270862,1.8929328171941517,1.8781177879362239,1.8635038153296068,1.8490855260391232,1.8348577640991037,1.8208155793223624,1.806954216471784,1.7932691051350937,1.7797558502487132,1.7664102232213992,1.7532281536126797,1.7402057213249846,1.727339149271884,1.7146247964880101,1.7020591516491146,1.689638826973306,1.6773605524768742,1.6652211705602409,1.6532176309015325,1.641346985637029,1.6296063848093687,1.6179930720658466,1.6065043805905022,1.595137729254906,1.58389061897369,1.5727606292518854,1.5617454149120837,1.5508427029902945,1.5400502897901691,1.5293660380859986,1.518787874465552,1.5083137868044507,1.497941821864341,1.487670083007653,1.477496728022218,1.4674199670494679,1.4574380606103543,1.4475493177235046,1.4377520941104927,1.4280447904834295,1.4184258509103806,1.4088937612544064,1.3994470476822782,1.3900842752391753,1.3808040464858826,1.3716050001952358,1.3624858101047435,1.353445183722511,1.3444818611837488,1.335594614155321,1.3267822447859294,1.3180435846996699,1.3093774940308287,1.3007828604979073,1.2922585985149757,1.2838036483385669,1.2754169752484132,1.2670975687604333,1.2588444418704519,1.250656630327226,1.2425331919334257,1.2344732058732855,1.2264757720657187,1.2185400105417412,1.210665060845122,1.2028500814552223,1.1950942492310477,1.187396758875585,1.1797568224195385,1.1721736687236322,1.1646465429986796,1.1571747063426665,1.1497574352941258,1.1423940214011257,1.135083770805214,1.1278260038397048,1.1206200546417178,1.1134652707774046,1.1063610128798336,1.0993066542990182,1.0923015807636076,1.0853451900537725,1.0784368916848461,1.0715761066012988,1.0647622668806427,1.0579948154468835,1.0512732057931504,1.0445969017131538,1.0379653770411386,1.031378115400008,1.0248346099573133,1.0183343631888193,1.0118768866493595,1.0054617007507167,0.9990883345462722,0.9927563255221741,0.9864652193947925,0.980214569914234,0.9740039386737,0.9678328949244811,0.9617010153963875,0.9556078841234273,0.949553092274547,0.9435362379892602,0.9375569262179961,0.9316147685670046,0.9257093831476644,0.9198403944300432,0.9140074331005685,0.9082101359236702,0.9024481456072615,0.8967211106719343,0.8910286853237424,0.8853705293304581,0.8797463079011888,0.8741556915692409,0.8685983560781323,0.8630739822706475,0.8575822559808401,0.8521228679288901,0.8466955136187237,0.8412998932383107,0.8359357115625548,0.8306026778586973,0.8253005057941549,0.8200289133467181,0.8147876227170371,0.8095763602433264,0.80439485631822,0.7992428453077133,0.7941200654721277,0.7890262588890395,0.7839611713781132,0.7789245524277829,0.7739161551237298,0.7689357360790994,0.7639830553664106,0.7590578764511084,0.7541599661267085,0.7492890944514934,0.7444450346867116,0.7396275632362402,0.7348364595876671,0.7300715062547537,0.725332488721241,0.7206191953859583,0.7159314175092015,0.7112689491603443,0.7066315871666485,0.7020191310632413,0.697431383044226,0.6928681479148989,0.688329233045038,0.6838144483232381,0.6793236061122628,0.6748565212053868,0.6704130107837011,0.6659928943743585,0.6615959938097311,0.6572221331874578,0.6528711388313592,0.6485428392531958,0.644237065115249,0.6399536491937026,0.6356924263428055,0.6314532334597951,0.6272359094505608,0.6230402951960322,0.6188662335192696,0.6147135691532428,0.6105821487092784,0.6064718206461605,0.6023824352398679,0.598313844553933,0.5942659024104052,0.5902384643614065,0.5862313876612628,0.5822445312391981,0.5782777556725774,0.5743309231606863,0.5704038974990318,0.5664965440541545,0.5626087297389398,0.5587403229884136,0.5548911937360149,0.5510612133903311,0.5472502548122865,0.5434581922927735,0.5396849015307151,0.5359302596115506,0.532194144986133,0.5284764374500291,0.5247770181232141,0.5210957694301498,0.5174325750802402,0.5137873200486532,0.5101598905575027,0.50655017405738,0.5029580592092313,0.49938343586656847,0.4958261950580087,0.4922862289701354,0.48876343093067287,0.48525769539196767,0.4817689179147703,0.47829699515231094,0.4748418248346622,0.47140330575338296,0.4679813377464377,0.46457582168338435,0.4611866594508261,0.4578137539381207,0.45445700902334213,0.4511163295594894,0.44779162136093653,0.4444827911901202,0.441189746744458,0.43791239664349435,0.43465065041626844,0.4314044184888992,0.4281736121723842,0.4249581436506063,0.42175792596854556,0.41857287302069063,0.41540289953964676,0.4122479210849357,0.40910785403198413,0.4059826155612963,0.40287212364780706,0.3997762970504134,0.396695055301678,0.3936283186977037,0.39057600828817485,0.38753804586656176,0.3845143539604856,0.3815048558222405,0.3785094754194693,0.375528137425991,0.3725607672127757,0.3696072908390648,0.3666676350436342,0.3637417272361964,0.36082949548894017,0.3579308685282043,0.3550457757262833,0.3521741470933624,0.34931591326957934,0.34647100551721044,0.3436393557129795,0.3408208963404855,0.33801556048274883,0.33522328181487215,0.3324439945968152,0.32967763366628,0.326924134431706,0.32418343286537143,0.32145546549660053,0.3187401694050735,0.31603748221423794,0.3133473420848197,0.3106696877084313,0.3080044583012769,0.30535159359795017,0.30271103384532594,0.30008271979654194,0.29746659270506975,0.29486259431887374,0.29227066687465586,0.28969075309218467,0.28712279616870806,0.2845667397734468,0.28202252804216893,0.2794901055718424,0.2769694174153651,0.2744604090763716,0.27196302650411336,0.2694772160884136,0.26700292465469344,0.2645400994590696,0.2620886881835211,0.2596486389311252,0.25721990022136004,0.25480242098547384,0.2523961505619191,0.2500010386918508,0.24761703551468725,0.24524409156373303,0.24288215776186278,0.24053118541726448,0.23819112621924185,0.2358619322340741,0.2335435559009332,0.23123595002785652,0.2289390677877746,0.2266528627145931,0.22437728869932785,0.22211229998629245,0.2198578511693369,0.21761389718813742,0.21538039332453573,0.21315729519892748,0.2109445587666988,0.20874214031471047,0.20654999645782882,0.20436808413550223,0.20219636060838314,0.2000347834549947,0.19788331056844072,0.19574190015315915,0.1936105107217179,0.19148910109165218,0.18937763038234323,0.1872760580119373,0.18518434369430467,0.18310244743603743,0.18103032953348658,0.17896795056983636,0.17691527141221647,0.17487225320885105,0.17283885738624374,0.17081504564639866,0.1688007799640765,0.16679602258408516,0.1648007360186046,0.1628148830445452,0.16083842670093929,0.1588713302863652,0.1569135573564033,0.15496507172112395,0.15302583744260617,0.15109581883248743,0.14917498044954322,0.14726328709729675,0.14536070382165775,0.14346719590859025,0.14158272888180876,0.1397072685005026,0.13784078075708783,0.13598323187498626,0.13413458830643152,0.1322948167303015,0.13046388404997672,0.12864175739122458,0.1268284041001087,0.12502379174092354,0.12322788809415305,0.12144066115445425,0.11966207912866413,0.11789211043383034,0.11613072369526511,0.11437788774462193,0.11263357161799487,0.1108977445540401,0.10917037599211941,0.10745143557046521,0.10574089312436698,0.10403871868437856,0.10234488247454637,0.10065935491065793,0.09898210659851052,0.09731310833219978,0.09565233109242773,0.09399974604483045,0.09235532453832446,0.09071903810347202,0.08909085845086508,0.0874707574695273,0.08585870722533433,0.08425467995945163,0.0826586480867901,0.08107058419447875,0.07949046104035472,0.07791825155146981,0.07635392882261398,0.07479746611485502,0.07324883685409442,0.07170801462963924,0.07017497319278973,0.06864968645544246,0.06713212848870875,0.06562227352154827,0.06412009593941759,0.06262557028293338,0.061138671246550196,0.059659373677252624,0.058187652573261514,0.05672348308275425,0.05526684050259881,0.05381770027710134,0.0523760379967673,0.05094182939707581,0.04951505035726699,0.04809567689914239,0.04668368518587803,0.04527905152085012,0.04388175234647315,0.04249176424305026,0.04110906392763576,0.03973362825290956,0.038365434206063434,0.03700445890769892,0.03565067961073675,0.03430407369933763,0.03296461868783424,0.03163229221967434,0.030307072066374778,0.0289889361264863,0.027677862424569086,0.026373829110178763,0.025076814456862852,0.023786796861167473,0.022503754841654216,0.02122766703792704,0.019958512209669037,0.01869626923568902,0.01744091711297774,0.016192434955773646,0.014950801994638089,0.013715997575539788,0.012488001158948565,0.011266792318938073,0.010052350742297593,0.008844656227652603,0.007643688684594184,0.006449428132817029,0.005261854701266014,0.004080948627291224,0.0029066902558113113,0.001739060038485108,0.0005780385328913797],"x":[-0.999,-0.997,-0.995,-0.993,-0.991,-0.989,-0.987,-0.985,-0.983,-0.981,-0.979,-0.977,-0.975,-0.973,-0.971,-0.969,-0.967,-0.965,-0.963,-0.961,-0.959,-0.957,-0.955,-0.953,-0.951,-0.949,-0.947,-0.945,-0.943,-0.941,-0.939,-0.937,-0.935,-0.933,-0.931,-0.929,-0.927,-0.925,-0.923,-0.921,-0.919,-0.917,-0.915,-0.913,-0.911,-0.909,-0.907,-0.905,-0.903,-0.901,-0.899,-0.897,-0.895,-0.893,-0.891,-0.889,-0.887,-0.885,-0.883,-0.881,-0.879,-0.877,-0.875,-0.873,-0.871,-0.869,-0.867,-0.865,-0.863,-0.861,-0.859,-0.857,-0.855,-0.853,-0.851,-0.849,-0.847,-0.845,-0.843,-0.841,-0.839,-0.837,-0.835,-0.833,-0.831,-0.829,-0.827,-0.825,-0.823,-0.821,-0.819,-0.817,-0.815,-0.813,-0.811,-0.809,-0.807,-0.805,-0.803,-0.801,-0.799,-0.797,-0.795,-0.793,-0.791,-0.789,-0.787,-0.785,-0.783,-0.781,-0.779,-0.777,-0.775,-0.773,-0.771,-0.769,-0.767,-0.765,-0.763,-0.761,-0.759,-0.757,-0.755,-0.753,-0.751,-0.749,-0.747,-0.745,-0.743,-0.741,-0.739,-0.737,-0.735,-0.733,-0.731,-0.729,-0.727,-0.725,-0.723,-0.721,-0.719,-0.717,-0.715,-0.713,-0.711,-0.709,-0.707,-0.705,-0.703,-0.701,-0.699,-0.697,-0.695,-0.693,-0.691,-0.689,-0.687,-0.685,-0.683,-0.681,-0.679,-0.677,-0.675,-0.673,-0.671,-0.669,-0.667,-0.665,-0.663,-0.661,-0.659,-0.657,-0.655,-0.653,-0.651,-0.649,-0.647,-0.645,-0.643,-0.641,-0.639,-0.637,-0.635,-0.633,-0.631,-0.629,-0.627,-0.625,-0.623,-0.621,-0.619,-0.617,-0.615,-0.613,-0.611,-0.609,-0.607,-0.605,-0.603,-0.601,-0.599,-0.597,-0.595,-0.593,-0.591,-0.589,-0.587,-0.585,-0.583,-0.581,-0.579,-0.577,-0.575,-0.573,-0.571,-0.569,-0.567,-0.565,-0.563,-0.561,-0.559,-0.557,-0.555,-0.553,-0.551,-0.549,-0.547,-0.545,-0.543,-0.541,-0.539,-0.537,-0.535,-0.533,-0.531,-0.529,-0.527,-0.525,-0.523,-0.521,-0.519,-0.517,-0.515,-0.513,-0.511,-0.509,-0.507,-0.505,-0.503,-0.501,-0.499,-0.497,-0.495,-0.493,-0.491,-0.489,-0.487,-0.485,-0.483,-0.481,-0.479,-0.477,-0.475,-0.473,-0.471,-0.469,-0.467,-0.465,-0.463,-0.461,-0.459,-0.457,-0.455,-0.453,-0.451,-0.449,-0.447,-0.445,-0.443,-0.441,-0.439,-0.437,-0.435,-0.433,-0.431,-0.429,-0.427,-0.425,-0.423,-0.421,-0.419,-0.417,-0.415,-0.413,-0.411,-0.409,-0.407,-0.405,-0.403,-0.401,-0.399,-0.397,-0.395,-0.393,-0.391,-0.389,-0.387,-0.385,-0.383,-0.381,-0.379,-0.377,-0.375,-0.373,-0.371,-0.369,-0.367,-0.365,-0.363,-0.361,-0.359,-0.357,-0.355,-0.353,-0.351,-0.349,-0.347,-0.345,-0.343,-0.341,-0.339,-0.337,-0.335,-0.333,-0.331,-0.329,-0.327,-0.325,-0.323,-0.321,-0.319,-0.317,-0.315,-0.313,-0.311,-0.309,-0.307,-0.305,-0.303,-0.301,-0.299,-0.297,-0.295,-0.293,-0.291,-0.289,-0.287,-0.285,-0.283,-0.281,-0.279,-0.277,-0.275,-0.273,-0.271,-0.269,-0.267,-0.265,-0.263,-0.261,-0.259,-0.257,-0.255,-0.253,-0.251,-0.249,-0.247,-0.245,-0.243,-0.241,-0.239,-0.237,-0.235,-0.233,-0.231,-0.229,-0.227,-0.225,-0.223,-0.221,-0.219,-0.217,-0.215,-0.213,-0.211,-0.209,-0.207,-0.205,-0.203,-0.201,-0.199,-0.197,-0.195,-0.193,-0.191,-0.189,-0.187,-0.185,-0.183,-0.181,-0.179,-0.177,-0.175,-0.173,-0.171,-0.169,-0.167,-0.165,-0.163,-0.161,-0.159,-0.157,-0.155,-0.153,-0.151,-0.149,-0.147,-0.145,-0.143,-0.141,-0.139,-0.137,-0.135,-0.133,-0.131,-0.129,-0.127,-0.125,-0.123,-0.121,-0.119,-0.117,-0.115,-0.113,-0.111,-0.109,-0.107,-0.105,-0.103,-0.101,-0.099,-0.097,-0.095,-0.093,-0.091,-0.089,-0.087,-0.085,-0.083,-0.081,-0.079,-0.077,-0.075,-0.073,-0.071,-0.069,-0.067,-0.065,-0.063,-0.061,-0.059,-0.057,-0.055,-0.053,-0.051,-0.049,-0.047,-0.045,-0.043,-0.041,-0.039,-0.037,-0.035,-0.033,-0.031,-0.029,-0.027,-0.025,-0.023,-0.021,-0.019,-0.017,-0.015,-0.013,-0.011,-0.009,-0.007,-0.005,-0.003,-0.001]}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Loading
Loading