Browse Source

LA: matrices of indexed enumerable

v2
Christoph Ruegg 14 years ago
parent
commit
2876913afe
  1. 12
      src/FSharp/LinearAlgebra.Double.Matrix.fs
  2. 11
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  3. 22
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  4. 11
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  5. 11
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  6. 22
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  7. 11
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  8. 11
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  9. 22
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  10. 11
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  11. 11
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  12. 22
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  13. 11
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  14. 15
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  15. 34
      src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs
  16. 43
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

12
src/FSharp/LinearAlgebra.Double.Matrix.fs

@ -249,6 +249,12 @@ module DenseMatrix =
/// Initialize a matrix by calling a construction function for every element.
let inline init (n: int) (m: int) (f: int -> int -> float) = DenseMatrix.Create(n, m, fun n m -> f n m)
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
let inline ofListi (n: int) (m: int) (fl: list<int * int * float>) = DenseMatrix.OfIndexed(n, m, Seq.ofList fl)
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline ofSeqi (n: int) (m: int) (fs: #seq<int * int * float>) = DenseMatrix.OfIndexed(n, m, fs)
/// Create a matrix from a list of float lists. Every list in the master list specifies a row.
let inline ofList (fll: float list list) =
let n = List.length fll
@ -311,6 +317,12 @@ module SparseMatrix =
/// Initialize a matrix by calling a construction function for every element.
let inline init (n: int) (m: int) (f: int -> int -> float) = SparseMatrix.Create(n, m, fun n m -> f n m)
/// Create a matrix with a given dimension from an indexed list of row, column, value tuples.
let inline ofListi (n: int) (m: int) (fl: list<int * int * float>) = SparseMatrix.OfIndexed(n, m, Seq.ofList fl)
/// Create a matrix with a given dimension from an indexed sequences of row, column, value tuples.
let inline ofSeqi (n: int) (m: int) (fs: #seq<int * int * float>) = SparseMatrix.OfIndexed(n, m, fs)
/// Create a matrix from a list of float lists. Every list in the master list specifies a row.
let inline ofList (rows: int) (cols: int) (fll: list<int * int * float>) =
let A = new SparseMatrix(rows, cols)

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

@ -134,6 +134,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfArray(array));
}
/// <summary>
/// Create a new dense matrix as a copy of the given indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int, int, Complex>> enumerable)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfIndexedEnumerable(rows, columns, enumerable));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in column-major order (column by column).

22
src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

@ -35,6 +35,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
using Properties;
using Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Numerics;
@ -142,6 +143,27 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DiagonalMatrix(DiagonalMatrixStorage<Complex>.OfArray(array));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value from the provided indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DiagonalMatrix OfIndexedDiagonal(int rows, int columns, IEnumerable<Tuple<int, Complex>> diagonal)
{
return new DiagonalMatrix(DiagonalMatrixStorage<Complex>.OfIndexedEnumerable(rows, columns, diagonal));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value from the provided enumerable.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DiagonalMatrix OfDiagonal(int rows, int columns, IEnumerable<Complex> diagonal)
{
return new DiagonalMatrix(DiagonalMatrixStorage<Complex>.OfEnumerable(rows, columns, diagonal));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value using the provided init function.
/// </summary>

11
src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs

@ -112,6 +112,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfArray(array));
}
/// <summary>
/// Create a new sparse matrix as a copy of the given indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int, int, Complex>> enumerable)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex>.OfIndexedEnumerable(rows, columns, enumerable));
}
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in row-major order (row by row).

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

@ -134,6 +134,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfArray(array));
}
/// <summary>
/// Create a new dense matrix as a copy of the given indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int, int, Complex32>> enumerable)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex32>.OfIndexedEnumerable(rows, columns, enumerable));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in column-major order (column by column).

22
src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs

@ -36,6 +36,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
using Properties;
using Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@ -142,6 +143,27 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DiagonalMatrix(DiagonalMatrixStorage<Complex32>.OfArray(array));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value from the provided indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DiagonalMatrix OfIndexedDiagonal(int rows, int columns, IEnumerable<Tuple<int, Complex32>> diagonal)
{
return new DiagonalMatrix(DiagonalMatrixStorage<Complex32>.OfIndexedEnumerable(rows, columns, diagonal));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value from the provided enumerable.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DiagonalMatrix OfDiagonal(int rows, int columns, IEnumerable<Complex32> diagonal)
{
return new DiagonalMatrix(DiagonalMatrixStorage<Complex32>.OfEnumerable(rows, columns, diagonal));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value using the provided init function.
/// </summary>

11
src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs

@ -112,6 +112,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfArray(array));
}
/// <summary>
/// Create a new sparse matrix as a copy of the given indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int, int, Complex32>> enumerable)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<Complex32>.OfIndexedEnumerable(rows, columns, enumerable));
}
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in row-major order (row by row).

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

@ -136,6 +136,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfArray(array));
}
/// <summary>
/// Create a new dense matrix as a copy of the given indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int, int, double>> enumerable)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<double>.OfIndexedEnumerable(rows, columns, enumerable));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in column-major order (column by column).

22
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.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.Linq;
@ -141,6 +142,27 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new DiagonalMatrix(DiagonalMatrixStorage<double>.OfArray(array));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value from the provided indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DiagonalMatrix OfIndexedDiagonal(int rows, int columns, IEnumerable<Tuple<int, double>> diagonal)
{
return new DiagonalMatrix(DiagonalMatrixStorage<double>.OfIndexedEnumerable(rows, columns, diagonal));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value from the provided enumerable.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DiagonalMatrix OfDiagonal(int rows, int columns, IEnumerable<double> diagonal)
{
return new DiagonalMatrix(DiagonalMatrixStorage<double>.OfEnumerable(rows, columns, diagonal));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value using the provided init function.
/// </summary>

11
src/Numerics/LinearAlgebra/Double/SparseMatrix.cs

@ -111,6 +111,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfArray(array));
}
/// <summary>
/// Create a new sparse matrix as a copy of the given indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int, int, double>> enumerable)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<double>.OfIndexedEnumerable(rows, columns, enumerable));
}
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in row-major order (row by row).

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

@ -134,6 +134,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfArray(array));
}
/// <summary>
/// Create a new dense matrix as a copy of the given indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DenseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int, int, float>> enumerable)
{
return new DenseMatrix(DenseColumnMajorMatrixStorage<float>.OfIndexedEnumerable(rows, columns, enumerable));
}
/// <summary>
/// Create a new dense matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in column-major order (column by column).

22
src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

@ -35,6 +35,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
using Properties;
using Storage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
@ -141,6 +142,27 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new DiagonalMatrix(DiagonalMatrixStorage<float>.OfArray(array));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value from the provided indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DiagonalMatrix OfIndexedDiagonal(int rows, int columns, IEnumerable<Tuple<int, float>> diagonal)
{
return new DiagonalMatrix(DiagonalMatrixStorage<float>.OfIndexedEnumerable(rows, columns, diagonal));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value from the provided enumerable.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static DiagonalMatrix OfDiagonal(int rows, int columns, IEnumerable<float> diagonal)
{
return new DiagonalMatrix(DiagonalMatrixStorage<float>.OfEnumerable(rows, columns, diagonal));
}
/// <summary>
/// Create a new diagonal matrix and initialize each diagonal value using the provided init function.
/// </summary>

11
src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

@ -111,6 +111,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfArray(array));
}
/// <summary>
/// Create a new sparse matrix as a copy of the given indexed enumerable.
/// Keys must be provided at most once, zero is assumed if a key is omitted.
/// This new matrix will be independent from the enumerable.
/// A new memory block will be allocated for storing the matrix.
/// </summary>
public static SparseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int, int, float>> enumerable)
{
return new SparseMatrix(SparseCompressedRowMatrixStorage<float>.OfIndexedEnumerable(rows, columns, enumerable));
}
/// <summary>
/// Create a new sparse matrix as a copy of the given enumerable.
/// The enumerable is assumed to be in row-major order (row by row).

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

@ -136,6 +136,21 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return storage;
}
public static DenseColumnMajorMatrixStorage<T> OfIndexedEnumerable(int rows, int columns, IEnumerable<Tuple<int, int, T>> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
var array = new T[rows * columns];
foreach (var item in data)
{
array[(item.Item2 * rows) + item.Item1] = item.Item3;
}
return new DenseColumnMajorMatrixStorage<T>(rows, columns, array);
}
public static DenseColumnMajorMatrixStorage<T> OfColumnMajorEnumerable(int rows, int columns, IEnumerable<T> data)
{
if (data == null)

34
src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs

@ -29,6 +29,7 @@
// </copyright>
using System;
using System.Collections.Generic;
using System.Linq;
using MathNet.Numerics.Properties;
@ -209,6 +210,39 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return storage;
}
public static DiagonalMatrixStorage<T> OfEnumerable(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 DiagonalMatrixStorage<T>(rows, columns, copy);
}
return new DiagonalMatrixStorage<T>(rows, columns, data.ToArray());
}
public static DiagonalMatrixStorage<T> OfIndexedEnumerable(int rows, int columns, IEnumerable<Tuple<int, T>> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
var storage = new DiagonalMatrixStorage<T>(rows, columns);
foreach(var item in data)
{
storage.Data[item.Item1] = item.Item2;
}
return storage;
}
// MATRIX COPY
internal override void CopyToUnchecked(MatrixStorage<T> target, bool skipClearing = false)

43
src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs

@ -428,6 +428,49 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return storage;
}
public static SparseCompressedRowMatrixStorage<T> OfIndexedEnumerable(int rows, int columns, IEnumerable<Tuple<int, int, T>> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
var trows = new List<Tuple<int, T>>[rows];
foreach (var item in data)
{
if (!Zero.Equals(item.Item3))
{
var row = trows[item.Item1] ?? (trows[item.Item1] = new List<Tuple<int, T>>());
row.Add(new Tuple<int, T>(item.Item2, item.Item3));
}
}
var storage = new SparseCompressedRowMatrixStorage<T>(rows, columns);
var rowPointers = storage.RowPointers;
var columnIndices = new List<int>();
var values = new List<T>();
int index = 0;
for (int row = 0; row < rows; row++)
{
rowPointers[row] = index;
if (trows[row] != null)
{
foreach (var item in trows[row])
{
values.Add(item.Item2);
columnIndices.Add(item.Item1);
index++;
}
}
}
storage.ColumnIndices = columnIndices.ToArray();
storage.Values = values.ToArray();
storage.ValueCount = values.Count;
return storage;
}
public static SparseCompressedRowMatrixStorage<T> OfRowMajorEnumerable(int rows, int columns, IEnumerable<T> data)
{
if (data == null)

Loading…
Cancel
Save