本文整理汇总了Golang中math.Log2函数的典型用法代码示例。如果您正苦于以下问题:Golang Log2函数的具体用法?Golang Log2怎么用?Golang Log2使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Log2函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestRandomDatasetHasExpectedStatistics
func TestRandomDatasetHasExpectedStatistics(t *testing.T) {
tests := []struct {
numSamples int
probability float64
}{
{100000, 0.02},
{100000, 0.5},
{100000, 0.9},
}
for _, tt := range tests {
d := randomDataset(tt.numSamples, tt.probability)
t.Log()
t.Log(d.String())
if !near(d.Calibration(), 1.0) {
t.Errorf("Calibration: expected %v, had %v", 1.0, d.Calibration())
}
expectedLogScore :=
tt.probability*math.Log2(tt.probability) +
(1-tt.probability)*math.Log2(1-tt.probability)
if !near(d.LogScore(), expectedLogScore) {
t.Errorf("Logscore: expected %v, had %v", expectedLogScore, d.LogScore())
}
if !near(d.NormalizedEntropy(), 1.0) {
t.Errorf("Entropy: expected %v, had %v", 1.0, d.NormalizedEntropy())
}
if !near(d.ROC(), 0.5) {
t.Errorf("ROC: expected %v, had %v", 0.5, d.ROC())
}
}
}
示例2: CalculateEntropy
// Calculates the entropy of each symbol, based on the counts of each symbol. The
// result is similar to the result of CalculateBitLengths, but with the
// actual theoritical bit lengths according to the entropy. Since the resulting
// values are fractional, they cannot be used to encode the tree specified by
// DEFLATE.
func CalculateEntropy(count []float64) (bitLengths []float64) {
var sum, log2sum float64
n := len(count)
for i := 0; i < n; i++ {
sum += count[i]
}
if sum == 0 {
log2sum = math.Log2(float64(n))
} else {
log2sum = math.Log2(sum)
}
bitLengths = make([]float64, n)
for i := 0; i < n; i++ {
// When the count of the symbol is 0, but its cost is requested anyway, it
// means the symbol will appear at least once anyway, so give it the cost as if
// its count is 1.
if count[i] == 0 {
bitLengths[i] = log2sum
} else {
bitLengths[i] = math.Log2(sum / count[i])
}
if !(bitLengths[i] >= 0) {
panic("bit length is not positive")
}
}
return bitLengths
}
示例3: Js
func Js(words_left map[string]int, words_right map[string]int) float64 {
len_left, len_right := 0, 0
for _, val := range words_left {
len_left += val
}
for _, val := range words_right {
len_right += val
}
dist := 0.0
for key, val := range words_left {
p := float64(val) / float64(len_left)
q := 0.0
if len_right > 0 {
q = float64(words_right[key]) / float64(len_right)
}
dist += p * math.Log2(2*p/(p+q))
}
for key, val := range words_right {
p := float64(val) / float64(len_right)
q := 0.0
if len_left > 0 {
q = float64(words_left[key]) / float64(len_left)
}
dist += p * math.Log2(2*p/(p+q))
}
return dist
}
示例4: CreateRankDirectory
/**
Used to build a rank directory from the given input string.
@param data A javascript string containing the data, as readable using the
BitString object.
@param numBits The number of bits to index.
@param l1Size The number of bits that each entry in the Level 1 table
summarizes. This should be a multiple of l2Size.
@param l2Size The number of bits that each entry in the Level 2 table
summarizes.
*/
func CreateRankDirectory(data string, numBits, l1Size, l2Size uint) RankDirectory {
bits := BitString{}
bits.Init(data)
var p, i uint = 0, 0
var count1, count2 uint = 0, 0
l1bits := uint(math.Ceil(math.Log2(float64(numBits))))
l2bits := uint(math.Ceil(math.Log2(float64(l1Size))))
directory := BitWriter{}
for p+l2Size <= numBits {
count2 += bits.Count(p, l2Size)
i += l2Size
p += l2Size
if i == l1Size {
count1 += count2
directory.Write(count1, l1bits)
count2 = 0
i = 0
} else {
directory.Write(count2, l2bits)
}
}
rd := RankDirectory{}
rd.Init(directory.GetData(), data, numBits, l1Size, l2Size)
return rd
}
示例5: expTables
// expTables prepares the exp-tables described in section 6.4 of the EIDMA report by F.M.J. Willems and Tj. J. Tjalkens.
// Complexity Reduction of the Context-Tree Weighting Algorithm: A Study for KPN Research, Technical University of Eindhoven, EIDMA Report RS.97.01
func expTables() ([]uint64, []uint64) {
var pow2f float64 = 1 << f
A := make([]uint64, int(pow2f)+1)
for i := 1; i <= int(pow2f); i++ {
A[i] = uint64(pow2f*math.Exp2(-float64(i)/pow2f) + 0.5)
}
// B entries for (1<<(f-1)), (1<<f)-1
B := make([]uint64, int(pow2f))
for j := 1 << (f - 1); j <= (1<<f)-1; j++ {
B[j] = uint64(-pow2f*math.Log2(float64(j)/pow2f) + 0.5)
}
// B entries for 1,(1<<(f-1))-1
for j := 1; j < (1 << (f - 1)); j++ {
k := math.Ceil(float64(f) - 1 - math.Log2(float64(j)))
b2kj := B[int(math.Exp2(k))*j]
if b2kj == 0 {
panic("")
}
B[j] = b2kj + uint64(k*pow2f)
}
return A, B
}
示例6: MutualInformation
func (corpus *Corpus) MutualInformation(seq []int) (I float64) {
// Returns the mutual information, in bits, conveyed by the items in a sequence.
I = math.Log2(corpus.Probability(seq))
for i := 0; i < len(seq); i++ {
I -= math.Log2(corpus.Probability(seq[i : i+1]))
}
return I
}
示例7: solve
func solve(n float64) int {
for digits := int(math.Log10(n)) + 2; ; digits++ {
low := int(math.Log2(n) + float64(digits)*log2_10)
high := int(math.Log2(n+1) + float64(digits)*log2_10)
if low+1 == high {
return high
}
}
}
示例8: InitOneLayer
// InitOneLayer
func (qt *QuadTree) InitOneLayer(xmin, xmax, ymin, ymax, z int64, resx, resy, resz float64) *QuadTree {
qt = new(QuadTree)
dimx := xmax - xmin + 1
dimy := ymax - ymin + 1
var depthx, depthy int
if dimx > qtW {
depthx = int(math.Log2(float64(dimx))+0.5) - int(math.Log2(float64(qtW))) + 1
} else {
depthx = 1
}
if dimy > qtH {
depthy = int(math.Log2(float64(dimy))+0.5) - int(math.Log2(float64(qtH))) + 1
} else {
depthy = 1
}
depth := int(math.Max(float64(depthx), float64(depthy)))
tasks := qt.TaskLoad(depth)
fmt.Printf("The depth of this quadtree is %v with %v tasks assigned\n", depth, tasks)
for i := 1; i < depth; i++ {
resx *= 2.0
resy *= 2.0
}
ch := make(chan bool)
wg.Add(1)
go qt.Construct(nil, 0, depth, -1, xmin, ymin, z, xmax, ymax, resx, resy, resz, 0, 0, 0, qtW, qtH, 1, ch, &wg)
//<-ch
go func() {
wg.Wait()
//close(ch)
<-ch
}()
//close(ch)
// for i := range ch {
// fmt.Println("~~~channels ",i)
// }
//wg.Wait()
fmt.Printf("~~~current tile %v children %v %v %v %v %v %v\n", qt, qt.TL, qt.TR, qt.BL, qt.BR, resx, resy)
qt.TraverseTree()
return qt
}
示例9: pushPullScale
// pushPushScale is used to scale the time interval at which push/pull
// syncs take place. It is used to prevent network saturation as the
// cluster size grows
func pushPullScale(interval time.Duration, n int) time.Duration {
// Don't scale until we cross the threshold
if n <= pushPullScaleThreshold {
return interval
}
multiplier := math.Ceil(math.Log2(float64(n))-math.Log2(pushPullScaleThreshold)) + 1.0
return time.Duration(multiplier) * interval
}
示例10: init
func init() {
flag.BoolVar(&sequenceMode, "s", false, "sequence mode")
lg3 = math.Log2(3)
lg5 = math.Log2(5)
front = [3]cursor{
{0, 0, 1}, // 2
{1, 0, lg3}, // 3
{2, 0, lg5}, // 5
}
}
示例11: Init
func (rd *RankDirectory) Init(directoryData, bitData string, numBits, l1Size, l2Size uint) {
rd.directory.Init(directoryData)
rd.data.Init(bitData)
rd.l1Size = l1Size
rd.l2Size = l2Size
rd.l1Bits = uint(math.Ceil(math.Log2(float64(numBits))))
rd.l2Bits = uint(math.Ceil(math.Log2(float64(l1Size))))
rd.sectionBits = (l1Size/l2Size-1)*rd.l2Bits + rd.l1Bits
rd.numBits = numBits
}
示例12: NormalizedEntropy
func (l labelledPredictions) NormalizedEntropy() float64 {
numPositives := 0
for _, e := range l {
if e.Label {
numPositives += 1
}
}
p := float64(numPositives) / float64(l.Len())
return l.LogScore() / (p*math.Log2(p) + (1-p)*math.Log2(1-p))
}
示例13: human_scale
func human_scale(value float64, base float64, unit string) string {
exp := []string{"y", "z", "a", "f", "p", "n", "µ", "m", "", "k", "M", "G", "T", "P", "E", "Z", "Y"}
s := math.Floor(math.Log2(value) / math.Log2(base))
h_v := value / math.Pow(base, s)
if s > -9 && s < 9 {
return strconv.FormatFloat(h_v, 'f', 2, 64) + " " + exp[int(s)+8] + unit
}
return strconv.FormatFloat(value, 'E', 6, 64) + " " + unit
}
示例14: LogScore
func (l labelledPredictions) LogScore() float64 {
cumulativeLogLoss := 0.0
for _, e := range l {
if e.Label {
cumulativeLogLoss += math.Log2(e.Prediction)
} else {
cumulativeLogLoss += math.Log2(1 - e.Prediction)
}
}
return cumulativeLogLoss / float64(l.Len())
}
示例15: entropy
// From http://rosettacode.org/wiki/Entropy#Go
func entropy(s string) float64 {
m := map[rune]float64{}
for _, r := range s {
m[r]++
}
hm := 0.
for _, c := range m {
hm += c * math.Log2(c)
}
l := float64(len(s))
return math.Log2(l) - hm/l
}