Skip to content

Commit

Permalink
most of the heavy owrk done except histogram, testing still needs to …
Browse files Browse the repository at this point in the history
…be done
  • Loading branch information
jacomago committed Mar 7, 2014
1 parent 7c90981 commit 0d45102
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 20 deletions.
25 changes: 25 additions & 0 deletions analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"fmt"
"github.com/jacomago/zeta-zeros-analysis/data"
)

func main() {
var f string
fmt.Scan(&f)
z := make(chan data.Zeros)
go func() { z <- data.ImportZeros(f) }()

fracDiff := make(chan []float64)

go func() { fracDiff <- data.FractionalDifferences(<-z) }()

d := <-fracDiff
go func() {
fmt.Println("The average is ", data.Avg(d))
}()
go func() {
fmt.Println("The max is ", data.Max(d))
}()
}
80 changes: 76 additions & 4 deletions data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,22 @@ func check(e error) {
}
}

func Max(values []float64) float64 {
result := 0.0
for _, v := range values {
result = math.Max(v, result)
}
return result
}

func Avg(values []float64) float64 {
sum := 0.0
for _, v := range values {
sum += v
}
return sum / float64(len(values))
}

func ImportZeros(filename string) Zeros {
f, err := os.Open(filename)
check(err)
Expand All @@ -31,11 +47,11 @@ func ImportZeros(filename string) Zeros {
return z
}

func ExportData(data) {
func ExportData(data []float64) {
}

func floatsEqual(a float64, b float64) bool {
Epsilon := 0.000001
Epsilon := 0.0000001
if math.Abs(a-b) < Epsilon {
return true
}
Expand All @@ -44,18 +60,74 @@ func floatsEqual(a float64, b float64) bool {

// fractional differences
func closestZero(z Zeros, zero float64) float64 {
diff := make(chan float64)
minDistance := 1.0
go func() {
for _, decimalZero := range z {
if !floatsEqual(decimalZero, zero) {
diff <- math.Abs(decimalZero - zero)
}
}
close(diff)
}()
for d := range diff {
minDistance = math.Min(d, minDistance)
}
return minDistance
}

func fractionalDifferences(z Zeros) (float64, float64) {
func FractionalDifferences(z Zeros) []float64 {
distance := make(chan float64)
result := make([]float64, 0, len(z))
go func() {
for _, zero := range z {
distance <- closestZero(z, zero)
}
close(distance)
}()

for d := range distance {
result = append(result, d)
}
return result
}

// zeros epsilon close to other
func fCloseTo(z Zeros, zero float64, epsilon float64) int {
result := 0
test := make(chan bool)
go func() {
for _, decimalZero := range z {
test <- math.Abs(decimalZero-zero) < epsilon
}
close(test)
}()
for b := range test {
if b {
result += 1
}
}
return result
}

func zerosFCloseTo(z Zeros, epsilon float64) float64 {
type Psi func(int) float64

func ZerosFCloseTo(z Zeros, p Psi) []int {
value := make(chan int)
values := make([]int, 0, len(z))
go func() {
for _, zero := range z {
value <- fCloseTo(z, zero, p(len(z)))
}
close(value)
}()
for v := range value {
values = append(values, v)
}
return values
}

// histogram creating
func createHistogram(z Zeros, epsilon float64) map[int]float64 {
return nil
}
58 changes: 48 additions & 10 deletions data/data_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ func TestFloatsEqual(t *testing.T) {
}
}

func TestMax(t *testing.T) {
in := []float64{1, 2, 3, 4, 5, 6}
const out = 6.0
if x := Max(in); !floatsEqual(x, out) {
t.Errorf("max(%v) = %v, want %v", in, x, out)
}
}

func TestAvg(t *testing.T) {
in := []float64{1, 2, 3, 4, 5, 6}
out := 3.5
if x := Avg(in); !floatsEqual(x, out) {
t.Errorf("Avg(%v) = %v, want %v", in, x, out)
}
}

func TestImportZeros(t *testing.T) {
const in string = "test.dat"
out := Zeros{3.14: 0.14, 2.71: 0.71}
Expand All @@ -28,21 +44,43 @@ func TestImportZeros(t *testing.T) {
}

func TestClosestZero(t *testing.T) {

in := Zeros{3.14: 0.14, 2.71: 0.71, 4.56: 0.56}
const out = 0.42
in2 := 0.14
if x := closestZero(in, 0.14); !floatsEqual(x, out) {
t.Errorf("closestZero(%v, %v) = %v, want %v", in, in2, x, out)
}
}

func TestFractionalDifferences(t *testing.T) {

}

func TestFCloseTo(t *testing.T) {

in := Zeros{3.14: 0.14, 2.71: 0.71, 4.56: 0.56}
out := []float64{0.42, 0.15, 0.15}
x := FractionalDifferences(in)
for i, v := range x {
if !floatsEqual(out[i], v) {
t.Errorf("fractionalDifferences(%v) = %v, want %v", in, x, out)
}
}
}

func TestZerosFCloseTo(t *testing.T) {

func TestfCloseTo(t *testing.T) {
in := Zeros{3.14: 0.14, 2.71: 0.71, 4.56: 0.56}
inZero := 0.14
inEpsilon := 0.43
out := 1
if x := fCloseTo(in, inZero, inEpsilon); x != out {
t.Errorf("fCloseTo(%v, %v, %v) = %v, want %v", in, inZero, inEpsilon, x, out)
}
}

func TestCreateHistogram(t *testing.T) {

func TestzerosFCloseTo(t *testing.T) {
in := Zeros{3.14: 0.14, 2.71: 0.71, 4.56: 0.56}
inP := func(n int) float64 { return 1 / float64(n) }
out := []int{0, 1, 1}
r := ZerosFCloseTo(in, inP)
for i, x := range r {
if x != out[i] {
t.Errorf("zerosFCloseTo(%v, %v) = %v, want %v", in, inP, x, out)
}
}
}
6 changes: 0 additions & 6 deletions main.go

This file was deleted.

0 comments on commit 0d45102

Please sign in to comment.