Browse Source

LA: Initialize dense storage by enumerable #104

pull/112/head
Christoph Ruegg 13 years ago
parent
commit
5e07f34a8e
  1. 12
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 12
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  3. 12
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  4. 12
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  5. 12
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  6. 13
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  7. 12
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  8. 13
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  9. 24
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  10. 21
      src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs

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

@ -36,6 +36,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
using Properties;
using Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
@ -144,6 +145,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in column-major order.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<Complex> other)
: this(DenseColumnMajorMatrixStorage<Complex>.FromColumnMajorEnumerable(rows, columns, other))
{
}
/// <summary>
/// Create a new dense matrix as a copy of the given other matrix.

12
src/Numerics/LinearAlgebra/Complex/DenseVector.cs

@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <summary>
/// Create a new dense vector as a copy of the given other vector.
/// This new vector will be independent from the other vector.
/// A new memory block will be allocated for storing the matrix.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseVector(Vector<Complex> other)
: this(other.Count)
@ -107,6 +107,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
other.Storage.CopyToUnchecked(Storage, skipClearing: true);
}
/// <summary>
/// Create a new dense vector as a copy of the given enumerable.
/// This new vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseVector(IEnumerable<Complex> other)
: this(DenseVectorStorage<Complex>.FromEnumerable(other))
{
}
/// <summary>
/// Create a new dense vector directly binding to a raw array.
/// The array is used directly without copying.

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

@ -37,6 +37,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
using Properties;
using Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
/// <summary>
@ -144,6 +145,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in column-major order.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<Complex32> other)
: this(DenseColumnMajorMatrixStorage<Complex32>.FromColumnMajorEnumerable(rows, columns, other))
{
}
/// <summary>
/// Create a new dense matrix as a copy of the given other matrix.

12
src/Numerics/LinearAlgebra/Complex32/DenseVector.cs

@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <summary>
/// Create a new dense vector as a copy of the given other vector.
/// This new vector will be independent from the other vector.
/// A new memory block will be allocated for storing the matrix.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseVector(Vector<Complex32> other)
: this(other.Count)
@ -107,6 +107,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
other.Storage.CopyToUnchecked(Storage, skipClearing: true);
}
/// <summary>
/// Create a new dense vector as a copy of the given enumerable.
/// This new vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseVector(IEnumerable<Complex32> other)
: this(DenseVectorStorage<Complex32>.FromEnumerable(other))
{
}
/// <summary>
/// Create a new dense vector directly binding to a raw array.
/// The array is used directly without copying.

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

@ -36,6 +36,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
using Properties;
using Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Threading;
@ -144,6 +145,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in column-major order.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<double> other)
: this(DenseColumnMajorMatrixStorage<double>.FromColumnMajorEnumerable(rows, columns, other))
{
}
/// <summary>
/// Create a new dense matrix as a copy of the given other matrix.

13
src/Numerics/LinearAlgebra/Double/DenseVector.cs

@ -35,6 +35,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
using Properties;
using Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
@ -99,7 +100,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Create a new dense vector as a copy of the given other vector.
/// This new vector will be independent from the other vector.
/// A new memory block will be allocated for storing the matrix.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseVector(Vector<double> other)
: this(other.Count)
@ -107,6 +108,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
other.Storage.CopyToUnchecked(Storage, skipClearing: true);
}
/// <summary>
/// Create a new dense vector as a copy of the given enumerable.
/// This new vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseVector(IEnumerable<double> other)
: this(DenseVectorStorage<double>.FromEnumerable(other))
{
}
/// <summary>
/// Create a new dense vector directly binding to a raw array.
/// The array is used directly without copying.

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

@ -36,6 +36,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
using Properties;
using Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using Threading;
@ -145,6 +146,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in column-major order.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseMatrix(int rows, int columns, IEnumerable<float> other)
: this(DenseColumnMajorMatrixStorage<float>.FromColumnMajorEnumerable(rows, columns, other))
{
}
/// <summary>
/// Create a new dense matrix as a copy of the given other matrix.
/// This new matrix will be independent from the other matrix.

13
src/Numerics/LinearAlgebra/Single/DenseVector.cs

@ -34,6 +34,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
using Generic;
using Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
@ -98,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <summary>
/// Create a new dense vector as a copy of the given other vector.
/// This new vector will be independent from the other vector.
/// A new memory block will be allocated for storing the matrix.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseVector(Vector<float> other)
: this(other.Count)
@ -106,6 +107,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
other.Storage.CopyToUnchecked(Storage, skipClearing: true);
}
/// <summary>
/// Create a new dense vector as a copy of the given enumerable.
/// This new vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public DenseVector(IEnumerable<float> other)
: this(DenseVectorStorage<float>.FromEnumerable(other))
{
}
/// <summary>
/// Create a new dense vector directly binding to a raw array.
/// The array is used directly without copying.

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

@ -29,6 +29,7 @@
// </copyright>
using System;
using System.Collections.Generic;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Storage
@ -98,6 +99,29 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
}
}
// INITIALIZATION
public static DenseColumnMajorMatrixStorage<T> FromColumnMajorEnumerable(int rows, int columns, IEnumerable<T> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
var arrayData = data as T[];
if (arrayData != null)
{
var copy = new T[arrayData.Length];
Array.Copy(arrayData, copy, arrayData.Length);
return new DenseColumnMajorMatrixStorage<T>(rows, columns, copy);
}
var array = System.Linq.Enumerable.ToArray(data);
return new DenseColumnMajorMatrixStorage<T>(rows, columns, array);
}
// MATRIX COPY
internal override void CopyToUnchecked(MatrixStorage<T> target, bool skipClearing = false)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;

21
src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs

@ -90,6 +90,27 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
Array.Clear(Data, index, count);
}
// INITIALIZATION
public static DenseVectorStorage<T> FromEnumerable(IEnumerable<T> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
var arrayData = data as T[];
if (arrayData != null)
{
var copy = new T[arrayData.Length];
Array.Copy(arrayData, copy, arrayData.Length);
return new DenseVectorStorage<T>(copy.Length, copy);
}
var array = System.Linq.Enumerable.ToArray(data);
return new DenseVectorStorage<T>(array.Length, array);
}
// ENUMERATION
public override IEnumerable<T> Enumerate()

Loading…
Cancel
Save