Free cookie consent management tool by TermsFeed Policy Generator

source: branches/NCA/HeuristicLab.Algorithms.NCA.Tests/MatrixTest.cs @ 8464

Last change on this file since 8464 was 8437, checked in by abeham, 12 years ago

#1913:

  • Improved speed of NCA
  • Reorganized things
File size: 9.6 KB
Line 
1using HeuristicLab.Common;
2using Microsoft.VisualStudio.TestTools.UnitTesting;
3
4namespace HeuristicLab.Algorithms.NCA.Tests {
5
6
7  /// <summary>
8  ///This is a test class for MatrixTest and is intended
9  ///to contain all MatrixTest Unit Tests
10  ///</summary>
11  [TestClass()]
12  public class MatrixTest {
13
14
15    private TestContext testContextInstance;
16
17    /// <summary>
18    ///Gets or sets the test context which provides
19    ///information about and functionality for the current test run.
20    ///</summary>
21    public TestContext TestContext {
22      get {
23        return testContextInstance;
24      }
25      set {
26        testContextInstance = value;
27      }
28    }
29
30    #region Additional test attributes
31    //
32    //You can use the following additional attributes as you write your tests:
33    //
34    //Use ClassInitialize to run code before running the first test in the class
35    //[ClassInitialize()]
36    //public static void MyClassInitialize(TestContext testContext)
37    //{
38    //}
39    //
40    //Use ClassCleanup to run code after all tests in a class have run
41    //[ClassCleanup()]
42    //public static void MyClassCleanup()
43    //{
44    //}
45    //
46    //Use TestInitialize to run code before running each test
47    //[TestInitialize()]
48    //public void MyTestInitialize()
49    //{
50    //}
51    //
52    //Use TestCleanup to run code after each test has run
53    //[TestCleanup()]
54    //public void MyTestCleanup()
55    //{
56    //}
57    //
58    #endregion
59
60    private bool IsEqual(Matrix_Accessor a, Matrix_Accessor b) {
61      var i1 = a.GetEnumerator();
62      var i2 = b.GetEnumerator();
63      bool f, g;
64      do {
65        f = i1.MoveNext();
66        g = i2.MoveNext();
67        if (!f && !g) return true;
68        if (!f && g || f && !g || i1.Current != i2.Current) return false;
69      } while (f && g);
70      return !f && !g;
71    }
72
73    /// <summary>
74    ///A test for Add
75    ///</summary>
76    [TestMethod()]
77    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
78    public void AddTest() {
79      var mat1 = new double[,] { { -3, 2 }, { 5, 6 } };
80      var mat2 = new double[,] { { 2, 1 }, { 1, 2 } };
81      var result = new double[,] { { -1, 3 }, { 6, 8 } };
82      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Add(new Matrix_Accessor(mat2)), new Matrix_Accessor(result)));
83      mat1 = new double[,] { { 2, 1 }, { -4, 7 }, { 10, 20 } };
84      mat2 = new double[,] { { 3, 0 }, { 2, 4 }, { 5, 8 } };
85      result = new double[,] { { 5, 1 }, { -2, 11 }, { 15, 28 } };
86      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Add(new Matrix_Accessor(mat2)), new Matrix_Accessor(result)));
87    }
88
89    /// <summary>
90    ///A test for AddTo
91    ///</summary>
92    [TestMethod()]
93    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
94    public void AddToTest() {
95      var mat1 = new double[,] { { 3, 2 }, { -5, 6 } };
96      var mat2 = new double[,] { { 2, 1 }, { 1, 2 } };
97      var result = new double[,] { { 5, 3 }, { -4, 8 } };
98      new Matrix_Accessor(mat1).AddTo(mat2);
99      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat2), new Matrix_Accessor(result)));
100      mat1 = new double[,] { { 2, 1 }, { 4, 7 }, { 10, -20 } };
101      mat2 = new double[,] { { 3, 0 }, { 2, 4 }, { 5, 8 } };
102      result = new double[,] { { 5, 1 }, { 6, 11 }, { 15, -12 } };
103      new Matrix_Accessor(mat1).AddTo(mat2);
104      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat2), new Matrix_Accessor(result)));
105    }
106
107    /// <summary>
108    ///A test for Apply
109    ///</summary>
110    [TestMethod()]
111    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
112    public void ApplyTest() {
113      var mat = new double[,] { { 2, 1 }, { 4, 7 }, { -10, 20 } };
114      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat).Apply(), new Matrix_Accessor(mat)));
115    }
116
117    /// <summary>
118    ///A test for Length
119    ///</summary>
120    [TestMethod()]
121    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
122    public void LengthTest() {
123      var vector = new double[] { 5, 4, 2, 2 };
124      Assert.AreEqual(new Matrix_Accessor(vector, vector.Length).VectorLength(), 7);
125      vector = new double[] { 5, -4, 2, 2 };
126      Assert.AreEqual(new Matrix_Accessor(vector, vector.Length).VectorLength(), 7);
127      vector = new double[] { 3, 2, 1, 5 };
128      Assert.IsTrue(new Matrix_Accessor(vector, vector.Length).VectorLength().IsAlmost(6.2449979983983982058468931209398));
129    }
130
131    /// <summary>
132    ///A test for Multiply
133    ///</summary>
134    [TestMethod()]
135    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
136    public void MultiplyTest() {
137      var mat1 = new double[,] { { 3, 2 }, { 5, 6 } };
138      var mat2 = new double[,] { { 2, 1 }, { 1, 2 } };
139      var result = new double[,] { { 8, 7 }, { 16, 17 } };
140      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Multiply(new Matrix_Accessor(mat2)), new Matrix_Accessor(result)));
141      mat1 = new double[,] { { 2, 4, 10 }, { 1, 7, 20 } };
142      mat2 = new double[,] { { 3, 0 }, { 2, 4 }, { 5, 8 } };
143      result = new double[,] { { 64, 96 }, { 117, 188 } };
144      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Multiply(new Matrix_Accessor(mat2)), new Matrix_Accessor(result)));
145      mat1 = new double[,] { { 2, 1 }, { 4, 7 }, { 10, 20 } };
146      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Transpose().Multiply(new Matrix_Accessor(mat2)), new Matrix_Accessor(result)));
147      mat1 = new double[,] { { -3, 2, -2 }, { 4, 0, -5 } };
148      mat2 = new double[,] { { 1, -1 }, { -2, 3 }, { 2, 5 } };
149      result = new double[,] { { -11, -1 }, { -6, -29 } };
150      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Multiply(new Matrix_Accessor(mat2)), new Matrix_Accessor(result)));
151      mat1 = new double[,] { { 4, 1, -2 } };
152      mat2 = new double[,] { { 1, -1 }, { -3, 2 }, { 2, 4 } };
153      result = new double[,] { { -3, -10 } };
154      var result2 = new double[] { -3, -10 };
155      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Multiply(new Matrix_Accessor(mat2)), new Matrix_Accessor(result)));
156      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Multiply(new Matrix_Accessor(mat2)), new Matrix_Accessor(result2)));
157    }
158
159    /// <summary>
160    ///A test for Multiply
161    ///</summary>
162    [TestMethod()]
163    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
164    public void MultiplyScalarTest() {
165      var mat1 = new double[,] { { 3, -2 }, { 5, 6 } };
166      var result = new double[,] { { 6, -4 }, { 10, 12 } };
167      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Multiply(2.0), new Matrix_Accessor(result)));
168    }
169
170    /// <summary>
171    ///A test for Negate
172    ///</summary>
173    [TestMethod()]
174    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
175    public void NegateTest() {
176      var mat1 = new double[,] { { 3, -2 }, { -5, 6 } };
177      var result = new double[,] { { -3, 2 }, { 5, -6 } };
178      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Negate(), new Matrix_Accessor(result)));
179      mat1 = new double[,] { { 3, -2 }, { -5, 6 } };
180      result = new double[,] { { -3, 2 }, { 5, -6 } };
181      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Negate().Negate(), new Matrix_Accessor(result).Negate()));
182    }
183
184    /// <summary>
185    ///A test for OuterProduct
186    ///</summary>
187    [TestMethod()]
188    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
189    public void OuterProductTest() {
190      var vec1 = new double[] { 2, 3, 5 };
191      var vec2 = new double[] { -1, 4, -2 };
192      var result = new double[,] { { -2, 8, -4 }, { -3, 12, -6 }, { -5, 20, -10 } };
193      Assert.IsTrue(IsEqual(new Matrix_Accessor(vec1).OuterProduct(new Matrix_Accessor(vec2)), new Matrix_Accessor(result)));
194    }
195
196    /// <summary>
197    ///A test for Subtract
198    ///</summary>
199    [TestMethod()]
200    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
201    public void SubtractTest() {
202      var mat1 = new double[,] { { -3, 2 }, { 5, 6 } };
203      var mat2 = new double[,] { { 2, 1 }, { 1, 2 } };
204      var result = new double[,] { { -5, 1 }, { 4, 4 } };
205      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Subtract(new Matrix_Accessor(mat2)), new Matrix_Accessor(result)));
206      mat1 = new double[,] { { 2, 1 }, { -4, 7 }, { 10, 20 } };
207      mat2 = new double[,] { { 3, 0 }, { 2, 4 }, { 5, 8 } };
208      result = new double[,] { { -1, 1 }, { -6, 3 }, { 5, 12 } };
209      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Subtract(new Matrix_Accessor(mat2)), new Matrix_Accessor(result)));
210      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Subtract(new Matrix_Accessor(mat2)), new Matrix_Accessor(mat1).Add(new Matrix_Accessor(mat2).Negate())));
211    }
212
213    /// <summary>
214    ///A test for Transpose
215    ///</summary>
216    [TestMethod()]
217    [DeploymentItem("HeuristicLab.Algorithms.NCA-3.3.dll")]
218    public void TransposeTest() {
219      var mat1 = new double[,] { { 2, 1 }, { -4, 7 }, { 10, 20 } };
220      var result = new double[,] { { 2, -4, 10 }, { 1, 7, 20 } };
221      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Transpose(), new Matrix_Accessor(result)));
222      mat1 = new double[,] { { 3, 4 }, { 5, 6 } };
223      result = new double[,] { { 3, 5 }, { 4, 6 } };
224      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Transpose(), new Matrix_Accessor(result)));
225      var enumMat = new double[] { 3, 4, 5, 6 };
226      Assert.IsTrue(IsEqual(new Matrix_Accessor(enumMat, 2, 2).Transpose(), new Matrix_Accessor(result)));
227      mat1 = new double[,] { { 3, 4 }, { 5, 6 }, { 7, 8 }, { 9, 10 }, { 11, 12 } };
228      result = new double[,] { { 3, 5, 7, 9, 11 }, { 4, 6, 8, 10, 12 } };
229      Assert.IsTrue(IsEqual(new Matrix_Accessor(mat1).Transpose(), new Matrix_Accessor(result)));
230    }
231  }
232}
Note: See TracBrowser for help on using the repository browser.