Example #1
0
func TestAppIterMu(t *testing.T) {
	ExpectedMatrix := mat.NewDense(4, 3, []float64{
		6, 4, 1,
		2, 3, 4,
		9, 1, 6,
		6, 2, 2,
	})
	iter := &AppIter{mu: ExpectedMatrix}

	ResultMat := iter.Mu()
	if !mat.Equal(ExpectedMatrix, ResultMat) {
		t.Errorf("Expected \n%v, got \n%v",
			printMatrix(ExpectedMatrix), printMatrix(ResultMat))
	}
}
Example #2
0
func TestAppIterIdx(t *testing.T) {
	expectedVec := mat.NewVector(4, []float64{
		3,
		4,
		8,
		1,
	})
	iter := &AppIter{idx: expectedVec}

	resultVec := iter.Idx()
	if !mat.Equal(expectedVec, resultVec) {
		t.Errorf("Expected \n%v got,\n%v",
			printMatrix(expectedVec), printMatrix(resultVec))
	}
}
Example #3
0
func TestGetRowVector(t *testing.T) {
	A := mat.NewDense(3, 3, []float64{
		6, 4, 2,
		10, 3, 9,
		4, 7, 1,
	})
	rowIndex := 1
	expectedVector := mat.NewVector(3, []float64{
		10,
		3,
		9,
	})

	if !mat.Equal(expectedVector, getRowVector(rowIndex, A)) {
		t.Errorf("Expected true, got false")
	}
}
Example #4
0
func TestColumnMean(t *testing.T) {
	M := mat.NewDense(4, 3, []float64{
		6, 4, 1,
		2, 3, 4,
		9, 1, 6,
		6, 2, 2,
	})
	_, cols := M.Dims()
	ExpectedRes := mat.NewDense(1, cols, []float64{
		5.75, 2.5, 3.25,
	})

	Result := columnMean(M)
	if !mat.Equal(ExpectedRes, Result) {
		t.Errorf("Expected \n%v, got\n%v", printMatrix(ExpectedRes), printMatrix(Result))
	}
}
Example #5
0
func TestFindIn(t *testing.T) {
	v := mat.NewVector(4, []float64{
		1,
		0,
		1,
		0,
	})
	x := 1
	expectedVec := mat.NewVector(2, []float64{
		0,
		2,
	})

	result := findIn(float64(x), v)
	if !mat.Equal(result, expectedVec) {
		t.Errorf("Expected \n%v, found \n%v",
			printMatrix(expectedVec), printMatrix(result))
	}
}
Example #6
0
func TestMultiHypothesis(t *testing.T) {
	for _, test := range []struct {
		theta *mat64.Vector
		x     *mat64.Dense
		y     *mat64.Vector
	}{
		{
			mat64.NewVector(2, []float64{0, 2}),
			mat64.NewDense(2, 3, []float64{0, 0, 0, 1, 2, 10}),
			mat64.NewVector(3, []float64{2, 4, 20}),
		},
	} {
		h := MultiHypothesis(test.x, test.theta)

		if !mat64.Equal(h, test.y) {
			t.Errorf("MultiHypothesis(%v,%v) is expected to be equal to %v, found %v", test.x, test.theta, test.y, h)
		}
	}

}
Example #7
0
func TestRowSum(t *testing.T) {
	M := mat.NewDense(4, 3, []float64{
		6, 4, 1,
		2, 3, 4,
		9, 1, 6,
		6, 2, 2,
	})
	r, _ := M.Dims()
	ExpectedM := mat.NewDense(r, 1, []float64{
		11,
		9,
		16,
		10,
	})

	ResultM := rowSum(M)
	if !mat.Equal(ExpectedM, ResultM) {
		t.Errorf("Expected \n%v, got\n%v", printMatrix(ExpectedM), printMatrix(ResultM))
	}
}
Example #8
0
func TestGetColumnVector(t *testing.T) {
	M := mat.NewDense(4, 3, []float64{
		6, 4, 1,
		2, 3, 4,
		9, 1, 6,
		6, 2, 2,
	})
	columnIndex := 1
	vectorLen, _ := M.Dims()
	expVec := mat.NewVector(vectorLen, []float64{
		4,
		3,
		1,
		2,
	})

	resultVec := getColumnVector(columnIndex, M)
	if !mat.Equal(expVec, resultVec) {
		t.Errorf("Expected \n%v, got\n %v", printMatrix(expVec), printMatrix(resultVec))
	}
}
Example #9
0
func TestRowIndexIn(t *testing.T) {
	Matrix := mat.NewDense(4, 3, []float64{
		6, 4, 1,
		2, 3, 4,
		9, 1, 6,
		6, 2, 2,
	})
	vecIndex := mat.NewVector(2, []float64{
		1,
		3,
	})
	ExpectedMatrix := mat.NewDense(2, 3, []float64{
		2, 3, 4,
		6, 2, 2,
	})

	Result := rowIndexIn(vecIndex, Matrix)
	if !mat.Equal(ExpectedMatrix, Result) {
		t.Errorf("Expected \n%v, got \n%v",
			printMatrix(ExpectedMatrix),
			printMatrix(Result))
	}
}
Example #10
0
func TestConstructXCentroidMatrix(t *testing.T) {
	Mu := mat.NewDense(2, 3, []float64{
		4, 9, 2,
		3, 1, 4,
	})
	idx := mat.NewVector(4, []float64{
		0,
		1,
		0,
		1,
	})
	ExpectedMu := mat.NewDense(4, 3, []float64{
		4, 9, 2,
		3, 1, 4,
		4, 9, 2,
		3, 1, 4,
	})

	ResultMu := ConstructXCentroidMatrix(idx, Mu)
	if !mat.Equal(ResultMu, ExpectedMu) {
		t.Errorf("Expected \n%v, got\n%v", printMatrix(ExpectedMu), printMatrix(ResultMu))
	}
}
Example #11
0
func TestUndirect(t *testing.T) {
	for _, test := range directedGraphs {
		g := test.g()
		for _, e := range test.edges {
			g.SetEdge(e)
		}

		src := graph.Undirect{G: g, Absent: test.absent, Merge: test.merge}
		dst := simple.NewUndirectedMatrixFrom(src.Nodes(), 0, 0, 0)
		for _, u := range src.Nodes() {
			for _, v := range src.From(u) {
				dst.SetEdge(src.Edge(u, v))
			}
		}

		if !mat64.Equal(dst.Matrix(), test.want) {
			t.Errorf("unexpected result:\ngot:\n%.4v\nwant:\n%.4v",
				mat64.Formatted(dst.Matrix()),
				mat64.Formatted(test.want),
			)
		}
	}
}
Example #12
0
func TestAssignCentroid(t *testing.T) {
	X := mat.NewDense(4, 3, []float64{
		2, 6, 1,
		3, 4, 9,
		9, 8, 2,
		6, 4, 0,
	})
	Mu := mat.NewDense(2, 3, []float64{
		4, 9, 2,
		3, 1, 4,
	})
	m, _ := X.Dims()
	expectedVec := mat.NewVector(m, []float64{
		0,
		1,
		0,
		0,
	})

	Idx := AssignCentroid(X, Mu)
	if !mat.Equal(expectedVec, Idx) {
		t.Errorf("Expected \n%v, found \n%v", printMatrix(expectedVec), printMatrix(Idx))
	}
}
Example #13
0
File: nmf.go Project: postfix/nmf
func nnlsSubproblem(V, W, Ho *mat64.Dense, tol float64, outer, inner int) (H, G *mat64.Dense, i int, ok bool) {
	H = new(mat64.Dense)
	H.Clone(Ho)

	var WtV, WtW mat64.Dense
	WtV.Mul(W.T(), V)
	WtW.Mul(W.T(), W)

	alpha, beta := 1., 0.1

	decFilt := func(r, c int, v float64) float64 {
		// decFilt is applied to G, so v = G.At(r, c).
		if v < 0 || H.At(r, c) > 0 {
			return v
		}
		return 0
	}

	G = new(mat64.Dense)
	for i = 0; i < outer; i++ {
		G.Mul(&WtW, H)
		G.Sub(G, &WtV)
		G.Apply(decFilt, G)

		if mat64.Norm(G, 2) < tol {
			break
		}

		var (
			reduce bool
			Hp     *mat64.Dense
			d, dQ  mat64.Dense
		)
		for j := 0; j < inner; j++ {
			var Hn mat64.Dense
			Hn.Scale(alpha, G)
			Hn.Sub(H, &Hn)
			Hn.Apply(posFilt, &Hn)

			d.Sub(&Hn, H)
			dQ.Mul(&WtW, &d)
			dQ.MulElem(&dQ, &d)
			d.MulElem(G, &d)

			sufficient := 0.99*mat64.Sum(&d)+0.5*mat64.Sum(&dQ) < 0

			if j == 0 {
				reduce = !sufficient
				Hp = H
			}
			if reduce {
				if sufficient {
					H = &Hn
					ok = true
					break
				} else {
					alpha *= beta
				}
			} else {
				if !sufficient || mat64.Equal(Hp, &Hn) {
					H = Hp
					break
				} else {
					alpha /= beta
					Hp = &Hn
				}
			}
		}
	}

	return H, G, i, ok
}
Example #14
0
func TestCorrelationMatrix(t *testing.T) {
	for i, test := range []struct {
		data    *mat64.Dense
		weights []float64
		ans     *mat64.Dense
	}{
		{
			data: mat64.NewDense(3, 3, []float64{
				1, 2, 3,
				3, 4, 5,
				5, 6, 7,
			}),
			weights: nil,
			ans: mat64.NewDense(3, 3, []float64{
				1, 1, 1,
				1, 1, 1,
				1, 1, 1,
			}),
		},
		{
			data: mat64.NewDense(5, 2, []float64{
				-2, -4,
				-1, 2,
				0, 0,
				1, -2,
				2, 4,
			}),
			weights: nil,
			ans: mat64.NewDense(2, 2, []float64{
				1, 0.6,
				0.6, 1,
			}),
		}, {
			data: mat64.NewDense(3, 2, []float64{
				1, 1,
				2, 4,
				3, 9,
			}),
			weights: []float64{
				1,
				1.5,
				1,
			},
			ans: mat64.NewDense(2, 2, []float64{
				1, 0.9868703275903379,
				0.9868703275903379, 1,
			}),
		},
	} {
		// Make a copy of the data to check that it isn't changing.
		r := test.data.RawMatrix()
		d := make([]float64, len(r.Data))
		copy(d, r.Data)

		w := make([]float64, len(test.weights))
		if test.weights != nil {
			copy(w, test.weights)
		}
		c := CorrelationMatrix(nil, test.data, test.weights)
		if !mat64.Equal(c, test.ans) {
			t.Errorf("%d: expected corr %v, found %v", i, test.ans, c)
		}
		if !floats.Equal(d, r.Data) {
			t.Errorf("%d: data was modified during execution", i)
		}
		if !floats.Equal(w, test.weights) {
			t.Errorf("%d: weights was modified during execution", i)
		}

		// compare with call to Covariance
		_, cols := c.Dims()
		for ci := 0; ci < cols; ci++ {
			for cj := 0; cj < cols; cj++ {
				x := mat64.Col(nil, ci, test.data)
				y := mat64.Col(nil, cj, test.data)
				corr := Correlation(x, y, test.weights)
				if math.Abs(corr-c.At(ci, cj)) > 1e-14 {
					t.Errorf("CorrMat does not match at (%v, %v). Want %v, got %v.", ci, cj, corr, c.At(ci, cj))
				}
			}
		}

	}
	if !Panics(func() { CorrelationMatrix(nil, mat64.NewDense(5, 2, nil), []float64{}) }) {
		t.Errorf("CorrelationMatrix did not panic with weight size mismatch")
	}
	if !Panics(func() { CorrelationMatrix(mat64.NewDense(1, 1, nil), mat64.NewDense(5, 2, nil), nil) }) {
		t.Errorf("CorrelationMatrix did not panic with preallocation size mismatch")
	}
	if !Panics(func() { CorrelationMatrix(nil, mat64.NewDense(2, 2, []float64{1, 2, 3, 4}), []float64{1, -1}) }) {
		t.Errorf("CorrelationMatrix did not panic with negative weights")
	}
}
Example #15
0
func TestCovarianceMatrix(t *testing.T) {
	// An alternative way to test this is to call the Variance and
	// Covariance functions and ensure that the results are identical.
	for i, test := range []struct {
		data    *mat64.Dense
		weights []float64
		ans     *mat64.Dense
	}{
		{
			data: mat64.NewDense(5, 2, []float64{
				-2, -4,
				-1, 2,
				0, 0,
				1, -2,
				2, 4,
			}),
			weights: nil,
			ans: mat64.NewDense(2, 2, []float64{
				2.5, 3,
				3, 10,
			}),
		}, {
			data: mat64.NewDense(3, 2, []float64{
				1, 1,
				2, 4,
				3, 9,
			}),
			weights: []float64{
				1,
				1.5,
				1,
			},
			ans: mat64.NewDense(2, 2, []float64{
				.8, 3.2,
				3.2, 13.142857142857146,
			}),
		},
	} {
		// Make a copy of the data to check that it isn't changing.
		r := test.data.RawMatrix()
		d := make([]float64, len(r.Data))
		copy(d, r.Data)

		w := make([]float64, len(test.weights))
		if test.weights != nil {
			copy(w, test.weights)
		}
		c := CovarianceMatrix(nil, test.data, test.weights)
		if !mat64.Equal(c, test.ans) {
			t.Errorf("%d: expected cov %v, found %v", i, test.ans, c)
		}
		if !floats.Equal(d, r.Data) {
			t.Errorf("%d: data was modified during execution", i)
		}
		if !floats.Equal(w, test.weights) {
			t.Errorf("%d: weights was modified during execution", i)
		}

		// compare with call to Covariance
		_, cols := c.Dims()
		for ci := 0; ci < cols; ci++ {
			for cj := 0; cj < cols; cj++ {
				x := mat64.Col(nil, ci, test.data)
				y := mat64.Col(nil, cj, test.data)
				cov := Covariance(x, y, test.weights)
				if math.Abs(cov-c.At(ci, cj)) > 1e-14 {
					t.Errorf("CovMat does not match at (%v, %v). Want %v, got %v.", ci, cj, cov, c.At(ci, cj))
				}
			}
		}

	}
	if !Panics(func() { CovarianceMatrix(nil, mat64.NewDense(5, 2, nil), []float64{}) }) {
		t.Errorf("CovarianceMatrix did not panic with weight size mismatch")
	}
	if !Panics(func() { CovarianceMatrix(mat64.NewDense(1, 1, nil), mat64.NewDense(5, 2, nil), nil) }) {
		t.Errorf("CovarianceMatrix did not panic with preallocation size mismatch")
	}
	if !Panics(func() { CovarianceMatrix(nil, mat64.NewDense(2, 2, []float64{1, 2, 3, 4}), []float64{1, -1}) }) {
		t.Errorf("CovarianceMatrix did not panic with negative weights")
	}
}