PageRenderTime 67ms CodeModel.GetById 36ms RepoModel.GetById 1ms app.codeStats 0ms

/src/UnitTests/LinearAlgebraTests/Complex32/DenseMatrixTests.cs

https://github.com/fxmozart/mathnet-numerics
C# | 191 lines | 105 code | 13 blank | 73 comment | 7 complexity | cb34f395c74a4b0b668a0f7ba871fb72 MD5 | raw file
  1. // <copyright file="DenseMatrixTests.cs" company="Math.NET">
  2. // Math.NET Numerics, part of the Math.NET Project
  3. // http://numerics.mathdotnet.com
  4. // http://github.com/mathnet/mathnet-numerics
  5. // http://mathnetnumerics.codeplex.com
  6. // Copyright (c) 2009-2010 Math.NET
  7. // Permission is hereby granted, free of charge, to any person
  8. // obtaining a copy of this software and associated documentation
  9. // files (the "Software"), to deal in the Software without
  10. // restriction, including without limitation the rights to use,
  11. // copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. // copies of the Software, and to permit persons to whom the
  13. // Software is furnished to do so, subject to the following
  14. // conditions:
  15. // The above copyright notice and this permission notice shall be
  16. // included in all copies or substantial portions of the Software.
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. // OTHER DEALINGS IN THE SOFTWARE.
  25. // </copyright>
  26. namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Complex32
  27. {
  28. using System;
  29. using System.Collections.Generic;
  30. using LinearAlgebra.Complex32;
  31. using NUnit.Framework;
  32. using Complex32 = Numerics.Complex32;
  33. /// <summary>
  34. /// Dense matrix tests.
  35. /// </summary>
  36. public class DenseMatrixTests : MatrixTests
  37. {
  38. /// <summary>
  39. /// Creates a matrix for the given number of rows and columns.
  40. /// </summary>
  41. /// <param name="rows">The number of rows.</param>
  42. /// <param name="columns">The number of columns.</param>
  43. /// <returns>A matrix with the given dimensions.</returns>
  44. protected override Matrix CreateMatrix(int rows, int columns)
  45. {
  46. return new DenseMatrix(rows, columns);
  47. }
  48. /// <summary>
  49. /// Creates a matrix from a 2D array.
  50. /// </summary>
  51. /// <param name="data">The 2D array to create this matrix from.</param>
  52. /// <returns>A matrix with the given values.</returns>
  53. protected override Matrix CreateMatrix(Complex32[,] data)
  54. {
  55. return new DenseMatrix(data);
  56. }
  57. /// <summary>
  58. /// Creates a vector of the given size.
  59. /// </summary>
  60. /// <param name="size">The size of the vector to create.
  61. /// </param>
  62. /// <returns>The new vector. </returns>
  63. protected override Vector CreateVector(int size)
  64. {
  65. return new DenseVector(size);
  66. }
  67. /// <summary>
  68. /// Creates a vector from an array.
  69. /// </summary>
  70. /// <param name="data">The array to create this vector from.</param>
  71. /// <returns>The new vector. </returns>
  72. protected override Vector CreateVector(Complex32[] data)
  73. {
  74. return new DenseVector(data);
  75. }
  76. /// <summary>
  77. /// Can create a matrix form array.
  78. /// </summary>
  79. [Test]
  80. public void CanCreateMatrixFrom1DArray()
  81. {
  82. var testData = new Dictionary<string, Matrix>
  83. {
  84. { "Singular3x3", new DenseMatrix(3, 3, new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1) }) },
  85. { "Square3x3", new DenseMatrix(3, 3, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.6f, 1) }) },
  86. { "Square4x4", new DenseMatrix(4, 4, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(1.0f, 1), new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(2.1f, 1), new Complex32(5.5f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1), new Complex32(6.2f, 1), new Complex32(6.6f, 1), new Complex32(-4.4f, 1), new Complex32(3.3f, 1), new Complex32(4.3f, 1), new Complex32(-7.7f, 1) }) },
  87. { "Tall3x2", new DenseMatrix(3, 2, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-4.4f, 1), new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(5.5f, 1) }) },
  88. { "Wide2x3", new DenseMatrix(2, 3, new[] { new Complex32(-1.1f, 1), Complex32.Zero, new Complex32(-2.2f, 1), new Complex32(1.1f, 1), new Complex32(-3.3f, 1), new Complex32(2.2f, 1) }) }
  89. };
  90. foreach (var name in testData.Keys)
  91. {
  92. Assert.AreEqual(TestMatrices[name], testData[name]);
  93. }
  94. }
  95. /// <summary>
  96. /// Matrix from array is a reference.
  97. /// </summary>
  98. [Test]
  99. public void MatrixFrom1DArrayIsReference()
  100. {
  101. var data = new[] { new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(1.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1), new Complex32(2.0f, 1) };
  102. var matrix = new DenseMatrix(3, 3, data);
  103. matrix[0, 0] = new Complex32(10.0f, 1);
  104. Assert.AreEqual(new Complex32(10.0f, 1), data[0]);
  105. }
  106. /// <summary>
  107. /// Matrix from two-dimensional array is a copy.
  108. /// </summary>
  109. [Test]
  110. public void MatrixFrom2DArrayIsCopy()
  111. {
  112. var matrix = new DenseMatrix(TestData2D["Singular3x3"]);
  113. matrix[0, 0] = 10.0f;
  114. Assert.AreEqual(new Complex32(1.0f, 1), TestData2D["Singular3x3"][0, 0]);
  115. }
  116. /// <summary>
  117. /// Can create a matrix from two-dimensional array.
  118. /// </summary>
  119. /// <param name="name">Matrix name.</param>
  120. [TestCase("Singular3x3")]
  121. [TestCase("Singular4x4")]
  122. [TestCase("Square3x3")]
  123. [TestCase("Square4x4")]
  124. [TestCase("Tall3x2")]
  125. [TestCase("Wide2x3")]
  126. public void CanCreateMatrixFrom2DArray(string name)
  127. {
  128. var matrix = new DenseMatrix(TestData2D[name]);
  129. for (var i = 0; i < TestData2D[name].GetLength(0); i++)
  130. {
  131. for (var j = 0; j < TestData2D[name].GetLength(1); j++)
  132. {
  133. Assert.AreEqual(TestData2D[name][i, j], matrix[i, j]);
  134. }
  135. }
  136. }
  137. /// <summary>
  138. /// Can create a matrix with uniform values.
  139. /// </summary>
  140. [Test]
  141. public void CanCreateMatrixWithUniformValues()
  142. {
  143. var matrix = new DenseMatrix(10, 10, new Complex32(10.0f, 1));
  144. for (var i = 0; i < matrix.RowCount; i++)
  145. {
  146. for (var j = 0; j < matrix.ColumnCount; j++)
  147. {
  148. Assert.AreEqual(matrix[i, j], new Complex32(10.0f, 1));
  149. }
  150. }
  151. }
  152. /// <summary>
  153. /// Can create an identity matrix.
  154. /// </summary>
  155. [Test]
  156. public void CanCreateIdentity()
  157. {
  158. var matrix = DenseMatrix.Identity(5);
  159. for (var i = 0; i < matrix.RowCount; i++)
  160. {
  161. for (var j = 0; j < matrix.ColumnCount; j++)
  162. {
  163. Assert.AreEqual(i == j ? Complex32.One : Complex32.Zero, matrix[i, j]);
  164. }
  165. }
  166. }
  167. /// <summary>
  168. /// Identity with wrong order throws <c>ArgumentOutOfRangeException</c>.
  169. /// </summary>
  170. /// <param name="order">The size of the square matrix</param>
  171. [TestCase(0)]
  172. [TestCase(-1)]
  173. public void IdentityWithWrongOrderThrowsArgumentOutOfRangeException(int order)
  174. {
  175. Assert.Throws<ArgumentOutOfRangeException>(() => DenseMatrix.Identity(order));
  176. }
  177. }
  178. }