Skip to content

Commit 834a2d6

Browse files
committed
More value tuples
1 parent 53eae56 commit 834a2d6

45 files changed

Lines changed: 637 additions & 124 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.paket/Paket.Restore.targets

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,8 @@
359359
NuspecProperties="$(NuspecProperties)"
360360
PackageLicenseFile="$(PackageLicenseFile)"
361361
PackageLicenseExpression="$(PackageLicenseExpression)"
362-
PackageLicenseExpressionVersion="$(PackageLicenseExpressionVersion)" />
362+
PackageLicenseExpressionVersion="$(PackageLicenseExpressionVersion)"
363+
NoDefaultExcludes="$(NoDefaultExcludes)" />
363364

364365
<PackTask Condition="$(UseMSBuild15_9_Pack)"
365366
PackItem="$(PackProjectInputFile)"

src/Data.Text/MatrixMarketReader.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public static Matrix<T> ReadMatrix<T>(TextReader reader) where T : struct, IEqua
133133

134134
if (sparse)
135135
{
136-
var indexed = ReadTokenLines(reader).Select(tokens => new Tuple<int, int, T>(int.Parse(tokens[0]) - 1, int.Parse(tokens[1]) - 1, parse(2, tokens)));
136+
var indexed = ReadTokenLines(reader).Select(tokens => (int.Parse(tokens[0]) - 1, int.Parse(tokens[1]) - 1, parse(2, tokens)));
137137
return Matrix<T>.Build.SparseOfIndexed(rows, cols, symmetry == MatrixMarketSymmetry.General ? indexed : ExpandSparse(symmetry, indexed));
138138
}
139139

@@ -207,7 +207,7 @@ public static Vector<T> ReadVector<T>(TextReader reader)
207207

208208
if (sparse)
209209
{
210-
var indexedSeq = ReadTokenLines(reader).Select(tokens => new Tuple<int, T>(int.Parse(tokens[0]) - 1, parse(1, tokens)));
210+
var indexedSeq = ReadTokenLines(reader).Select(tokens => (int.Parse(tokens[0]) - 1, parse(1, tokens)));
211211
return Vector<T>.Build.SparseOfIndexed(length, indexedSeq);
212212
}
213213

@@ -326,15 +326,15 @@ static IEnumerable<string[]> ReadTokenLines(TextReader reader)
326326
}
327327
}
328328

329-
static IEnumerable<Tuple<int, int, T>> ExpandSparse<T>(MatrixMarketSymmetry symmetry, IEnumerable<Tuple<int, int, T>> indexedValues)
329+
static IEnumerable<(int, int, T)> ExpandSparse<T>(MatrixMarketSymmetry symmetry, IEnumerable<(int, int, T)> indexedValues)
330330
{
331331
var map = CreateSymmetryMap<T>(symmetry);
332332
foreach (var x in indexedValues)
333333
{
334334
yield return x;
335335
if (x.Item1 != x.Item2)
336336
{
337-
yield return new Tuple<int, int, T>(x.Item2, x.Item1, map(x.Item3));
337+
yield return (x.Item2, x.Item1, map(x.Item3));
338338
}
339339
}
340340
}

src/FSharp/LinearAlgebra.Matrix.fs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,25 @@ module Matrix =
5252
let inline toSeq (m: #Matrix<_>) = m.Enumerate(Zeros.Include)
5353

5454
/// Transform a matrix into an indexed sequence.
55-
let inline toSeqi (m: #Matrix<_>) = m.EnumerateIndexed(Zeros.Include)
55+
let inline toSeqi (m: #Matrix<_>) = m.EnumerateIndexed(Zeros.Include) |> Seq.map (fun t -> t.ToTuple())
5656

5757
/// Transform a matrix into a sequence where zero-values are skipped. Skipping zeros is efficient on sparse data.
5858
let inline toSeqSkipZeros (m: #Matrix<_>) = m.Enumerate(Zeros.AllowSkip)
5959

6060
/// Transform a matrix into an indexed sequence where zero-values are skipped. Skipping zeros is efficient on sparse data.
61-
let inline toSeqiSkipZeros (m: #Matrix<_>) = m.EnumerateIndexed(Zeros.AllowSkip)
61+
let inline toSeqiSkipZeros (m: #Matrix<_>) = m.EnumerateIndexed(Zeros.AllowSkip) |> Seq.map (fun t -> t.ToTuple())
6262

6363
/// Transform a matrix into a column sequence.
6464
let inline toColSeq (m: #Matrix<_>) = m.EnumerateColumns()
6565

6666
/// Transform a matrix into an indexed column sequence.
67-
let inline toColSeqi (m: #Matrix<_>) = m.EnumerateColumnsIndexed()
67+
let inline toColSeqi (m: #Matrix<_>) = m.EnumerateColumnsIndexed() |> Seq.map (fun t -> t.ToTuple())
6868

6969
/// Transform a matrix into a row sequence.
7070
let inline toRowSeq (m: #Matrix<_>) = m.EnumerateRows()
7171

7272
/// Transform a matrix into an indexed row sequence.
73-
let inline toRowSeqi (m: #Matrix<_>) = m.EnumerateRowsIndexed()
73+
let inline toRowSeqi (m: #Matrix<_>) = m.EnumerateRowsIndexed() |> Seq.map (fun t -> t.ToTuple())
7474

7575

7676
/// Applies a function to all elements of the matrix.

src/FSharp/LinearAlgebra.Vector.fs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ module Vector =
4949
let inline toSeq (v: #Vector<_>) = v.Enumerate(Zeros.Include)
5050

5151
/// Transform a vector into an indexed sequence.
52-
let inline toSeqi (v: #Vector<_>) = v.EnumerateIndexed(Zeros.Include)
52+
let inline toSeqi (v: #Vector<_>) = v.EnumerateIndexed(Zeros.Include) |> Seq.map (fun t -> t.ToTuple())
5353

5454
/// Transform a vector into a sequence where zero-values are skipped. Skipping zeros is efficient on sparse data.
5555
let inline toSeqSkipZeros (v: #Vector<_>) = v.Enumerate(Zeros.AllowSkip)
5656

5757
/// Transform a vector into an indexed sequence where zero-values are skipped. Skipping zeros is efficient on sparse data.
58-
let inline toSeqiSkipZeros (v: #Vector<_>) = v.EnumerateIndexed(Zeros.AllowSkip)
58+
let inline toSeqiSkipZeros (v: #Vector<_>) = v.EnumerateIndexed(Zeros.AllowSkip) |> Seq.map (fun t -> t.ToTuple())
5959

6060

6161
/// Applies a function to all elements of the vector.

src/Numerics.Tests/GenerateTests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ public void ImpulseConsistentWithSequence()
222222
public void UnfoldConsistentWithSequence()
223223
{
224224
Assert.That(
225-
Generate.UnfoldSequence((s => new Tuple<int, int>(s + 1, s + 1)), 0).Take(250).ToArray(),
226-
Is.EqualTo(Generate.Unfold(250, (s => new Tuple<int, int>(s + 1, s + 1)), 0)).AsCollection);
225+
Generate.UnfoldSequence((s => (s + 1, s + 1)), 0).Take(250).ToArray(),
226+
Is.EqualTo(Generate.Unfold(250, (s => (s + 1, s + 1)), 0)).AsCollection);
227227
}
228228

229229
[Test]
@@ -239,11 +239,11 @@ public void FibonacciConsistentWithUnfold()
239239
{
240240
Assert.That(
241241
Generate.FibonacciSequence().Take(250).ToArray(),
242-
Is.EqualTo(new[] { BigInteger.Zero, BigInteger.One }.Concat(Generate.Unfold(248, (s =>
242+
Is.EqualTo(new[] { BigInteger.Zero, BigInteger.One }.Concat(Generate.Unfold(248, s =>
243243
{
244244
var z = s.Item1 + s.Item2;
245-
return new Tuple<BigInteger, Tuple<BigInteger, BigInteger>>(z, new Tuple<BigInteger, BigInteger>(s.Item2, z));
246-
}), new Tuple<BigInteger, BigInteger>(BigInteger.Zero, BigInteger.One)))).AsCollection);
245+
return (z, (s.Item2, z));
246+
}, (BigInteger.Zero, BigInteger.One)))).AsCollection);
247247
}
248248
}
249249
}

src/Numerics/Generate.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -774,9 +774,25 @@ public static T[] Unfold<T, TState>(int length, Func<TState, Tuple<T, TState>> f
774774
var data = new T[length];
775775
for (int i = 0; i < data.Length; i++)
776776
{
777-
Tuple<T, TState> next = f(state);
778-
data[i] = next.Item1;
779-
state = next.Item2;
777+
(data[i], state) = f(state);
778+
}
779+
return data;
780+
}
781+
782+
/// <summary>
783+
/// Generate samples generated by the given computation.
784+
/// </summary>
785+
public static T[] Unfold<T, TState>(int length, Func<TState, (T, TState)> f, TState state)
786+
{
787+
if (length < 0)
788+
{
789+
throw new ArgumentOutOfRangeException(nameof(length));
790+
}
791+
792+
var data = new T[length];
793+
for (int i = 0; i < data.Length; i++)
794+
{
795+
(data[i], state) = f(state);
780796
}
781797
return data;
782798
}
@@ -788,9 +804,22 @@ public static IEnumerable<T> UnfoldSequence<T, TState>(Func<TState, Tuple<T, TSt
788804
{
789805
while (true)
790806
{
791-
Tuple<T, TState> next = f(state);
792-
state = next.Item2;
793-
yield return next.Item1;
807+
var (item, nextState) = f(state);
808+
state = nextState;
809+
yield return item;
810+
}
811+
}
812+
813+
/// <summary>
814+
/// Generate an infinite sequence generated by the given computation.
815+
/// </summary>
816+
public static IEnumerable<T> UnfoldSequence<T, TState>(Func<TState, (T, TState)> f, TState state)
817+
{
818+
while (true)
819+
{
820+
var (item, nextState) = f(state);
821+
state = nextState;
822+
yield return item;
794823
}
795824
}
796825

src/Numerics/LinearAlgebra/Builder.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,17 @@ public Matrix<T> DenseOfIndexed(int rows, int columns, IEnumerable<Tuple<int, in
565565
return Dense(DenseColumnMajorMatrixStorage<T>.OfIndexedEnumerable(rows, columns, enumerable));
566566
}
567567

568+
/// <summary>
569+
/// Create a new dense matrix as a copy of the given indexed enumerable.
570+
/// Keys must be provided at most once, zero is assumed if a key is omitted.
571+
/// This new matrix will be independent from the enumerable.
572+
/// A new memory block will be allocated for storing the matrix.
573+
/// </summary>
574+
public Matrix<T> DenseOfIndexed(int rows, int columns, IEnumerable<(int, int, T)> enumerable)
575+
{
576+
return Dense(DenseColumnMajorMatrixStorage<T>.OfIndexedEnumerable(rows, columns, enumerable));
577+
}
578+
568579
/// <summary>
569580
/// Create a new dense matrix as a copy of the given enumerable.
570581
/// The enumerable is assumed to be in column-major order (column by column).
@@ -911,6 +922,17 @@ public Matrix<T> SparseOfIndexed(int rows, int columns, IEnumerable<Tuple<int, i
911922
return Sparse(SparseCompressedRowMatrixStorage<T>.OfIndexedEnumerable(rows, columns, enumerable));
912923
}
913924

925+
/// <summary>
926+
/// Create a new sparse matrix as a copy of the given indexed enumerable.
927+
/// Keys must be provided at most once, zero is assumed if a key is omitted.
928+
/// This new matrix will be independent from the enumerable.
929+
/// A new memory block will be allocated for storing the matrix.
930+
/// </summary>
931+
public Matrix<T> SparseOfIndexed(int rows, int columns, IEnumerable<(int, int, T)> enumerable)
932+
{
933+
return Sparse(SparseCompressedRowMatrixStorage<T>.OfIndexedEnumerable(rows, columns, enumerable));
934+
}
935+
914936
/// <summary>
915937
/// Create a new sparse matrix as a copy of the given enumerable.
916938
/// The enumerable is assumed to be in row-major order (row by row).
@@ -1537,6 +1559,17 @@ public Vector<T> DenseOfIndexed(int length, IEnumerable<Tuple<int, T>> enumerabl
15371559
return Dense(DenseVectorStorage<T>.OfIndexedEnumerable(length, enumerable));
15381560
}
15391561

1562+
/// <summary>
1563+
/// Create a new dense vector as a copy of the given indexed enumerable.
1564+
/// Keys must be provided at most once, zero is assumed if a key is omitted.
1565+
/// This new vector will be independent from the enumerable.
1566+
/// A new memory block will be allocated for storing the vector.
1567+
/// </summary>
1568+
public Vector<T> DenseOfIndexed(int length, IEnumerable<(int, T)> enumerable)
1569+
{
1570+
return Dense(DenseVectorStorage<T>.OfIndexedEnumerable(length, enumerable));
1571+
}
1572+
15401573
/// <summary>
15411574
/// Create a new sparse vector straight from an initialized vector storage instance.
15421575
/// The storage is used directly without copying.
@@ -1611,6 +1644,17 @@ public Vector<T> SparseOfIndexed(int length, IEnumerable<Tuple<int, T>> enumerab
16111644
{
16121645
return Sparse(SparseVectorStorage<T>.OfIndexedEnumerable(length, enumerable));
16131646
}
1647+
1648+
/// <summary>
1649+
/// Create a new sparse vector as a copy of the given indexed enumerable.
1650+
/// Keys must be provided at most once, zero is assumed if a key is omitted.
1651+
/// This new vector will be independent from the enumerable.
1652+
/// A new memory block will be allocated for storing the vector.
1653+
/// </summary>
1654+
public Vector<T> SparseOfIndexed(int length, IEnumerable<(int, T)> enumerable)
1655+
{
1656+
return Sparse(SparseVectorStorage<T>.OfIndexedEnumerable(length, enumerable));
1657+
}
16141658
}
16151659

16161660
internal static class BuilderInstance<T> where T : struct, IEquatable<T>, IFormattable

src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ public static DenseMatrix OfIndexed(int rows, int columns, IEnumerable<Tuple<int
145145
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfIndexedEnumerable(rows, columns, enumerable));
146146
}
147147

148+
/// <summary>
149+
/// Create a new dense matrix as a copy of the given indexed enumerable.
150+
/// Keys must be provided at most once, zero is assumed if a key is omitted.
151+
/// This new matrix will be independent from the enumerable.
152+
/// A new memory block will be allocated for storing the matrix.
153+
/// </summary>
154+
public static DenseMatrix OfIndexed(int rows, int columns, IEnumerable<(int, int, Complex)> enumerable)
155+
{
156+
return new DenseMatrix(DenseColumnMajorMatrixStorage<Complex>.OfIndexedEnumerable(rows, columns, enumerable));
157+
}
158+
148159
/// <summary>
149160
/// Create a new dense matrix as a copy of the given enumerable.
150161
/// The enumerable is assumed to be in column-major order (column by column).

src/Numerics/LinearAlgebra/Complex/DenseVector.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ public static DenseVector OfIndexedEnumerable(int length, IEnumerable<Tuple<int,
131131
return new DenseVector(DenseVectorStorage<Complex>.OfIndexedEnumerable(length, enumerable));
132132
}
133133

134+
/// <summary>
135+
/// Create a new dense vector as a copy of the given indexed enumerable.
136+
/// Keys must be provided at most once, zero is assumed if a key is omitted.
137+
/// This new vector will be independent from the enumerable.
138+
/// A new memory block will be allocated for storing the vector.
139+
/// </summary>
140+
public static DenseVector OfIndexedEnumerable(int length, IEnumerable<(int, Complex)> enumerable)
141+
{
142+
return new DenseVector(DenseVectorStorage<Complex>.OfIndexedEnumerable(length, enumerable));
143+
}
144+
134145
/// <summary>
135146
/// Create a new dense vector and initialize each value using the provided value.
136147
/// </summary>

src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ public static DiagonalMatrix OfIndexedDiagonal(int rows, int columns, IEnumerabl
148148
return new DiagonalMatrix(DiagonalMatrixStorage<Complex>.OfIndexedEnumerable(rows, columns, diagonal));
149149
}
150150

151+
/// <summary>
152+
/// Create a new diagonal matrix and initialize each diagonal value from the provided indexed enumerable.
153+
/// Keys must be provided at most once, zero is assumed if a key is omitted.
154+
/// This new matrix will be independent from the enumerable.
155+
/// A new memory block will be allocated for storing the matrix.
156+
/// </summary>
157+
public static DiagonalMatrix OfIndexedDiagonal(int rows, int columns, IEnumerable<(int, Complex)> diagonal)
158+
{
159+
return new DiagonalMatrix(DiagonalMatrixStorage<Complex>.OfIndexedEnumerable(rows, columns, diagonal));
160+
}
161+
151162
/// <summary>
152163
/// Create a new diagonal matrix and initialize each diagonal value from the provided enumerable.
153164
/// This new matrix will be independent from the enumerable.

0 commit comments

Comments
 (0)