Skip to content

Commit a71e646

Browse files
committed
Simplification: pattern matching
1 parent baab796 commit a71e646

10 files changed

Lines changed: 16 additions & 34 deletions

File tree

src/Data.Text/MatrixMarketWriter.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ public static void WriteMatrix<T>(TextWriter writer, Matrix<T> matrix) where T :
127127
var format = CreateValueFormatter<T>();
128128
var storage = matrix.Storage;
129129

130-
var sparse = storage as SparseCompressedRowMatrixStorage<T>;
131-
if (sparse != null)
130+
if (storage is SparseCompressedRowMatrixStorage<T> sparse)
132131
{
133132
writer.WriteLine("%%MatrixMarket matrix coordinate {0} general", complex ? "complex" : "real");
134133
writer.WriteLine("{0} {1} {2}", sparse.RowCount, sparse.ColumnCount, sparse.ValueCount);
@@ -144,8 +143,7 @@ public static void WriteMatrix<T>(TextWriter writer, Matrix<T> matrix) where T :
144143
return;
145144
}
146145

147-
var diagonal = storage as DiagonalMatrixStorage<T>;
148-
if (diagonal != null)
146+
if (storage is DiagonalMatrixStorage<T> diagonal)
149147
{
150148
writer.WriteLine("%%MatrixMarket matrix coordinate {0} general", complex ? "complex" : "real");
151149
writer.WriteLine("{0} {1} {2}", diagonal.RowCount, diagonal.ColumnCount, diagonal.Data.Length);
@@ -157,8 +155,7 @@ public static void WriteMatrix<T>(TextWriter writer, Matrix<T> matrix) where T :
157155
return;
158156
}
159157

160-
var dense = storage as DenseColumnMajorMatrixStorage<T>;
161-
if (dense != null)
158+
if (storage is DenseColumnMajorMatrixStorage<T> dense)
162159
{
163160
writer.WriteLine("%%MatrixMarket matrix array {0} general", complex ? "complex" : "real");
164161
writer.WriteLine("{0} {1}", dense.RowCount, dense.ColumnCount);
@@ -184,8 +181,7 @@ public static void WriteVector<T>(TextWriter writer, Vector<T> vector) where T :
184181
var format = CreateValueFormatter<T>();
185182
var storage = vector.Storage;
186183

187-
var sparse = storage as SparseVectorStorage<T>;
188-
if (sparse != null)
184+
if (storage is SparseVectorStorage<T> sparse)
189185
{
190186
writer.WriteLine("%%MatrixMarket vector coordinate {0}", complex ? "complex" : "real");
191187
writer.WriteLine("{0} {1}", sparse.Length, sparse.ValueCount);
@@ -197,8 +193,7 @@ public static void WriteVector<T>(TextWriter writer, Vector<T> vector) where T :
197193
return;
198194
}
199195

200-
var dense = storage as DenseVectorStorage<T>;
201-
if (dense != null)
196+
if (storage is DenseVectorStorage<T> dense)
202197
{
203198
writer.WriteLine("%%MatrixMarket vector array {0}", complex ? "complex" : "real");
204199
writer.WriteLine("{0}", dense.Length);

src/Numerics.Tests/Random/RandomTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ public void Sample()
7878
Assert.IsTrue(sum >= (N/2.0) - (.05*N));
7979
Assert.IsTrue(sum <= (N/2.0) + (.05*N));
8080

81-
var disposable = random as IDisposable;
82-
if (disposable != null)
81+
if (random is IDisposable disposable)
8382
{
8483
disposable.Dispose();
8584
}
@@ -101,8 +100,7 @@ public void Boundaries()
101100
Assert.IsTrue(next < j, string.Format("Value {0} is larger or equal to upper bound {1}", next, j));
102101
}
103102

104-
var disposable = random as IDisposable;
105-
if (disposable != null)
103+
if (random is IDisposable disposable)
106104
{
107105
disposable.Dispose();
108106
}

src/Numerics/LinearAlgebra/Complex/Solvers/MILU0Preconditioner.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ public MILU0Preconditioner(bool modified = true)
8282
/// instance of SparseCompressedRowMatrixStorage.</exception>
8383
public void Initialize(Matrix<Complex> matrix)
8484
{
85-
var csr = matrix.Storage as SparseCompressedRowMatrixStorage<Complex>;
86-
if (csr == null)
85+
if (!(matrix.Storage is SparseCompressedRowMatrixStorage<Complex> csr))
8786
{
8887
throw new ArgumentException("Matrix must be in sparse storage format", nameof(matrix));
8988
}

src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -768,10 +768,7 @@ protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result)
768768
/// <exception cref="ArgumentOutOfRangeException">If the two matrices don't have the same dimensions.</exception>
769769
protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result)
770770
{
771-
var sparseOther = other as SparseMatrix;
772-
var sparseResult = result as SparseMatrix;
773-
774-
if (sparseOther == null || sparseResult == null)
771+
if (!(other is SparseMatrix sparseOther) || !(result is SparseMatrix sparseResult))
775772
{
776773
base.DoSubtract(other, result);
777774
return;
@@ -886,9 +883,8 @@ protected override void DoMultiply(Complex scalar, Matrix<Complex> result)
886883
/// <param name="result">The result of the multiplication.</param>
887884
protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result)
888885
{
889-
var sparseOther = other as SparseMatrix;
890886
var sparseResult = result as SparseMatrix;
891-
if (sparseOther != null && sparseResult != null)
887+
if (other is SparseMatrix sparseOther && sparseResult != null)
892888
{
893889
DoMultiplySparse(sparseOther, sparseResult);
894890
return;

src/Numerics/LinearAlgebra/Complex32/Solvers/MILU0Preconditioner.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,7 @@ public MILU0Preconditioner(bool modified = true)
8282
/// instance of SparseCompressedRowMatrixStorage.</exception>
8383
public void Initialize(Matrix<Complex32> matrix)
8484
{
85-
var csr = matrix.Storage as SparseCompressedRowMatrixStorage<Complex32>;
86-
if (csr == null)
85+
if (!(matrix.Storage is SparseCompressedRowMatrixStorage<Complex32> csr))
8786
{
8887
throw new ArgumentException("Matrix must be in sparse storage format", nameof(matrix));
8988
}

src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,9 +884,8 @@ protected override void DoMultiply(Complex32 scalar, Matrix<Complex32> result)
884884
/// <param name="result">The result of the multiplication.</param>
885885
protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
886886
{
887-
var sparseOther = other as SparseMatrix;
888887
var sparseResult = result as SparseMatrix;
889-
if (sparseOther != null && sparseResult != null)
888+
if (other is SparseMatrix sparseOther && sparseResult != null)
890889
{
891890
DoMultiplySparse(sparseOther, sparseResult);
892891
return;

src/Numerics/LinearAlgebra/Double/Solvers/MILU0Preconditioner.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ public MILU0Preconditioner(bool modified = true)
8080
/// instance of SparseCompressedRowMatrixStorage.</exception>
8181
public void Initialize(Matrix<double> matrix)
8282
{
83-
var csr = matrix.Storage as SparseCompressedRowMatrixStorage<double>;
84-
if (csr == null)
83+
if (!(matrix.Storage is SparseCompressedRowMatrixStorage<double> csr))
8584
{
8685
throw new ArgumentException("Matrix must be in sparse storage format", nameof(matrix));
8786
}

src/Numerics/LinearAlgebra/Double/SparseMatrix.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -884,9 +884,8 @@ protected override void DoMultiply(double scalar, Matrix<double> result)
884884
/// <param name="result">The result of the multiplication.</param>
885885
protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
886886
{
887-
var sparseOther = other as SparseMatrix;
888887
var sparseResult = result as SparseMatrix;
889-
if (sparseOther != null && sparseResult != null)
888+
if (other is SparseMatrix sparseOther && sparseResult != null)
890889
{
891890
DoMultiplySparse(sparseOther, sparseResult);
892891
return;

src/Numerics/LinearAlgebra/Single/Solvers/MILU0Preconditioner.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ public MILU0Preconditioner(bool modified = true)
8080
/// instance of SparseCompressedRowMatrixStorage.</exception>
8181
public void Initialize(Matrix<float> matrix)
8282
{
83-
var csr = matrix.Storage as SparseCompressedRowMatrixStorage<float>;
84-
if (csr == null)
83+
if (!(matrix.Storage is SparseCompressedRowMatrixStorage<float> csr))
8584
{
8685
throw new ArgumentException("Matrix must be in sparse storage format", nameof(matrix));
8786
}

src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -889,9 +889,8 @@ protected override void DoMultiply(float scalar, Matrix<float> result)
889889
/// <param name="result">The result of the multiplication.</param>
890890
protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
891891
{
892-
var sparseOther = other as SparseMatrix;
893892
var sparseResult = result as SparseMatrix;
894-
if (sparseOther != null && sparseResult != null)
893+
if (other is SparseMatrix sparseOther && sparseResult != null)
895894
{
896895
DoMultiplySparse(sparseOther, sparseResult);
897896
return;

0 commit comments

Comments
 (0)