Example #1
0
func randmatmul(n int) matrix.MatrixRO {
	a := matrix.Zeros(n, n)
	b := matrix.Zeros(n, n)
	for i := 0; i < n; i++ {
		for k := 0; k < n; k++ {
			a.Set(i, k, rand.Float64())
			b.Set(i, k, rand.Float64())
		}
	}
	return matrix.Product(a, b)
}
Example #2
0
func randmatstat(t int) (float64, float64) {
	n := 5
	var v stats.Stats
	var w stats.Stats
	for i := 0; i < t; i++ {
		a := matrix.Zeros(n, n)
		b := matrix.Zeros(n, n)
		c := matrix.Zeros(n, n)
		d := matrix.Zeros(n, n)
		for j := 0; j < n; j++ {
			for k := 0; k < n; k++ {
				a.Set(j, k, rand.NormFloat64())
				b.Set(j, k, rand.NormFloat64())
				c.Set(j, k, rand.NormFloat64())
				d.Set(j, k, rand.NormFloat64())
			}
		}
		P := matrix.Zeros(n, 4*n)
		for j := 0; j < n; j++ {
			for k := 0; k < n; k++ {
				P.Set(j, k, a.Get(j, k))
				P.Set(j, n+k, b.Get(j, k))
				P.Set(j, 2*n+k, c.Get(j, k))
				P.Set(j, 3*n+k, d.Get(j, k))
			}
		}
		Q := matrix.Zeros(2*n, 2*n)
		for j := 0; j < n; j++ {
			for k := 0; k < n; k++ {
				Q.Set(j, k, a.Get(j, k))
				Q.Set(j, n+k, b.Get(j, k))
				Q.Set(n+j, k, c.Get(j, k))
				Q.Set(n+j, n+k, d.Get(j, k))
			}
		}
		P = matrix.Product(matrix.Transpose(P), P)
		P = matrix.Product(P, P)
		P = matrix.Product(P, P)
		Q = matrix.Product(matrix.Transpose(Q), Q)
		Q = matrix.Product(Q, Q)
		Q = matrix.Product(Q, Q)
		v.Update(P.Trace())
		w.Update(Q.Trace())
	}
	return v.PopulationStandardDeviation() / float64(v.Count()) / v.Mean(), w.PopulationStandardDeviation() / float64(w.Count()) / w.Mean()
}
Example #3
0
func shiftMatrix(A *matrix.DenseMatrix, x int, y int) *matrix.DenseMatrix {
	result := A

	// fmt.Println("Shift x: ", x)
	// fmt.Println("Shift y: ", y)

	// A*shiftRightOrUp will shift A right
	// shiftRightOrUp*A will shift A up
	shiftRightOrUp := matrix.MakeDenseMatrix([]float64{
		0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
	}, 15, 15)

	// A*shiftLeftOrDown will shift A left
	// shiftLeftOrDown*A will shift A down
	shiftLeftOrDown := matrix.MakeDenseMatrix([]float64{
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
	}, 15, 15)

	// fmt.Println("Start Matrix: ", result.String(), "\n\n")

	if x < 0 {
		for i := x; i < 0; i++ {
			// fmt.Print("<")
			result = matrix.Product(result, shiftLeftOrDown)
			// fmt.Println("Shifted Matrix ", i, ": ", result.String(), "\n\n")
		}
	} else if x > 0 {
		for i := 0; i < x; i++ {
			// fmt.Print(">")
			result = matrix.Product(result, shiftRightOrUp)
			// fmt.Println("Shifted Matrix ", i, ": ", result.String(), "\n\n")
		}
	}
	// fmt.Println()

	if y < 0 {
		for i := y; i < 0; i++ {
			result = matrix.Product(shiftLeftOrDown, result)
			// fmt.Println("Shifted Matrix ", i, ": ", result.String(), "\n\n")
		}
	} else if y > 0 {
		for i := 0; i < y; i++ {
			result = matrix.Product(shiftRightOrUp, result)
			// fmt.Println("Shifted Matrix ", i, ": ", result.String(), "\n\n")
		}
	}

	// fmt.Println("Shifted Matrix: ", result.String())

	return result
}