Exemplo n.º 1
0
// Reads a .dat file where the entries are matricies of numbers
// separated by empty new lines and individually aligned with
// whitespace between row entries and newlines between rows
func Read(file []byte) []matrix.Matrix {
	data := make([]matrix.Matrix, 0)
	for _, mat := range bytes.Split(file, []byte("\n\n")) {
		temp := make(matrix.Matrix, 0)
		didParse := true
	Element:
		for _, row := range bytes.Split(mat, []byte("\n")) {
			floatsAsStrings := strings.Fields(string(row))
			elms := make([]matrix.Element, 0)
			for _, s := range floatsAsStrings {
				f, err := strconv.ParseFloat(s, 64)
				if err != nil {
					didParse = false
					break Element
				}
				elms = append(elms, matrix.Element(f))
			}
			temp = append(temp, elms)
		}

		if didParse && len(temp[0]) > 0 {
			data = append(data, temp)
		}
	}
	return data
}
Exemplo n.º 2
0
func (g *generator) Distance() (matrix.Matrix, error) {
	m := matrix.Matrix(make([][]matrix.Element, g.n))

	for i := 0; i < g.n; i++ {
		m[i] = make([]matrix.Element, g.n)
		for j := 0; j < g.n; j++ {
			m[i][j] = matrix.Element(rand.Float64())
		}
	}

	return m, nil
}
Exemplo n.º 3
0
func (g *generator) Flow(spread float64) (matrix.Matrix, error) {
	if spread < 0 || spread >= 1 {
		return nil, fmt.Errorf("Error: spread must be between 0 and 1.")
	}

	// Create Zipf generator
	scale := 1000
	r := rand.New(rand.NewSource(0))
	zipf := rand.NewZipf(r, 1.01, float64(g.n), uint64(scale))

	// Populate frequencies of unigrams
	k := make([]float64, g.n)
	for i := 0; i < g.n; i++ {
		k[i] = float64(zipf.Uint64())
	}

	// Populate ideal bigram matrix
	m := matrix.Matrix(make([][]matrix.Element, g.n))
	for i := 0; i < g.n; i++ {
		m[i] = make([]matrix.Element, g.n)
		for j := 0; j < g.n; j++ {
			e := (rand.Float64() - 0.5) * spread
			m[i][j] = matrix.Element((k[i] * k[j]) * (1 + e))
		}
	}

	// Scale back to 100,000 total freq
	totalF := m.Sum()
	s := g.fscale / totalF
	for i := 0; i < g.n; i++ {
		for j := 0; j < g.n; j++ {
			m[i][j] = matrix.Element(math.Floor(float64(m[i][j]) * s))
		}
	}

	return m, nil
}