<commit_before>package dual

import (
	"fmt"
	"math"
	"strings"
)

// A Real represents a dual real number as an ordered array of two float64
// values.
type Real [2]float64

// String returns the string version of a Real value. If z = a + bε, then the
// string is "(a+bε)", similar to complex128 values.
func (z *Real) String() string {
	a := make([]string, 5)
	a[0] = "("
	a[1] = fmt.Sprintf("%g", z[0])
	switch {
	case math.Signbit(z[1]):
		a[2] = fmt.Sprintf("%g", z[1])
	case math.IsInf(z[1], +1):
		a[2] = "+Inf"
	default:
		a[2] = fmt.Sprintf("+%g", z[1])
	}
	a[3] = "ε"
	a[4] = ")"
	return strings.Join(a, "")
}

// Equals returns true if z and y are equal.
func (z *Real) Equals(y *Real) bool {
	for i := range z {
		if notEquals(z[i], y[i]) {
			return false
		}
	}
	return true
}

// Copy copies y onto z, and returns z.
func (z *Real) Copy(y *Real) *Real {
	for i, v := range y {
		z[i] = v
	}
	return z
}

// NewReal returns a pointer to a Real value made from two given float64 values.
func NewReal(a, b float64) *Real {
	z := new(Real)
	z[0] = a
	z[1] = b
	return z
}

// IsRealInf returns true if any of the components of z are infinite.
func (z *Real) IsRealInf() bool {
	for _, v := range z {
		if math.IsInf(v, 0) {
			return true
		}
	}
	return false
}

// RealInf returns a pointer to a dual real infinity value.
func RealInf(a, b int) *Real {
	z := new(Real)
	z[0] = math.Inf(a)
	z[1] = math.Inf(b)
	return z
}

// IsRealNaN returns true if any component of z is NaN and neither is an
// infinity.
func (z *Real) IsRealNaN() bool {
	for _, v := range z {
		if math.IsInf(v, 0) {
			return false
		}
	}
	for _, v := range z {
		if math.IsNaN(v) {
			return true
		}
	}
	return false
}

// RealNaN returns a pointer to a dual real NaN value.
func RealNaN() *Real {
	nan := math.NaN()
	return &Real{nan, nan}
}

// Scal sets z equal to y scaled by a, and returns z.
func (z *Real) Scal(y *Real, a float64) *Real {
	for i, v := range y {
		z[i] = a * v
	}
	return z
}

// Neg sets z equal to the negative of y, and returns z.
func (z *Real) Neg(y *Real) *Real {
	return z.Scal(y, -1)
}

// DualConj sets z equal to the dual conjugate of y, and returns z.
func (z *Real) DualConj(y *Real) *Real {
	z[0] = +y[0]
	z[1] = -y[1]
	return z
}

// Add sets z equal to the sum of x and y, and returns z.
func (z *Real) Add(x, y *Real) *Real {
	for i, v := range x {
		z[i] = v + y[i]
	}
	return z
}

// Sub sets z equal to the difference of x and y, and returns z.
func (z *Real) Sub(x, y *Real) *Real {
	for i, v := range x {
		z[i] = v - y[i]
	}
	return z
}

// Mul sets z equal to the product of x and y, and returns z.
//
// The basic rule is:<commit_msg>Remove some for loops in real.go<commit_after>package dual

import (
	"fmt"
	"math"
	"strings"
)

// A Real represents a dual real number as an ordered array of two float64
// values.
type Real [2]float64

// String returns the string version of a Real value. If z = a + bε, then the
// string is "(a+bε)", similar to complex128 values.
func (z *Real) String() string {
	a := make([]string, 5)
	a[0] = "("
	a[1] = fmt.Sprintf("%g", z[0])
	switch {
	case math.Signbit(z[1]):
		a[2] = fmt.Sprintf("%g", z[1])
	case math.IsInf(z[1], +1):
		a[2] = "+Inf"
	default:
		a[2] = fmt.Sprintf("+%g", z[1])
	}
	a[3] = "ε"
	a[4] = ")"
	return strings.Join(a, "")
}

// Equals returns true if z and y are equal.
func (z *Real) Equals(y *Real) bool {
	if notEquals(z[0], y[0]) || notEquals(z[1], y[1]) {
		return false
	}
	return true
}

// Copy copies y onto z, and returns z.
func (z *Real) Copy(y *Real) *Real {
	z[0] = y[0]
	z[1] = y[1]
	return z
}

// NewReal returns a pointer to a Real value made from two given float64 values.
func NewReal(a, b float64) *Real {
	z := new(Real)
	z[0] = a
	z[1] = b
	return z
}

// IsRealInf returns true if any of the components of z are infinite.
func (z *Real) IsRealInf() bool {
	if math.IsInf(z[0], 0) || math.IsInf(z[1], 0) {
		return true
	}
	return false
}

// RealInf returns a pointer to a dual real infinity value.
func RealInf(a, b int) *Real {
	z := new(Real)
	z[0] = math.Inf(a)
	z[1] = math.Inf(b)
	return z
}

// IsRealNaN returns true if any component of z is NaN and neither is an
// infinity.
func (z *Real) IsRealNaN() bool {
	if math.IsInf(z[0], 0) || math.IsInf(z[1], 0) {
		return false
	}
	if math.IsNaN(z[0]) || math.IsNaN(z[1]) {
		return true
	}
	return false
}

// RealNaN returns a pointer to a dual real NaN value.
func RealNaN() *Real {
	nan := math.NaN()
	return &Real{nan, nan}
}

// Scal sets z equal to y scaled by a, and returns z.
func (z *Real) Scal(y *Real, a float64) *Real {
	z[0] = y[0] * a
	z[1] = y[1] * a
	return z
}

// Neg sets z equal to the negative of y, and returns z.
func (z *Real) Neg(y *Real) *Real {
	return z.Scal(y, -1)
}

// DualConj sets z equal to the dual conjugate of y, and returns z.
func (z *Real) DualConj(y *Real) *Real {
	z[0] = +y[0]
	z[1] = -y[1]
	return z
}

// Add sets z equal to the sum of x and y, and returns z.
func (z *Real) Add(x, y *Real) *Real {
	z[0] = x[0] + y[0]
	z[1] = x[1] + y[1]
	return z
}

// Sub sets z equal to the difference of x and y, and returns z.
func (z *Real) Sub(x, y *Real) *Real {
	z[0] = x[0] - y[0]
	z[1] = x[1] - y[1]
	return z
}

// Mul sets z equal to the product of x and y, and returns z.
//
// The basic rule is:
{'fraction_non_alphanumeric': 0.1626551335088379, 'fraction_numerical': 0.02068446784505453, 'mean_word_length': 2.873998543335761, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7328672', 'n_tokens_mistral': 2265, 'n_tokens_neox': 2192, 'n_words': 822}