Esempio n. 1
0
File: ml_test.go Progetto: creack/ml
func TestMatrixAddFailure(t *testing.T) {
	defer func() {
		if err := recover(); err == nil {
			t.Fatal("no panic received when adding mismtch dim matrix")
		}
	}()
	m1 := ml.NewMatrix(2, 4)
	m2 := ml.NewMatrix(2, 2)

	m1.Add(m2)
}
Esempio n. 2
0
File: ml_test.go Progetto: creack/ml
func TestMatrixDimMatch(t *testing.T) {
	m1 := ml.NewMatrix(2, 4)
	m2 := ml.NewMatrix(2, 4)

	if !m1.DimMatch(m2) {
		x1, y1 := m1.Dim()
		x2, y2 := m2.Dim()
		t.Fatalf("Identical dimension didn't match: (%d,%d) vs (%d,%d).", x1, y1, x2, y2)
	}

	m2 = ml.NewMatrix(2, 8)
	if m1.DimMatch(m2) {
		t.Fatal("Different dim matrix matched.")
	}
}
Esempio n. 3
0
func TestInverse(t *testing.T) {
	m1 := ml.Matrix{
		{1, 3, 3},
		{1, 4, 3},
		{1, 3, 4},
	}
	m2 := ml.Matrix{
		{7, -3, -3},
		{-1, 1, 0},
		{-1, 0, 1},
	}
	// Check that m1^-1 == m2.
	if inv := m1.Inverse(); !inv.Equal(m2) {
		t.Fatalf("m1 ^ -1 != m2\nm1:\n%s\nm1^1 got:\n%s\nm1^1 expect:\n%s\n", m1, inv, m2)

	}
	// Check that m1 * m2 == I (m1 * m1^-1)
	if !m2.Mul(m1).Equal(ml.NewMatrix(m1.Dim()).Identity()) {
		t.Fatalf("constants m2 * m1 with m2 = m1^-1 is not the Identity\n%s\n*\n%s\n--->\n%s\n", m2, m1, m2.Mul(m1))
	}
	if !m1.Inverse().Mul(m1).Equal(ml.NewMatrix(m1.Dim()).Identity()) {
		t.Fatalf("m1 ^ -1 * m1 is not the Identity\n%s\n*\n%s\n--->\n%s\n", m2, m1, m2.Mul(m1))
	}
}