<commit_before>package scalapplcodefest

import scalapplcodefest.term._
import scalapplcodefest.value.Fun
import scalapplcodefest.term.LambdaAbstraction
import scala.Some
import org.scalautils.{Bad, Good, Or}
import TermDSL._

/**
 * @author sameer
 */
object LogZ {
  def unapply(term: Term[Double]): Option[(Variable[Vector], Term[Vector])] = term match {
    case m: LogZ[_] => Some(m.parameter, m.gradient)
    case _ => None
  }

  /**
   * Computes logZ of the term by exhaustive search.
   * @param term the term to maximize.
   */
  case class ByBruteForce[T](term: LambdaAbstraction[T, Double]) extends LogZ[T] with Composite1[Fun[T, Double], Double] with DoubleTerm {
    val ForceLinear(coefficient, parameter, _) = term.body
    private var logZ: Term[Double] = _
    private var marginals: Term[Vector] = _
    private val withStateDo = new WithStateDo(state => {
      val argument = state(parameter)
      val body = term.body | parameter -> argument
      val coeffs = coefficient | parameter -> argument
      val (lZ, ms) = compute(body, coeffs)
      logZ = Constant(lZ)
      marginals = Constant(ms)
    })

    def compute(body: Term[Double], coeff: Term[Vector]): (Double, Vector) = {
      import StrictMath._
      val vars = body.variables
      var Z = 0.0

      for (s <- State.allStates(vars.toList)) {
        val score = exp(body.value(s))
        Z += score<commit_msg>fix from sebastian<commit_after>package scalapplcodefest

import scalapplcodefest.term._
import scalapplcodefest.value.Fun
import scalapplcodefest.term.LambdaAbstraction
import scala.Some
import org.scalautils.{Bad, Good, Or}
import TermDSL._

/**
 * @author sameer
 */
object LogZ {
  def unapply(term: Term[Double]): Option[(Variable[Vector], Term[Vector])] = term match {
    case m: LogZ[_] => Some(m.parameter, m.gradient)
    case _ => None
  }

  /**
   * Computes logZ of the term by exhaustive search.
   * @param term the term to maximize.
   */
  case class ByBruteForce[T](term: LambdaAbstraction[T, Double]) extends LogZ[T] with Composite1[Fun[T, Double], Double] with DoubleTerm {
    val ForceLinear(coefficient, parameter, _) = term.body
    private var logZ: Term[Double] = _
    private var marginals: Term[Vector] = _
    private val withStateDo = new WithStateDo(state => {
      val argument = parameter.value(state)
      val body = term.body | parameter -> argument
      val coeffs = coefficient | parameter -> argument
      val (lZ, ms) = compute(body, coeffs)
      logZ = Constant(lZ)
      marginals = Constant(ms)
    })

    def compute(body: Term[Double], coeff: Term[Vector]): (Double, Vector) = {
      import StrictMath._
      val vars = body.variables
      var Z = 0.0

      for (s <- State.allStates(vars.toList)) {
        val score = exp(body.value(s))
        Z += score
{'fraction_non_alphanumeric': 0.11758436944937833, 'fraction_numerical': 0.0021314387211367673, 'mean_word_length': 3.292682926829268, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '4615542', 'n_tokens_mistral': 911, 'n_tokens_neox': 896, 'n_words': 247}