Browse Source

Code Style and Cleanups

v4
Christoph Ruegg 6 years ago
parent
commit
e51123cda0
  1. 12
      src/Numerics.Tests/StatisticsTests/MCMCTests/MetropolisHastingsSamplerTests.cs
  2. 11
      src/Numerics.Tests/StatisticsTests/MCMCTests/MetropolisSamplerTests.cs
  3. 11
      src/Numerics.Tests/StatisticsTests/MCMCTests/RejectionSamplerTests.cs
  4. 47
      src/Numerics/Compatibility.cs
  5. 11
      src/Numerics/Complex32.cs
  6. 2
      src/Numerics/Differentiation/FiniteDifferenceCoefficients.cs
  7. 15
      src/Numerics/Differentiation/NumericalDerivative.cs
  8. 5
      src/Numerics/Differentiation/NumericalHessian.cs
  9. 5
      src/Numerics/Differentiation/NumericalJacobian.cs
  10. 4
      src/Numerics/Distributions/SkewedGeneralizedError.cs
  11. 36
      src/Numerics/Integration/GaussKronrodRule.cs
  12. 40
      src/Numerics/Integration/GaussLegendreRule.cs
  13. 10
      src/Numerics/Interpolation/Barycentric.cs
  14. 10
      src/Numerics/Interpolation/BulirschStoerRationalInterpolation.cs
  15. 10
      src/Numerics/Interpolation/CubicSpline.cs
  16. 10
      src/Numerics/Interpolation/LinearSpline.cs
  17. 10
      src/Numerics/Interpolation/LogLinear.cs
  18. 10
      src/Numerics/Interpolation/NevillePolynomialInterpolation.cs
  19. 10
      src/Numerics/Interpolation/QuadraticSpline.cs
  20. 10
      src/Numerics/Interpolation/StepInterpolation.cs
  21. 10
      src/Numerics/Interpolation/TransformedInterpolation.cs
  22. 114
      src/Numerics/LinearAlgebra/Builder.cs
  23. 68
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  24. 5
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  25. 67
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  26. 8
      src/Numerics/LinearAlgebra/Complex/Factorization/Svd.cs
  27. 4
      src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs
  28. 6
      src/Numerics/LinearAlgebra/Complex/Solvers/ILUTPPreconditioner.cs
  29. 4
      src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs
  30. 13
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  31. 10
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  32. 64
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  33. 5
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  34. 67
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  35. 8
      src/Numerics/LinearAlgebra/Complex32/Factorization/Svd.cs
  36. 4
      src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs
  37. 6
      src/Numerics/LinearAlgebra/Complex32/Solvers/ILUTPPreconditioner.cs
  38. 4
      src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs
  39. 11
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  40. 10
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  41. 43
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  42. 5
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  43. 46
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  44. 8
      src/Numerics/LinearAlgebra/Double/Factorization/Svd.cs
  45. 10
      src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs
  46. 6
      src/Numerics/LinearAlgebra/Double/Solvers/ILUTPPreconditioner.cs
  47. 10
      src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs
  48. 13
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  49. 7
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  50. 15
      src/Numerics/LinearAlgebra/Factorization/LU.cs
  51. 7
      src/Numerics/LinearAlgebra/Factorization/QR.cs
  52. 11
      src/Numerics/LinearAlgebra/Factorization/Svd.cs
  53. 5
      src/Numerics/LinearAlgebra/Matrix.BCL.cs
  54. 56
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  55. 5
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  56. 46
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  57. 8
      src/Numerics/LinearAlgebra/Single/Factorization/Svd.cs
  58. 4
      src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs
  59. 6
      src/Numerics/LinearAlgebra/Single/Solvers/ILUTPPreconditioner.cs
  60. 10
      src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs
  61. 13
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  62. 7
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  63. 2
      src/Numerics/LinearAlgebra/Solvers/CancellationStopCriterion.cs
  64. 5
      src/Numerics/LinearAlgebra/Solvers/DelegateStopCriterion.cs
  65. 8
      src/Numerics/LinearAlgebra/Solvers/DivergenceStopCriterion.cs
  66. 2
      src/Numerics/LinearAlgebra/Solvers/FailureStopCriterion.cs
  67. 4
      src/Numerics/LinearAlgebra/Solvers/IterationCountStopCriterion.cs
  68. 5
      src/Numerics/LinearAlgebra/Solvers/Iterator.cs
  69. 6
      src/Numerics/LinearAlgebra/Solvers/ResidualStopCriterion.cs
  70. 64
      src/Numerics/LinearAlgebra/Storage/DenseColumnMajorMatrixStorage.cs
  71. 53
      src/Numerics/LinearAlgebra/Storage/DenseVectorStorage.cs
  72. 58
      src/Numerics/LinearAlgebra/Storage/DiagonalMatrixStorage.cs
  73. 60
      src/Numerics/LinearAlgebra/Storage/SparseCompressedRowMatrixStorage.cs
  74. 38
      src/Numerics/LinearAlgebra/Storage/SparseVectorStorage.cs
  75. 34
      src/Numerics/LinearAlgebra/Vector.BCL.cs
  76. 2
      src/Numerics/Optimization/BfgsBMinimizer.cs
  77. 2
      src/Numerics/Optimization/BfgsMinimizer.cs
  78. 2
      src/Numerics/Optimization/ConjugateGradientMinimizer.cs
  79. 2
      src/Numerics/Optimization/LimitedMemoryBfgsMinimizer.cs
  80. 5
      src/Numerics/Optimization/LineSearch/StrongWolfeLineSearch.cs
  81. 9
      src/Numerics/Optimization/LineSearch/WeakWolfeLineSearch.cs
  82. 11
      src/Numerics/Optimization/LineSearch/WolfeLineSearch.cs
  83. 8
      src/Numerics/Optimization/MinimizationResult.cs
  84. 2
      src/Numerics/Optimization/NelderMeadSimplex.cs
  85. 2
      src/Numerics/Optimization/NewtonMinimizer.cs
  86. 14
      src/Numerics/Optimization/NonlinearMinimizationResult.cs
  87. 6
      src/Numerics/Optimization/NonlinearMinimizerBase.cs
  88. 30
      src/Numerics/Optimization/ObjectiveFunctions/ForwardDifferenceGradientObjectiveFunction.cs
  89. 10
      src/Numerics/Optimization/ObjectiveFunctions/GradientHessianObjectiveFunction.cs
  90. 15
      src/Numerics/Optimization/ObjectiveFunctions/GradientObjectiveFunction.cs
  91. 15
      src/Numerics/Optimization/ObjectiveFunctions/HessianObjectiveFunction.cs
  92. 5
      src/Numerics/Optimization/ObjectiveFunctions/LazyObjectiveFunction.cs
  93. 5
      src/Numerics/Optimization/ObjectiveFunctions/LazyObjectiveFunctionBase.cs
  94. 24
      src/Numerics/Optimization/ObjectiveFunctions/NonlinearObjectiveFunction.cs
  95. 24
      src/Numerics/Optimization/ObjectiveFunctions/ScalarObjectiveFunction.cs
  96. 22
      src/Numerics/Optimization/ObjectiveFunctions/ScalarValueObjectiveFunction.cs
  97. 20
      src/Numerics/Optimization/ObjectiveFunctions/ValueObjectiveFunction.cs
  98. 8
      src/Numerics/Optimization/ScalarMinimizationResult.cs
  99. 13
      src/Numerics/Permutation.cs
  100. 2
      src/Numerics/Providers/Common/Cuda/SafeNativeMethods.cs

12
src/Numerics.Tests/StatisticsTests/MCMCTests/MetropolisHastingsSamplerTests.cs

@ -93,17 +93,5 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
ms.Sample(5);
}
/// <summary>
/// Set <c>null</c> RNG throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void NullRandomNumberGenerator()
{
var random = MersenneTwister.Default;
var normal = new Normal(0.0, 1.0, random);
var ms = new MetropolisHastingsSampler<double>(0.2, normal.Density, (x, y) => Normal.PDF(x, 0.1, y), x => Normal.Sample(random, x, 0.1), 10);
Assert.That(() => ms.RandomSource = null, Throws.TypeOf<ArgumentNullException>());
}
}
}

11
src/Numerics.Tests/StatisticsTests/MCMCTests/MetropolisSamplerTests.cs

@ -92,16 +92,5 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
ms.Sample(5);
}
/// <summary>
/// Set <c>null</c> RNG throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void NullRandomNumberGenerator()
{
var normal = new Normal(0.0, 1.0);
var ms = new MetropolisSampler<double>(0.2, normal.Density, x => Normal.Sample(new Random(0), x, 0.1), 10);
Assert.That(() => ms.RandomSource = null, Throws.TypeOf<ArgumentNullException>());
}
}
}

11
src/Numerics.Tests/StatisticsTests/MCMCTests/RejectionSamplerTests.cs

@ -97,16 +97,5 @@ namespace MathNet.Numerics.UnitTests.StatisticsTests.McmcTests
var rs = new RejectionSampler<double>(x => Math.Pow(x, 1.7)*Math.Pow(1.0 - x, 5.3), x => Double.NegativeInfinity, uniform.Sample);
Assert.That(() => rs.Sample(), Throws.ArgumentException);
}
/// <summary>
/// Set <c>null</c> RNG throws <c>ArgumentNullException</c>.
/// </summary>
[Test]
public void NullRandomNumberGenerator()
{
var uniform = new ContinuousUniform(0.0, 1.0, new SystemRandomSource(1));
var rs = new RejectionSampler<double>(x => Math.Pow(x, 1.7)*Math.Pow(1.0 - x, 5.3), x => Double.NegativeInfinity, uniform.Sample);
Assert.That(() => rs.RandomSource = null, Throws.TypeOf<ArgumentNullException>());
}
}
}

47
src/Numerics/Compatibility.cs

@ -1,4 +1,6 @@
#if NETSTANDARD1_3
using System.Globalization;
#if NETSTANDARD1_3
namespace MathNet.Numerics
{
using System;
@ -156,3 +158,46 @@ namespace MathNet.Numerics
}
}
#endif
#if NET40
namespace System.Runtime.CompilerServices
{
internal class FormattableStringFactory
{
public static FormattableString Create(string format, params object[] args)
{
return new FormattableString(format, args);
}
}
}
namespace System
{
internal class FormattableString
{
private readonly string format;
private readonly object[] args;
public FormattableString(string format, object[] args)
{
this.format = format;
this.args = args;
}
public static string Invariant(FormattableString messageFormat)
{
return messageFormat.ToString(CultureInfo.InvariantCulture);
}
public string ToString(IFormatProvider formatProvider)
{
return string.Format(formatProvider, format, args);
}
public override string ToString()
{
return string.Format(format, args);
}
}
}
#endif

11
src/Numerics/Complex32.cs

@ -145,7 +145,7 @@ namespace MathNet.Numerics
public float Real
{
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get { return _real; }
get => _real;
}
/// <summary>
@ -155,7 +155,7 @@ namespace MathNet.Numerics
public float Imaginary
{
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get { return _imag; }
get => _imag;
}
/// <summary>
@ -171,7 +171,7 @@ namespace MathNet.Numerics
{
// NOTE: the special case for negative real numbers fixes negative-zero value behavior. Do not remove.
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get { return _imag == 0f && _real < 0f ? (float)Constants.Pi : (float)Math.Atan2(_imag, _real); }
get => _imag == 0f && _real < 0f ? (float)Constants.Pi : (float)Math.Atan2(_imag, _real);
}
/// <summary>
@ -212,10 +212,7 @@ namespace MathNet.Numerics
/// Gets the squared magnitude (or squared absolute value) of a complex number.
/// </summary>
/// <returns>The squared magnitude of the current instance.</returns>
public float MagnitudeSquared
{
get { return (_real * _real) + (_imag * _imag); }
}
public float MagnitudeSquared => _real * _real + _imag * _imag;
/// <summary>
/// Gets the unity of this complex (same argument, but on the unit circle; exp(I*arg))

2
src/Numerics/Differentiation/FiniteDifferenceCoefficients.cs

@ -49,7 +49,7 @@ namespace MathNet.Numerics.Differentiation
/// </summary>
public int Points
{
get { return _points; }
get => _points;
set
{
CalculateCoefficients(value);

15
src/Numerics/Differentiation/NumericalDerivative.cs

@ -72,7 +72,6 @@ namespace MathNet.Numerics.Differentiation
double _stepSize = Math.Pow(2, -10);
double _epsilon = Precision.PositiveMachineEpsilon;
double _baseStepSize = Math.Pow(2, -26);
StepType _stepType = StepType.Relative;
readonly FiniteDifferenceCoefficients _coefficients;
/// <summary>
@ -110,7 +109,7 @@ namespace MathNet.Numerics.Differentiation
/// </remarks>
public double StepSize
{
get { return _stepSize; }
get => _stepSize;
set
{
//Base 2 yields more accurate results...
@ -125,7 +124,7 @@ namespace MathNet.Numerics.Differentiation
/// </summary>
public double BaseStepSize
{
get { return _baseStepSize; }
get => _baseStepSize;
set
{
//Base 2 yields more accurate results...
@ -140,7 +139,7 @@ namespace MathNet.Numerics.Differentiation
/// </summary>
public double Epsilon
{
get { return _epsilon; }
get => _epsilon;
set
{
//Base 2 yields more accurate results...
@ -154,7 +153,7 @@ namespace MathNet.Numerics.Differentiation
/// </summary>
public int Center
{
get { return _center; }
get => _center;
set
{
if (value >= _points || value < 0)
@ -173,11 +172,7 @@ namespace MathNet.Numerics.Differentiation
/// If set to relative, dx = (1+abs(x))*h^(2/(order+1)). This provides accurate results when
/// h is approximately equal to the square-root of machine accuracy, epsilon.
/// </summary>
public StepType StepType
{
get { return _stepType; }
set { _stepType = value; }
}
public StepType StepType { get; set; } = StepType.Relative;
/// <summary>
/// Evaluates the derivative of equidistant points using the finite difference method.

5
src/Numerics/Differentiation/NumericalHessian.cs

@ -40,10 +40,7 @@ namespace MathNet.Numerics.Differentiation
/// <summary>
/// Number of function evaluations.
/// </summary>
public int FunctionEvaluations
{
get { return _df.Evaluations; }
}
public int FunctionEvaluations => _df.Evaluations;
private readonly NumericalDerivative _df;

5
src/Numerics/Differentiation/NumericalJacobian.cs

@ -40,10 +40,7 @@ namespace MathNet.Numerics.Differentiation
/// <summary>
/// Number of function evaluations.
/// </summary>
public int FunctionEvaluations
{
get { return _df.Evaluations; }
}
public int FunctionEvaluations => _df.Evaluations;
private readonly NumericalDerivative _df;

4
src/Numerics/Distributions/SkewedGeneralizedError.cs

@ -102,8 +102,8 @@ namespace MathNet.Numerics.Distributions
/// </summary>
public System.Random RandomSource
{
get { return _random; }
set { _random = value ?? SystemRandomSource.Default; }
get => _random;
set => _random = value ?? SystemRandomSource.Default;
}
/// <summary>

36
src/Numerics/Integration/GaussKronrodRule.cs

@ -48,46 +48,22 @@ namespace MathNet.Numerics.Integration
/// <summary>
/// Getter for the order.
/// </summary>
public int Order
{
get
{
return gaussKronrodPoint.Order;
}
}
public int Order => gaussKronrodPoint.Order;
/// <summary>
/// Getter that returns a clone of the array containing the Kronrod abscissas.
/// </summary>
public double[] KronrodAbscissas
{
get
{
return gaussKronrodPoint.Abscissas.Clone() as double[];
}
}
public double[] KronrodAbscissas => gaussKronrodPoint.Abscissas.Clone() as double[];
/// <summary>
/// Getter that returns a clone of the array containing the Kronrod weights.
/// </summary>
public double[] KronrodWeights
{
get
{
return gaussKronrodPoint.Weights.Clone() as double[];
}
}
public double[] KronrodWeights => gaussKronrodPoint.Weights.Clone() as double[];
/// <summary>
/// Getter that returns a clone of the array containing the Gauss weights.
/// </summary>
public double[] GaussWeights
{
get
{
return gaussKronrodPoint.SecondWeights.Clone() as double[];
}
}
public double[] GaussWeights => gaussKronrodPoint.SecondWeights.Clone() as double[];
public GaussKronrodRule(int order)
{
@ -107,7 +83,7 @@ namespace MathNet.Numerics.Integration
/// <param name="order">The number of Gauss-Kronrod points. Pre-computed for 15, 21, 31, 41, 51 and 61 points</param>
public static double Integrate(Func<double, double> f, double intervalBegin, double intervalEnd, out double error, out double L1Norm, double targetRelativeError = 1E-10, int maximumDepth = 15, int order = 15)
{
// Formula used for variable subsitution from
// Formula used for variable subsitution from
// 1. Shampine, L. F. (2008). Vectorized adaptive quadrature in MATLAB. Journal of Computational and Applied Mathematics, 211(2), 131-140.
// 2. quadgk.m, GNU Octave
@ -193,7 +169,7 @@ namespace MathNet.Numerics.Integration
/// <returns></returns>
public static Complex ContourIntegrate(Func<double, Complex> f, double intervalBegin, double intervalEnd, out double error, out double L1Norm, double targetRelativeError = 1E-10, int maximumDepth = 15, int order = 15)
{
// Formula used for variable subsitution from
// Formula used for variable subsitution from
// 1. Shampine, L. F. (2008). Vectorized adaptive quadrature in MATLAB. Journal of Computational and Applied Mathematics, 211(2), 131-140.
// 2. quadgk.m, GNU Octave

40
src/Numerics/Integration/GaussLegendreRule.cs

@ -64,13 +64,7 @@ namespace MathNet.Numerics.Integration
/// <summary>
/// Getter that returns a clone of the array containing the abscissas.
/// </summary>
public double[] Abscissas
{
get
{
return _gaussLegendrePoint.Abscissas.Clone() as double[];
}
}
public double[] Abscissas => _gaussLegendrePoint.Abscissas.Clone() as double[];
/// <summary>
/// Getter for the ith weight.
@ -85,46 +79,22 @@ namespace MathNet.Numerics.Integration
/// <summary>
/// Getter that returns a clone of the array containing the weights.
/// </summary>
public double[] Weights
{
get
{
return _gaussLegendrePoint.Weights.Clone() as double[];
}
}
public double[] Weights => _gaussLegendrePoint.Weights.Clone() as double[];
/// <summary>
/// Getter for the order.
/// </summary>
public int Order
{
get
{
return _gaussLegendrePoint.Order;
}
}
public int Order => _gaussLegendrePoint.Order;
/// <summary>
/// Getter for the InvervalBegin.
/// </summary>
public double IntervalBegin
{
get
{
return _gaussLegendrePoint.IntervalBegin;
}
}
public double IntervalBegin => _gaussLegendrePoint.IntervalBegin;
/// <summary>
/// Getter for the InvervalEnd.
/// </summary>
public double IntervalEnd
{
get
{
return _gaussLegendrePoint.IntervalEnd;
}
}
public double IntervalEnd => _gaussLegendrePoint.IntervalEnd;
/// <summary>
/// Approximates a definite integral using an Nth order Gauss-Legendre rule.

10
src/Numerics/Interpolation/Barycentric.cs

@ -254,18 +254,12 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
bool IInterpolation.SupportsDifferentiation
{
get { return false; }
}
bool IInterpolation.SupportsDifferentiation => false;
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
bool IInterpolation.SupportsIntegration
{
get { return false; }
}
bool IInterpolation.SupportsIntegration => false;
/// <summary>
/// Interpolate at point t.

10
src/Numerics/Interpolation/BulirschStoerRationalInterpolation.cs

@ -100,18 +100,12 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
bool IInterpolation.SupportsDifferentiation
{
get { return false; }
}
bool IInterpolation.SupportsDifferentiation => false;
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
bool IInterpolation.SupportsIntegration
{
get { return false; }
}
bool IInterpolation.SupportsIntegration => false;
/// <summary>
/// Interpolate at point t.

10
src/Numerics/Interpolation/CubicSpline.cs

@ -434,18 +434,12 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
}
bool IInterpolation.SupportsDifferentiation => true;
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
bool IInterpolation.SupportsIntegration
{
get { return true; }
}
bool IInterpolation.SupportsIntegration => true;
/// <summary>
/// Interpolate at point t.

10
src/Numerics/Interpolation/LinearSpline.cs

@ -117,18 +117,12 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
}
bool IInterpolation.SupportsDifferentiation => true;
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
bool IInterpolation.SupportsIntegration
{
get { return true; }
}
bool IInterpolation.SupportsIntegration => true;
/// <summary>
/// Interpolate at point t.

10
src/Numerics/Interpolation/LogLinear.cs

@ -110,18 +110,12 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
}
bool IInterpolation.SupportsDifferentiation => true;
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
bool IInterpolation.SupportsIntegration
{
get { return false; }
}
bool IInterpolation.SupportsIntegration => false;
/// <summary>
/// Interpolate at point t.

10
src/Numerics/Interpolation/NevillePolynomialInterpolation.cs

@ -113,18 +113,12 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
}
bool IInterpolation.SupportsDifferentiation => true;
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
bool IInterpolation.SupportsIntegration
{
get { return false; }
}
bool IInterpolation.SupportsIntegration => false;
/// <summary>
/// Interpolate at point t.

10
src/Numerics/Interpolation/QuadraticSpline.cs

@ -70,18 +70,12 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
}
bool IInterpolation.SupportsDifferentiation => true;
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
bool IInterpolation.SupportsIntegration
{
get { return true; }
}
bool IInterpolation.SupportsIntegration => true;
/// <summary>
/// Interpolate at point t.

10
src/Numerics/Interpolation/StepInterpolation.cs

@ -97,15 +97,9 @@ namespace MathNet.Numerics.Interpolation
return InterpolateInplace(x.ToArray(), y.ToArray());
}
bool IInterpolation.SupportsDifferentiation
{
get { return true; }
}
bool IInterpolation.SupportsDifferentiation => true;
bool IInterpolation.SupportsIntegration
{
get { return true; }
}
bool IInterpolation.SupportsIntegration => true;
/// <summary>
/// Interpolate at point t.

10
src/Numerics/Interpolation/TransformedInterpolation.cs

@ -119,18 +119,12 @@ namespace MathNet.Numerics.Interpolation
/// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary>
bool IInterpolation.SupportsDifferentiation
{
get { return false; }
}
bool IInterpolation.SupportsDifferentiation => false;
/// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary>
bool IInterpolation.SupportsIntegration
{
get { return false; }
}
bool IInterpolation.SupportsIntegration => false;
/// <summary>
/// Interpolate at point t.

114
src/Numerics/LinearAlgebra/Builder.cs

@ -39,15 +39,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
internal class MatrixBuilder : MatrixBuilder<double>
{
public override double Zero
{
get { return 0d; }
}
public override double Zero => 0d;
public override double One
{
get { return 1d; }
}
public override double One => 1d;
public override Matrix<double> Dense(DenseColumnMajorMatrixStorage<double> storage)
{
@ -83,15 +77,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
internal class VectorBuilder : VectorBuilder<double>
{
public override double Zero
{
get { return 0d; }
}
public override double Zero => 0d;
public override double One
{
get { return 1d; }
}
public override double One => 1d;
public override Vector<double> Dense(DenseVectorStorage<double> storage)
{
@ -114,15 +102,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{
internal class MatrixBuilder : MatrixBuilder<float>
{
public override float Zero
{
get { return 0f; }
}
public override float Zero => 0f;
public override float One
{
get { return 1f; }
}
public override float One => 1f;
public override Matrix<float> Dense(DenseColumnMajorMatrixStorage<float> storage)
{
@ -158,15 +140,9 @@ namespace MathNet.Numerics.LinearAlgebra.Single
internal class VectorBuilder : VectorBuilder<float>
{
public override float Zero
{
get { return 0f; }
}
public override float Zero => 0f;
public override float One
{
get { return 1f; }
}
public override float One => 1f;
public override Vector<float> Dense(DenseVectorStorage<float> storage)
{
@ -191,15 +167,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
internal class MatrixBuilder : MatrixBuilder<Complex>
{
public override Complex Zero
{
get { return Complex.Zero; }
}
public override Complex Zero => Complex.Zero;
public override Complex One
{
get { return Complex.One; }
}
public override Complex One => Complex.One;
public override Matrix<Complex> Dense(DenseColumnMajorMatrixStorage<Complex> storage)
{
@ -235,15 +205,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
internal class VectorBuilder : VectorBuilder<Complex>
{
public override Complex Zero
{
get { return Complex.Zero; }
}
public override Complex Zero => Complex.Zero;
public override Complex One
{
get { return Complex.One; }
}
public override Complex One => Complex.One;
public override Vector<Complex> Dense(DenseVectorStorage<Complex> storage)
{
@ -266,15 +230,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{
internal class MatrixBuilder : MatrixBuilder<Numerics.Complex32>
{
public override Numerics.Complex32 Zero
{
get { return Numerics.Complex32.Zero; }
}
public override Numerics.Complex32 Zero => Numerics.Complex32.Zero;
public override Numerics.Complex32 One
{
get { return Numerics.Complex32.One; }
}
public override Numerics.Complex32 One => Numerics.Complex32.One;
public override Matrix<Numerics.Complex32> Dense(DenseColumnMajorMatrixStorage<Numerics.Complex32> storage)
{
@ -310,15 +268,9 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
internal class VectorBuilder : VectorBuilder<Numerics.Complex32>
{
public override Numerics.Complex32 Zero
{
get { return Numerics.Complex32.Zero; }
}
public override Numerics.Complex32 Zero => Numerics.Complex32.Zero;
public override Numerics.Complex32 One
{
get { return Numerics.Complex32.One; }
}
public override Numerics.Complex32 One => Numerics.Complex32.One;
public override Vector<Numerics.Complex32> Dense(DenseVectorStorage<Numerics.Complex32> storage)
{
@ -364,16 +316,11 @@ namespace MathNet.Numerics.LinearAlgebra
{
if (storage == null) throw new ArgumentNullException(nameof(storage));
var dense = storage as DenseColumnMajorMatrixStorage<T>;
if (dense != null) return Dense(dense);
var sparse = storage as SparseCompressedRowMatrixStorage<T>;
if (sparse != null) return Sparse(sparse);
if (storage is DenseColumnMajorMatrixStorage<T> dense) return Dense(dense);
if (storage is SparseCompressedRowMatrixStorage<T> sparse) return Sparse(sparse);
if (storage is DiagonalMatrixStorage<T> diagonal) return Diagonal(diagonal);
var diagonal = storage as DiagonalMatrixStorage<T>;
if (diagonal != null) return Diagonal(diagonal);
throw new NotSupportedException(string.Format("Matrix storage type '{0}' is not supported. Only DenseColumnMajorMatrixStorage, SparseCompressedRowMatrixStorage and DiagonalMatrixStorage are supported as this point.", storage.GetType().Name));
throw new NotSupportedException(FormattableString.Invariant($"Matrix storage type '{storage.GetType().Name}' is not supported. Only DenseColumnMajorMatrixStorage, SparseCompressedRowMatrixStorage and DiagonalMatrixStorage are supported as this point."));
}
/// <summary>
@ -1329,13 +1276,10 @@ namespace MathNet.Numerics.LinearAlgebra
{
if (storage == null) throw new ArgumentNullException(nameof(storage));
var dense = storage as DenseVectorStorage<T>;
if (dense != null) return Dense(dense);
var sparse = storage as SparseVectorStorage<T>;
if (sparse != null) return Sparse(sparse);
if (storage is DenseVectorStorage<T> dense) return Dense(dense);
if (storage is SparseVectorStorage<T> sparse) return Sparse(sparse);
throw new NotSupportedException(string.Format("Vector storage type '{0}' is not supported. Only DenseVectorStorage and SparseVectorStorage are supported as this point.", storage.GetType().Name));
throw new NotSupportedException(FormattableString.Invariant($"Vector storage type '{storage.GetType().Name}' is not supported. Only DenseVectorStorage and SparseVectorStorage are supported as this point."));
}
/// <summary>
@ -1603,7 +1547,7 @@ namespace MathNet.Numerics.LinearAlgebra
(VectorBuilder<T>)(object)new Single.VectorBuilder());
}
throw new NotSupportedException(string.Format("Matrices and vectors of type '{0}' are not supported. Only Double, Single, Complex or Complex32 are supported at this point.", typeof(T).Name));
throw new NotSupportedException(FormattableString.Invariant($"Matrices and vectors of type '{typeof(T).Name}' are not supported. Only Double, Single, Complex or Complex32 are supported at this point."));
}
public static void Register(MatrixBuilder<T> matrixBuilder, VectorBuilder<T> vectorBuilder)
@ -1611,14 +1555,8 @@ namespace MathNet.Numerics.LinearAlgebra
_singleton = new Lazy<Tuple<MatrixBuilder<T>, VectorBuilder<T>>>(() => new Tuple<MatrixBuilder<T>, VectorBuilder<T>>(matrixBuilder, vectorBuilder));
}
public static MatrixBuilder<T> Matrix
{
get { return _singleton.Value.Item1; }
}
public static MatrixBuilder<T> Matrix => _singleton.Value.Item1;
public static VectorBuilder<T> Vector
{
get { return _singleton.Value.Item2; }
}
public static VectorBuilder<T> Vector => _singleton.Value.Item2;
}
}

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

@ -395,10 +395,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Gets the matrix's data.
/// </summary>
/// <value>The matrix's data.</value>
public Complex[] Values
{
get { return _values; }
}
public Complex[] Values => _values;
/// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns>
@ -427,8 +424,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
if (result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values);
return;
@ -443,8 +439,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
if (result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.ConjugateArray(_values, denseResult._values);
return;
@ -487,17 +482,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result)
{
// dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null && denseResult != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther && result.Storage is DenseColumnMajorMatrixStorage<Complex> denseResult)
{
LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data);
return;
}
// dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
{
Storage.CopyToUnchecked(result.Storage, ExistingData.Clear);
var diagonal = diagonalOther.Data;
@ -543,17 +535,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result)
{
// dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null && denseResult != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther && result.Storage is DenseColumnMajorMatrixStorage<Complex> denseResult)
{
LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data);
return;
}
// dense + diagonal = matrix
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
{
CopyTo(result);
var diagonal = diagonalOther.Data;
@ -619,9 +608,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiply(
_values,
@ -634,8 +621,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.ColumnCount);
@ -665,9 +651,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose,
@ -684,8 +668,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.RowCount);
@ -715,9 +698,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose,
@ -734,8 +715,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
{
var diagonal = diagonalOther.Data;
var conjugateDiagonal = new Complex[diagonal.Length];
@ -771,9 +751,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Vector<Complex> rightSide, Vector<Complex> result)
{
var denseRight = rightSide as DenseVector;
var denseResult = result as DenseVector;
if (denseRight != null && denseResult != null)
if (rightSide is DenseVector denseRight && result is DenseVector denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose,
@ -800,9 +778,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Vector<Complex> rightSide, Vector<Complex> result)
{
var denseRight = rightSide as DenseVector;
var denseResult = result as DenseVector;
if (denseRight != null && denseResult != null)
if (rightSide is DenseVector denseRight && result is DenseVector denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.ConjugateTranspose,
@ -829,9 +805,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose,
@ -848,8 +822,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount);
@ -880,9 +853,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.ConjugateTranspose,
@ -899,8 +870,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount);

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

@ -162,10 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Gets the vector's data.
/// </summary>
/// <value>The vector's data.</value>
public Complex[] Values
{
get { return _values; }
}
public Complex[] Values => _values;
/// <summary>
/// Returns a reference to the internal data structure.

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

@ -192,8 +192,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data);
return;
@ -212,8 +211,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ConjugateArray(_data, diagResult._data);
return;
@ -235,9 +233,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result)
{
// diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data);
return;
@ -259,9 +255,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result)
{
// diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data);
return;
@ -320,9 +314,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (d == ColumnCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex>;
var denseResult = result.Storage as DenseVectorStorage<Complex>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<Complex> denseOther && result.Storage is DenseVectorStorage<Complex> denseResult)
{
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
@ -342,9 +334,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -354,8 +344,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -395,9 +384,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -407,8 +394,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -439,9 +425,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -453,8 +437,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -485,9 +468,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -497,8 +478,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -538,9 +518,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -552,8 +530,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
{
var dense = denseOther.Data;
var conjugateDiagonal = new Complex[_data.Length];
@ -598,9 +575,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex>;
var denseResult = result.Storage as DenseVectorStorage<Complex>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<Complex> denseOther && result.Storage is DenseVectorStorage<Complex> denseResult)
{
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
@ -628,9 +603,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex>;
var denseResult = result.Storage as DenseVectorStorage<Complex>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<Complex> denseOther && result.Storage is DenseVectorStorage<Complex> denseResult)
{
// TODO: merge/MulByConj
LinearAlgebraControl.Provider.ConjugateArray(_data, denseResult.Data);
@ -658,8 +631,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ScaleArray(1.0/divisor, _data, diagResult._data);
return;
@ -679,8 +651,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(Complex dividend, Matrix<Complex> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
var resultData = diagResult._data;
CommonParallel.For(0, _data.Length, 4096, (a, b) =>

8
src/Numerics/LinearAlgebra/Complex/Factorization/Svd.cs

@ -74,13 +74,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Factorization
/// Gets the two norm of the <see cref="Matrix{T}"/>.
/// </summary>
/// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns>
public override double L2Norm
{
get
{
return S[0].Magnitude;
}
}
public override double L2Norm => S[0].Magnitude;
/// <summary>
/// Gets the condition number <b>max(S) / min(S)</b>

4
src/Numerics/LinearAlgebra/Complex/Solvers/GpBiCg.cs

@ -82,7 +82,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
/// </summary>
public int NumberOfBiCgStabSteps
{
get { return _numberOfBiCgStabSteps; }
get => _numberOfBiCgStabSteps;
set
{
@ -101,7 +101,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
/// </summary>
public int NumberOfGpBiCgSteps
{
get { return _numberOfGpbiCgSteps; }
get => _numberOfGpbiCgSteps;
set
{

6
src/Numerics/LinearAlgebra/Complex/Solvers/ILUTPPreconditioner.cs

@ -162,7 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel
{
get { return _fillLevel; }
get => _fillLevel;
set
{
if (value < 0)
@ -193,7 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance
{
get { return _dropTolerance; }
get => _dropTolerance;
set
{
if (value < 0)
@ -226,7 +226,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance
{
get { return _pivotTolerance; }
get => _pivotTolerance;
set
{
if (value < 0)

4
src/Numerics/LinearAlgebra/Complex/Solvers/MlkBiCgStab.cs

@ -89,7 +89,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
public int NumberOfStartingVectors
{
[DebuggerStepThrough]
get { return _numberOfStartingVectors; }
get => _numberOfStartingVectors;
[DebuggerStepThrough]
set
@ -118,7 +118,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
public IList<Vector<Complex>> StartingVectors
{
[DebuggerStepThrough]
get { return _startingVectors; }
get => _startingVectors;
[DebuggerStepThrough]
set

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

@ -53,10 +53,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Gets the number of non zero elements in the matrix.
/// </summary>
/// <value>The number of non zero elements.</value>
public int NonZerosCount
{
get { return _storage.ValueCount; }
}
public int NonZerosCount => _storage.ValueCount;
/// <summary>
/// Create a new sparse matrix straight from an initialized matrix storage instance.
@ -890,8 +887,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>;
if (diagonalOther != null && sparseResult != null)
if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther && sparseResult != null)
{
var diagonal = diagonalOther.Data;
if (other.ColumnCount == other.RowCount)
@ -912,8 +908,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
var rowPointers = _storage.RowPointers;
var columnIndices = _storage.ColumnIndices;
var values = _storage.Values;
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
{
// in this case we can directly address the underlying data-array
for (var row = 0; row < RowCount; row++)
@ -1547,7 +1542,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public override string ToTypeString()
{
return string.Format("SparseMatrix {0}x{1}-Complex {2:P2} Filled", RowCount, ColumnCount, NonZerosCount / (RowCount * (double)ColumnCount));
return FormattableString.Invariant($"SparseMatrix {RowCount}x{ColumnCount}-Complex {NonZerosCount / (RowCount * (double) ColumnCount):P2} Filled");
}
}
}

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

@ -52,10 +52,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Gets the number of non zero elements in the vector.
/// </summary>
/// <value>The number of non zero elements.</value>
public int NonZerosCount
{
get { return _storage.ValueCount; }
}
public int NonZerosCount => _storage.ValueCount;
/// <summary>
/// Create a new sparse vector straight from an initialized vector storage instance.
@ -404,8 +401,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex> result)
{
var sparseResult = result as SparseVector;
if (sparseResult != null)
if (result is SparseVector sparseResult)
{
if (!ReferenceEquals(this, result))
{
@ -931,7 +927,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public override string ToTypeString()
{
return string.Format("SparseVector {0}-Complex {1:P2} Filled", Count, NonZerosCount / (double)Count);
return FormattableString.Invariant($"SparseVector {Count}-Complex {NonZerosCount / (double) Count:P2} Filled");
}
}
}

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

@ -395,10 +395,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Gets the matrix's data.
/// </summary>
/// <value>The matrix's data.</value>
public Complex32[] Values
{
get { return _values; }
}
public Complex32[] Values => _values;
/// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns>
@ -427,8 +424,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
if (result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values);
return;
@ -443,8 +439,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex32> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
if (result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.ConjugateArray(_values, denseResult._values);
return;
@ -487,17 +482,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result)
{
// dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null && denseResult != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther && result.Storage is DenseColumnMajorMatrixStorage<Complex32> denseResult)
{
LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data);
return;
}
// dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
{
Storage.CopyToUnchecked(result.Storage, ExistingData.Clear);
var diagonal = diagonalOther.Data;
@ -543,17 +535,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result)
{
// dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null && denseResult != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther && result.Storage is DenseColumnMajorMatrixStorage<Complex32> denseResult)
{
LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data);
return;
}
// dense + diagonal = matrix
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
{
CopyTo(result);
var diagonal = diagonalOther.Data;
@ -619,9 +608,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiply(
_values,
@ -634,8 +621,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.ColumnCount);
@ -665,9 +651,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose,
@ -684,8 +668,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.RowCount);
@ -715,9 +698,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose,
@ -734,8 +715,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
{
var diagonal = diagonalOther.Data;
var conjugateDiagonal = new Complex32[diagonal.Length];
@ -802,9 +782,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Vector<Complex32> rightSide, Vector<Complex32> result)
{
var denseRight = rightSide as DenseVector;
var denseResult = result as DenseVector;
if (denseRight != null && denseResult != null)
if (rightSide is DenseVector denseRight && result is DenseVector denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.ConjugateTranspose,
@ -831,9 +809,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose,
@ -850,8 +826,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount);
@ -882,9 +857,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.ConjugateTranspose,
@ -901,8 +874,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount);

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

@ -162,10 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Gets the vector's data.
/// </summary>
/// <value>The vector's data.</value>
public Complex32[] Values
{
get { return _values; }
}
public Complex32[] Values => _values;
/// <summary>
/// Returns a reference to the internal data structure.

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

@ -192,8 +192,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data);
return;
@ -212,8 +211,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex32> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ConjugateArray(_data, diagResult._data);
return;
@ -235,9 +233,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result)
{
// diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data);
return;
@ -259,9 +255,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result)
{
// diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data);
return;
@ -319,9 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (d == ColumnCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex32>;
var denseResult = result.Storage as DenseVectorStorage<Complex32>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<Complex32> denseOther && result.Storage is DenseVectorStorage<Complex32> denseResult)
{
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
@ -341,9 +333,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -353,8 +343,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -394,9 +383,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -406,8 +393,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -438,9 +424,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -452,8 +436,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -484,9 +467,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -496,8 +477,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -537,9 +517,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -551,8 +529,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
{
var dense = denseOther.Data;
var conjugateDiagonal = new Complex32[_data.Length];
@ -597,9 +574,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex32>;
var denseResult = result.Storage as DenseVectorStorage<Complex32>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<Complex32> denseOther && result.Storage is DenseVectorStorage<Complex32> denseResult)
{
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
@ -627,9 +602,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex32>;
var denseResult = result.Storage as DenseVectorStorage<Complex32>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<Complex32> denseOther && result.Storage is DenseVectorStorage<Complex32> denseResult)
{
// TODO: merge/MulByConj
LinearAlgebraControl.Provider.ConjugateArray(_data, denseResult.Data);
@ -657,8 +630,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ScaleArray(1.0f/divisor, _data, diagResult._data);
return;
@ -678,8 +650,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(Complex32 dividend, Matrix<Complex32> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
var resultData = diagResult._data;
CommonParallel.For(0, _data.Length, 4096, (a, b) =>

8
src/Numerics/LinearAlgebra/Complex32/Factorization/Svd.cs

@ -74,13 +74,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Factorization
/// Gets the two norm of the <see cref="Matrix{T}"/>.
/// </summary>
/// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns>
public override double L2Norm
{
get
{
return S[0].Magnitude;
}
}
public override double L2Norm => S[0].Magnitude;
/// <summary>
/// Gets the condition number <b>max(S) / min(S)</b>

4
src/Numerics/LinearAlgebra/Complex32/Solvers/GpBiCg.cs

@ -80,7 +80,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
/// </summary>
public int NumberOfBiCgStabSteps
{
get { return _numberOfBiCgStabSteps; }
get => _numberOfBiCgStabSteps;
set
{
@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
/// </summary>
public int NumberOfGpBiCgSteps
{
get { return _numberOfGpbiCgSteps; }
get => _numberOfGpbiCgSteps;
set
{

6
src/Numerics/LinearAlgebra/Complex32/Solvers/ILUTPPreconditioner.cs

@ -162,7 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel
{
get { return _fillLevel; }
get => _fillLevel;
set
{
if (value < 0)
@ -193,7 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance
{
get { return _dropTolerance; }
get => _dropTolerance;
set
{
if (value < 0)
@ -226,7 +226,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance
{
get { return _pivotTolerance; }
get => _pivotTolerance;
set
{
if (value < 0)

4
src/Numerics/LinearAlgebra/Complex32/Solvers/MlkBiCgStab.cs

@ -87,7 +87,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
public int NumberOfStartingVectors
{
[DebuggerStepThrough]
get { return _numberOfStartingVectors; }
get => _numberOfStartingVectors;
[DebuggerStepThrough]
set
@ -116,7 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
public IList<Vector<Numerics.Complex32>> StartingVectors
{
[DebuggerStepThrough]
get { return _startingVectors; }
get => _startingVectors;
[DebuggerStepThrough]
set

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

@ -53,10 +53,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Gets the number of non zero elements in the matrix.
/// </summary>
/// <value>The number of non zero elements.</value>
public int NonZerosCount
{
get { return _storage.ValueCount; }
}
public int NonZerosCount => _storage.ValueCount;
/// <summary>
/// Create a new sparse matrix straight from an initialized matrix storage instance.
@ -889,8 +886,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>;
if (diagonalOther != null && sparseResult != null)
if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther && sparseResult != null)
{
var diagonal = diagonalOther.Data;
if (other.ColumnCount == other.RowCount)
@ -911,8 +907,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
var rowPointers = _storage.RowPointers;
var columnIndices = _storage.ColumnIndices;
var values = _storage.Values;
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
{
// in this case we can directly address the underlying data-array
for (var row = 0; row < RowCount; row++)

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

@ -52,10 +52,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// Gets the number of non zero elements in the vector.
/// </summary>
/// <value>The number of non zero elements.</value>
public int NonZerosCount
{
get { return _storage.ValueCount; }
}
public int NonZerosCount => _storage.ValueCount;
/// <summary>
/// Create a new sparse vector straight from an initialized vector storage instance.
@ -404,8 +401,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex32> result)
{
var sparseResult = result as SparseVector;
if (sparseResult != null)
if (result is SparseVector sparseResult)
{
if (!ReferenceEquals(this, result))
{
@ -931,7 +927,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public override string ToTypeString()
{
return string.Format("SparseVector {0}-Complex32 {1:P2} Filled", Count, NonZerosCount / (double)Count);
return FormattableString.Invariant($"SparseVector {Count}-Complex32 {NonZerosCount / (double) Count:P2} Filled");
}
}
}

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

@ -393,10 +393,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Gets the matrix's data.
/// </summary>
/// <value>The matrix's data.</value>
public double[] Values
{
get { return _values; }
}
public double[] Values => _values;
/// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns>
@ -425,8 +422,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
if (result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values);
return;
@ -469,17 +465,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
protected override void DoAdd(Matrix<double> other, Matrix<double> result)
{
// dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>;
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<double>;
if (denseOther != null && denseResult != null)
if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther && result.Storage is DenseColumnMajorMatrixStorage<double> denseResult)
{
LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data);
return;
}
// dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
{
Storage.CopyToUnchecked(result.Storage, ExistingData.Clear);
var diagonal = diagonalOther.Data;
@ -525,17 +518,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
protected override void DoSubtract(Matrix<double> other, Matrix<double> result)
{
// dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>;
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<double>;
if (denseOther != null && denseResult != null)
if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther && result.Storage is DenseColumnMajorMatrixStorage<double> denseResult)
{
LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data);
return;
}
// dense + diagonal = matrix
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
{
CopyTo(result);
var diagonal = diagonalOther.Data;
@ -601,9 +591,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiply(
_values,
@ -616,8 +604,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.ColumnCount);
@ -647,9 +634,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<double> other, Matrix<double> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose,
@ -666,8 +651,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.RowCount);
@ -728,9 +712,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<double> other, Matrix<double> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose,
@ -747,8 +729,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount);

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

@ -162,10 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Gets the vector's data.
/// </summary>
/// <value>The vector's data.</value>
public double[] Values
{
get { return _values; }
}
public double[] Values => _values;
/// <summary>
/// Returns a reference to the internal data structure.

46
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs

@ -190,8 +190,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data);
return;
@ -213,9 +212,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
protected override void DoAdd(Matrix<double> other, Matrix<double> result)
{
// diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data);
return;
@ -237,9 +234,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
protected override void DoSubtract(Matrix<double> other, Matrix<double> result)
{
// diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data);
return;
@ -298,9 +293,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (d == ColumnCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<double>;
var denseResult = result.Storage as DenseVectorStorage<double>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<double> denseOther && result.Storage is DenseVectorStorage<double> denseResult)
{
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
@ -320,9 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new double[diagonalResult._data.Length];
var otherDataCopy = new double[diagonalResult._data.Length];
@ -332,8 +323,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -373,9 +363,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<double> other, Matrix<double> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new double[diagonalResult._data.Length];
var otherDataCopy = new double[diagonalResult._data.Length];
@ -385,8 +373,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -417,9 +404,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<double> other, Matrix<double> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new double[diagonalResult._data.Length];
var otherDataCopy = new double[diagonalResult._data.Length];
@ -429,8 +414,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -478,9 +462,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<double>;
var denseResult = result.Storage as DenseVectorStorage<double>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<double> denseOther && result.Storage is DenseVectorStorage<double> denseResult)
{
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
@ -506,8 +488,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ScaleArray(1.0/divisor, _data, diagResult._data);
return;
@ -527,8 +508,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(double dividend, Matrix<double> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
var resultData = diagResult._data;
CommonParallel.For(0, _data.Length, 4096, (a, b) =>

8
src/Numerics/LinearAlgebra/Double/Factorization/Svd.cs

@ -72,13 +72,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Factorization
/// Gets the two norm of the <see cref="Matrix{T}"/>.
/// </summary>
/// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns>
public override double L2Norm
{
get
{
return Math.Abs(S[0]);
}
}
public override double L2Norm => Math.Abs(S[0]);
/// <summary>
/// Gets the condition number <b>max(S) / min(S)</b>

10
src/Numerics/LinearAlgebra/Double/Solvers/GpBiCg.cs

@ -80,10 +80,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
/// </summary>
public int NumberOfBiCgStabSteps
{
get
{
return _numberOfBiCgStabSteps;
}
get => _numberOfBiCgStabSteps;
set
{
@ -102,10 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
/// </summary>
public int NumberOfGpBiCgSteps
{
get
{
return _numberOfGpbiCgSteps;
}
get => _numberOfGpbiCgSteps;
set
{

6
src/Numerics/LinearAlgebra/Double/Solvers/ILUTPPreconditioner.cs

@ -160,7 +160,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel
{
get { return _fillLevel; }
get => _fillLevel;
set
{
if (value < 0)
@ -191,7 +191,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance
{
get { return _dropTolerance; }
get => _dropTolerance;
set
{
if (value < 0)
@ -224,7 +224,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance
{
get { return _pivotTolerance; }
get => _pivotTolerance;
set
{
if (value < 0)

10
src/Numerics/LinearAlgebra/Double/Solvers/MlkBiCgStab.cs

@ -87,10 +87,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
public int NumberOfStartingVectors
{
[DebuggerStepThrough]
get
{
return _numberOfStartingVectors;
}
get => _numberOfStartingVectors;
[DebuggerStepThrough]
set
@ -119,10 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
public IList<Vector<double>> StartingVectors
{
[DebuggerStepThrough]
get
{
return _startingVectors;
}
get => _startingVectors;
[DebuggerStepThrough]
set

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

@ -51,10 +51,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Gets the number of non zero elements in the matrix.
/// </summary>
/// <value>The number of non zero elements.</value>
public int NonZerosCount
{
get { return _storage.ValueCount; }
}
public int NonZerosCount => _storage.ValueCount;
/// <summary>
/// Create a new sparse matrix straight from an initialized matrix storage instance.
@ -890,8 +887,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>;
if (diagonalOther != null && sparseResult != null)
if (other.Storage is DiagonalMatrixStorage<double> diagonalOther && sparseResult != null)
{
var diagonal = diagonalOther.Data;
if (other.ColumnCount == other.RowCount)
@ -911,8 +907,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var columnIndices = _storage.ColumnIndices;
var values = _storage.Values;
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther)
{
// in this case we can directly address the underlying data-array
for (var row = 0; row < RowCount; row++)
@ -1561,7 +1556,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override string ToTypeString()
{
return string.Format("SparseMatrix {0}x{1}-Double {2:P2} Filled", RowCount, ColumnCount, NonZerosCount / (RowCount * (double)ColumnCount));
return FormattableString.Invariant($"SparseMatrix {RowCount}x{ColumnCount}-Double {NonZerosCount / (RowCount * (double) ColumnCount):P2} Filled");
}
}
}

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

@ -52,10 +52,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Gets the number of non zero elements in the vector.
/// </summary>
/// <value>The number of non zero elements.</value>
public int NonZerosCount
{
get { return _storage.ValueCount; }
}
public int NonZerosCount => _storage.ValueCount;
/// <summary>
/// Create a new sparse vector straight from an initialized vector storage instance.
@ -952,7 +949,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override string ToTypeString()
{
return string.Format("SparseVector {0}-Double {1:P2} Filled", Count, NonZerosCount / (double)Count);
return FormattableString.Invariant($"SparseVector {Count}-Double {NonZerosCount / (double) Count:P2} Filled");
}
}
}

15
src/Numerics/LinearAlgebra/Factorization/LU.cs

@ -77,26 +77,17 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization
/// <summary>
/// Gets the lower triangular factor.
/// </summary>
public Matrix<T> L
{
get { return _lazyL.Value; }
}
public Matrix<T> L => _lazyL.Value;
/// <summary>
/// Gets the upper triangular factor.
/// </summary>
public Matrix<T> U
{
get { return _lazyU.Value; }
}
public Matrix<T> U => _lazyU.Value;
/// <summary>
/// Gets the permutation applied to LU factorization.
/// </summary>
public Permutation P
{
get { return _lazyP.Value; }
}
public Permutation P => _lazyP.Value;
/// <summary>
/// Gets the determinant of the matrix for which the LU factorization was computed.

7
src/Numerics/LinearAlgebra/Factorization/QR.cs

@ -80,15 +80,12 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization
/// <summary>
/// Gets or sets orthogonal Q matrix
/// </summary>
public Matrix<T> Q { get; private set; }
public Matrix<T> Q { get; }
/// <summary>
/// Gets the upper triangular factor R.
/// </summary>
public Matrix<T> R
{
get { return _lazyR.Value; }
}
public Matrix<T> R => _lazyR.Value;
/// <summary>
/// Gets the absolute determinant value of the matrix for which the QR matrix was computed.

11
src/Numerics/LinearAlgebra/Factorization/Svd.cs

@ -88,26 +88,23 @@ namespace MathNet.Numerics.LinearAlgebra.Factorization
/// <summary>
/// Gets the singular values (Σ) of matrix in ascending value.
/// </summary>
public Vector<T> S { get; private set; }
public Vector<T> S { get; }
/// <summary>
/// Gets the left singular vectors (U - m-by-m unitary matrix)
/// </summary>
public Matrix<T> U { get; private set; }
public Matrix<T> U { get; }
/// <summary>
/// Gets the transpose right singular vectors (transpose of V, an n-by-n unitary matrix)
/// </summary>
public Matrix<T> VT { get; private set; }
public Matrix<T> VT { get; }
/// <summary>
/// Returns the singular values as a diagonal <see cref="Matrix{T}"/>.
/// </summary>
/// <returns>The singular values as a diagonal <see cref="Matrix{T}"/>.</returns>
public Matrix<T> W
{
get { return _lazyW.Value; }
}
public Matrix<T> W => _lazyW.Value;
/// <summary>
/// Gets the effective numerical matrix rank.

5
src/Numerics/LinearAlgebra/Matrix.BCL.cs

@ -62,8 +62,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </returns>
public override bool Equals(object obj)
{
var other = obj as Matrix<T>;
return other != null && Storage.Equals(other.Storage);
return obj is Matrix<T> other && Storage.Equals(other.Storage);
}
/// <summary>
@ -97,7 +96,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </summary>
public virtual string ToTypeString()
{
return string.Format("{0} {1}x{2}-{3}", GetType().Name, RowCount, ColumnCount, typeof (T).Name);
return FormattableString.Invariant($"{GetType().Name} {RowCount}x{ColumnCount}-{typeof(T).Name}");
}
/// <summary>

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

@ -393,10 +393,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Gets the matrix's data.
/// </summary>
/// <value>The matrix's data.</value>
public float[] Values
{
get { return _values; }
}
public float[] Values => _values;
/// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns>
@ -425,8 +422,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
if (result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values);
return;
@ -469,17 +465,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
protected override void DoAdd(Matrix<float> other, Matrix<float> result)
{
// dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>;
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<float>;
if (denseOther != null && denseResult != null)
if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther && result.Storage is DenseColumnMajorMatrixStorage<float> denseResult)
{
LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data);
return;
}
// dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
{
Storage.CopyToUnchecked(result.Storage, ExistingData.Clear);
var diagonal = diagonalOther.Data;
@ -525,17 +518,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
protected override void DoSubtract(Matrix<float> other, Matrix<float> result)
{
// dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>;
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<float>;
if (denseOther != null && denseResult != null)
if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther && result.Storage is DenseColumnMajorMatrixStorage<float> denseResult)
{
LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data);
return;
}
// dense + diagonal = matrix
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
{
CopyTo(result);
var diagonal = diagonalOther.Data;
@ -601,9 +591,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiply(
_values,
@ -616,8 +604,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.ColumnCount);
@ -647,9 +634,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<float> other, Matrix<float> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose,
@ -666,8 +651,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.RowCount);
@ -697,14 +681,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Vector<float> rightSide, Vector<float> result)
{
var denseRight = rightSide as DenseVector;
var denseResult = result as DenseVector;
if (denseRight == null || denseResult == null)
{
base.DoTransposeThisAndMultiply(rightSide, result);
}
else
if (rightSide is DenseVector denseRight && result is DenseVector denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose,
@ -719,6 +696,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single
0.0f,
denseResult.Values);
}
else
{
base.DoTransposeThisAndMultiply(rightSide, result);
}
}
/// <summary>
@ -728,9 +709,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<float> other, Matrix<float> result)
{
var denseOther = other as DenseMatrix;
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
{
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose,
@ -747,8 +726,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>;
if (diagonalOther != null)
if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
{
var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount);

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

@ -162,10 +162,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Gets the vector's data.
/// </summary>
/// <value>The vector's data.</value>
public float[] Values
{
get { return _values; }
}
public float[] Values => _values;
/// <summary>
/// Returns a reference to the internal data structure.

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

@ -190,8 +190,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data);
return;
@ -213,9 +212,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
protected override void DoAdd(Matrix<float> other, Matrix<float> result)
{
// diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data);
return;
@ -237,9 +234,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
protected override void DoSubtract(Matrix<float> other, Matrix<float> result)
{
// diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix;
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data);
return;
@ -298,9 +293,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (d == ColumnCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<float>;
var denseResult = result.Storage as DenseVectorStorage<float>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<float> denseOther && result.Storage is DenseVectorStorage<float> denseResult)
{
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
@ -320,9 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new float[diagonalResult._data.Length];
var otherDataCopy = new float[diagonalResult._data.Length];
@ -332,8 +323,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -373,9 +363,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<float> other, Matrix<float> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new float[diagonalResult._data.Length];
var otherDataCopy = new float[diagonalResult._data.Length];
@ -385,8 +373,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -417,9 +404,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<float> other, Matrix<float> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
{
var thisDataCopy = new float[diagonalResult._data.Length];
var otherDataCopy = new float[diagonalResult._data.Length];
@ -429,8 +414,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther)
{
var dense = denseOther.Data;
var diagonal = _data;
@ -478,9 +462,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<float>;
var denseResult = result.Storage as DenseVectorStorage<float>;
if (denseOther != null && denseResult != null)
if (rightSide.Storage is DenseVectorStorage<float> denseOther && result.Storage is DenseVectorStorage<float> denseResult)
{
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
@ -506,8 +488,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return;
}
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
LinearAlgebraControl.Provider.ScaleArray(1.0f/divisor, _data, diagResult._data);
return;
@ -527,8 +508,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(float dividend, Matrix<float> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
if (result is DiagonalMatrix diagResult)
{
var resultData = diagResult._data;
CommonParallel.For(0, _data.Length, 4096, (a, b) =>

8
src/Numerics/LinearAlgebra/Single/Factorization/Svd.cs

@ -72,13 +72,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Factorization
/// Gets the two norm of the <see cref="Matrix{T}"/>.
/// </summary>
/// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns>
public override double L2Norm
{
get
{
return Math.Abs(S[0]);
}
}
public override double L2Norm => Math.Abs(S[0]);
/// <summary>
/// Gets the condition number <b>max(S) / min(S)</b>

4
src/Numerics/LinearAlgebra/Single/Solvers/GpBiCg.cs

@ -80,7 +80,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
/// </summary>
public int NumberOfBiCgStabSteps
{
get { return _numberOfBiCgStabSteps; }
get => _numberOfBiCgStabSteps;
set
{
@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
/// </summary>
public int NumberOfGpBiCgSteps
{
get { return _numberOfGpbiCgSteps; }
get => _numberOfGpbiCgSteps;
set
{

6
src/Numerics/LinearAlgebra/Single/Solvers/ILUTPPreconditioner.cs

@ -160,7 +160,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel
{
get { return _fillLevel; }
get => _fillLevel;
set
{
if (value < 0)
@ -191,7 +191,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance
{
get { return _dropTolerance; }
get => _dropTolerance;
set
{
if (value < 0)
@ -224,7 +224,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance
{
get { return _pivotTolerance; }
get => _pivotTolerance;
set
{
if (value < 0)

10
src/Numerics/LinearAlgebra/Single/Solvers/MlkBiCgStab.cs

@ -86,10 +86,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
public int NumberOfStartingVectors
{
[DebuggerStepThrough]
get
{
return _numberOfStartingVectors;
}
get => _numberOfStartingVectors;
[DebuggerStepThrough]
set
@ -118,10 +115,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
public IList<Vector<float>> StartingVectors
{
[DebuggerStepThrough]
get
{
return _startingVectors;
}
get => _startingVectors;
[DebuggerStepThrough]
set

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

@ -52,10 +52,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Gets the number of non zero elements in the matrix.
/// </summary>
/// <value>The number of non zero elements.</value>
public int NonZerosCount
{
get { return _storage.ValueCount; }
}
public int NonZerosCount => _storage.ValueCount;
/// <summary>
/// Create a new sparse matrix straight from an initialized matrix storage instance.
@ -894,8 +891,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return;
}
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>;
if (diagonalOther != null && sparseResult != null)
if (other.Storage is DiagonalMatrixStorage<float> diagonalOther && sparseResult != null)
{
var diagonal = diagonalOther.Data;
if (other.ColumnCount == other.RowCount)
@ -914,8 +910,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
var rowPointers = _storage.RowPointers;
var columnIndices = _storage.ColumnIndices;
var values = _storage.Values;
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>;
if (denseOther != null)
if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther)
{
// in this case we can directly address the underlying data-array
for (var row = 0; row < RowCount; row++)
@ -1568,7 +1563,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public override string ToTypeString()
{
return string.Format("SparseMatrix {0}x{1}-Single {2:P2} Filled", RowCount, ColumnCount, NonZerosCount / (RowCount * (double)ColumnCount));
return FormattableString.Invariant($"SparseMatrix {RowCount}x{ColumnCount}-Single {NonZerosCount / (RowCount * (double) ColumnCount):P2} Filled");
}
}
}

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

@ -52,10 +52,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Gets the number of non zero elements in the vector.
/// </summary>
/// <value>The number of non zero elements.</value>
public int NonZerosCount
{
get { return _storage.ValueCount; }
}
public int NonZerosCount => _storage.ValueCount;
/// <summary>
/// Create a new sparse vector straight from an initialized vector storage instance.
@ -953,7 +950,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public override string ToTypeString()
{
return string.Format("SparseVector {0}-Single {1:P2} Filled", Count, NonZerosCount / (double)Count);
return FormattableString.Invariant($"SparseVector {Count}-Single {NonZerosCount / (double) Count:P2} Filled");
}
}
}

2
src/Numerics/LinearAlgebra/Solvers/CancellationStopCriterion.cs

@ -83,7 +83,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public IterationStatus Status
{
[DebuggerStepThrough]
get { return _currentTcs.Token.IsCancellationRequested ? IterationStatus.Cancelled : IterationStatus.Continue; }
get => _currentTcs.Token.IsCancellationRequested ? IterationStatus.Cancelled : IterationStatus.Continue;
}
public void Cancel()

5
src/Numerics/LinearAlgebra/Solvers/DelegateStopCriterion.cs

@ -70,10 +70,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <summary>
/// Gets the current calculation status.
/// </summary>
public IterationStatus Status
{
get { return _status; }
}
public IterationStatus Status => _status;
/// <summary>
/// Resets the IIterationStopCriterion to the pre-calculation state.

8
src/Numerics/LinearAlgebra/Solvers/DivergenceStopCriterion.cs

@ -92,7 +92,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public double MaximumRelativeIncrease
{
[DebuggerStepThrough]
get { return _maximumRelativeIncrease; }
get => _maximumRelativeIncrease;
[DebuggerStepThrough]
set
@ -114,7 +114,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public int MinimumNumberOfIterations
{
[DebuggerStepThrough]
get { return _minimumNumberOfIterations; }
get => _minimumNumberOfIterations;
[DebuggerStepThrough]
set
@ -220,7 +220,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
int RequiredHistoryLength
{
[DebuggerStepThrough]
get { return _minimumNumberOfIterations + 1; }
get => _minimumNumberOfIterations + 1;
}
/// <summary>
@ -229,7 +229,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public IterationStatus Status
{
[DebuggerStepThrough]
get { return _status; }
get => _status;
}
/// <summary>

2
src/Numerics/LinearAlgebra/Solvers/FailureStopCriterion.cs

@ -96,7 +96,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public IterationStatus Status
{
[DebuggerStepThrough]
get { return _status; }
get => _status;
}
/// <summary>

4
src/Numerics/LinearAlgebra/Solvers/IterationCountStopCriterion.cs

@ -84,7 +84,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public int MaximumNumberOfIterations
{
[DebuggerStepThrough]
get { return _maximumNumberOfIterations; }
get => _maximumNumberOfIterations;
[DebuggerStepThrough]
set
@ -137,7 +137,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public IterationStatus Status
{
[DebuggerStepThrough]
get { return _status; }
get => _status;
}
/// <summary>

5
src/Numerics/LinearAlgebra/Solvers/Iterator.cs

@ -85,10 +85,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <summary>
/// Gets the current calculation status.
/// </summary>
public IterationStatus Status
{
get { return _status; }
}
public IterationStatus Status => _status;
/// <summary>
/// Determines the status of the iterative calculation based on the stop criteria stored

6
src/Numerics/LinearAlgebra/Solvers/ResidualStopCriterion.cs

@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public double Maximum
{
[DebuggerStepThrough]
get { return _maximum; }
get => _maximum;
[DebuggerStepThrough]
set
@ -121,7 +121,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public int MinimumIterationsBelowMaximum
{
[DebuggerStepThrough]
get { return _minimumIterationsBelowMaximum; }
get => _minimumIterationsBelowMaximum;
[DebuggerStepThrough]
set
@ -220,7 +220,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public IterationStatus Status
{
[DebuggerStepThrough]
get { return _status; }
get => _status;
}
/// <summary>

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

@ -71,19 +71,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <summary>
/// True if the matrix storage format is dense.
/// </summary>
public override bool IsDense
{
get { return true; }
}
public override bool IsDense => true;
/// <summary>
/// True if all fields of this matrix can be set to any value.
/// False if some fields are fixed, like on a diagonal matrix.
/// </summary>
public override bool IsFullyMutable
{
get { return true; }
}
public override bool IsFullyMutable => true;
/// <summary>
/// True if the specified field can be set to any value.
@ -300,8 +294,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
for (int j = 0; j < data.Length; j++)
{
var column = data[j];
var denseColumn = column as DenseVectorStorage<T>;
if (denseColumn != null)
if (column is DenseVectorStorage<T> denseColumn)
{
Array.Copy(denseColumn.Data, 0, array, j*rows, rows);
}
@ -351,8 +344,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
public static DenseColumnMajorMatrixStorage<T> OfColumnMajorEnumerable(int rows, int columns, IEnumerable<T> data)
{
var arrayData = data as T[];
if (arrayData != null)
if (data is T[] arrayData)
{
return OfColumnMajorArray(rows, columns, arrayData);
}
@ -373,8 +365,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
for (int column = 0; column < columns; column++)
{
if (!columnIterator.MoveNext()) throw new ArgumentOutOfRangeException(nameof(data), string.Format(Resources.ArgumentArrayWrongLength, columns));
var arrayColumn = columnIterator.Current as T[];
if (arrayColumn != null)
if (columnIterator.Current is T[] arrayColumn)
{
Array.Copy(arrayColumn, 0, array, column*rows, rows);
}
@ -424,8 +415,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopyToUnchecked(MatrixStorage<T> target, ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
CopyToUnchecked(denseTarget);
return;
@ -453,8 +443,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
int sourceColumnIndex, int targetColumnIndex, int columnCount,
ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
CopySubMatrixToUnchecked(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount);
return;
@ -490,8 +479,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopySubRowToUnchecked(VectorStorage<T> target, int rowIndex, int sourceColumnIndex, int targetColumnIndex, int columnCount,
ExistingData existingData)
{
var targetDense = target as DenseVectorStorage<T>;
if (targetDense != null)
if (target is DenseVectorStorage<T> targetDense)
{
for (int j = 0; j < columnCount; j++)
{
@ -513,8 +501,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopySubColumnToUnchecked(VectorStorage<T> target, int columnIndex, int sourceRowIndex, int targetRowIndex, int rowCount,
ExistingData existingData)
{
var targetDense = target as DenseVectorStorage<T>;
if (targetDense != null)
if (target is DenseVectorStorage<T> targetDense)
{
Array.Copy(Data, columnIndex*RowCount + sourceRowIndex, targetDense.Data, targetRowIndex, rowCount);
return;
@ -533,15 +520,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void TransposeToUnchecked(MatrixStorage<T> target, ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
TransposeToUnchecked(denseTarget);
return;
}
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null)
if (target is SparseCompressedRowMatrixStorage<T> sparseTarget)
{
TransposeToUnchecked(sparseTarget);
return;
@ -743,8 +728,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override Tuple<int, int, T, TOther> Find2Unchecked<TOther>(MatrixStorage<TOther> other, Func<T, TOther, bool> predicate, Zeros zeros)
{
var denseOther = other as DenseColumnMajorMatrixStorage<TOther>;
if (denseOther != null)
if (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
{
TOther[] otherData = denseOther.Data;
for (int i = 0; i < Data.Length; i++)
@ -760,8 +744,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null;
}
var diagonalOther = other as DiagonalMatrixStorage<TOther>;
if (diagonalOther != null)
if (other is DiagonalMatrixStorage<TOther> diagonalOther)
{
TOther[] otherData = diagonalOther.Data;
TOther otherZero = BuilderInstance<TOther>.Matrix.Zero;
@ -780,8 +763,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null;
}
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>;
if (sparseOther != null)
if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
{
int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices;
@ -849,8 +831,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void MapToUnchecked<TU>(MatrixStorage<TU> target, Func<T, TU> f,
Zeros zeros, ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<TU>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<TU> denseTarget)
{
CommonParallel.For(0, Data.Length, 4096, (a, b) =>
{
@ -877,8 +858,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void MapIndexedToUnchecked<TU>(MatrixStorage<TU> target, Func<int, int, T, TU> f,
Zeros zeros, ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<TU>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<TU> denseTarget)
{
CommonParallel.For(0, ColumnCount, Math.Max(4096/RowCount, 32), (a, b) =>
{
@ -912,8 +892,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
int sourceColumnIndex, int targetColumnIndex, int columnCount,
Zeros zeros, ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<TU>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<TU> denseTarget)
{
CommonParallel.For(0, columnCount, Math.Max(4096/rowCount, 32), (a, b) =>
{
@ -975,8 +954,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override TState Fold2Unchecked<TOther, TState>(MatrixStorage<TOther> other, Func<TState, T, TOther, TState> f, TState state, Zeros zeros)
{
var denseOther = other as DenseColumnMajorMatrixStorage<TOther>;
if (denseOther != null)
if (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
{
TOther[] otherData = denseOther.Data;
for (int i = 0; i < Data.Length; i++)
@ -986,8 +964,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state;
}
var diagonalOther = other as DiagonalMatrixStorage<TOther>;
if (diagonalOther != null)
if (other is DiagonalMatrixStorage<TOther> diagonalOther)
{
TOther[] otherData = diagonalOther.Data;
TOther otherZero = BuilderInstance<TOther>.Matrix.Zero;
@ -1003,8 +980,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state;
}
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>;
if (sparseOther != null)
if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
{
int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices;

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

@ -71,10 +71,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <summary>
/// True if the vector storage format is dense.
/// </summary>
public override bool IsDense
{
get { return true; }
}
public override bool IsDense => true;
/// <summary>
/// Retrieves the requested element without range checking.
@ -156,8 +153,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
throw new ArgumentNullException(nameof(data));
}
var arrayData = data as T[];
if (arrayData != null)
if (data is T[] arrayData)
{
var copy = new T[arrayData.Length];
Array.Copy(arrayData, 0, copy, 0, arrayData.Length);
@ -187,8 +183,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopyToUnchecked(VectorStorage<T> target, ExistingData existingData)
{
var denseTarget = target as DenseVectorStorage<T>;
if (denseTarget != null)
if (target is DenseVectorStorage<T> denseTarget)
{
if (!ReferenceEquals(this, denseTarget))
{
@ -198,8 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return;
}
var sparseTarget = target as SparseVectorStorage<T>;
if (sparseTarget != null)
if (target is SparseVectorStorage<T> sparseTarget)
{
var indices = new List<int>();
var values = new List<T>();
@ -232,8 +226,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopyToRowUnchecked(MatrixStorage<T> target, int rowIndex, ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
for (int j = 0; j < Data.Length; j++)
{
@ -254,8 +247,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopyToColumnUnchecked(MatrixStorage<T> target, int columnIndex, ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
Array.Copy(Data, 0, denseTarget.Data, columnIndex*denseTarget.RowCount, Data.Length);
return;
@ -274,8 +266,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopySubVectorToUnchecked(VectorStorage<T> target,
int sourceIndex, int targetIndex, int count, ExistingData existingData)
{
var denseTarget = target as DenseVectorStorage<T>;
if (denseTarget != null)
if (target is DenseVectorStorage<T> denseTarget)
{
Array.Copy(Data, sourceIndex, denseTarget.Data, targetIndex, count);
return;
@ -291,8 +282,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopyToSubRowUnchecked(MatrixStorage<T> target, int rowIndex,
int sourceColumnIndex, int targetColumnIndex, int columnCount, ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
for (int j = 0; j < Data.Length; j++)
{
@ -314,8 +304,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopyToSubColumnUnchecked(MatrixStorage<T> target, int columnIndex,
int sourceRowIndex, int targetRowIndex, int rowCount, ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
Array.Copy(Data, sourceRowIndex, denseTarget.Data, columnIndex*denseTarget.RowCount + targetRowIndex, rowCount);
return;
@ -387,8 +376,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override Tuple<int, T, TOther> Find2Unchecked<TOther>(VectorStorage<TOther> other, Func<T, TOther, bool> predicate, Zeros zeros)
{
var denseOther = other as DenseVectorStorage<TOther>;
if (denseOther != null)
if (other is DenseVectorStorage<TOther> denseOther)
{
TOther[] otherData = denseOther.Data;
for (int i = 0; i < Data.Length; i++)
@ -402,8 +390,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null;
}
var sparseOther = other as SparseVectorStorage<TOther>;
if (sparseOther != null)
if (other is SparseVectorStorage<TOther> sparseOther)
{
int[] otherIndices = sparseOther.Indices;
TOther[] otherValues = sparseOther.Values;
@ -462,8 +449,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void MapToUnchecked<TU>(VectorStorage<TU> target, Func<T, TU> f, Zeros zeros, ExistingData existingData)
{
var denseTarget = target as DenseVectorStorage<TU>;
if (denseTarget != null)
if (target is DenseVectorStorage<TU> denseTarget)
{
CommonParallel.For(0, Data.Length, 4096, (a, b) =>
{
@ -485,8 +471,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void MapIndexedToUnchecked<TU>(VectorStorage<TU> target, Func<int, T, TU> f, Zeros zeros, ExistingData existingData)
{
var denseTarget = target as DenseVectorStorage<TU>;
if (denseTarget != null)
if (target is DenseVectorStorage<TU> denseTarget)
{
CommonParallel.For(0, Data.Length, 4096, (a, b) =>
{
@ -519,8 +504,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
}
var denseTarget = target as DenseVectorStorage<T>;
var denseOther = other as DenseVectorStorage<T>;
if (denseTarget != null && denseOther != null)
if (denseTarget != null && other is DenseVectorStorage<T> denseOther)
{
CommonParallel.For(0, Data.Length, 4096, (a, b) =>
{
@ -533,8 +517,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return;
}
var sparseOther = other as SparseVectorStorage<T>;
if (denseTarget != null && sparseOther != null)
if (denseTarget != null && other is SparseVectorStorage<T> sparseOther)
{
T[] targetData = denseTarget.Data;
int[] otherIndices = sparseOther.Indices;
@ -565,8 +548,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override TState Fold2Unchecked<TOther, TState>(VectorStorage<TOther> other, Func<TState, T, TOther, TState> f, TState state, Zeros zeros)
{
var denseOther = other as DenseVectorStorage<TOther>;
if (denseOther != null)
if (other is DenseVectorStorage<TOther> denseOther)
{
var otherData = denseOther.Data;
for (int i = 0; i < Data.Length; i++)
@ -577,8 +559,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state;
}
var sparseOther = other as SparseVectorStorage<TOther>;
if (sparseOther != null)
if (other is SparseVectorStorage<TOther> sparseOther)
{
int[] otherIndices = sparseOther.Indices;
TOther[] otherValues = sparseOther.Values;

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

@ -71,19 +71,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <summary>
/// True if the matrix storage format is dense.
/// </summary>
public override bool IsDense
{
get { return false; }
}
public override bool IsDense => false;
/// <summary>
/// True if all fields of this matrix can be set to any value.
/// False if some fields are fixed, like on a diagonal matrix.
/// </summary>
public override bool IsFullyMutable
{
get { return false; }
}
public override bool IsFullyMutable => false;
/// <summary>
/// True if the specified field can be set to any value.
@ -226,8 +220,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
throw new ArgumentNullException(nameof(data));
}
var arrayData = data as T[];
if (arrayData != null)
if (data is T[] arrayData)
{
var copy = new T[arrayData.Length];
Array.Copy(arrayData, 0, copy, 0, arrayData.Length);
@ -256,22 +249,19 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopyToUnchecked(MatrixStorage<T> target, ExistingData existingData)
{
var diagonalTarget = target as DiagonalMatrixStorage<T>;
if (diagonalTarget != null)
if (target is DiagonalMatrixStorage<T> diagonalTarget)
{
CopyToUnchecked(diagonalTarget);
return;
}
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
CopyToUnchecked(denseTarget, existingData);
return;
}
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null)
if (target is SparseCompressedRowMatrixStorage<T> sparseTarget)
{
CopyToUnchecked(sparseTarget, existingData);
return;
@ -327,15 +317,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
int sourceColumnIndex, int targetColumnIndex, int columnCount,
ExistingData existingData)
{
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
CopySubMatrixToUnchecked(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, existingData);
return;
}
var diagonalTarget = target as DiagonalMatrixStorage<T>;
if (diagonalTarget != null)
if (target is DiagonalMatrixStorage<T> diagonalTarget)
{
CopySubMatrixToUnchecked(diagonalTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount);
return;
@ -625,8 +613,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override Tuple<int, int, T, TOther> Find2Unchecked<TOther>(MatrixStorage<TOther> other, Func<T, TOther, bool> predicate, Zeros zeros)
{
var denseOther = other as DenseColumnMajorMatrixStorage<TOther>;
if (denseOther != null)
if (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
{
TOther[] otherData = denseOther.Data;
int k = 0;
@ -644,8 +631,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null;
}
var diagonalOther = other as DiagonalMatrixStorage<TOther>;
if (diagonalOther != null)
if (other is DiagonalMatrixStorage<TOther> diagonalOther)
{
TOther[] otherData = diagonalOther.Data;
for (int i = 0; i < Data.Length; i++)
@ -666,8 +652,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null;
}
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>;
if (sparseOther != null)
if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
{
int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices;
@ -772,8 +757,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{
var processZeros = zeros == Zeros.Include || !Zero.Equals(f(Zero));
var diagonalTarget = target as DiagonalMatrixStorage<TU>;
if (diagonalTarget != null)
if (target is DiagonalMatrixStorage<TU> diagonalTarget)
{
if (processZeros)
{
@ -821,8 +805,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{
var processZeros = zeros == Zeros.Include || !Zero.Equals(f(0, 1, Zero));
var diagonalTarget = target as DiagonalMatrixStorage<TU>;
if (diagonalTarget != null)
if (target is DiagonalMatrixStorage<TU> diagonalTarget)
{
if (processZeros)
{
@ -870,15 +853,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
int sourceColumnIndex, int targetColumnIndex, int columnCount,
Zeros zeros, ExistingData existingData)
{
var diagonalTarget = target as DiagonalMatrixStorage<TU>;
if (diagonalTarget != null)
if (target is DiagonalMatrixStorage<TU> diagonalTarget)
{
MapSubMatrixIndexedToUnchecked(diagonalTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros);
return;
}
var denseTarget = target as DenseColumnMajorMatrixStorage<TU>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<TU> denseTarget)
{
MapSubMatrixIndexedToUnchecked(denseTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros, existingData);
return;
@ -1097,8 +1078,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override TState Fold2Unchecked<TOther, TState>(MatrixStorage<TOther> other, Func<TState, T, TOther, TState> f, TState state, Zeros zeros)
{
var denseOther = other as DenseColumnMajorMatrixStorage<TOther>;
if (denseOther != null)
if (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
{
TOther[] otherData = denseOther.Data;
int k = 0;
@ -1113,8 +1093,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state;
}
var diagonalOther = other as DiagonalMatrixStorage<TOther>;
if (diagonalOther != null)
if (other is DiagonalMatrixStorage<TOther> diagonalOther)
{
TOther[] otherData = diagonalOther.Data;
for (int i = 0; i < Data.Length; i++)
@ -1136,8 +1115,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state;
}
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>;
if (sparseOther != null)
if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
{
int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices;

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

@ -69,10 +69,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// Gets the number of non zero elements in the matrix.
/// </summary>
/// <value>The number of non zero elements.</value>
public int ValueCount
{
get { return RowPointers[RowCount]; }
}
public int ValueCount => RowPointers[RowCount];
internal SparseCompressedRowMatrixStorage(int rows, int columns)
: base(rows, columns)
@ -85,19 +82,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <summary>
/// True if the matrix storage format is dense.
/// </summary>
public override bool IsDense
{
get { return false; }
}
public override bool IsDense => false;
/// <summary>
/// True if all fields of this matrix can be set to any value.
/// False if some fields are fixed, like on a diagonal matrix.
/// </summary>
public override bool IsFullyMutable
{
get { return true; }
}
public override bool IsFullyMutable => true;
/// <summary>
/// True if the specified field can be set to any value.
@ -842,15 +833,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopyToUnchecked(MatrixStorage<T> target, ExistingData existingData)
{
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null)
if (target is SparseCompressedRowMatrixStorage<T> sparseTarget)
{
CopyToUnchecked(sparseTarget);
return;
}
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
CopyToUnchecked(denseTarget, existingData);
return;
@ -923,8 +912,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
throw new ArgumentNullException(nameof(target));
}
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null)
if (target is SparseCompressedRowMatrixStorage<T> sparseTarget)
{
CopySubMatrixToUnchecked(sparseTarget,
sourceRowIndex, targetRowIndex, rowCount,
@ -1046,8 +1034,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return;
}
var targetSparse = target as SparseVectorStorage<T>;
if (targetSparse != null)
if (target is SparseVectorStorage<T> targetSparse)
{
if ((sourceColumnIndex == 0) && (targetColumnIndex == 0) && (columnCount == ColumnCount) && (ColumnCount == targetSparse.Length))
{
@ -1132,15 +1119,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void TransposeToUnchecked(MatrixStorage<T> target, ExistingData existingData)
{
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>;
if (sparseTarget != null)
if (target is SparseCompressedRowMatrixStorage<T> sparseTarget)
{
TransposeToUnchecked(sparseTarget);
return;
}
var denseTarget = target as DenseColumnMajorMatrixStorage<T>;
if (denseTarget != null)
if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
{
TransposeToUnchecked(denseTarget, existingData);
return;
@ -1459,8 +1444,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override Tuple<int, int, T, TOther> Find2Unchecked<TOther>(MatrixStorage<TOther> other, Func<T, TOther, bool> predicate, Zeros zeros)
{
var denseOther = other as DenseColumnMajorMatrixStorage<TOther>;
if (denseOther != null)
if (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
{
TOther[] otherData = denseOther.Data;
int k = 0;
@ -1478,8 +1462,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null;
}
var diagonalOther = other as DiagonalMatrixStorage<TOther>;
if (diagonalOther != null)
if (other is DiagonalMatrixStorage<TOther> diagonalOther)
{
TOther[] otherData = diagonalOther.Data;
TOther otherZero = BuilderInstance<TOther>.Matrix.Zero;
@ -1537,8 +1520,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null;
}
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>;
if (sparseOther != null)
if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
{
int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices;
@ -1714,8 +1696,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{
var processZeros = zeros == Zeros.Include || !Zero.Equals(f(Zero));
var sparseTarget = target as SparseCompressedRowMatrixStorage<TU>;
if (sparseTarget != null)
if (target is SparseCompressedRowMatrixStorage<TU> sparseTarget)
{
var newRowPointers = sparseTarget.RowPointers;
var newColumnIndices = new List<int>(ColumnIndices.Length);
@ -1808,8 +1789,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{
var processZeros = zeros == Zeros.Include || !Zero.Equals(f(0, 1, Zero));
var sparseTarget = target as SparseCompressedRowMatrixStorage<TU>;
if (sparseTarget != null)
if (target is SparseCompressedRowMatrixStorage<TU> sparseTarget)
{
var newRowPointers = sparseTarget.RowPointers;
var newColumnIndices = new List<int>(ColumnIndices.Length);
@ -1903,8 +1883,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
int sourceColumnIndex, int targetColumnIndex, int columnCount,
Zeros zeros, ExistingData existingData)
{
var sparseTarget = target as SparseCompressedRowMatrixStorage<TU>;
if (sparseTarget != null)
if (target is SparseCompressedRowMatrixStorage<TU> sparseTarget)
{
MapSubMatrixIndexedToUnchecked(sparseTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros, existingData);
return;
@ -2203,8 +2182,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override TState Fold2Unchecked<TOther, TState>(MatrixStorage<TOther> other, Func<TState, T, TOther, TState> f, TState state, Zeros zeros)
{
var denseOther = other as DenseColumnMajorMatrixStorage<TOther>;
if (denseOther != null)
if (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
{
TOther[] otherData = denseOther.Data;
int k = 0;
@ -2219,8 +2197,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state;
}
var diagonalOther = other as DiagonalMatrixStorage<TOther>;
if (diagonalOther != null)
if (other is DiagonalMatrixStorage<TOther> diagonalOther)
{
TOther[] otherData = diagonalOther.Data;
TOther otherZero = BuilderInstance<TOther>.Matrix.Zero;
@ -2266,8 +2243,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state;
}
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>;
if (sparseOther != null)
if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
{
int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices;

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

@ -73,10 +73,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <summary>
/// True if the vector storage format is dense.
/// </summary>
public override bool IsDense
{
get { return false; }
}
public override bool IsDense => false;
/// <summary>
/// Retrieves the requested element without range checking.
@ -425,8 +422,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopyToUnchecked(VectorStorage<T> target, ExistingData existingData)
{
var sparseTarget = target as SparseVectorStorage<T>;
if (sparseTarget != null)
if (target is SparseVectorStorage<T> sparseTarget)
{
CopyToUnchecked(sparseTarget);
return;
@ -517,8 +513,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void CopySubVectorToUnchecked(VectorStorage<T> target,
int sourceIndex, int targetIndex, int count, ExistingData existingData)
{
var sparseTarget = target as SparseVectorStorage<T>;
if (sparseTarget != null)
if (target is SparseVectorStorage<T> sparseTarget)
{
CopySubVectorToUnchecked(sparseTarget, sourceIndex, targetIndex, count, existingData);
return;
@ -689,8 +684,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override Tuple<int, T, TOther> Find2Unchecked<TOther>(VectorStorage<TOther> other, Func<T, TOther, bool> predicate, Zeros zeros)
{
var denseOther = other as DenseVectorStorage<TOther>;
if (denseOther != null)
if (other is DenseVectorStorage<TOther> denseOther)
{
TOther[] otherData = denseOther.Data;
int k = 0;
@ -715,8 +709,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null;
}
var sparseOther = other as SparseVectorStorage<TOther>;
if (sparseOther != null)
if (other is SparseVectorStorage<TOther> sparseOther)
{
int[] otherIndices = sparseOther.Indices;
TOther[] otherValues = sparseOther.Values;
@ -846,8 +839,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void MapToUnchecked<TU>(VectorStorage<TU> target, Func<T, TU> f, Zeros zeros, ExistingData existingData)
{
var sparseTarget = target as SparseVectorStorage<TU>;
if (sparseTarget != null)
if (target is SparseVectorStorage<TU> sparseTarget)
{
var indices = new List<int>();
var values = new List<TU>();
@ -882,8 +874,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return;
}
var denseTarget = target as DenseVectorStorage<TU>;
if (denseTarget != null)
if (target is DenseVectorStorage<TU> denseTarget)
{
if (existingData == ExistingData.Clear)
{
@ -920,8 +911,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override void MapIndexedToUnchecked<TU>(VectorStorage<TU> target, Func<int, T, TU> f, Zeros zeros, ExistingData existingData)
{
var sparseTarget = target as SparseVectorStorage<TU>;
if (sparseTarget != null)
if (target is SparseVectorStorage<TU> sparseTarget)
{
var indices = new List<int>();
var values = new List<TU>();
@ -956,8 +946,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return;
}
var denseTarget = target as DenseVectorStorage<TU>;
if (denseTarget != null)
if (target is DenseVectorStorage<TU> denseTarget)
{
if (existingData == ExistingData.Clear)
{
@ -1083,8 +1072,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return;
}
var sparseTarget = target as SparseVectorStorage<T>;
if (sparseOther != null && sparseTarget != null)
if (sparseOther != null && target is SparseVectorStorage<T> sparseTarget)
{
var indices = new List<int>();
var values = new List<T>();
@ -1146,8 +1134,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
internal override TState Fold2Unchecked<TOther, TState>(VectorStorage<TOther> other, Func<TState, T, TOther, TState> f, TState state, Zeros zeros)
{
var sparseOther = other as SparseVectorStorage<TOther>;
if (sparseOther != null)
if (other is SparseVectorStorage<TOther> sparseOther)
{
int[] otherIndices = sparseOther.Indices;
TOther[] otherValues = sparseOther.Values;
@ -1192,8 +1179,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state;
}
var denseOther = other as DenseVectorStorage<TOther>;
if (denseOther != null)
if (other is DenseVectorStorage<TOther> denseOther)
{
TOther[] otherData = denseOther.Data;

34
src/Numerics/LinearAlgebra/Vector.BCL.cs

@ -62,8 +62,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </returns>
public sealed override bool Equals(object obj)
{
var other = obj as Vector<T>;
return other != null && Storage.Equals(other.Storage);
return obj is Vector<T> other && Storage.Equals(other.Storage);
}
/// <summary>
@ -112,10 +111,7 @@ namespace MathNet.Numerics.LinearAlgebra
throw new NotSupportedException();
}
bool ICollection<T>.IsReadOnly
{
get { return false; }
}
bool ICollection<T>.IsReadOnly => false;
void ICollection<T>.Add(T item)
{
@ -148,20 +144,14 @@ namespace MathNet.Numerics.LinearAlgebra
Storage.CopySubVectorTo(new DenseVectorStorage<T>(array.Length, array), 0, arrayIndex, Count);
}
bool IList.IsReadOnly
{
get { return false; }
}
bool IList.IsReadOnly => false;
bool IList.IsFixedSize
{
get { return true; }
}
bool IList.IsFixedSize => true;
object IList.this[int index]
{
get { return Storage[index]; }
set { Storage[index] = (T) value; }
get => Storage[index];
set => Storage[index] = (T) value;
}
int IList.IndexOf(object value)
@ -204,15 +194,9 @@ namespace MathNet.Numerics.LinearAlgebra
throw new NotSupportedException();
}
bool ICollection.IsSynchronized
{
get { return false; }
}
bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot
{
get { return Storage; }
}
object ICollection.SyncRoot => Storage;
void ICollection.CopyTo(Array array, int index)
{
@ -255,7 +239,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </summary>
public virtual string ToTypeString()
{
return string.Format("{0} {1}-{2}", GetType().Name, Count, typeof (T).Name);
return FormattableString.Invariant($"{GetType().Name} {Count}-{typeof(T).Name}");
}
public string[,] ToVectorStringArray(int maxPerColumn, int maxCharactersWidth, int padding, string ellipsis, Func<T, string> formatValue)

2
src/Numerics/Optimization/BfgsBMinimizer.cs

@ -157,7 +157,7 @@ namespace MathNet.Numerics.Optimization
int iterations = DoBfgsUpdate(ref currentExitCondition, lineSearcher, ref pseudoHessian, ref lineSearchDirection, ref previousPoint, ref lineSearchResult, ref candidatePoint, ref step, ref totalLineSearchSteps, ref iterationsWithNontrivialLineSearch);
if (iterations == MaximumIterations && currentExitCondition == ExitCondition.None)
throw new MaximumIterationsException(string.Format("Maximum iterations ({0}) reached.", MaximumIterations));
throw new MaximumIterationsException(FormattableString.Invariant($"Maximum iterations ({MaximumIterations}) reached."));
return new MinimizationWithLineSearchResult(candidatePoint, iterations, currentExitCondition, totalLineSearchSteps, iterationsWithNontrivialLineSearch);
}

2
src/Numerics/Optimization/BfgsMinimizer.cs

@ -107,7 +107,7 @@ namespace MathNet.Numerics.Optimization
iterations = DoBfgsUpdate(ref currentExitCondition, lineSearcher, ref inversePseudoHessian, ref lineSearchDirection, ref previousPoint, ref lineSearchResult, ref candidate, ref step, ref totalLineSearchSteps, ref iterationsWithNontrivialLineSearch);
if (iterations == MaximumIterations && currentExitCondition == ExitCondition.None)
throw new MaximumIterationsException(String.Format("Maximum iterations ({0}) reached.", MaximumIterations));
throw new MaximumIterationsException(FormattableString.Invariant($"Maximum iterations ({MaximumIterations}) reached."));
return new MinimizationWithLineSearchResult(candidate, iterations, ExitCondition.AbsoluteGradient, totalLineSearchSteps, iterationsWithNontrivialLineSearch);
}

2
src/Numerics/Optimization/ConjugateGradientMinimizer.cs

@ -124,7 +124,7 @@ namespace MathNet.Numerics.Optimization
if (iterations == maxIterations)
{
throw new MaximumIterationsException(String.Format("Maximum iterations ({0}) reached.", maxIterations));
throw new MaximumIterationsException(FormattableString.Invariant($"Maximum iterations ({maxIterations}) reached."));
}
return new MinimizationWithLineSearchResult(objective, iterations, ExitCondition.AbsoluteGradient, totalLineSearchSteps, iterationsWithNontrivialLineSearch);

2
src/Numerics/Optimization/LimitedMemoryBfgsMinimizer.cs

@ -150,7 +150,7 @@ namespace MathNet.Numerics.Optimization
}
if (iterations == MaximumIterations && currentExitCondition == ExitCondition.None)
throw new MaximumIterationsException(String.Format("Maximum iterations ({0}) reached.", MaximumIterations));
throw new MaximumIterationsException(FormattableString.Invariant($"Maximum iterations ({MaximumIterations}) reached."));
return new MinimizationWithLineSearchResult(candidate, iterations, ExitCondition.AbsoluteGradient, totalLineSearchSteps, iterationsWithNontrivialLineSearch);
}

5
src/Numerics/Optimization/LineSearch/StrongWolfeLineSearch.cs

@ -39,10 +39,7 @@ namespace MathNet.Numerics.Optimization.LineSearch
// Argument validation in base class
}
protected override ExitCondition WolfeExitCondition
{
get { return ExitCondition.StrongWolfeCriteria; }
}
protected override ExitCondition WolfeExitCondition => ExitCondition.StrongWolfeCriteria;
protected override bool WolfeCondition(double stepDd, double initialDd)
{

9
src/Numerics/Optimization/LineSearch/WeakWolfeLineSearch.cs

@ -53,10 +53,7 @@ namespace MathNet.Numerics.Optimization.LineSearch
// Validation in base class
}
protected override ExitCondition WolfeExitCondition
{
get { return ExitCondition.WeakWolfeCriteria; }
}
protected override ExitCondition WolfeExitCondition => ExitCondition.WeakWolfeCriteria;
protected override bool WolfeCondition(double stepDd, double initialDd)
{
@ -67,7 +64,7 @@ namespace MathNet.Numerics.Optimization.LineSearch
{
if (!IsFinite(eval.Value))
{
throw new EvaluationException(String.Format("Non-finite value returned by objective function: {0}", eval.Value), eval);
throw new EvaluationException(FormattableString.Invariant($"Non-finite value returned by objective function: {eval.Value}"), eval);
}
}
@ -83,7 +80,7 @@ namespace MathNet.Numerics.Optimization.LineSearch
{
if (!IsFinite(x))
{
throw new EvaluationException(string.Format("Non-finite value returned by gradient: {0}", x), eval);
throw new EvaluationException(FormattableString.Invariant($"Non-finite value returned by gradient: {x}"), eval);
}
}
}

11
src/Numerics/Optimization/LineSearch/WolfeLineSearch.cs

@ -29,6 +29,7 @@
using System;
using MathNet.Numerics.LinearAlgebra;
using static System.FormattableString;
namespace MathNet.Numerics.Optimization.LineSearch
{
@ -42,11 +43,11 @@ namespace MathNet.Numerics.Optimization.LineSearch
public WolfeLineSearch(double c1, double c2, double parameterTolerance, int maxIterations = 10)
{
if (c1 <= 0)
throw new ArgumentException(string.Format("c1 {0} should be greater than 0", c1));
throw new ArgumentException(Invariant($"c1 {c1} should be greater than 0"));
if (c2 <= c1)
throw new ArgumentException(string.Format("c1 {0} should be less than c2 {1}", c1, c2));
throw new ArgumentException(Invariant($"c1 {c1} should be less than c2 {c2}"));
if (c2 >= 1)
throw new ArgumentException(string.Format("c2 {0} should be less than 1", c2));
throw new ArgumentException(Invariant($"c2 {c2} should be less than 1"));
C1 = c1;
C2 = c2;
@ -125,12 +126,12 @@ namespace MathNet.Numerics.Optimization.LineSearch
if (ii == MaximumIterations && Double.IsPositiveInfinity(upperBound))
{
throw new MaximumIterationsException(String.Format("Maximum iterations ({0}) reached. Function appears to be unbounded in search direction.", MaximumIterations));
throw new MaximumIterationsException(Invariant($"Maximum iterations ({MaximumIterations}) reached. Function appears to be unbounded in search direction."));
}
if (ii == MaximumIterations)
{
throw new MaximumIterationsException(String.Format("Maximum iterations ({0}) reached.", MaximumIterations));
throw new MaximumIterationsException(Invariant($"Maximum iterations ({MaximumIterations}) reached."));
}
return new LineSearchResult(objective, ii, step, reasonForExit);

8
src/Numerics/Optimization/MinimizationResult.cs

@ -33,10 +33,10 @@ namespace MathNet.Numerics.Optimization
{
public class MinimizationResult
{
public Vector<double> MinimizingPoint { get { return FunctionInfoAtMinimum.Point; } }
public IObjectiveFunction FunctionInfoAtMinimum { get; private set; }
public int Iterations { get; private set; }
public ExitCondition ReasonForExit { get; private set; }
public Vector<double> MinimizingPoint => FunctionInfoAtMinimum.Point;
public IObjectiveFunction FunctionInfoAtMinimum { get; }
public int Iterations { get; }
public ExitCondition ReasonForExit { get; }
public MinimizationResult(IObjectiveFunction functionInfo, int iterations, ExitCondition reasonForExit)
{

2
src/Numerics/Optimization/NelderMeadSimplex.cs

@ -180,7 +180,7 @@ namespace MathNet.Numerics.Optimization
// check to see if we have exceeded our alloted number of evaluations
if (evaluationCount >= maximumIterations)
{
throw new MaximumIterationsException(String.Format("Maximum iterations ({0}) reached.", maximumIterations));
throw new MaximumIterationsException(FormattableString.Invariant($"Maximum iterations ({maximumIterations}) reached."));
}
}
objectiveFunction.EvaluateAt(vertices[errorProfile.LowestIndex]);

2
src/Numerics/Optimization/NewtonMinimizer.cs

@ -119,7 +119,7 @@ namespace MathNet.Numerics.Optimization
if (iterations == maxIterations)
{
throw new MaximumIterationsException(String.Format("Maximum iterations ({0}) reached.", maxIterations));
throw new MaximumIterationsException(FormattableString.Invariant($"Maximum iterations ({maxIterations}) reached."));
}
return new MinimizationWithLineSearchResult(objective, iterations, ExitCondition.AbsoluteGradient, totalLineSearchSteps, iterationsWithNontrivialLineSearch);

14
src/Numerics/Optimization/NonlinearMinimizationResult.cs

@ -4,22 +4,22 @@ namespace MathNet.Numerics.Optimization
{
public class NonlinearMinimizationResult
{
public IObjectiveModel ModelInfoAtMinimum { get; private set; }
public IObjectiveModel ModelInfoAtMinimum { get; }
/// <summary>
/// Returns the best fit parameters.
/// </summary>
public Vector<double> MinimizingPoint { get { return ModelInfoAtMinimum.Point; } }
public Vector<double> MinimizingPoint => ModelInfoAtMinimum.Point;
/// <summary>
/// Returns the standard errors of the corresponding parameters
/// Returns the standard errors of the corresponding parameters
/// </summary>
public Vector<double> StandardErrors { get; private set; }
/// <summary>
/// Returns the y-values of the fitted model that correspond to the independent values.
/// </summary>
public Vector<double> MinimizedValues { get { return ModelInfoAtMinimum.ModelValues; } }
public Vector<double> MinimizedValues => ModelInfoAtMinimum.ModelValues;
/// <summary>
/// Returns the covariance matrix at minimizing point.
@ -31,9 +31,9 @@ namespace MathNet.Numerics.Optimization
/// </summary>
public Matrix<double> Correlation { get; private set; }
public int Iterations { get; private set; }
public int Iterations { get; }
public ExitCondition ReasonForExit { get; private set; }
public ExitCondition ReasonForExit { get; }
public NonlinearMinimizationResult(IObjectiveModel modelInfo, int iterations, ExitCondition reasonForExit)
{
@ -72,7 +72,7 @@ namespace MathNet.Numerics.Optimization
{
StandardErrors = null;
Correlation = null;
}
}
}
}
}

6
src/Numerics/Optimization/NonlinearMinimizerBase.cs

@ -19,8 +19,8 @@ namespace MathNet.Numerics.Optimization
/// <summary>
/// The stopping threshold for infinity norm of the gradient.
/// </summary>
public static double GradientTolerance { get; set; }
public static double GradientTolerance { get; set; }
/// <summary>
/// The maximum number of iterations.
/// </summary>
@ -41,7 +41,7 @@ namespace MathNet.Numerics.Optimization
/// </summary>
public static Vector<double> Scales { get; private set; }
private static bool IsBounded { get { return LowerBound != null || UpperBound != null || Scales != null; } }
private static bool IsBounded => LowerBound != null || UpperBound != null || Scales != null;
protected NonlinearMinimizerBase(double gradientTolerance = 1E-18, double stepTolerance = 1E-18, double functionTolerance = 1E-18, int maximumIterations = -1)
{

30
src/Numerics/Optimization/ObjectiveFunctions/ForwardDifferenceGradientObjectiveFunction.cs

@ -104,32 +104,14 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
EvaluateGradient();
return _gradient;
}
protected set { _gradient = value; }
protected set => _gradient = value;
}
public Matrix<double> Hessian
{
get
{
throw new NotImplementedException();
}
}
public Matrix<double> Hessian => throw new NotImplementedException();
public bool IsGradientSupported
{
get
{
return true;
}
}
public bool IsGradientSupported => true;
public bool IsHessianSupported
{
get
{
return false;
}
}
public bool IsHessianSupported => false;
public Vector<double> Point { get; protected set; }
@ -145,7 +127,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public IObjectiveFunction CreateNew()
{
var tmp = new ForwardDifferenceGradientObjectiveFunction(this.InnerObjectiveFunction.CreateNew(), LowerBound, UpperBound, this.RelativeIncrement, this.MinimumIncrement);
var tmp = new ForwardDifferenceGradientObjectiveFunction(InnerObjectiveFunction.CreateNew(), LowerBound, UpperBound, this.RelativeIncrement, this.MinimumIncrement);
return tmp;
}
@ -159,7 +141,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public IObjectiveFunction Fork()
{
return new ForwardDifferenceGradientObjectiveFunction(this.InnerObjectiveFunction.Fork(), LowerBound, UpperBound, this.RelativeIncrement, this.MinimumIncrement)
return new ForwardDifferenceGradientObjectiveFunction(InnerObjectiveFunction.Fork(), LowerBound, UpperBound, this.RelativeIncrement, this.MinimumIncrement)
{
Point = Point?.Clone(),
GradientEvaluated = GradientEvaluated,

10
src/Numerics/Optimization/ObjectiveFunctions/GradientHessianObjectiveFunction.cs

@ -58,15 +58,9 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
};
}
public bool IsGradientSupported
{
get { return true; }
}
public bool IsGradientSupported => true;
public bool IsHessianSupported
{
get { return true; }
}
public bool IsHessianSupported => true;
public void EvaluateAt(Vector<double> point)
{

15
src/Numerics/Optimization/ObjectiveFunctions/GradientObjectiveFunction.cs

@ -57,15 +57,9 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
};
}
public bool IsGradientSupported
{
get { return true; }
}
public bool IsGradientSupported => true;
public bool IsHessianSupported
{
get { return false; }
}
public bool IsHessianSupported => false;
public void EvaluateAt(Vector<double> point)
{
@ -80,9 +74,6 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public double Value { get; private set; }
public Vector<double> Gradient { get; private set; }
public Matrix<double> Hessian
{
get { throw new NotSupportedException(); }
}
public Matrix<double> Hessian => throw new NotSupportedException();
}
}

15
src/Numerics/Optimization/ObjectiveFunctions/HessianObjectiveFunction.cs

@ -57,15 +57,9 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
};
}
public bool IsGradientSupported
{
get { return false; }
}
public bool IsGradientSupported => false;
public bool IsHessianSupported
{
get { return true; }
}
public bool IsHessianSupported => true;
public void EvaluateAt(Vector<double> point)
{
@ -80,9 +74,6 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public double Value { get; private set; }
public Matrix<double> Hessian { get; private set; }
public Vector<double> Gradient
{
get { throw new NotSupportedException(); }
}
public Vector<double> Gradient => throw new NotSupportedException();
}
}

5
src/Numerics/Optimization/ObjectiveFunctions/LazyObjectiveFunction.cs

@ -94,10 +94,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
_hessianValue = null;
}
public Vector<double> Point
{
get { return _point; }
}
public Vector<double> Point => _point;
public double Value
{

5
src/Numerics/Optimization/ObjectiveFunctions/LazyObjectiveFunctionBase.cs

@ -89,10 +89,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
Hessian = null;
}
public Vector<double> Point
{
get { return _point; }
}
public Vector<double> Point => _point;
public double Value
{

24
src/Numerics/Optimization/ObjectiveFunctions/NonlinearObjectiveFunction.cs

@ -23,7 +23,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
Matrix<double> jacobianValue; // the Jacobian matrix.
Vector<double> gradientValue; // the Gradient vector.
Matrix<double> hessianValue; // the Hessian matrix.
#endregion Private Variables
#region Public Variables
@ -37,7 +37,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
/// Set or get the values of the observations.
/// </summary>
public Vector<double> ObservedY { get; private set; }
/// <summary>
/// Set or get the values of the weights for the observations.
/// </summary>
@ -52,12 +52,12 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
/// <summary>
/// Get the number of observations.
/// </summary>
public int NumberOfObservations { get { return (ObservedY == null) ? 0 : ObservedY.Count; } }
public int NumberOfObservations => ObservedY?.Count ?? 0;
/// <summary>
/// Get the number of unknown parameters.
/// </summary>
public int NumberOfParameters { get { return (Point == null) ? 0 : Point.Count; } }
public int NumberOfParameters => Point?.Count ?? 0;
/// <summary>
/// Get the degree of freedom
@ -123,7 +123,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
/// <summary>
/// Set or get the values of the parameters.
/// </summary>
public Vector<double> Point { get { return coefficients; } }
public Vector<double> Point => coefficients;
/// <summary>
/// Get the y-values of the fitted model that correspond to the independent values.
@ -178,8 +178,8 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
}
}
public bool IsGradientSupported { get { return true; } }
public bool IsHessianSupported { get { return true; } }
public bool IsGradientSupported => true;
public bool IsHessianSupported => true;
/// <summary>
/// Set observed data to fit.
@ -235,7 +235,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
throw new ArgumentNullException("initialGuess");
}
coefficients = initialGuess;
if (isFixed != null && isFixed.Count != initialGuess.Count)
{
throw new ArgumentException("The isFixed can't have different size from the initial guess.");
@ -342,11 +342,11 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
// approximated Hessian, H = J'WJ + ∑LRiHi ~ J'WJ near the minimum
hessianValue = jacobianValue.Transpose() * jacobianValue;
}
private Matrix<double> NumericalJacobian(Vector<double> parameters, Vector<double> currentValues, int accuracyOrder = 2)
{
{
const double sqrtEpsilon = 1.4901161193847656250E-8; // sqrt(machineEpsilon)
Matrix<double> derivertives = Matrix<double>.Build.Dense(NumberOfObservations, NumberOfParameters);
var d = 0.000003 * parameters.PointwiseAbs().PointwiseMaximum(sqrtEpsilon);

24
src/Numerics/Optimization/ObjectiveFunctions/ScalarObjectiveFunction.cs

@ -63,17 +63,17 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
return _secondDerivative.Value;
}
public double Point { get { return _point; } }
public double Value { get { return _value ?? SetValue(); } }
public double Derivative { get { return _derivative ?? SetDerivative(); } }
public double SecondDerivative { get { return _secondDerivative ?? SetSecondDerivative(); } }
public double Point => _point;
public double Value => _value ?? SetValue();
public double Derivative => _derivative ?? SetDerivative();
public double SecondDerivative => _secondDerivative ?? SetSecondDerivative();
}
internal class ScalarObjectiveFunction : IScalarObjectiveFunction
{
public Func<double, double> Objective { get; private set; }
public Func<double, double> Derivative { get; private set; }
public Func<double, double> SecondDerivative { get; private set; }
public Func<double, double> Objective { get; }
public Func<double, double> Derivative { get; }
public Func<double, double> SecondDerivative { get; }
public ScalarObjectiveFunction(Func<double, double> objective)
{
@ -96,15 +96,9 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
SecondDerivative = secondDerivative;
}
public bool IsDerivativeSupported
{
get { return Derivative != null; }
}
public bool IsDerivativeSupported => Derivative != null;
public bool IsSecondDerivativeSupported
{
get { return SecondDerivative != null; }
}
public bool IsSecondDerivativeSupported => SecondDerivative != null;
public IScalarObjectiveFunctionEvaluation Evaluate(double point)
{

22
src/Numerics/Optimization/ObjectiveFunctions/ScalarValueObjectiveFunction.cs

@ -42,35 +42,23 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public double Point { get; }
public double Value { get; }
public double Derivative
{
get { throw new NotSupportedException(); }
}
public double Derivative => throw new NotSupportedException();
public double SecondDerivative
{
get { throw new NotSupportedException(); }
}
public double SecondDerivative => throw new NotSupportedException();
}
internal class ScalarValueObjectiveFunction : IScalarObjectiveFunction
{
public Func<double, double> Objective { get; private set; }
public Func<double, double> Objective { get; }
public ScalarValueObjectiveFunction(Func<double, double> objective)
{
Objective = objective;
}
public bool IsDerivativeSupported
{
get { return false; }
}
public bool IsDerivativeSupported => false;
public bool IsSecondDerivativeSupported
{
get { return false; }
}
public bool IsSecondDerivativeSupported => false;
public IScalarObjectiveFunctionEvaluation Evaluate(double point)
{

20
src/Numerics/Optimization/ObjectiveFunctions/ValueObjectiveFunction.cs

@ -56,15 +56,9 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
};
}
public bool IsGradientSupported
{
get { return false; }
}
public bool IsGradientSupported => false;
public bool IsHessianSupported
{
get { return false; }
}
public bool IsHessianSupported => false;
public void EvaluateAt(Vector<double> point)
{
@ -75,14 +69,8 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public Vector<double> Point { get; private set; }
public double Value { get; private set; }
public Matrix<double> Hessian
{
get { throw new NotSupportedException(); }
}
public Matrix<double> Hessian => throw new NotSupportedException();
public Vector<double> Gradient
{
get { throw new NotSupportedException(); }
}
public Vector<double> Gradient => throw new NotSupportedException();
}
}

8
src/Numerics/Optimization/ScalarMinimizationResult.cs

@ -31,10 +31,10 @@ namespace MathNet.Numerics.Optimization
{
public class ScalarMinimizationResult
{
public double MinimizingPoint { get { return FunctionInfoAtMinimum.Point; } }
public IScalarObjectiveFunctionEvaluation FunctionInfoAtMinimum { get; private set; }
public int Iterations { get; private set; }
public ExitCondition ReasonForExit { get; private set; }
public double MinimizingPoint => FunctionInfoAtMinimum.Point;
public IScalarObjectiveFunctionEvaluation FunctionInfoAtMinimum { get; }
public int Iterations { get; }
public ExitCondition ReasonForExit { get; }
public ScalarMinimizationResult(IScalarObjectiveFunctionEvaluation functionInfo, int iterations, ExitCondition reasonForExit)
{

13
src/Numerics/Permutation.cs

@ -69,23 +69,14 @@ namespace MathNet.Numerics
/// <summary>
/// Gets the number of elements this permutation is over.
/// </summary>
public int Dimension
{
get { return _indices.Length; }
}
public int Dimension => _indices.Length;
/// <summary>
/// Computes where <paramref name="idx"/> permutes too.
/// </summary>
/// <param name="idx">The index to permute from.</param>
/// <returns>The index which is permuted to.</returns>
public int this[int idx]
{
get
{
return _indices[idx];
}
}
public int this[int idx] => _indices[idx];
/// <summary>
/// Computes the inverse of the permutation.

2
src/Numerics/Providers/Common/Cuda/SafeNativeMethods.cs

@ -49,7 +49,7 @@ namespace MathNet.Numerics.Providers.Common.Cuda
/// Name of the native DLL.
/// </summary>
const string _DllName = "MathNet.Numerics.CUDA.dll";
internal static string DllName { get { return _DllName; } }
internal static string DllName => _DllName;
[DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
internal static extern int query_capability(int capability);

Some files were not shown because too many files changed in this diff

Loading…
Cancel
Save