Skip to content

Commit 16f98eb

Browse files
Merge pull request SpiceSharp#79 from SpiceSharp/development
Development
2 parents 595b8c5 + 3e9f6dd commit 16f98eb

File tree

214 files changed

+3093
-2512
lines changed

Some content is hidden

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

214 files changed

+3093
-2512
lines changed

SpiceSharp/Algebra/Matrix/Row.cs

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ public void Insert(SparseMatrixElement newElement)
5959
/// <returns>True if the element was found, false if it was created.</returns>
6060
public bool CreateGetElement(int row, int column, out SparseMatrixElement result)
6161
{
62-
result = null;
6362
SparseMatrixElement element = FirstInRow, lastElement = null;
6463
while (element != null)
6564
{

SpiceSharp/Algebra/Solve/ComplexSolver.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ public override bool Factor()
137137
/// <exception cref="SparseException">Solver is not factored yet</exception>
138138
public override void Solve(Vector<Complex> solution)
139139
{
140-
if (solution == null)
141-
throw new ArgumentNullException(nameof(solution));
140+
solution.ThrowIfNull(nameof(solution));
142141
if (!IsFactored)
143142
throw new SparseException("Solver is not factored yet");
144143

@@ -206,8 +205,7 @@ public override void Solve(Vector<Complex> solution)
206205
/// <exception cref="SparseException">Solver is not factored yet</exception>
207206
public override void SolveTransposed(Vector<Complex> solution)
208207
{
209-
if (solution == null)
210-
throw new ArgumentNullException(nameof(solution));
208+
solution.ThrowIfNull(nameof(solution));
211209
if (!IsFactored)
212210
throw new SparseException("Solver is not factored yet");
213211

SpiceSharp/Algebra/Solve/Markowitz/Markowitz.cs

+16-10
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,7 @@ public Markowitz()
7676
/// <exception cref="ArgumentNullException">pivot</exception>
7777
public override bool IsValidPivot(MatrixElement<T> pivot)
7878
{
79-
if (pivot == null)
80-
throw new ArgumentNullException(nameof(pivot));
79+
pivot.ThrowIfNull(nameof(pivot));
8180

8281
// Get the magnitude of the current pivot
8382
var magnitude = Magnitude(pivot.Value);
@@ -104,8 +103,7 @@ public override bool IsValidPivot(MatrixElement<T> pivot)
104103
/// <exception cref="ArgumentNullException">matrix</exception>
105104
public void Initialize(Matrix<T> matrix)
106105
{
107-
if (matrix == null)
108-
throw new ArgumentNullException(nameof(matrix));
106+
matrix.ThrowIfNull(nameof(matrix));
109107

110108
// Allocate arrays
111109
_markowitzRow = new int[matrix.Size + 1];
@@ -194,8 +192,8 @@ private void Products(SparseMatrix<T> matrix, int step)
194192
/// <exception cref="ArgumentNullException">matrix</exception>
195193
public override void Setup(SparseMatrix<T> matrix, SparseVector<T> rhs, int eliminationStep, Func<T, double> magnitude)
196194
{
197-
if (matrix == null)
198-
throw new ArgumentNullException(nameof(matrix));
195+
matrix.ThrowIfNull(nameof(matrix));
196+
rhs.ThrowIfNull(nameof(rhs));
199197

200198
Magnitude = magnitude;
201199

@@ -220,11 +218,13 @@ public override void Setup(SparseMatrix<T> matrix, SparseVector<T> rhs, int elim
220218
/// </remarks>
221219
public override void MovePivot(SparseMatrix<T> matrix, SparseVector<T> rhs, MatrixElement<T> pivot, int eliminationStep)
222220
{
221+
matrix.ThrowIfNull(nameof(matrix));
222+
rhs.ThrowIfNull(nameof(rhs));
223+
pivot.ThrowIfNull(nameof(pivot));
224+
223225
// If we haven't setup, just skip
224226
if (_markowitzProduct == null)
225227
return;
226-
if (pivot == null)
227-
throw new ArgumentNullException(nameof(pivot));
228228
int oldProduct;
229229

230230
var row = pivot.Row;
@@ -304,11 +304,12 @@ public override void MovePivot(SparseMatrix<T> matrix, SparseVector<T> rhs, Matr
304304
/// <exception cref="ArgumentNullException">pivot</exception>
305305
public override void Update(SparseMatrix<T> matrix, MatrixElement<T> pivot, int eliminationStep)
306306
{
307+
matrix.ThrowIfNull(nameof(matrix));
308+
pivot.ThrowIfNull(nameof(pivot));
309+
307310
// If we haven't setup, just skip
308311
if (_markowitzProduct == null)
309312
return;
310-
if (pivot == null)
311-
throw new ArgumentNullException(nameof(pivot));
312313

313314
// Go through all elements below the pivot. If they exist, then we can subtract 1 from the Markowitz row vector!
314315
for (var column = pivot.Below; column != null; column = column.Below)
@@ -347,6 +348,9 @@ public override void Update(SparseMatrix<T> matrix, MatrixElement<T> pivot, int
347348
/// <param name="fillin">The fill-in.</param>
348349
public override void CreateFillin(SparseMatrix<T> matrix, MatrixElement<T> fillin)
349350
{
351+
matrix.ThrowIfNull(nameof(matrix));
352+
fillin.ThrowIfNull(nameof(fillin));
353+
350354
// Update the markowitz row count
351355
int index = fillin.Row;
352356
_markowitzRow[index]++;
@@ -377,6 +381,8 @@ public override void CreateFillin(SparseMatrix<T> matrix, MatrixElement<T> filli
377381
/// </remarks>
378382
public override MatrixElement<T> FindPivot(SparseMatrix<T> matrix, int eliminationStep)
379383
{
384+
matrix.ThrowIfNull(nameof(matrix));
385+
380386
foreach (var strategy in Strategies)
381387
{
382388
var chosen = strategy.FindPivot(this, matrix, eliminationStep);

SpiceSharp/Algebra/Solve/Markowitz/MarkowitzDiagonal.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@ public class MarkowitzDiagonal<T> : MarkowitzSearchStrategy<T> where T : IFormat
3030
/// <exception cref="ArgumentException">Invalid elimination step</exception>
3131
public override MatrixElement<T> FindPivot(Markowitz<T> markowitz, SparseMatrix<T> matrix, int eliminationStep)
3232
{
33-
if (matrix == null)
34-
throw new ArgumentNullException(nameof(matrix));
35-
if (markowitz == null)
36-
throw new ArgumentNullException(nameof(markowitz));
33+
markowitz.ThrowIfNull(nameof(markowitz));
34+
matrix.ThrowIfNull(nameof(matrix));
3735
if (eliminationStep < 1)
3836
throw new ArgumentException("Invalid elimination step");
3937

SpiceSharp/Algebra/Solve/Markowitz/MarkowitzEntireMatrix.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ public class MarkowitzEntireMatrix<T> : MarkowitzSearchStrategy<T> where T : IFo
3131
/// <exception cref="SingularException"></exception>
3232
public override MatrixElement<T> FindPivot(Markowitz<T> markowitz, SparseMatrix<T> matrix, int eliminationStep)
3333
{
34-
if (markowitz == null)
35-
throw new ArgumentNullException(nameof(markowitz));
36-
if (matrix == null)
37-
throw new ArgumentNullException(nameof(matrix));
34+
markowitz.ThrowIfNull(nameof(markowitz));
35+
matrix.ThrowIfNull(nameof(matrix));
3836
if (eliminationStep < 1)
3937
throw new ArgumentException("Invalid elimination step");
4038

SpiceSharp/Algebra/Solve/Markowitz/MarkowitzQuickDiagonal.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,8 @@ public class MarkowitzQuickDiagonal<T> : MarkowitzSearchStrategy<T> where T : IF
2525
/// <exception cref="ArgumentException">Invalid elimination step</exception>
2626
public override MatrixElement<T> FindPivot(Markowitz<T> markowitz, SparseMatrix<T> matrix, int eliminationStep)
2727
{
28-
if (markowitz == null)
29-
throw new ArgumentNullException(nameof(markowitz));
30-
if (matrix == null)
31-
throw new ArgumentNullException(nameof(matrix));
28+
markowitz.ThrowIfNull(nameof(markowitz));
29+
matrix.ThrowIfNull(nameof(matrix));
3230
if (eliminationStep < 1)
3331
throw new ArgumentException("Invalid elimination step");
3432

SpiceSharp/Algebra/Solve/Markowitz/MarkowitzSingleton.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ public class MarkowitzSingleton<T> : MarkowitzSearchStrategy<T> where T : IForma
2626
/// <exception cref="ArgumentException">Invalid elimination step</exception>
2727
public override MatrixElement<T> FindPivot(Markowitz<T> markowitz, SparseMatrix<T> matrix, int eliminationStep)
2828
{
29-
if (markowitz == null)
30-
throw new ArgumentNullException(nameof(markowitz));
31-
if (matrix == null)
32-
throw new ArgumentNullException(nameof(matrix));
29+
markowitz.ThrowIfNull(nameof(markowitz));
30+
matrix.ThrowIfNull(nameof(matrix));
3331
if (eliminationStep < 1)
3432
throw new ArgumentException("Invalid elimination step");
3533

SpiceSharp/Algebra/Solve/RealSolver.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,7 @@ public override bool Factor()
137137
/// <exception cref="SparseException">Solver is not yet factored</exception>
138138
public override void Solve(Vector<double> solution)
139139
{
140-
if (solution == null)
141-
throw new ArgumentNullException(nameof(solution));
140+
solution.ThrowIfNull(nameof(solution));
142141
if (!IsFactored)
143142
throw new SparseException("Solver is not yet factored");
144143

@@ -206,8 +205,7 @@ public override void Solve(Vector<double> solution)
206205
/// <exception cref="SparseException">Solver is not yet factored</exception>
207206
public override void SolveTransposed(Vector<double> solution)
208207
{
209-
if (solution == null)
210-
throw new ArgumentNullException(nameof(solution));
208+
solution.ThrowIfNull(nameof(solution));
211209
if (!IsFactored)
212210
throw new SparseException("Solver is not yet factored");
213211

SpiceSharp/Algebra/Solve/Solver.cs

+3-4
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public abstract class Solver<T> : SparseLinearSystem<T> where T : IFormattable,
4242
protected Solver(PivotStrategy<T> strategy)
4343
{
4444
NeedsReordering = true;
45-
Strategy = strategy;
45+
Strategy = strategy.ThrowIfNull(nameof(strategy));
4646
}
4747

4848
/// <summary>
@@ -54,7 +54,7 @@ protected Solver(PivotStrategy<T> strategy, int size)
5454
: base(size)
5555
{
5656
NeedsReordering = true;
57-
Strategy = strategy;
57+
Strategy = strategy.ThrowIfNull(nameof(strategy));
5858
}
5959

6060
/// <summary>
@@ -87,8 +87,7 @@ protected Solver(PivotStrategy<T> strategy, int size)
8787
/// <param name="step">The current step of factoring.</param>
8888
public void MovePivot(MatrixElement<T> pivot, int step)
8989
{
90-
if (pivot == null)
91-
throw new ArgumentNullException(nameof(pivot));
90+
pivot.ThrowIfNull(nameof(pivot));
9291
Strategy.MovePivot(Matrix, Rhs, pivot, step);
9392

9493
// Move the pivot in the matrix

SpiceSharp/Algebra/Solve/SparseLinearSystem.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,7 @@ public VectorElement<T> FindRhsElement(int index)
173173
/// <exception cref="ArgumentNullException">externalIndexes</exception>
174174
public Tuple<int, int> ExternalToInternal(Tuple<int, int> externalIndexes)
175175
{
176-
if (externalIndexes == null)
177-
throw new ArgumentNullException(nameof(externalIndexes));
176+
externalIndexes.ThrowIfNull(nameof(externalIndexes));
178177

179178
var row = Row[externalIndexes.Item1];
180179
var column = Column[externalIndexes.Item2];
@@ -191,8 +190,7 @@ public Tuple<int, int> ExternalToInternal(Tuple<int, int> externalIndexes)
191190
/// <exception cref="ArgumentNullException">internalIndexes</exception>
192191
public Tuple<int, int> InternalToExternal(Tuple<int, int> internalIndexes)
193192
{
194-
if (internalIndexes == null)
195-
throw new ArgumentNullException(nameof(internalIndexes));
193+
internalIndexes.ThrowIfNull(nameof(internalIndexes));
196194

197195
var row = Row.Reverse(internalIndexes.Item1);
198196
var column = Column.Reverse(internalIndexes.Item2);

SpiceSharp/Algebra/Solve/Translation.cs

+4-8
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,8 @@ public void Swap(int index1, int index2)
114114
/// <exception cref="ArgumentException">Length of inputs does not match</exception>
115115
public void Scramble<T>(Vector<T> source, Vector<T> target) where T : IFormattable
116116
{
117-
if (source == null)
118-
throw new ArgumentNullException(nameof(source));
119-
if (target == null)
120-
throw new ArgumentNullException(nameof(target));
117+
source.ThrowIfNull(nameof(source));
118+
target.ThrowIfNull(nameof(target));
121119
if (source.Length != target.Length)
122120
throw new ArgumentException("Length of inputs does not match");
123121

@@ -137,10 +135,8 @@ public void Scramble<T>(Vector<T> source, Vector<T> target) where T : IFormattab
137135
/// <param name="target">The target vector.</param>
138136
public void Unscramble<T>(T[] source, Vector<T> target) where T : IFormattable
139137
{
140-
if (source == null)
141-
throw new ArgumentNullException(nameof(source));
142-
if (target == null)
143-
throw new ArgumentNullException(nameof(target));
138+
source.ThrowIfNull(nameof(source));
139+
target.ThrowIfNull(nameof(target));
144140
if (source.Length != target.Length + 1)
145141
throw new ArgumentException("Length of inputs does not match");
146142

SpiceSharp/Algebra/Vector/DenseVector.cs

+2-4
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,7 @@ protected DenseVector(T[] values)
8787
/// <exception cref="SparseException">Vector lengths do not match</exception>
8888
public void CopyTo(DenseVector<T> vector)
8989
{
90-
if (vector == null)
91-
throw new ArgumentNullException(nameof(vector));
90+
vector.ThrowIfNull(nameof(vector));
9291
if (vector.Length != Length)
9392
throw new SparseException("Vector lengths do not match");
9493
for (var i = 0; i < Length; i++)
@@ -103,8 +102,7 @@ public void CopyTo(DenseVector<T> vector)
103102
/// <exception cref="SparseException">Vector lengths do not match</exception>
104103
public void CopyFrom(DenseVector<T> vector)
105104
{
106-
if (vector == null)
107-
throw new ArgumentNullException(nameof(vector));
105+
vector.ThrowIfNull(nameof(vector));
108106
if (vector.Length != Length)
109107
throw new SparseException("Vector lengths do not match");
110108
for (var i = 0; i < Length; i++)

SpiceSharp/Algebra/Vector/Vector.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,7 @@ protected Vector(int length)
4747
/// <param name="target">The target vector.</param>
4848
public void CopyTo(Vector<T> target)
4949
{
50-
if (target == null)
51-
throw new ArgumentNullException(nameof(target));
50+
target.ThrowIfNull(nameof(target));
5251
if (target.Length != Length)
5352
throw new ArgumentException("Vector lengths do not match");
5453
for (var i = 1; i <= Length; i++)

0 commit comments

Comments
 (0)