-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgenerate.go
67 lines (58 loc) · 1.79 KB
/
generate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package matrix
import (
"fmt"
"math/rand"
)
// GenerateMatrix creates a zero matrix with `rows` rows and `cols` cols.
func GenerateMatrix(rows, cols int) (matrix Matrix) {
matrix = make(Matrix, rows*cols+2)
matrix[0] = float64(rows)
matrix[1] = float64(cols)
return
}
// Build generates a new matrix by passing from `Builder`.
//
// This allows to have a human friendly looking way of initializing matrices:
//
// myMatrix, _ := matrix.Build(
// matrix.Builder {
// matrix.Row{10, -5.3, 22},
// matrix.Row{-2, -25, 12},
// matrix.Row{ 7, 5, -12.5},
// },
// )
//
// It returns an error if you try to provide a builder with no rows or
// rows with no cols (you can safely ignore error if you're confident
// your builder is valid).
func Build(builder Builder) (resultMatrix Matrix, err error) {
if len(builder) == 0 || len(builder[0]) == 0 {
err = generateError(fmt.Sprintf("Can't build empty matrix. If you want to generate a zero matrix, use GenerateMatrix()"))
return
}
resultMatrix = GenerateMatrix(len(builder), len(builder[0]))
for i, row := range builder {
for j, value := range row {
resultMatrix[resultMatrix.IndexFor(i, j)] = value
}
}
return
}
// ZeroMatrixFrom generates a Matrix having the same dimensions than origin matrix,
// but filled with 0.0
func ZeroMatrixFrom(origin Matrix) Matrix {
return GenerateMatrix(int(origin[0]), int(origin[1]))
}
// RandomMatrix generates a matrix of `rows` rows and `cols` cols with randomized
// values.
//
// Values are randomized with standard normal distribution, using
// `math.NormFloat64()` (don't forget to seed randomizer if you
// want it to be truly random).
func RandomMatrix(rows, cols int) Matrix {
matrix := GenerateMatrix(rows, cols)
for i := 2; i < len(matrix); i++ {
matrix[i] = rand.NormFloat64()
}
return matrix
}