Browse Source

LA: vectors of indexed enumerable

pull/112/head
Christoph Ruegg 13 years ago
parent
commit
68b620ebee
  1. 30
      src/FSharp/LinearAlgebra.Double.Vector.fs
  2. 2
      src/FSharpUnitTests/DenseMatrixTests.fs
  3. 10
      src/FSharpUnitTests/DenseVectorTests.fs
  4. 2
      src/FSharpUnitTests/SparseMatrixTests.fs
  5. 8
      src/FSharpUnitTests/SparseVectorTests.fs
  6. 11
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  7. 11
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  8. 11
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  9. 11
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  10. 11
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  11. 11
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  12. 11
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  13. 11
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  14. 15
      src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs
  15. 40
      src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs

30
src/FSharp/LinearAlgebra.Double.Vector.fs

@ -192,8 +192,14 @@ module DenseVector =
/// Create a vector from a float list.
let inline ofList (fl: float list) = DenseVector(Array.ofList fl)
/// Create a vector with a given dimension from an indexed list of index, value pairs.
let inline ofListi (dim: int) (fl: list<int * float>) = DenseVector.OfIndexedEnumerable(dim, Seq.ofList fl)
/// Create a vector from a sequences.
let inline ofSeq (fs: #seq<float>) = DenseVector(Array.ofSeq fs)
let inline ofSeq (fs: #seq<float>) = DenseVector.OfEnumerable(fs)
/// Create a vector with a given dimension from an indexed sequences of index, value pairs.
let inline ofSeqi (dim: int) (fs: #seq<int * float>) = DenseVector.OfIndexedEnumerable(dim, fs)
/// Create a vector with evenly spaced entries: e.g. rangef -1.0 0.5 1.0 = [-1.0 -0.5 0.0 0.5 1.0]
let inline rangef (start: float) (step: float) (stop: float) =
@ -214,14 +220,16 @@ module SparseVector =
/// Initialize a vector by calling a construction function for every element.
let inline init (n: int) (f: int -> float) = SparseVector.Create(n, fun i -> f i)
/// Create a sparse vector with a given dimension from a list of entry, value pairs.
let inline ofList (dim: int) (fl: list<int * float>) =
let v = new SparseVector(dim)
fl |> List.iter (fun (i, f) -> v.[i] <- f)
v
/// Create a sparse vector with a given dimension from a list of index, value pairs.
[<System.ObsoleteAttribute("Use ofListi instead. Will be changed to expect a non-indexed seq in a future version.")>]
let inline ofList (dim: int) (fl: list<int * float>) = SparseVector.OfIndexedEnumerable(dim, Seq.ofList fl)
/// Create a sparse vector with a given dimension from a sequence of entry, value pairs.
let inline ofSeq (dim: int) (fs: #seq<int * float>) =
let v = new SparseVector(dim)
fs |> Seq.iter (fun (i, f) -> v.[i] <- f)
v
/// Create a sparse vector with a given dimension from an indexed list of index, value pairs.
let inline ofListi (dim: int) (fl: list<int * float>) = SparseVector.OfIndexedEnumerable(dim, Seq.ofList fl)
/// Create a sparse vector with a given dimension from a sequence of index, value pairs.
[<System.ObsoleteAttribute("Use ofSeqi instead. Will be changed to expect a non-indexed seq in a future version.")>]
let inline ofSeq (dim: int) (fs: #seq<int * float>) = SparseVector.OfIndexedEnumerable(dim, fs)
/// Create a sparse vector with a given dimension from an indexed sequence of index, value pairs.
let inline ofSeqi (dim: int) (fs: #seq<int * float>) = SparseVector.OfIndexedEnumerable(dim, fs)

2
src/FSharpUnitTests/DenseMatrixTests.fs

@ -40,7 +40,7 @@ module DenseMatrixTests =
[<Test>]
let ``DenseMatrix.diag`` () =
DenseMatrix.diag (new DenseVector(100, 2.0)) |> should equal (2.0 * (DenseMatrix.Identity 100))
DenseMatrix.diag (DenseVector.Create(100, fun i -> 2.0)) |> should equal (2.0 * (DenseMatrix.Identity 100))
[<Test>]
let ``DenseMatrix.init_row`` () =

10
src/FSharpUnitTests/DenseVectorTests.fs

@ -9,7 +9,7 @@ open MathNet.Numerics.LinearAlgebra.Double
module DenseVectorTests =
/// A small uniform vector.
let smallv = new DenseVector(5, 0.3 )
let smallv = DenseVector.Create(5, fun i -> 0.3)
/// A large vector with increasingly large entries
let largev = new DenseVector( Array.init 100 (fun i -> float i / 100.0) )
@ -22,10 +22,18 @@ module DenseVectorTests =
let ``DenseVector.ofList`` () =
DenseVector.ofList [ for i in 0 .. 99 -> float i / 100.0 ] |> should equal largev
[<Test>]
let ``DenseVector.ofListi`` () =
DenseVector.ofListi 100 [ for i in 0 .. 99 -> i, float i / 100.0 ] |> should equal largev
[<Test>]
let ``DenseVector.ofSeq`` () =
DenseVector.ofSeq (seq { for i in 0 .. 99 -> float i / 100.0 }) |> should equal largev
[<Test>]
let ``DenseVector.ofSeqi`` () =
DenseVector.ofSeqi 100 (seq { for i in 99 .. -1 .. 0 -> i, float i / 100.0 }) |> should equal largev
[<Test>]
let ``DenseVector.rangef`` () =
DenseVector.rangef 0.0 0.01 0.99 |> should equal (new DenseVector( [| for i in 0 .. 99 -> 0.01 * float i |] ) )

2
src/FSharpUnitTests/SparseMatrixTests.fs

@ -25,4 +25,4 @@ module SparseMatrixTests =
[<Test>]
let ``SparseMatrix.diag`` () =
SparseMatrix.diag (new DenseVector(100, 2.0)) |> should equal (2.0 * (SparseMatrix.Identity 100))
SparseMatrix.diag (DenseVector.Create(100, fun i -> 2.0)) |> should equal (2.0 * (SparseMatrix.Identity 100))

8
src/FSharpUnitTests/SparseVectorTests.fs

@ -12,10 +12,10 @@ module SparseVectorTests =
let smallv = new DenseVector( [|0.0;0.3;0.0;0.0;0.0|] ) :> Vector<float>
[<Test>]
let ``SparseVector.ofList`` () =
(SparseVector.ofList 5 [ (1,0.3) ] :> Vector<float>) |> should equal smallv
let ``SparseVector.ofListi`` () =
(SparseVector.ofListi 5 [ (1,0.3) ] :> Vector<float>) |> should equal smallv
[<Test>]
let ``SparseVector.ofSeq`` () =
(SparseVector.ofSeq 5 (List.toSeq [ (1,0.3) ]) :> Vector<float>) |> should equal smallv
let ``SparseVector.ofSeqi`` () =
(SparseVector.ofSeqi 5 (List.toSeq [ (1,0.3) ]) :> Vector<float>) |> should equal smallv

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

@ -111,6 +111,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new DenseVector(DenseVectorStorage<Complex>.OfEnumerable(enumerable));
}
/// <summary>
/// Create a new dense vector 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 vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public static DenseVector OfIndexedEnumerable(int length, IEnumerable<Tuple<int, Complex>> enumerable)
{
return new DenseVector(DenseVectorStorage<Complex>.OfIndexedEnumerable(length, enumerable));
}
/// <summary>
/// Create a new dense vector and initialize each value using the provided init function.
/// </summary>

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

@ -101,6 +101,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new SparseVector(SparseVectorStorage<Complex>.OfEnumerable(enumerable));
}
/// <summary>
/// Create a new sparse vector 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 vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public static SparseVector OfIndexedEnumerable(int length, IEnumerable<Tuple<int, Complex>> enumerable)
{
return new SparseVector(SparseVectorStorage<Complex>.OfIndexedEnumerable(length, enumerable));
}
/// <summary>
/// Create a new sparse vector and initialize each value using the provided init function.
/// </summary>

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

@ -111,6 +111,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new DenseVector(DenseVectorStorage<Complex32>.OfEnumerable(enumerable));
}
/// <summary>
/// Create a new dense vector 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 vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public static DenseVector OfIndexedEnumerable(int length, IEnumerable<Tuple<int, Complex32>> enumerable)
{
return new DenseVector(DenseVectorStorage<Complex32>.OfIndexedEnumerable(length, enumerable));
}
/// <summary>
/// Create a new dense vector and initialize each value using the provided init function.
/// </summary>

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

@ -101,6 +101,17 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new SparseVector(SparseVectorStorage<Complex32>.OfEnumerable(enumerable));
}
/// <summary>
/// Create a new sparse vector 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 vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public static SparseVector OfIndexedEnumerable(int length, IEnumerable<Tuple<int, Complex32>> enumerable)
{
return new SparseVector(SparseVectorStorage<Complex32>.OfIndexedEnumerable(length, enumerable));
}
/// <summary>
/// Create a new sparse vector and initialize each value using the provided init function.
/// </summary>

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

@ -112,6 +112,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new DenseVector(DenseVectorStorage<double>.OfEnumerable(enumerable));
}
/// <summary>
/// Create a new dense vector 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 vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public static DenseVector OfIndexedEnumerable(int length, IEnumerable<Tuple<int,double>> enumerable)
{
return new DenseVector(DenseVectorStorage<double>.OfIndexedEnumerable(length, enumerable));
}
/// <summary>
/// Create a new dense vector and initialize each value using the provided init function.
/// </summary>

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

@ -101,6 +101,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return new SparseVector(SparseVectorStorage<double>.OfEnumerable(enumerable));
}
/// <summary>
/// Create a new sparse vector 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 vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public static SparseVector OfIndexedEnumerable(int length, IEnumerable<Tuple<int, double>> enumerable)
{
return new SparseVector(SparseVectorStorage<double>.OfIndexedEnumerable(length, enumerable));
}
/// <summary>
/// Create a new sparse vector and initialize each value using the provided init function.
/// </summary>

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

@ -111,6 +111,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new DenseVector(DenseVectorStorage<float>.OfEnumerable(enumerable));
}
/// <summary>
/// Create a new dense vector 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 vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public static DenseVector OfIndexedEnumerable(int length, IEnumerable<Tuple<int, float>> enumerable)
{
return new DenseVector(DenseVectorStorage<float>.OfIndexedEnumerable(length, enumerable));
}
/// <summary>
/// Create a new dense vector and initialize each value using the provided init function.
/// </summary>

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

@ -101,6 +101,17 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return new SparseVector(SparseVectorStorage<float>.OfEnumerable(enumerable));
}
/// <summary>
/// Create a new sparse vector 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 vector will be independent from the enumerable.
/// A new memory block will be allocated for storing the vector.
/// </summary>
public static SparseVector OfIndexedEnumerable(int length, IEnumerable<Tuple<int, float>> enumerable)
{
return new SparseVector(SparseVectorStorage<float>.OfIndexedEnumerable(length, enumerable));
}
/// <summary>
/// Create a new sparse vector and initialize each value using the provided init function.
/// </summary>

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

@ -133,6 +133,21 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return new DenseVectorStorage<T>(array.Length, array);
}
public static DenseVectorStorage<T> OfIndexedEnumerable(int length, IEnumerable<Tuple<int, T>> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
var array = new T[length];
foreach (var item in data)
{
array[item.Item1] = item.Item2;
}
return new DenseVectorStorage<T>(array.Length, array);
}
// ENUMERATION
public override IEnumerable<T> Enumerate()

40
src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs

@ -306,7 +306,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
if (!Zero.Equals(item))
{
values.Add(item);
indices.Add(length);
indices.Add(i);
}
}
return new SparseVectorStorage<T>(length)
@ -326,19 +326,19 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
var indices = new List<int>();
var values = new List<T>();
int length = 0;
int index = 0;
foreach (T item in data)
{
if (!Zero.Equals(item))
{
values.Add(item);
indices.Add(length);
indices.Add(index);
}
length++;
index++;
}
return new SparseVectorStorage<T>(length)
return new SparseVectorStorage<T>(index)
{
Indices = indices.ToArray(),
Values = values.ToArray(),
@ -346,6 +346,36 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
};
}
public static SparseVectorStorage<T> OfIndexedEnumerable(int length, IEnumerable<Tuple<int, T>> data)
{
if (data == null)
{
throw new ArgumentNullException("data");
}
var indices = new List<int>();
var values = new List<T>();
foreach (var item in data)
{
if (!Zero.Equals(item.Item2))
{
values.Add(item.Item2);
indices.Add(item.Item1);
}
}
var indicesArray = indices.ToArray();
var valuesArray = values.ToArray();
Sorting.Sort(indicesArray, valuesArray);
return new SparseVectorStorage<T>(length)
{
Indices = indicesArray,
Values = valuesArray,
ValueCount = values.Count
};
}
// ENUMERATION
public override IEnumerable<T> Enumerate()

Loading…
Cancel
Save