Îõ³h&Aò@À      !"#$%&'()*+,-./0123456789:;<=>?*Copyright (c) 2007--2021 wren gayle romanoBSD3 wren@cpan.orgstableportable (with CPP) Safe-InferredŒ*Copyright (c) 2007--2021 wren gayle romanoBSD3 wren@cpan.orgstable(semi-portable (OverlappingInstances,...) Safe-InferredÁÃv logfloatæThis class defines a partially ordered type. The method names were chosen so as not to conflict with @ and A . We use B instead of defining new types PartialOrdering and  FuzzyBool7 because this way should make the class easier to use.Minimum complete definition: logfloatlike Clogfloatlike (D)logfloatlike (E)logfloatlike (F)logfloatlike (G)logfloatlike (H) logfloatlike (I) logfloatlike JÅ. The default instance returns the left argument when they're equal. logfloatlike KÅ. The default instance returns the left argument when they're equal. logfloatLike Data.Ord.comparing#. Helpful in conjunction with the xxxBy family of functions from  Data.List   44444 4 4 4*Copyright (c) 2007--2021 wren gayle romanoBSD3 wren@cpan.orgstableportable Safe-InferredWlogfloatMany numbers are not L– yet, even though they can represent arbitrarily large values, they are not necessarily able to represent transfinite values such as infinity itself. This class is for types which are capable of representing such values. Notably, this class does not require the type to be M nor Næ since integral types could also have representations for transfinite values. By popular demand the OÈ restriction has been lifted as well, due to complications of defining P or A for some types.ÒIn particular, this class extends the ordered projection to have a maximum value  and a minimum value #, as well as an exceptional value ". All the natural laws regarding infinity and negativeInfinity6 should pertain. (Some of these are discussed below.)9Hugs (September 2006) has buggy Prelude definitions for  and Ì on Float and Double. This module provides correct definitions, so long as Hugs.RealFloat is compiled correctly.logfloat¯A transfinite value which is greater than all finite values. Adding or subtracting any finite value is a no-op. As is multiplying by any non-zero positive value (including infinityÂ), and dividing by any positive finite value. Also obeys the law "negate infinity = negativeInfinity% with all appropriate ramifications.logfloatÖA transfinite value which is less than all finite values. Obeys all the same laws as infinity7 with the appropriate changes for the sign difference.logfloatŽAn exceptional transfinite value for dealing with undefined results when manipulating infinite values. The following operations must return  notANumber, where inf is any value which  isInfinite: infinity + negativeInfinity negativeInfinity + infinity infinity - infinity #negativeInfinity - negativeInfinity inf * 0 0 * inf  inf / inf  inf `div` inf 0 / 0  0 `div` 0-Additionally, any mathematical operations on  notANumber must also return  notANumber., and any equality or ordering comparison on  notANumber must return FalseÑ (violating the law of the excluded middle, often assumed but not required for A; thus,  and  are preferred over (F) and (Gä)). Since it returns false for equality, there may be more than one machine representation of this value.logfloatReturn true for both infinity and negativeInfinity, false for all other values.logfloatReturn true only for  notANumber.logfloatSince the normal Qú throws an error on zero, we have to redefine it in order for things to work right. Arguing from limits we can see that log 0 == negativeInfinityÙ. Newer versions of GHC have this behavior already, but older versions and Hugs do not.âThis function will raise an error when taking the log of negative numbers, rather than returning æ as the newer GHC implementation does. The reason being that typically this is a logical error, and  notANumber) allows the error to propagate silently.%In order to improve portability, the ) class is required to indicate that the NÞ type does in fact have a representation for negative infinity. Both native floating types (R and S5) are supported. If you define your own instance of  Transfinite,, verify the above equation holds for your 0 and negativeInfinity6. If it doesn't, then you should avoid importing our log> and will probably want converters to handle the discrepancy.For GHC, this version of log has rules for fusion with expÀ. These can give different behavior by preventing overflow to infinityÉ and preventing errors for taking the logarithm of negative values. For R and SØ they can also give different answers due to eliminating floating point fuzz. The rules strictly improve mathematical accuracy, however they should be noted in case your code depends on the implementation details.*Copyright (c) 2007--2021 wren gayle romanoBSD3 wren@cpan.orgstable/semi-portable (CPP, MPTC, OverlappingInstances) Safe-InferredÁÃÄÅÊ·logfloatThe T) function is defined to pivot through a UØ according to the haskell98 spec. This is non-portable and problematic as discussed in Data.Number.Transfinite”. Since there is resistance to breaking from the spec, this class defines a reasonable variant which deals with transfinite values appropriately.ùThere is a generic instance from any Transfinite Real to any Transfinite Fractional, using checks to ensure correctness. GHC has specialized versions for some types which use primitive converters instead, for large performance gains. (These definitions are hidden from other compilers via CPP.) Due to a bug in Haddock the specialized instances are shown twice and the generic instance isn't shown at all. Since the instances are overlapped, you'll need to give type signatures if the arguments to › are polymorphic. There's also a generic instance for any Real Fractional type to itself, thus if you write any generic instances beware of incoherence.¡If any of these restrictions (CPP, GHC-only optimizations, OverlappingInstances) are onerous to you, contact the maintainer (we like patches). Note that this does, work for Hugs with suitable options (e.g. hugs -98 +o -F'cpp -P' ). However, Hugs doesn't allow IncoherentInstances" nor does it allow diamonds with OverlappingInstancesÃ, which restricts the ability to add additional generic instances.2007--2022 wren romano BSD-3-Clause wren@cpan.org provisionalportable (with CPP, FFI)None+ñVlogfloatA helper type for -È. As a data type, this is really just so we can phrase things as using W.XlogfloatA helper type for **. As a semigroup, this is isomorphic to: %(WrappedMonoid (Sum Int), Max Double)Ç; however, we strictify and flatten everything to improve performance.#logfloatCompute  exp x - 1 without losing precision.6Standard C libraries provide a special definition for #Ú which is more accurate than doing the naive thing, especially for very small arguments.6This installation was compiled to use the FFI version.$logfloatCompute  log (1 + x) without losing precision.ÑStandard C libraries provide a special definition for this function, which is more accurate than doing the naive thing, especially for very small arguments. For example, the naive version underflows around 2 ** -535, whereas the specialized version underflows around  2 ** -1074. N.B. The statistics:Statistics.Math3 module provides a pure Haskell implementation of log1pÐ for those who are interested. We do not copy it here because it relies on the vectorÚ package which is non-portable. If there is sufficient interest, a portable variant of that implementation could be made. Contact the maintainer if the FFI and naive implementations are insufficient for your needs.6This installation was compiled to use the FFI version.%logfloatCompute log (1 - exp x) without losing precision.&logfloatCompute log (1 + exp x)2 without losing precision. Algebraically this is 0 ”E x(, which is the log-domain's analogue of 1 + x.'logfloat+The logistic function; aka, the inverse of (â. > sigmoid x = 1 / (1 + exp (-x)) > sigmoid x = exp x / (exp x + 1) > sigmoid x = (1 + tanh (x2))  2(logfloat+The quantile function; aka, the inverse of 'Á. > logit x = log (x / (1 - x)) > logit x = 2 * atanh (2*x - 1))logfloat A variant of (= for when the argument is already in the log-domain; hence, logitExp = logit . expYlogfloatÎCompute the length and maximum of a list. This is a semigroup reduction. However we roll it ourselves rather than using the semigroup class: since that would incur an otherwise unnecessary dependency on base >= 4.9.0.0.*logfloatO(n). Log-domain summation, aka: (log . sum . fmap exp). Algebraically this is †T xs), which is the log-domain equivalent of ‘D xs.N.B.š, this function requires two passes over the input. Thus, it is not amenable to list fusion, and hence will use a lot of memory when summing long lists.+logfloatO(n). Log-domain softmax, aka: (fmap log . softmax).N.B.9, this requires three passes over the data: two for the *’, and a third for the normalization itself. Thus, it is not amenable to list fusion, and hence will use a lot of memory when summing long lists.,logfloatO(n)Õ. Normal-domain softmax: > softmax xs = [ exp x / sum [ exp y | y <- xs] | x <- xs ]N.B.4, this requires three passes over the data: same as +.-logfloatO(n)Ô. Floating-point summation, via Kahan's algorithm. This is nominally equivalent to Z9, but greatly mitigates the problem of losing precision.N.B., this only requires a single pass over the data; but we use a strict left fold for performance, so it's still not amenable to list fusion.$logfloatN.B., only defined on the  [-1,infty] interval.%logfloatN.B., only defined on the  [-infty,0] interval.(logfloatN.B., only defined on the [0,1] interval.)logfloatN.B., only defined on the  [-infty,0] interval. #$%&'()*+,- #$%&*-+,'()*Copyright (c) 2007--2021 wren gayle romanoBSD3 wren@cpan.orgstableportable (with CPP, FFI) Safe-Inferred2ÄÅÑÜ?ð .logfloatA LogFloat is just a R% with a special interpretation. The /ë function is presented instead of the constructor, in order to ensure semantic conversion. At present the P• instance will convert back to the normal-domain, and hence will underflow at that point. This behavior may change in the future. At present, the [‡ instance parses things in the normal-domain and then converts them to the log-domain. Again, this behavior may change in the future.Because /Ë performs the semantic conversion, we can use operators which say what we mean© rather than saying what we're actually doing to the underlying representation. That is, equivalences like the following are true[1] thanks to type-class overloading: ×logFloat (p + q) == logFloat p + logFloat q logFloat (p * q) == logFloat p * logFloat qÏ(Do note, however, that subtraction can and negation will throw errors: since LogFloat* can only represent the positive half of R. Oõ is the wrong abstraction to put at the bottom of the numeric type-class hierarchy; but alas, we're stuck with it.)ºPerforming operations in the log-domain is cheap, prevents underflow, and is otherwise very nice for dealing with miniscule probabilities. However, crossing into and out of the log-domain is expensive and should be avoided as much as possible. In particular, if you're doing a series of multiplications as in lp * logFloat q * logFloat r it's faster to do lp * logFloat (q * r)‰ if you're reasonably sure the normal-domain multiplication won't underflow; because that way you enter the log-domain only once, instead of twice. Also note that, for precision, if you're doing more than a few multiplications in the log-domain, you should use 5 rather than using \ repeatedly.#Even more particularly, you should avoid additionš whenever possible. Addition is provided because sometimes we need it, and the proper implementation is not immediately apparent. However, between two LogFloatÁs addition requires crossing the exp/log boundary twice; with a LogFloat and a RÄ it's three times, since the regular number needs to enter the log-domain first. This makes addition incredibly slow. Again, if you can parenthesize to do normal-domain operations first, do it! 1ñThat is, true up-to underflow and floating point fuzziness. Which is, of course, the whole point of this module.]logfloat?Reduce the number of constant string literals we need to store./logfloatûConstructor which does semantic conversion from normal-domain to log-domain. Throws errors on negative and NaN inputs. If p4 is non-negative, then following equivalence holds: #logFloat p == logToLogFloat (log p)If pÎ is NaN or negative, then the two sides differ only in which error is thrown.0logfloat×Constructor which assumes the argument is already in the log-domain. Throws errors on  notANumber inputs.1logfloatŸSemantically convert our log-domain value back into the normal-domain. Beware of overflow/underflow. The following equivalence holds (without qualification): %fromLogFloat == exp . logFromLogFloat2logfloat6Return the log-domain value itself without conversion.^logfloat‹A curried function for converting arbitrary pairs into ordered pairs. The continuation recieves the minimum first and the maximum second.ÈThis combinator is primarily intended to reduce repetition in the source code; but hopefully it should also help reduce bloat in the compiled code, by sharing the continuation and just swapping the variables in place. Of course, if the continuation is very small, then requiring a join point after the conditional swap may end up being more expensive than simply duplicating the continuation. Also, given as we're inlining it, I'm not sure whether GHC will decide to keep the sharing we introduced or whether it'll end up duplicating the continuation into the two call sites.3logfloatO(1)î. Compute powers in the log-domain; that is, the following equivalence holds (modulo underflow and all that): 'logFloat (p ** m) == logFloat p `pow` m Since: 0.134logfloatO(n)&. Compute the sum of a finite list of .÷s, being careful to avoid underflow issues. That is, the following equivalence holds (modulo underflow and all that): $logFloat . sum == sum . map logFloatN.B.š, this function requires two passes over the input. Thus, it is not amenable to list fusion, and hence will use a lot of memory when summing long lists. Since: 0.135logfloatO(n)*. Compute the product of a finite list of .s, being careful to avoid numerical error due to loss of precision. That is, the following equivalence holds (modulo underflow and all that): ,logFloat . product == product . map logFloat Since: 0.13#$./012345 ./102453$#38ß      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGEFHIJKEFLEFMEFNEFOEFPEFQEFREFSEFTIUVIWXIYZI[\I]^IYE_`E_aIWIWbcIdefgId9IhiI[jklí&logfloat-0.14.0-LffXmRCJDLm8duusKLknm0Hugs.RealFloatData.Number.PartialOrdData.Number.TransfiniteData.Number.RealToFracData.Number.LogFloat.RawData.Number.LogFloat isInfiniteisNaN PartialOrdcmpgtgeeqneleltmaxPOminPO comparingPO$fPartialOrdDouble$fPartialOrdFloat $fPartialOrda TransfiniteinfinitynegativeInfinity notANumberlog$fTransfiniteFloat$fTransfiniteDouble RealToFrac realToFrac$fRealToFracDoubleFloat$fRealToFracFloatDouble$fRealToFracIntegerDouble$fRealToFracIntegerFloat$fRealToFracIntDouble$fRealToFracIntFloat$fRealToFracab$fRealToFracaaexpm1log1plog1mexplog1pexpsigmoidlogitlogitExp logSumExp logSoftmaxsoftmaxkahanSumLogFloatlogFloat logToLogFloat fromLogFloatlogFromLogFloatpowsumproduct$fRealLogFloat$fFractionalLogFloat $fNumLogFloat$fShowLogFloat$fReadLogFloat$fPartialOrdLogFloat$fIArrayUArrayLogFloat $fEqLogFloat $fOrdLogFloat$fStorableLogFloatghc-prim GHC.ClassesOrdEqbase GHC.MaybeMaybecompare>>===/=<=<maxminGHC.EnumBoundedGHC.Real Fractional GHC.FloatFloatingGHC.NumNumGHC.ShowShow GHC.TypesDoubleFloatRationalKahan Data.Foldablefoldl'LSEfoldLSEGHC.ReadRead*errorOutOfRangeordered