<commit_before>package breeze

import generic.UFunc
import linalg.Tensor
import scala.math._

/**
 * Provides some functions left out of java.lang.math.
 *
 * @author dlwh, afwlehmann
 */
package object numerics extends UniversalFuncs {

  val inf, Inf = Double.PositiveInfinity
  val nan, NaN = Double.NaN


  /**
   * The standard digamma function. Cribbed from <NAME>
   *
   * http://google.com/codesearch/p?hl=en#EbB356_xxkI/fbm.2003-06-29/util/digamma.c
   */
  val digamma = new UFunc[Double, Double] {
    def apply(xx: Double) = {
      var x = xx
      var r = 0.0

      while (x<=5) {
        r -= 1/x
        x += 1
      }

      val f = 1./(x * x)
      val t = f*(-1/12.0 +
        f*(1/120.0 +
          f*(-1/252.0 +
            f*(1/240.0 +
              f*(-1/132.0 +
                f*(691/32760.0 +
                  f*(-1/12.0 +
                    f*3617.0/8160.0)))))))
      r + log(x) - 0.5/x + t
    }
  }

  private val cof =  Array(76.18009172947146, -86.50532032941677,
    24.01409824083091,-1.231739572450155,
    0.1208650973866179e-2,-0.5395239384953e-5
  )

  /**
   * Evaluates the log of the generalized beta function.
   *  = \sum_a lgamma(c(a))- lgamma(c.sum)
   */
  def lbeta[T](c: Tensor[T,Double]) = {
    c.valuesIterator.foldLeft(-lgamma(c.sum))( (acc,x)=> acc +lgamma(x))
  }

  /**
  * Computes the log of the gamma function.
  *
  * Reference: Numerical Recipes in C
  * http://www.library.cornell.edu/nr/cbookcpdf.html
  * www.cs.berkeley.edu/~milch/blog/versions/blog-0.1.3/blog/distrib
  * @return an approximation of the log of the Gamma function * of x.  Laczos Approximation<commit_msg>lbeta is now for quasitensors<commit_after>package breeze

import generic.UFunc
import linalg.{QuasiTensor, Tensor}
import scala.math._

/**
 * Provides some functions left out of java.lang.math.
 *
 * @author dlwh, afwlehmann
 */
package object numerics extends UniversalFuncs {

  val inf, Inf = Double.PositiveInfinity
  val nan, NaN = Double.NaN


  /**
   * The standard digamma function. Cribbed from <NAME>
   *
   * http://google.com/codesearch/p?hl=en#EbB356_xxkI/fbm.2003-06-29/util/digamma.c
   */
  val digamma = new UFunc[Double, Double] {
    def apply(xx: Double) = {
      var x = xx
      var r = 0.0

      while (x<=5) {
        r -= 1/x
        x += 1
      }

      val f = 1./(x * x)
      val t = f*(-1/12.0 +
        f*(1/120.0 +
          f*(-1/252.0 +
            f*(1/240.0 +
              f*(-1/132.0 +
                f*(691/32760.0 +
                  f*(-1/12.0 +
                    f*3617.0/8160.0)))))))
      r + log(x) - 0.5/x + t
    }
  }

  private val cof =  Array(76.18009172947146, -86.50532032941677,
    24.01409824083091,-1.231739572450155,
    0.1208650973866179e-2,-0.5395239384953e-5
  )

  /**
   * Evaluates the log of the generalized beta function.
   *  = \sum_a lgamma(c(a))- lgamma(c.sum)
   */
  def lbeta[T](c: QuasiTensor[T,Double]) = {
    c.valuesIterator.foldLeft(-lgamma(c.sum))( (acc,x)=> acc +lgamma(x))
  }

  /**
  * Computes the log of the gamma function.
  *
  * Reference: Numerical Recipes in C
  * http://www.library.cornell.edu/nr/cbookcpdf.html
  * www.cs.berkeley.edu/~milch/blog/versions/blog-0.1.3/blog/distrib
  * @return an approximation of the log of the Gamma function * of x.  Laczos Approximation
{'fraction_non_alphanumeric': 0.1491862567811935, 'fraction_numerical': 0.10006027727546715, 'mean_word_length': 2.101869158878505, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4223226', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1279, 'n_words': 264}