Browse Source

Matrix: create directly from row/column vectors

v2
Christoph Ruegg 13 years ago
parent
commit
498e9c0a6d
  1. 30
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 30
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  3. 32
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  4. 30
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  5. 44
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs

30
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -167,6 +167,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// Create a new dense matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfColumnVectors(params Vector<Complex>[] columns)
{
var storage = new VectorStorage<Complex>[columns.Length];
for (int i = 0; i < columns.Length; i++)
{
storage[i] = columns[i].Storage;
}
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfColumnVectors(storage));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable columns.
/// Each enumerable in the master enumerable specifies a column.
@ -190,6 +205,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// Create a new dense matrix as a copy of the given row vectors.
/// This new matrix will be independent from the vectors.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfRowVectors(params Vector<Complex>[] rows)
{
var storage = new VectorStorage<Complex>[rows.Length];
for (int i = 0; i < rows.Length; i++)
{
storage[i] = rows[i].Storage;
}
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfRowVectors(storage));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.

30
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -167,6 +167,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// Create a new dense matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfColumnVectors(params Vector<Complex32>[] columns)
{
var storage = new VectorStorage<Complex32>[columns.Length];
for (int i = 0; i < columns.Length; i++)
{
storage[i] = columns[i].Storage;
}
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfColumnVectors(storage));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable columns.
/// Each enumerable in the master enumerable specifies a column.
@ -190,6 +205,21 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// Create a new dense matrix as a copy of the given row vectors.
/// This new matrix will be independent from the vectors.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfRowVectors(params Vector<Complex32>[] rows)
{
var storage = new VectorStorage<Complex32>[rows.Length];
for (int i = 0; i < rows.Length; i++)
{
storage[i] = rows[i].Storage;
}
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfRowVectors(storage));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.

32
src/Numerics/LinearAlgebra/Double/DenseMatrix.cs

@ -28,8 +28,6 @@
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
using System.Runtime;
namespace MathNet.Numerics.LinearAlgebra.Double
{
using Algorithms.LinearAlgebra;
@ -169,6 +167,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// Create a new dense matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfColumnVectors(params Vector<double>[] columns)
{
var storage = new VectorStorage<double>[columns.Length];
for (int i = 0; i < columns.Length; i++)
{
storage[i] = columns[i].Storage;
}
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfColumnVectors(storage));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable columns.
/// Each enumerable in the master enumerable specifies a column.
@ -192,6 +205,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// Create a new dense matrix as a copy of the given row vectors.
/// This new matrix will be independent from the vectors.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfRowVectors(params Vector<double>[] rows)
{
var storage = new VectorStorage<double>[rows.Length];
for (int i = 0; i < rows.Length; i++)
{
storage[i] = rows[i].Storage;
}
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfRowVectors(storage));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.

30
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -167,6 +167,21 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfColumnEnumerables(rows, columns, data));
}
/// <summary>
/// Create a new dense matrix as a copy of the given column vectors.
/// This new matrix will be independent from the vectors.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfColumnVectors(params Vector<float>[] columns)
{
var storage = new VectorStorage<float>[columns.Length];
for (int i = 0; i < columns.Length; i++)
{
storage[i] = columns[i].Storage;
}
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfColumnVectors(storage));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable columns.
/// Each enumerable in the master enumerable specifies a column.
@ -190,6 +205,21 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowEnumerables(rows, columns, data));
}
/// <summary>
/// Create a new dense matrix as a copy of the given row vectors.
/// This new matrix will be independent from the vectors.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfRowVectors(params Vector<float>[] rows)
{
var storage = new VectorStorage<float>[rows.Length];
for (int i = 0; i < rows.Length; i++)
{
storage[i] = rows[i].Storage;
}
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfRowVectors(storage));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable of enumerable rows.
/// Each enumerable in the master enumerable specifies a row.

44
src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs

@ -171,6 +171,50 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return new DenseColumnMajorMatrixStorage<T>(rows, columns, array);
}
public static DenseColumnMajorMatrixStorage<T> OfColumnVectors(VectorStorage<T>[] data)
{
if (data == null) throw new ArgumentNullException("data");
int columns = data.Length;
int rows = data[0].Length;
var array = new T[rows * columns];
for (int j = 0; j < data.Length; j++)
{
var column = data[j];
var denseColumn = column as DenseVectorStorage<T>;
if (denseColumn != null)
{
Array.Copy(denseColumn.Data, 0, array, j * rows, rows);
}
else
{
// FALL BACK
int offset = j*rows;
for (int i = 0; i < rows; i++)
{
array[offset + i] = column.At(i);
}
}
}
return new DenseColumnMajorMatrixStorage<T>(rows, columns, array);
}
public static DenseColumnMajorMatrixStorage<T> OfRowVectors(VectorStorage<T>[] data)
{
if (data == null) throw new ArgumentNullException("data");
int rows = data.Length;
int columns = data[0].Length;
var array = new T[rows * columns];
for (int j = 0; j < columns; j++)
{
int offset = j*rows;
for (int i = 0; i < rows; i++)
{
array[offset + i] = data[i].At(j);
}
}
return new DenseColumnMajorMatrixStorage<T>(rows, columns, array);
}
public static DenseColumnMajorMatrixStorage<T> OfColumnEnumerables<TColumn>(int rows, int columns, IEnumerable<TColumn> data)
// NOTE: flexible typing to 'backport' generic covariance.
where TColumn : IEnumerable<T>

Loading…
Cancel
Save