From 532f7439075d51a726071f45202b6c4a8a508089 Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 12 Nov 2016 09:44:32 +0100 Subject: [PATCH] LA: change matrix.As*() methods to return null instead of fallback to copy in case of a mismatch --- src/Numerics/LinearAlgebra/Matrix.cs | 25 +++++++--------- .../LinearAlgebra/Storage/MatrixStorage.cs | 10 +++---- .../MatrixStructureTheory.cs | 30 +++++++++++++++++++ 3 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/Numerics/LinearAlgebra/Matrix.cs b/src/Numerics/LinearAlgebra/Matrix.cs index 1461b471..6eb2e3d3 100644 --- a/src/Numerics/LinearAlgebra/Matrix.cs +++ b/src/Numerics/LinearAlgebra/Matrix.cs @@ -1372,9 +1372,8 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Returns this matrix as a multidimensional array. - /// If the internal data structure matches the requested shape it is returned directly, without copying. - /// If the internal structure is returned, changes to the array and the matrix will affect each other. + /// Returns the internal multidimensional array of this matrix if, and only if, this matrix is stored by such an array internally. + /// Otherwise returns null. Changes to the returned array and the matrix will affect each other. /// Use ToArray instead if you always need an independent array. /// /// A multidimensional containing the values of this matrix. @@ -1384,9 +1383,8 @@ namespace MathNet.Numerics.LinearAlgebra } /// - /// Returns the matrix's elements as an array with the data laid out column by column (column major). - /// If the internal data structure matches the requested shape it is returned directly, without copying. - /// If the internal structure is returned, changes to the array and the matrix will affect each other. + /// Returns the internal column by column (column major) array of this matrix if, and only if, this matrix is stored by such arrays internally. + /// Otherwise returns null. Changes to the returned arrays and the matrix will affect each other. /// Use ToColumnMajorArray instead if you always need an independent array. /// ///
@@ -1403,9 +1401,8 @@ namespace MathNet.Numerics.LinearAlgebra
         }
 
         /// 
-        /// Returns the matrix's elements as an array with the data laid row by row (row major).
-        /// If the internal data structure matches the requested shape it is returned directly, without copying.
-        /// If the internal structure is returned, changes to the array and the matrix will affect each other.
+        /// Returns the internal row by row (row major) array of this matrix if, and only if, this matrix is stored by such arrays internally.
+        /// Otherwise returns null. Changes to the returned arrays and the matrix will affect each other.
         /// Use ToRowMajorArray instead if you always need an independent array.
         /// 
         /// 
@@ -1422,9 +1419,8 @@ namespace MathNet.Numerics.LinearAlgebra
         }
 
         /// 
-        /// Returns this matrix as array of row arrays.
-        /// If the internal data structure matches the requested shape it is returned directly, without copying.
-        /// If the internal structure is returned, changes to the array and the matrix will affect each other.
+        /// Returns the internal row arrays of this matrix if, and only if, this matrix is stored by such arrays internally.
+        /// Otherwise returns null. Changes to the returned arrays and the matrix will affect each other.
         /// Use ToRowArrays instead if you always need an independent array.
         /// 
         public T[][] AsRowArrays()
@@ -1433,9 +1429,8 @@ namespace MathNet.Numerics.LinearAlgebra
         }
 
         /// 
-        /// Returns this matrix as array of column arrays.
-        /// If the internal data structure matches the requested shape it is returned directly, without copying.
-        /// If the internal structure is returned, changes to the array and the matrix will affect each other.
+        /// Returns the internal column arrays of this matrix if, and only if, this matrix is stored by such arrays internally.
+        /// Otherwise returns null. Changes to the returned arrays and the matrix will affect each other.
         /// Use ToColumnArrays instead if you always need an independent array.
         /// 
         public T[][] AsColumnArrays()
diff --git a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
index b8c65143..a7470dc3 100644
--- a/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
+++ b/src/Numerics/LinearAlgebra/Storage/MatrixStorage.cs
@@ -581,27 +581,27 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
 
         public virtual T[] AsRowMajorArray()
         {
-            return ToRowMajorArray();
+            return null;
         }
 
         public virtual T[] AsColumnMajorArray()
         {
-            return ToColumnMajorArray();
+            return null;
         }
 
         public virtual T[][] AsRowArrays()
         {
-            return ToRowArrays();
+            return null;
         }
 
         public virtual T[][] AsColumnArrays()
         {
-            return ToColumnArrays();
+            return null;
         }
 
         public virtual T[,] AsArray()
         {
-            return ToArray();
+            return null;
         }
 
         // ENUMERATION
diff --git a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs
index 8ad6a925..c8fdc112 100644
--- a/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs
+++ b/src/UnitTests/LinearAlgebraTests/MatrixStructureTheory.cs
@@ -307,6 +307,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
         {
             Matrix matrix = Get(testMatrix);
             var array = matrix.AsArray();
+            if (array == null)
+            {
+                return;
+            }
+
+            Assert.That(array, Is.SameAs(matrix.AsArray()));
             Assert.That(array, Is.Not.SameAs(matrix.ToArray()));
             Assert.That(array.GetLength(0), Is.EqualTo(matrix.RowCount));
             Assert.That(array.GetLength(1), Is.EqualTo(matrix.ColumnCount));
@@ -324,6 +330,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
         {
             Matrix matrix = Get(testMatrix);
             var columnArrays = matrix.AsColumnArrays();
+            if (columnArrays == null)
+            {
+                return;
+            }
+
+            Assert.That(columnArrays, Is.SameAs(matrix.AsColumnArrays()));
             Assert.That(columnArrays, Is.Not.SameAs(matrix.ToColumnArrays()));
             Assert.That(columnArrays.Length, Is.EqualTo(matrix.ColumnCount));
             Assert.That(columnArrays[0].Length, Is.EqualTo(matrix.RowCount));
@@ -341,6 +353,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
         {
             Matrix matrix = Get(testMatrix);
             var rowArrays = matrix.AsRowArrays();
+            if (rowArrays == null)
+            {
+                return;
+            }
+
+            Assert.That(rowArrays, Is.SameAs(matrix.AsRowArrays()));
             Assert.That(rowArrays, Is.Not.SameAs(matrix.ToRowArrays()));
             Assert.That(rowArrays.Length, Is.EqualTo(matrix.RowCount));
             Assert.That(rowArrays[0].Length, Is.EqualTo(matrix.ColumnCount));
@@ -358,6 +376,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
         {
             Matrix matrix = Get(testMatrix);
             var array = matrix.AsColumnMajorArray();
+            if (array == null)
+            {
+                return;
+            }
+
+            Assert.That(array, Is.SameAs(matrix.AsColumnMajorArray()));
             Assert.That(array, Is.Not.SameAs(matrix.ToColumnMajorArray()));
             Assert.That(array.Length, Is.EqualTo(matrix.RowCount * matrix.ColumnCount));
             for (int i = 0; i < array.Length; i++)
@@ -371,6 +395,12 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests
         {
             Matrix matrix = Get(testMatrix);
             var array = matrix.AsRowMajorArray();
+            if (array == null)
+            {
+                return;
+            }
+
+            Assert.That(array, Is.SameAs(matrix.AsRowMajorArray()));
             Assert.That(array, Is.Not.SameAs(matrix.ToRowMajorArray()));
             Assert.That(array.Length, Is.EqualTo(matrix.RowCount * matrix.ColumnCount));
             for (int i = 0; i < array.Length; i++)