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); 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); 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); 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); 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 namespace MathNet.Numerics
{ {
using System; using System;
@ -156,3 +158,46 @@ namespace MathNet.Numerics
} }
} }
#endif #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 public float Real
{ {
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get { return _real; } get => _real;
} }
/// <summary> /// <summary>
@ -155,7 +155,7 @@ namespace MathNet.Numerics
public float Imaginary public float Imaginary
{ {
[TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")] [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
get { return _imag; } get => _imag;
} }
/// <summary> /// <summary>
@ -171,7 +171,7 @@ namespace MathNet.Numerics
{ {
// NOTE: the special case for negative real numbers fixes negative-zero value behavior. Do not remove. // 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")] [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> /// <summary>
@ -212,10 +212,7 @@ namespace MathNet.Numerics
/// Gets the squared magnitude (or squared absolute value) of a complex number. /// Gets the squared magnitude (or squared absolute value) of a complex number.
/// </summary> /// </summary>
/// <returns>The squared magnitude of the current instance.</returns> /// <returns>The squared magnitude of the current instance.</returns>
public float MagnitudeSquared public float MagnitudeSquared => _real * _real + _imag * _imag;
{
get { return (_real * _real) + (_imag * _imag); }
}
/// <summary> /// <summary>
/// Gets the unity of this complex (same argument, but on the unit circle; exp(I*arg)) /// 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> /// </summary>
public int Points public int Points
{ {
get { return _points; } get => _points;
set set
{ {
CalculateCoefficients(value); CalculateCoefficients(value);

15
src/Numerics/Differentiation/NumericalDerivative.cs

@ -72,7 +72,6 @@ namespace MathNet.Numerics.Differentiation
double _stepSize = Math.Pow(2, -10); double _stepSize = Math.Pow(2, -10);
double _epsilon = Precision.PositiveMachineEpsilon; double _epsilon = Precision.PositiveMachineEpsilon;
double _baseStepSize = Math.Pow(2, -26); double _baseStepSize = Math.Pow(2, -26);
StepType _stepType = StepType.Relative;
readonly FiniteDifferenceCoefficients _coefficients; readonly FiniteDifferenceCoefficients _coefficients;
/// <summary> /// <summary>
@ -110,7 +109,7 @@ namespace MathNet.Numerics.Differentiation
/// </remarks> /// </remarks>
public double StepSize public double StepSize
{ {
get { return _stepSize; } get => _stepSize;
set set
{ {
//Base 2 yields more accurate results... //Base 2 yields more accurate results...
@ -125,7 +124,7 @@ namespace MathNet.Numerics.Differentiation
/// </summary> /// </summary>
public double BaseStepSize public double BaseStepSize
{ {
get { return _baseStepSize; } get => _baseStepSize;
set set
{ {
//Base 2 yields more accurate results... //Base 2 yields more accurate results...
@ -140,7 +139,7 @@ namespace MathNet.Numerics.Differentiation
/// </summary> /// </summary>
public double Epsilon public double Epsilon
{ {
get { return _epsilon; } get => _epsilon;
set set
{ {
//Base 2 yields more accurate results... //Base 2 yields more accurate results...
@ -154,7 +153,7 @@ namespace MathNet.Numerics.Differentiation
/// </summary> /// </summary>
public int Center public int Center
{ {
get { return _center; } get => _center;
set set
{ {
if (value >= _points || value < 0) 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 /// 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. /// h is approximately equal to the square-root of machine accuracy, epsilon.
/// </summary> /// </summary>
public StepType StepType public StepType StepType { get; set; } = StepType.Relative;
{
get { return _stepType; }
set { _stepType = value; }
}
/// <summary> /// <summary>
/// Evaluates the derivative of equidistant points using the finite difference method. /// 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> /// <summary>
/// Number of function evaluations. /// Number of function evaluations.
/// </summary> /// </summary>
public int FunctionEvaluations public int FunctionEvaluations => _df.Evaluations;
{
get { return _df.Evaluations; }
}
private readonly NumericalDerivative _df; private readonly NumericalDerivative _df;

5
src/Numerics/Differentiation/NumericalJacobian.cs

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

4
src/Numerics/Distributions/SkewedGeneralizedError.cs

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

36
src/Numerics/Integration/GaussKronrodRule.cs

@ -48,46 +48,22 @@ namespace MathNet.Numerics.Integration
/// <summary> /// <summary>
/// Getter for the order. /// Getter for the order.
/// </summary> /// </summary>
public int Order public int Order => gaussKronrodPoint.Order;
{
get
{
return gaussKronrodPoint.Order;
}
}
/// <summary> /// <summary>
/// Getter that returns a clone of the array containing the Kronrod abscissas. /// Getter that returns a clone of the array containing the Kronrod abscissas.
/// </summary> /// </summary>
public double[] KronrodAbscissas public double[] KronrodAbscissas => gaussKronrodPoint.Abscissas.Clone() as double[];
{
get
{
return gaussKronrodPoint.Abscissas.Clone() as double[];
}
}
/// <summary> /// <summary>
/// Getter that returns a clone of the array containing the Kronrod weights. /// Getter that returns a clone of the array containing the Kronrod weights.
/// </summary> /// </summary>
public double[] KronrodWeights public double[] KronrodWeights => gaussKronrodPoint.Weights.Clone() as double[];
{
get
{
return gaussKronrodPoint.Weights.Clone() as double[];
}
}
/// <summary> /// <summary>
/// Getter that returns a clone of the array containing the Gauss weights. /// Getter that returns a clone of the array containing the Gauss weights.
/// </summary> /// </summary>
public double[] GaussWeights public double[] GaussWeights => gaussKronrodPoint.SecondWeights.Clone() as double[];
{
get
{
return gaussKronrodPoint.SecondWeights.Clone() as double[];
}
}
public GaussKronrodRule(int order) 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> /// <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) 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. // 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 // 2. quadgk.m, GNU Octave
@ -193,7 +169,7 @@ namespace MathNet.Numerics.Integration
/// <returns></returns> /// <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) 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. // 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 // 2. quadgk.m, GNU Octave

40
src/Numerics/Integration/GaussLegendreRule.cs

@ -64,13 +64,7 @@ namespace MathNet.Numerics.Integration
/// <summary> /// <summary>
/// Getter that returns a clone of the array containing the abscissas. /// Getter that returns a clone of the array containing the abscissas.
/// </summary> /// </summary>
public double[] Abscissas public double[] Abscissas => _gaussLegendrePoint.Abscissas.Clone() as double[];
{
get
{
return _gaussLegendrePoint.Abscissas.Clone() as double[];
}
}
/// <summary> /// <summary>
/// Getter for the ith weight. /// Getter for the ith weight.
@ -85,46 +79,22 @@ namespace MathNet.Numerics.Integration
/// <summary> /// <summary>
/// Getter that returns a clone of the array containing the weights. /// Getter that returns a clone of the array containing the weights.
/// </summary> /// </summary>
public double[] Weights public double[] Weights => _gaussLegendrePoint.Weights.Clone() as double[];
{
get
{
return _gaussLegendrePoint.Weights.Clone() as double[];
}
}
/// <summary> /// <summary>
/// Getter for the order. /// Getter for the order.
/// </summary> /// </summary>
public int Order public int Order => _gaussLegendrePoint.Order;
{
get
{
return _gaussLegendrePoint.Order;
}
}
/// <summary> /// <summary>
/// Getter for the InvervalBegin. /// Getter for the InvervalBegin.
/// </summary> /// </summary>
public double IntervalBegin public double IntervalBegin => _gaussLegendrePoint.IntervalBegin;
{
get
{
return _gaussLegendrePoint.IntervalBegin;
}
}
/// <summary> /// <summary>
/// Getter for the InvervalEnd. /// Getter for the InvervalEnd.
/// </summary> /// </summary>
public double IntervalEnd public double IntervalEnd => _gaussLegendrePoint.IntervalEnd;
{
get
{
return _gaussLegendrePoint.IntervalEnd;
}
}
/// <summary> /// <summary>
/// Approximates a definite integral using an Nth order Gauss-Legendre rule. /// 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> /// <summary>
/// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative). /// Gets a value indicating whether the algorithm supports differentiation (interpolated derivative).
/// </summary> /// </summary>
bool IInterpolation.SupportsDifferentiation bool IInterpolation.SupportsDifferentiation => false;
{
get { return false; }
}
/// <summary> /// <summary>
/// Gets a value indicating whether the algorithm supports integration (interpolated quadrature). /// Gets a value indicating whether the algorithm supports integration (interpolated quadrature).
/// </summary> /// </summary>
bool IInterpolation.SupportsIntegration bool IInterpolation.SupportsIntegration => false;
{
get { return false; }
}
/// <summary> /// <summary>
/// Interpolate at point t. /// Interpolate at point t.

10
src/Numerics/Interpolation/BulirschStoerRationalInterpolation.cs

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

10
src/Numerics/Interpolation/CubicSpline.cs

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

10
src/Numerics/Interpolation/LinearSpline.cs

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

10
src/Numerics/Interpolation/LogLinear.cs

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

10
src/Numerics/Interpolation/NevillePolynomialInterpolation.cs

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

10
src/Numerics/Interpolation/QuadraticSpline.cs

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

10
src/Numerics/Interpolation/StepInterpolation.cs

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

10
src/Numerics/Interpolation/TransformedInterpolation.cs

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

114
src/Numerics/LinearAlgebra/Builder.cs

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

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

@ -395,10 +395,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// Gets the matrix's data. /// Gets the matrix's data.
/// </summary> /// </summary>
/// <value>The matrix's data.</value> /// <value>The matrix's data.</value>
public Complex[] Values public Complex[] Values => _values;
{
get { return _values; }
}
/// <summary>Calculates the induced L1 norm of this matrix.</summary> /// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns> /// <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> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result) protected override void DoNegate(Matrix<Complex> result)
{ {
var denseResult = result as DenseMatrix; if (result is DenseMatrix denseResult)
if (denseResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values); LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values);
return; return;
@ -443,8 +439,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the conjugation.</param> /// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex> result) protected override void DoConjugate(Matrix<Complex> result)
{ {
var denseResult = result as DenseMatrix; if (result is DenseMatrix denseResult)
if (denseResult != null)
{ {
LinearAlgebraControl.Provider.ConjugateArray(_values, denseResult._values); LinearAlgebraControl.Provider.ConjugateArray(_values, denseResult._values);
return; return;
@ -487,17 +482,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result) protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result)
{ {
// dense + dense = dense // dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther && result.Storage is DenseColumnMajorMatrixStorage<Complex> denseResult)
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data);
return; return;
} }
// dense + diagonal = any // dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>; if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
if (diagonalOther != null)
{ {
Storage.CopyToUnchecked(result.Storage, ExistingData.Clear); Storage.CopyToUnchecked(result.Storage, ExistingData.Clear);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
@ -543,17 +535,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result) protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result)
{ {
// dense + dense = dense // dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther && result.Storage is DenseColumnMajorMatrixStorage<Complex> denseResult)
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data);
return; return;
} }
// dense + diagonal = matrix // dense + diagonal = matrix
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>; if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
if (diagonalOther != null)
{ {
CopyTo(result); CopyTo(result);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
@ -619,9 +608,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiply( LinearAlgebraControl.Provider.MatrixMultiply(
_values, _values,
@ -634,8 +621,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>; if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.ColumnCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose, Providers.LinearAlgebra.Transpose.DontTranspose,
@ -684,8 +668,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>; if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.RowCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoConjugateTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose, Providers.LinearAlgebra.Transpose.DontTranspose,
@ -734,8 +715,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>; if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var conjugateDiagonal = new Complex[diagonal.Length]; var conjugateDiagonal = new Complex[diagonal.Length];
@ -771,9 +751,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Vector<Complex> rightSide, Vector<Complex> result) protected override void DoTransposeThisAndMultiply(Vector<Complex> rightSide, Vector<Complex> result)
{ {
var denseRight = rightSide as DenseVector; if (rightSide is DenseVector denseRight && result is DenseVector denseResult)
var denseResult = result as DenseVector;
if (denseRight != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose, Providers.LinearAlgebra.Transpose.Transpose,
@ -800,9 +778,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Vector<Complex> rightSide, Vector<Complex> result) protected override void DoConjugateTransposeThisAndMultiply(Vector<Complex> rightSide, Vector<Complex> result)
{ {
var denseRight = rightSide as DenseVector; if (rightSide is DenseVector denseRight && result is DenseVector denseResult)
var denseResult = result as DenseVector;
if (denseRight != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.ConjugateTranspose, Providers.LinearAlgebra.Transpose.ConjugateTranspose,
@ -829,9 +805,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose, Providers.LinearAlgebra.Transpose.Transpose,
@ -848,8 +822,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>; if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.ConjugateTranspose, Providers.LinearAlgebra.Transpose.ConjugateTranspose,
@ -899,8 +870,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>; if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount); 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. /// Gets the vector's data.
/// </summary> /// </summary>
/// <value>The vector's data.</value> /// <value>The vector's data.</value>
public Complex[] Values public Complex[] Values => _values;
{
get { return _values; }
}
/// <summary> /// <summary>
/// Returns a reference to the internal data structure. /// 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> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result) protected override void DoNegate(Matrix<Complex> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data); LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data);
return; return;
@ -212,8 +211,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the conjugation.</param> /// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex> result) protected override void DoConjugate(Matrix<Complex> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ConjugateArray(_data, diagResult._data); LinearAlgebraControl.Provider.ConjugateArray(_data, diagResult._data);
return; return;
@ -235,9 +233,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result) protected override void DoAdd(Matrix<Complex> other, Matrix<Complex> result)
{ {
// diagonal + diagonal = diagonal // diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
{ {
LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data); LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data);
return; return;
@ -259,9 +255,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result) protected override void DoSubtract(Matrix<Complex> other, Matrix<Complex> result)
{ {
// diagonal - diagonal = diagonal // diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
{ {
LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data); LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data);
return; return;
@ -320,9 +314,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (d == ColumnCount) if (d == ColumnCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<Complex>; if (rightSide.Storage is DenseVectorStorage<Complex> denseOther && result.Storage is DenseVectorStorage<Complex> denseResult)
var denseResult = result.Storage as DenseVectorStorage<Complex>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return; return;
@ -342,9 +334,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex[diagonalResult._data.Length]; var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length]; var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -354,8 +344,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -395,9 +384,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex[diagonalResult._data.Length]; var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length]; var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -407,8 +394,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -439,9 +425,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoConjugateTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex[diagonalResult._data.Length]; var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length]; var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -453,8 +437,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -485,9 +468,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex[diagonalResult._data.Length]; var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length]; var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -497,8 +478,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -538,9 +518,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result) protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex[diagonalResult._data.Length]; var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length]; var otherDataCopy = new Complex[diagonalResult._data.Length];
@ -552,8 +530,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var conjugateDiagonal = new Complex[_data.Length]; var conjugateDiagonal = new Complex[_data.Length];
@ -598,9 +575,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (d == RowCount) if (d == RowCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<Complex>; if (rightSide.Storage is DenseVectorStorage<Complex> denseOther && result.Storage is DenseVectorStorage<Complex> denseResult)
var denseResult = result.Storage as DenseVectorStorage<Complex>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return; return;
@ -628,9 +603,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (d == RowCount) if (d == RowCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<Complex>; if (rightSide.Storage is DenseVectorStorage<Complex> denseOther && result.Storage is DenseVectorStorage<Complex> denseResult)
var denseResult = result.Storage as DenseVectorStorage<Complex>;
if (denseOther != null && denseResult != null)
{ {
// TODO: merge/MulByConj // TODO: merge/MulByConj
LinearAlgebraControl.Provider.ConjugateArray(_data, denseResult.Data); LinearAlgebraControl.Provider.ConjugateArray(_data, denseResult.Data);
@ -658,8 +631,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(1.0/divisor, _data, diagResult._data); LinearAlgebraControl.Provider.ScaleArray(1.0/divisor, _data, diagResult._data);
return; return;
@ -679,8 +651,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <param name="result">The matrix to store the result of the division.</param> /// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(Complex dividend, Matrix<Complex> result) protected override void DoDivideByThis(Complex dividend, Matrix<Complex> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
var resultData = diagResult._data; var resultData = diagResult._data;
CommonParallel.For(0, _data.Length, 4096, (a, b) => 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}"/>. /// Gets the two norm of the <see cref="Matrix{T}"/>.
/// </summary> /// </summary>
/// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns> /// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns>
public override double L2Norm public override double L2Norm => S[0].Magnitude;
{
get
{
return S[0].Magnitude;
}
}
/// <summary> /// <summary>
/// Gets the condition number <b>max(S) / min(S)</b> /// 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> /// </summary>
public int NumberOfBiCgStabSteps public int NumberOfBiCgStabSteps
{ {
get { return _numberOfBiCgStabSteps; } get => _numberOfBiCgStabSteps;
set set
{ {
@ -101,7 +101,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
/// </summary> /// </summary>
public int NumberOfGpBiCgSteps public int NumberOfGpBiCgSteps
{ {
get { return _numberOfGpbiCgSteps; } get => _numberOfGpbiCgSteps;
set 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> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel public double FillLevel
{ {
get { return _fillLevel; } get => _fillLevel;
set set
{ {
if (value < 0) if (value < 0)
@ -193,7 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance public double DropTolerance
{ {
get { return _dropTolerance; } get => _dropTolerance;
set set
{ {
if (value < 0) if (value < 0)
@ -226,7 +226,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance public double PivotTolerance
{ {
get { return _pivotTolerance; } get => _pivotTolerance;
set set
{ {
if (value < 0) if (value < 0)

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

@ -89,7 +89,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
public int NumberOfStartingVectors public int NumberOfStartingVectors
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _numberOfStartingVectors; } get => _numberOfStartingVectors;
[DebuggerStepThrough] [DebuggerStepThrough]
set set
@ -118,7 +118,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex.Solvers
public IList<Vector<Complex>> StartingVectors public IList<Vector<Complex>> StartingVectors
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _startingVectors; } get => _startingVectors;
[DebuggerStepThrough] [DebuggerStepThrough]
set 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. /// Gets the number of non zero elements in the matrix.
/// </summary> /// </summary>
/// <value>The number of non zero elements.</value> /// <value>The number of non zero elements.</value>
public int NonZerosCount public int NonZerosCount => _storage.ValueCount;
{
get { return _storage.ValueCount; }
}
/// <summary> /// <summary>
/// Create a new sparse matrix straight from an initialized matrix storage instance. /// Create a new sparse matrix straight from an initialized matrix storage instance.
@ -890,8 +887,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex>; if (other.Storage is DiagonalMatrixStorage<Complex> diagonalOther && sparseResult != null)
if (diagonalOther != null && sparseResult != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
if (other.ColumnCount == other.RowCount) if (other.ColumnCount == other.RowCount)
@ -912,8 +908,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
var rowPointers = _storage.RowPointers; var rowPointers = _storage.RowPointers;
var columnIndices = _storage.ColumnIndices; var columnIndices = _storage.ColumnIndices;
var values = _storage.Values; var values = _storage.Values;
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex> denseOther)
if (denseOther != null)
{ {
// in this case we can directly address the underlying data-array // in this case we can directly address the underlying data-array
for (var row = 0; row < RowCount; row++) for (var row = 0; row < RowCount; row++)
@ -1547,7 +1542,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public override string ToTypeString() 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. /// Gets the number of non zero elements in the vector.
/// </summary> /// </summary>
/// <value>The number of non zero elements.</value> /// <value>The number of non zero elements.</value>
public int NonZerosCount public int NonZerosCount => _storage.ValueCount;
{
get { return _storage.ValueCount; }
}
/// <summary> /// <summary>
/// Create a new sparse vector straight from an initialized vector storage instance. /// 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> /// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex> result) protected override void DoConjugate(Vector<Complex> result)
{ {
var sparseResult = result as SparseVector; if (result is SparseVector sparseResult)
if (sparseResult != null)
{ {
if (!ReferenceEquals(this, result)) if (!ReferenceEquals(this, result))
{ {
@ -931,7 +927,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
public override string ToTypeString() 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. /// Gets the matrix's data.
/// </summary> /// </summary>
/// <value>The matrix's data.</value> /// <value>The matrix's data.</value>
public Complex32[] Values public Complex32[] Values => _values;
{
get { return _values; }
}
/// <summary>Calculates the induced L1 norm of this matrix.</summary> /// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns> /// <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> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result) protected override void DoNegate(Matrix<Complex32> result)
{ {
var denseResult = result as DenseMatrix; if (result is DenseMatrix denseResult)
if (denseResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values); LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values);
return; return;
@ -443,8 +439,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the conjugation.</param> /// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex32> result) protected override void DoConjugate(Matrix<Complex32> result)
{ {
var denseResult = result as DenseMatrix; if (result is DenseMatrix denseResult)
if (denseResult != null)
{ {
LinearAlgebraControl.Provider.ConjugateArray(_values, denseResult._values); LinearAlgebraControl.Provider.ConjugateArray(_values, denseResult._values);
return; return;
@ -487,17 +482,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
// dense + dense = dense // dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther && result.Storage is DenseColumnMajorMatrixStorage<Complex32> denseResult)
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data);
return; return;
} }
// dense + diagonal = any // dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>; if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
if (diagonalOther != null)
{ {
Storage.CopyToUnchecked(result.Storage, ExistingData.Clear); Storage.CopyToUnchecked(result.Storage, ExistingData.Clear);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
@ -543,17 +535,14 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
// dense + dense = dense // dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther && result.Storage is DenseColumnMajorMatrixStorage<Complex32> denseResult)
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data);
return; return;
} }
// dense + diagonal = matrix // dense + diagonal = matrix
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>; if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
if (diagonalOther != null)
{ {
CopyTo(result); CopyTo(result);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
@ -619,9 +608,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiply( LinearAlgebraControl.Provider.MatrixMultiply(
_values, _values,
@ -634,8 +621,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>; if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.ColumnCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose, Providers.LinearAlgebra.Transpose.DontTranspose,
@ -684,8 +668,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>; if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.RowCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoConjugateTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose, Providers.LinearAlgebra.Transpose.DontTranspose,
@ -734,8 +715,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>; if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var conjugateDiagonal = new Complex32[diagonal.Length]; var conjugateDiagonal = new Complex32[diagonal.Length];
@ -802,9 +782,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Vector<Complex32> rightSide, Vector<Complex32> result) protected override void DoConjugateTransposeThisAndMultiply(Vector<Complex32> rightSide, Vector<Complex32> result)
{ {
var denseRight = rightSide as DenseVector; if (rightSide is DenseVector denseRight && result is DenseVector denseResult)
var denseResult = result as DenseVector;
if (denseRight != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.ConjugateTranspose, Providers.LinearAlgebra.Transpose.ConjugateTranspose,
@ -831,9 +809,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose, Providers.LinearAlgebra.Transpose.Transpose,
@ -850,8 +826,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>; if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.ConjugateTranspose, Providers.LinearAlgebra.Transpose.ConjugateTranspose,
@ -901,8 +874,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>; if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount); 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. /// Gets the vector's data.
/// </summary> /// </summary>
/// <value>The vector's data.</value> /// <value>The vector's data.</value>
public Complex32[] Values public Complex32[] Values => _values;
{
get { return _values; }
}
/// <summary> /// <summary>
/// Returns a reference to the internal data structure. /// 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> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result) protected override void DoNegate(Matrix<Complex32> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data); LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data);
return; return;
@ -212,8 +211,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the conjugation.</param> /// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex32> result) protected override void DoConjugate(Matrix<Complex32> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ConjugateArray(_data, diagResult._data); LinearAlgebraControl.Provider.ConjugateArray(_data, diagResult._data);
return; return;
@ -235,9 +233,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoAdd(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
// diagonal + diagonal = diagonal // diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
{ {
LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data); LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data);
return; return;
@ -259,9 +255,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoSubtract(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
// diagonal - diagonal = diagonal // diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
{ {
LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data); LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data);
return; return;
@ -319,9 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (d == ColumnCount) if (d == ColumnCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<Complex32>; if (rightSide.Storage is DenseVectorStorage<Complex32> denseOther && result.Storage is DenseVectorStorage<Complex32> denseResult)
var denseResult = result.Storage as DenseVectorStorage<Complex32>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return; return;
@ -341,9 +333,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex32[diagonalResult._data.Length]; var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length]; var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -353,8 +343,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -394,9 +383,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex32[diagonalResult._data.Length]; var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length]; var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -406,8 +393,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -438,9 +424,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoConjugateTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex32[diagonalResult._data.Length]; var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length]; var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -452,8 +436,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -484,9 +467,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex32[diagonalResult._data.Length]; var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length]; var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -496,8 +477,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -537,9 +517,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result) protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new Complex32[diagonalResult._data.Length]; var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length]; var otherDataCopy = new Complex32[diagonalResult._data.Length];
@ -551,8 +529,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var conjugateDiagonal = new Complex32[_data.Length]; var conjugateDiagonal = new Complex32[_data.Length];
@ -597,9 +574,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (d == RowCount) if (d == RowCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<Complex32>; if (rightSide.Storage is DenseVectorStorage<Complex32> denseOther && result.Storage is DenseVectorStorage<Complex32> denseResult)
var denseResult = result.Storage as DenseVectorStorage<Complex32>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return; return;
@ -627,9 +602,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (d == RowCount) if (d == RowCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<Complex32>; if (rightSide.Storage is DenseVectorStorage<Complex32> denseOther && result.Storage is DenseVectorStorage<Complex32> denseResult)
var denseResult = result.Storage as DenseVectorStorage<Complex32>;
if (denseOther != null && denseResult != null)
{ {
// TODO: merge/MulByConj // TODO: merge/MulByConj
LinearAlgebraControl.Provider.ConjugateArray(_data, denseResult.Data); LinearAlgebraControl.Provider.ConjugateArray(_data, denseResult.Data);
@ -657,8 +630,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(1.0f/divisor, _data, diagResult._data); LinearAlgebraControl.Provider.ScaleArray(1.0f/divisor, _data, diagResult._data);
return; return;
@ -678,8 +650,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <param name="result">The matrix to store the result of the division.</param> /// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(Complex32 dividend, Matrix<Complex32> result) protected override void DoDivideByThis(Complex32 dividend, Matrix<Complex32> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
var resultData = diagResult._data; var resultData = diagResult._data;
CommonParallel.For(0, _data.Length, 4096, (a, b) => 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}"/>. /// Gets the two norm of the <see cref="Matrix{T}"/>.
/// </summary> /// </summary>
/// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns> /// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns>
public override double L2Norm public override double L2Norm => S[0].Magnitude;
{
get
{
return S[0].Magnitude;
}
}
/// <summary> /// <summary>
/// Gets the condition number <b>max(S) / min(S)</b> /// 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> /// </summary>
public int NumberOfBiCgStabSteps public int NumberOfBiCgStabSteps
{ {
get { return _numberOfBiCgStabSteps; } get => _numberOfBiCgStabSteps;
set set
{ {
@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
/// </summary> /// </summary>
public int NumberOfGpBiCgSteps public int NumberOfGpBiCgSteps
{ {
get { return _numberOfGpbiCgSteps; } get => _numberOfGpbiCgSteps;
set 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> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel public double FillLevel
{ {
get { return _fillLevel; } get => _fillLevel;
set set
{ {
if (value < 0) if (value < 0)
@ -193,7 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance public double DropTolerance
{ {
get { return _dropTolerance; } get => _dropTolerance;
set set
{ {
if (value < 0) if (value < 0)
@ -226,7 +226,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance public double PivotTolerance
{ {
get { return _pivotTolerance; } get => _pivotTolerance;
set set
{ {
if (value < 0) if (value < 0)

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

@ -87,7 +87,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
public int NumberOfStartingVectors public int NumberOfStartingVectors
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _numberOfStartingVectors; } get => _numberOfStartingVectors;
[DebuggerStepThrough] [DebuggerStepThrough]
set set
@ -116,7 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32.Solvers
public IList<Vector<Numerics.Complex32>> StartingVectors public IList<Vector<Numerics.Complex32>> StartingVectors
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _startingVectors; } get => _startingVectors;
[DebuggerStepThrough] [DebuggerStepThrough]
set 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. /// Gets the number of non zero elements in the matrix.
/// </summary> /// </summary>
/// <value>The number of non zero elements.</value> /// <value>The number of non zero elements.</value>
public int NonZerosCount public int NonZerosCount => _storage.ValueCount;
{
get { return _storage.ValueCount; }
}
/// <summary> /// <summary>
/// Create a new sparse matrix straight from an initialized matrix storage instance. /// Create a new sparse matrix straight from an initialized matrix storage instance.
@ -889,8 +886,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<Complex32>; if (other.Storage is DiagonalMatrixStorage<Complex32> diagonalOther && sparseResult != null)
if (diagonalOther != null && sparseResult != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
if (other.ColumnCount == other.RowCount) if (other.ColumnCount == other.RowCount)
@ -911,8 +907,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
var rowPointers = _storage.RowPointers; var rowPointers = _storage.RowPointers;
var columnIndices = _storage.ColumnIndices; var columnIndices = _storage.ColumnIndices;
var values = _storage.Values; var values = _storage.Values;
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>; if (other.Storage is DenseColumnMajorMatrixStorage<Complex32> denseOther)
if (denseOther != null)
{ {
// in this case we can directly address the underlying data-array // in this case we can directly address the underlying data-array
for (var row = 0; row < RowCount; row++) 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. /// Gets the number of non zero elements in the vector.
/// </summary> /// </summary>
/// <value>The number of non zero elements.</value> /// <value>The number of non zero elements.</value>
public int NonZerosCount public int NonZerosCount => _storage.ValueCount;
{
get { return _storage.ValueCount; }
}
/// <summary> /// <summary>
/// Create a new sparse vector straight from an initialized vector storage instance. /// 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> /// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex32> result) protected override void DoConjugate(Vector<Complex32> result)
{ {
var sparseResult = result as SparseVector; if (result is SparseVector sparseResult)
if (sparseResult != null)
{ {
if (!ReferenceEquals(this, result)) if (!ReferenceEquals(this, result))
{ {
@ -931,7 +927,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
public override string ToTypeString() 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. /// Gets the matrix's data.
/// </summary> /// </summary>
/// <value>The matrix's data.</value> /// <value>The matrix's data.</value>
public double[] Values public double[] Values => _values;
{
get { return _values; }
}
/// <summary>Calculates the induced L1 norm of this matrix.</summary> /// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns> /// <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> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result) protected override void DoNegate(Matrix<double> result)
{ {
var denseResult = result as DenseMatrix; if (result is DenseMatrix denseResult)
if (denseResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values); LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values);
return; return;
@ -469,17 +465,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
protected override void DoAdd(Matrix<double> other, Matrix<double> result) protected override void DoAdd(Matrix<double> other, Matrix<double> result)
{ {
// dense + dense = dense // dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>; if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther && result.Storage is DenseColumnMajorMatrixStorage<double> denseResult)
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<double>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data);
return; return;
} }
// dense + diagonal = any // dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>; if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
if (diagonalOther != null)
{ {
Storage.CopyToUnchecked(result.Storage, ExistingData.Clear); Storage.CopyToUnchecked(result.Storage, ExistingData.Clear);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
@ -525,17 +518,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
protected override void DoSubtract(Matrix<double> other, Matrix<double> result) protected override void DoSubtract(Matrix<double> other, Matrix<double> result)
{ {
// dense + dense = dense // dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>; if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther && result.Storage is DenseColumnMajorMatrixStorage<double> denseResult)
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<double>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data);
return; return;
} }
// dense + diagonal = matrix // dense + diagonal = matrix
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>; if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
if (diagonalOther != null)
{ {
CopyTo(result); CopyTo(result);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
@ -601,9 +591,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<double> other, Matrix<double> result) protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiply( LinearAlgebraControl.Provider.MatrixMultiply(
_values, _values,
@ -616,8 +604,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>; if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.ColumnCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<double> other, Matrix<double> result) protected override void DoTransposeAndMultiply(Matrix<double> other, Matrix<double> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose, Providers.LinearAlgebra.Transpose.DontTranspose,
@ -666,8 +651,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>; if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.RowCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<double> other, Matrix<double> result) protected override void DoTransposeThisAndMultiply(Matrix<double> other, Matrix<double> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose, Providers.LinearAlgebra.Transpose.Transpose,
@ -747,8 +729,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>; if (other.Storage is DiagonalMatrixStorage<double> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount); 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. /// Gets the vector's data.
/// </summary> /// </summary>
/// <value>The vector's data.</value> /// <value>The vector's data.</value>
public double[] Values public double[] Values => _values;
{
get { return _values; }
}
/// <summary> /// <summary>
/// Returns a reference to the internal data structure. /// 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> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result) protected override void DoNegate(Matrix<double> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data); LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data);
return; return;
@ -213,9 +212,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
protected override void DoAdd(Matrix<double> other, Matrix<double> result) protected override void DoAdd(Matrix<double> other, Matrix<double> result)
{ {
// diagonal + diagonal = diagonal // diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
{ {
LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data); LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data);
return; return;
@ -237,9 +234,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
protected override void DoSubtract(Matrix<double> other, Matrix<double> result) protected override void DoSubtract(Matrix<double> other, Matrix<double> result)
{ {
// diagonal - diagonal = diagonal // diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
{ {
LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data); LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data);
return; return;
@ -298,9 +293,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (d == ColumnCount) if (d == ColumnCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<double>; if (rightSide.Storage is DenseVectorStorage<double> denseOther && result.Storage is DenseVectorStorage<double> denseResult)
var denseResult = result.Storage as DenseVectorStorage<double>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return; return;
@ -320,9 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<double> other, Matrix<double> result) protected override void DoMultiply(Matrix<double> other, Matrix<double> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new double[diagonalResult._data.Length]; var thisDataCopy = new double[diagonalResult._data.Length];
var otherDataCopy = new double[diagonalResult._data.Length]; var otherDataCopy = new double[diagonalResult._data.Length];
@ -332,8 +323,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>; if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -373,9 +363,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<double> other, Matrix<double> result) protected override void DoTransposeAndMultiply(Matrix<double> other, Matrix<double> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new double[diagonalResult._data.Length]; var thisDataCopy = new double[diagonalResult._data.Length];
var otherDataCopy = new double[diagonalResult._data.Length]; var otherDataCopy = new double[diagonalResult._data.Length];
@ -385,8 +373,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>; if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -417,9 +404,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<double> other, Matrix<double> result) protected override void DoTransposeThisAndMultiply(Matrix<double> other, Matrix<double> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new double[diagonalResult._data.Length]; var thisDataCopy = new double[diagonalResult._data.Length];
var otherDataCopy = new double[diagonalResult._data.Length]; var otherDataCopy = new double[diagonalResult._data.Length];
@ -429,8 +414,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>; if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -478,9 +462,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (d == RowCount) if (d == RowCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<double>; if (rightSide.Storage is DenseVectorStorage<double> denseOther && result.Storage is DenseVectorStorage<double> denseResult)
var denseResult = result.Storage as DenseVectorStorage<double>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return; return;
@ -506,8 +488,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(1.0/divisor, _data, diagResult._data); LinearAlgebraControl.Provider.ScaleArray(1.0/divisor, _data, diagResult._data);
return; return;
@ -527,8 +508,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="result">The matrix to store the result of the division.</param> /// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(double dividend, Matrix<double> result) protected override void DoDivideByThis(double dividend, Matrix<double> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
var resultData = diagResult._data; var resultData = diagResult._data;
CommonParallel.For(0, _data.Length, 4096, (a, b) => 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}"/>. /// Gets the two norm of the <see cref="Matrix{T}"/>.
/// </summary> /// </summary>
/// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns> /// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns>
public override double L2Norm public override double L2Norm => Math.Abs(S[0]);
{
get
{
return Math.Abs(S[0]);
}
}
/// <summary> /// <summary>
/// Gets the condition number <b>max(S) / min(S)</b> /// 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> /// </summary>
public int NumberOfBiCgStabSteps public int NumberOfBiCgStabSteps
{ {
get get => _numberOfBiCgStabSteps;
{
return _numberOfBiCgStabSteps;
}
set set
{ {
@ -102,10 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
/// </summary> /// </summary>
public int NumberOfGpBiCgSteps public int NumberOfGpBiCgSteps
{ {
get get => _numberOfGpbiCgSteps;
{
return _numberOfGpbiCgSteps;
}
set 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> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel public double FillLevel
{ {
get { return _fillLevel; } get => _fillLevel;
set set
{ {
if (value < 0) if (value < 0)
@ -191,7 +191,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance public double DropTolerance
{ {
get { return _dropTolerance; } get => _dropTolerance;
set set
{ {
if (value < 0) if (value < 0)
@ -224,7 +224,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance public double PivotTolerance
{ {
get { return _pivotTolerance; } get => _pivotTolerance;
set set
{ {
if (value < 0) if (value < 0)

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

@ -87,10 +87,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
public int NumberOfStartingVectors public int NumberOfStartingVectors
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get get => _numberOfStartingVectors;
{
return _numberOfStartingVectors;
}
[DebuggerStepThrough] [DebuggerStepThrough]
set set
@ -119,10 +116,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double.Solvers
public IList<Vector<double>> StartingVectors public IList<Vector<double>> StartingVectors
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get get => _startingVectors;
{
return _startingVectors;
}
[DebuggerStepThrough] [DebuggerStepThrough]
set 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. /// Gets the number of non zero elements in the matrix.
/// </summary> /// </summary>
/// <value>The number of non zero elements.</value> /// <value>The number of non zero elements.</value>
public int NonZerosCount public int NonZerosCount => _storage.ValueCount;
{
get { return _storage.ValueCount; }
}
/// <summary> /// <summary>
/// Create a new sparse matrix straight from an initialized matrix storage instance. /// Create a new sparse matrix straight from an initialized matrix storage instance.
@ -890,8 +887,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<double>; if (other.Storage is DiagonalMatrixStorage<double> diagonalOther && sparseResult != null)
if (diagonalOther != null && sparseResult != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
if (other.ColumnCount == other.RowCount) if (other.ColumnCount == other.RowCount)
@ -911,8 +907,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var columnIndices = _storage.ColumnIndices; var columnIndices = _storage.ColumnIndices;
var values = _storage.Values; var values = _storage.Values;
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<double>; if (other.Storage is DenseColumnMajorMatrixStorage<double> denseOther)
if (denseOther != null)
{ {
// in this case we can directly address the underlying data-array // in this case we can directly address the underlying data-array
for (var row = 0; row < RowCount; row++) for (var row = 0; row < RowCount; row++)
@ -1561,7 +1556,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override string ToTypeString() 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. /// Gets the number of non zero elements in the vector.
/// </summary> /// </summary>
/// <value>The number of non zero elements.</value> /// <value>The number of non zero elements.</value>
public int NonZerosCount public int NonZerosCount => _storage.ValueCount;
{
get { return _storage.ValueCount; }
}
/// <summary> /// <summary>
/// Create a new sparse vector straight from an initialized vector storage instance. /// 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() 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> /// <summary>
/// Gets the lower triangular factor. /// Gets the lower triangular factor.
/// </summary> /// </summary>
public Matrix<T> L public Matrix<T> L => _lazyL.Value;
{
get { return _lazyL.Value; }
}
/// <summary> /// <summary>
/// Gets the upper triangular factor. /// Gets the upper triangular factor.
/// </summary> /// </summary>
public Matrix<T> U public Matrix<T> U => _lazyU.Value;
{
get { return _lazyU.Value; }
}
/// <summary> /// <summary>
/// Gets the permutation applied to LU factorization. /// Gets the permutation applied to LU factorization.
/// </summary> /// </summary>
public Permutation P public Permutation P => _lazyP.Value;
{
get { return _lazyP.Value; }
}
/// <summary> /// <summary>
/// Gets the determinant of the matrix for which the LU factorization was computed. /// 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> /// <summary>
/// Gets or sets orthogonal Q matrix /// Gets or sets orthogonal Q matrix
/// </summary> /// </summary>
public Matrix<T> Q { get; private set; } public Matrix<T> Q { get; }
/// <summary> /// <summary>
/// Gets the upper triangular factor R. /// Gets the upper triangular factor R.
/// </summary> /// </summary>
public Matrix<T> R public Matrix<T> R => _lazyR.Value;
{
get { return _lazyR.Value; }
}
/// <summary> /// <summary>
/// Gets the absolute determinant value of the matrix for which the QR matrix was computed. /// 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> /// <summary>
/// Gets the singular values (Σ) of matrix in ascending value. /// Gets the singular values (Σ) of matrix in ascending value.
/// </summary> /// </summary>
public Vector<T> S { get; private set; } public Vector<T> S { get; }
/// <summary> /// <summary>
/// Gets the left singular vectors (U - m-by-m unitary matrix) /// Gets the left singular vectors (U - m-by-m unitary matrix)
/// </summary> /// </summary>
public Matrix<T> U { get; private set; } public Matrix<T> U { get; }
/// <summary> /// <summary>
/// Gets the transpose right singular vectors (transpose of V, an n-by-n unitary matrix) /// Gets the transpose right singular vectors (transpose of V, an n-by-n unitary matrix)
/// </summary> /// </summary>
public Matrix<T> VT { get; private set; } public Matrix<T> VT { get; }
/// <summary> /// <summary>
/// Returns the singular values as a diagonal <see cref="Matrix{T}"/>. /// Returns the singular values as a diagonal <see cref="Matrix{T}"/>.
/// </summary> /// </summary>
/// <returns>The singular values as a diagonal <see cref="Matrix{T}"/>.</returns> /// <returns>The singular values as a diagonal <see cref="Matrix{T}"/>.</returns>
public Matrix<T> W public Matrix<T> W => _lazyW.Value;
{
get { return _lazyW.Value; }
}
/// <summary> /// <summary>
/// Gets the effective numerical matrix rank. /// Gets the effective numerical matrix rank.

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

@ -62,8 +62,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </returns> /// </returns>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
var other = obj as Matrix<T>; return obj is Matrix<T> other && Storage.Equals(other.Storage);
return other != null && Storage.Equals(other.Storage);
} }
/// <summary> /// <summary>
@ -97,7 +96,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </summary> /// </summary>
public virtual string ToTypeString() 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> /// <summary>

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

@ -393,10 +393,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Gets the matrix's data. /// Gets the matrix's data.
/// </summary> /// </summary>
/// <value>The matrix's data.</value> /// <value>The matrix's data.</value>
public float[] Values public float[] Values => _values;
{
get { return _values; }
}
/// <summary>Calculates the induced L1 norm of this matrix.</summary> /// <summary>Calculates the induced L1 norm of this matrix.</summary>
/// <returns>The maximum absolute column sum of the matrix.</returns> /// <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> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result) protected override void DoNegate(Matrix<float> result)
{ {
var denseResult = result as DenseMatrix; if (result is DenseMatrix denseResult)
if (denseResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values); LinearAlgebraControl.Provider.ScaleArray(-1, _values, denseResult._values);
return; return;
@ -469,17 +465,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
protected override void DoAdd(Matrix<float> other, Matrix<float> result) protected override void DoAdd(Matrix<float> other, Matrix<float> result)
{ {
// dense + dense = dense // dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>; if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther && result.Storage is DenseColumnMajorMatrixStorage<float> denseResult)
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<float>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.AddArrays(_values, denseOther.Data, denseResult.Data);
return; return;
} }
// dense + diagonal = any // dense + diagonal = any
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>; if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
if (diagonalOther != null)
{ {
Storage.CopyToUnchecked(result.Storage, ExistingData.Clear); Storage.CopyToUnchecked(result.Storage, ExistingData.Clear);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
@ -525,17 +518,14 @@ namespace MathNet.Numerics.LinearAlgebra.Single
protected override void DoSubtract(Matrix<float> other, Matrix<float> result) protected override void DoSubtract(Matrix<float> other, Matrix<float> result)
{ {
// dense + dense = dense // dense + dense = dense
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>; if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther && result.Storage is DenseColumnMajorMatrixStorage<float> denseResult)
var denseResult = result.Storage as DenseColumnMajorMatrixStorage<float>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.SubtractArrays(_values, denseOther.Data, denseResult.Data);
return; return;
} }
// dense + diagonal = matrix // dense + diagonal = matrix
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>; if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
if (diagonalOther != null)
{ {
CopyTo(result); CopyTo(result);
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
@ -601,9 +591,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<float> other, Matrix<float> result) protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiply( LinearAlgebraControl.Provider.MatrixMultiply(
_values, _values,
@ -616,8 +604,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>; if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.ColumnCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<float> other, Matrix<float> result) protected override void DoTransposeAndMultiply(Matrix<float> other, Matrix<float> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.DontTranspose, Providers.LinearAlgebra.Transpose.DontTranspose,
@ -666,8 +651,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>; if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(ColumnCount, other.RowCount); 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> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Vector<float> rightSide, Vector<float> result) protected override void DoTransposeThisAndMultiply(Vector<float> rightSide, Vector<float> result)
{ {
var denseRight = rightSide as DenseVector; if (rightSide is DenseVector denseRight && result is DenseVector denseResult)
var denseResult = result as DenseVector;
if (denseRight == null || denseResult == null)
{
base.DoTransposeThisAndMultiply(rightSide, result);
}
else
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose, Providers.LinearAlgebra.Transpose.Transpose,
@ -719,6 +696,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single
0.0f, 0.0f,
denseResult.Values); denseResult.Values);
} }
else
{
base.DoTransposeThisAndMultiply(rightSide, result);
}
} }
/// <summary> /// <summary>
@ -728,9 +709,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<float> other, Matrix<float> result) protected override void DoTransposeThisAndMultiply(Matrix<float> other, Matrix<float> result)
{ {
var denseOther = other as DenseMatrix; if (other is DenseMatrix denseOther && result is DenseMatrix denseResult)
var denseResult = result as DenseMatrix;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate( LinearAlgebraControl.Provider.MatrixMultiplyWithUpdate(
Providers.LinearAlgebra.Transpose.Transpose, Providers.LinearAlgebra.Transpose.Transpose,
@ -747,8 +726,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>; if (other.Storage is DiagonalMatrixStorage<float> diagonalOther)
if (diagonalOther != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
var d = Math.Min(RowCount, other.ColumnCount); 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. /// Gets the vector's data.
/// </summary> /// </summary>
/// <value>The vector's data.</value> /// <value>The vector's data.</value>
public float[] Values public float[] Values => _values;
{
get { return _values; }
}
/// <summary> /// <summary>
/// Returns a reference to the internal data structure. /// 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> /// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result) protected override void DoNegate(Matrix<float> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data); LinearAlgebraControl.Provider.ScaleArray(-1, _data, diagResult._data);
return; return;
@ -213,9 +212,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
protected override void DoAdd(Matrix<float> other, Matrix<float> result) protected override void DoAdd(Matrix<float> other, Matrix<float> result)
{ {
// diagonal + diagonal = diagonal // diagonal + diagonal = diagonal
var diagOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
{ {
LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data); LinearAlgebraControl.Provider.AddArrays(_data, diagOther._data, diagResult._data);
return; return;
@ -237,9 +234,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
protected override void DoSubtract(Matrix<float> other, Matrix<float> result) protected override void DoSubtract(Matrix<float> other, Matrix<float> result)
{ {
// diagonal - diagonal = diagonal // diagonal - diagonal = diagonal
var diagOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagOther && result is DiagonalMatrix diagResult)
var diagResult = result as DiagonalMatrix;
if (diagOther != null && diagResult != null)
{ {
LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data); LinearAlgebraControl.Provider.SubtractArrays(_data, diagOther._data, diagResult._data);
return; return;
@ -298,9 +293,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (d == ColumnCount) if (d == ColumnCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<float>; if (rightSide.Storage is DenseVectorStorage<float> denseOther && result.Storage is DenseVectorStorage<float> denseResult)
var denseResult = result.Storage as DenseVectorStorage<float>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return; return;
@ -320,9 +313,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Matrix<float> other, Matrix<float> result) protected override void DoMultiply(Matrix<float> other, Matrix<float> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new float[diagonalResult._data.Length]; var thisDataCopy = new float[diagonalResult._data.Length];
var otherDataCopy = new float[diagonalResult._data.Length]; var otherDataCopy = new float[diagonalResult._data.Length];
@ -332,8 +323,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>; if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -373,9 +363,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeAndMultiply(Matrix<float> other, Matrix<float> result) protected override void DoTransposeAndMultiply(Matrix<float> other, Matrix<float> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new float[diagonalResult._data.Length]; var thisDataCopy = new float[diagonalResult._data.Length];
var otherDataCopy = new float[diagonalResult._data.Length]; var otherDataCopy = new float[diagonalResult._data.Length];
@ -385,8 +373,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>; if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -417,9 +404,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The result of the multiplication.</param> /// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Matrix<float> other, Matrix<float> result) protected override void DoTransposeThisAndMultiply(Matrix<float> other, Matrix<float> result)
{ {
var diagonalOther = other as DiagonalMatrix; if (other is DiagonalMatrix diagonalOther && result is DiagonalMatrix diagonalResult)
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{ {
var thisDataCopy = new float[diagonalResult._data.Length]; var thisDataCopy = new float[diagonalResult._data.Length];
var otherDataCopy = new float[diagonalResult._data.Length]; var otherDataCopy = new float[diagonalResult._data.Length];
@ -429,8 +414,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return; return;
} }
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>; if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther)
if (denseOther != null)
{ {
var dense = denseOther.Data; var dense = denseOther.Data;
var diagonal = _data; var diagonal = _data;
@ -478,9 +462,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (d == RowCount) if (d == RowCount)
{ {
var denseOther = rightSide.Storage as DenseVectorStorage<float>; if (rightSide.Storage is DenseVectorStorage<float> denseOther && result.Storage is DenseVectorStorage<float> denseResult)
var denseResult = result.Storage as DenseVectorStorage<float>;
if (denseOther != null && denseResult != null)
{ {
LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data); LinearAlgebraControl.Provider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return; return;
@ -506,8 +488,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return; return;
} }
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
LinearAlgebraControl.Provider.ScaleArray(1.0f/divisor, _data, diagResult._data); LinearAlgebraControl.Provider.ScaleArray(1.0f/divisor, _data, diagResult._data);
return; return;
@ -527,8 +508,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <param name="result">The matrix to store the result of the division.</param> /// <param name="result">The matrix to store the result of the division.</param>
protected override void DoDivideByThis(float dividend, Matrix<float> result) protected override void DoDivideByThis(float dividend, Matrix<float> result)
{ {
var diagResult = result as DiagonalMatrix; if (result is DiagonalMatrix diagResult)
if (diagResult != null)
{ {
var resultData = diagResult._data; var resultData = diagResult._data;
CommonParallel.For(0, _data.Length, 4096, (a, b) => 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}"/>. /// Gets the two norm of the <see cref="Matrix{T}"/>.
/// </summary> /// </summary>
/// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns> /// <returns>The 2-norm of the <see cref="Matrix{T}"/>.</returns>
public override double L2Norm public override double L2Norm => Math.Abs(S[0]);
{
get
{
return Math.Abs(S[0]);
}
}
/// <summary> /// <summary>
/// Gets the condition number <b>max(S) / min(S)</b> /// 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> /// </summary>
public int NumberOfBiCgStabSteps public int NumberOfBiCgStabSteps
{ {
get { return _numberOfBiCgStabSteps; } get => _numberOfBiCgStabSteps;
set set
{ {
@ -99,7 +99,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
/// </summary> /// </summary>
public int NumberOfGpBiCgSteps public int NumberOfGpBiCgSteps
{ {
get { return _numberOfGpbiCgSteps; } get => _numberOfGpbiCgSteps;
set 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> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double FillLevel public double FillLevel
{ {
get { return _fillLevel; } get => _fillLevel;
set set
{ {
if (value < 0) if (value < 0)
@ -191,7 +191,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double DropTolerance public double DropTolerance
{ {
get { return _dropTolerance; } get => _dropTolerance;
set set
{ {
if (value < 0) if (value < 0)
@ -224,7 +224,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
/// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if a negative value is provided.</exception>
public double PivotTolerance public double PivotTolerance
{ {
get { return _pivotTolerance; } get => _pivotTolerance;
set set
{ {
if (value < 0) if (value < 0)

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

@ -86,10 +86,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
public int NumberOfStartingVectors public int NumberOfStartingVectors
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get get => _numberOfStartingVectors;
{
return _numberOfStartingVectors;
}
[DebuggerStepThrough] [DebuggerStepThrough]
set set
@ -118,10 +115,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single.Solvers
public IList<Vector<float>> StartingVectors public IList<Vector<float>> StartingVectors
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get get => _startingVectors;
{
return _startingVectors;
}
[DebuggerStepThrough] [DebuggerStepThrough]
set 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. /// Gets the number of non zero elements in the matrix.
/// </summary> /// </summary>
/// <value>The number of non zero elements.</value> /// <value>The number of non zero elements.</value>
public int NonZerosCount public int NonZerosCount => _storage.ValueCount;
{
get { return _storage.ValueCount; }
}
/// <summary> /// <summary>
/// Create a new sparse matrix straight from an initialized matrix storage instance. /// Create a new sparse matrix straight from an initialized matrix storage instance.
@ -894,8 +891,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return; return;
} }
var diagonalOther = other.Storage as DiagonalMatrixStorage<float>; if (other.Storage is DiagonalMatrixStorage<float> diagonalOther && sparseResult != null)
if (diagonalOther != null && sparseResult != null)
{ {
var diagonal = diagonalOther.Data; var diagonal = diagonalOther.Data;
if (other.ColumnCount == other.RowCount) if (other.ColumnCount == other.RowCount)
@ -914,8 +910,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
var rowPointers = _storage.RowPointers; var rowPointers = _storage.RowPointers;
var columnIndices = _storage.ColumnIndices; var columnIndices = _storage.ColumnIndices;
var values = _storage.Values; var values = _storage.Values;
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<float>; if (other.Storage is DenseColumnMajorMatrixStorage<float> denseOther)
if (denseOther != null)
{ {
// in this case we can directly address the underlying data-array // in this case we can directly address the underlying data-array
for (var row = 0; row < RowCount; row++) for (var row = 0; row < RowCount; row++)
@ -1568,7 +1563,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
public override string ToTypeString() 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. /// Gets the number of non zero elements in the vector.
/// </summary> /// </summary>
/// <value>The number of non zero elements.</value> /// <value>The number of non zero elements.</value>
public int NonZerosCount public int NonZerosCount => _storage.ValueCount;
{
get { return _storage.ValueCount; }
}
/// <summary> /// <summary>
/// Create a new sparse vector straight from an initialized vector storage instance. /// 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() 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 public IterationStatus Status
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _currentTcs.Token.IsCancellationRequested ? IterationStatus.Cancelled : IterationStatus.Continue; } get => _currentTcs.Token.IsCancellationRequested ? IterationStatus.Cancelled : IterationStatus.Continue;
} }
public void Cancel() public void Cancel()

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

@ -70,10 +70,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <summary> /// <summary>
/// Gets the current calculation status. /// Gets the current calculation status.
/// </summary> /// </summary>
public IterationStatus Status public IterationStatus Status => _status;
{
get { return _status; }
}
/// <summary> /// <summary>
/// Resets the IIterationStopCriterion to the pre-calculation state. /// 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 public double MaximumRelativeIncrease
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _maximumRelativeIncrease; } get => _maximumRelativeIncrease;
[DebuggerStepThrough] [DebuggerStepThrough]
set set
@ -114,7 +114,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public int MinimumNumberOfIterations public int MinimumNumberOfIterations
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _minimumNumberOfIterations; } get => _minimumNumberOfIterations;
[DebuggerStepThrough] [DebuggerStepThrough]
set set
@ -220,7 +220,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
int RequiredHistoryLength int RequiredHistoryLength
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _minimumNumberOfIterations + 1; } get => _minimumNumberOfIterations + 1;
} }
/// <summary> /// <summary>
@ -229,7 +229,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public IterationStatus Status public IterationStatus Status
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _status; } get => _status;
} }
/// <summary> /// <summary>

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

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

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

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

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

@ -85,10 +85,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
/// <summary> /// <summary>
/// Gets the current calculation status. /// Gets the current calculation status.
/// </summary> /// </summary>
public IterationStatus Status public IterationStatus Status => _status;
{
get { return _status; }
}
/// <summary> /// <summary>
/// Determines the status of the iterative calculation based on the stop criteria stored /// 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 public double Maximum
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _maximum; } get => _maximum;
[DebuggerStepThrough] [DebuggerStepThrough]
set set
@ -121,7 +121,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public int MinimumIterationsBelowMaximum public int MinimumIterationsBelowMaximum
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _minimumIterationsBelowMaximum; } get => _minimumIterationsBelowMaximum;
[DebuggerStepThrough] [DebuggerStepThrough]
set set
@ -220,7 +220,7 @@ namespace MathNet.Numerics.LinearAlgebra.Solvers
public IterationStatus Status public IterationStatus Status
{ {
[DebuggerStepThrough] [DebuggerStepThrough]
get { return _status; } get => _status;
} }
/// <summary> /// <summary>

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

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

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

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

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

@ -71,19 +71,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <summary> /// <summary>
/// True if the matrix storage format is dense. /// True if the matrix storage format is dense.
/// </summary> /// </summary>
public override bool IsDense public override bool IsDense => false;
{
get { return false; }
}
/// <summary> /// <summary>
/// True if all fields of this matrix can be set to any value. /// True if all fields of this matrix can be set to any value.
/// False if some fields are fixed, like on a diagonal matrix. /// False if some fields are fixed, like on a diagonal matrix.
/// </summary> /// </summary>
public override bool IsFullyMutable public override bool IsFullyMutable => false;
{
get { return false; }
}
/// <summary> /// <summary>
/// True if the specified field can be set to any value. /// 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)); throw new ArgumentNullException(nameof(data));
} }
var arrayData = data as T[]; if (data is T[] arrayData)
if (arrayData != null)
{ {
var copy = new T[arrayData.Length]; var copy = new T[arrayData.Length];
Array.Copy(arrayData, 0, copy, 0, 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) internal override void CopyToUnchecked(MatrixStorage<T> target, ExistingData existingData)
{ {
var diagonalTarget = target as DiagonalMatrixStorage<T>; if (target is DiagonalMatrixStorage<T> diagonalTarget)
if (diagonalTarget != null)
{ {
CopyToUnchecked(diagonalTarget); CopyToUnchecked(diagonalTarget);
return; return;
} }
var denseTarget = target as DenseColumnMajorMatrixStorage<T>; if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
if (denseTarget != null)
{ {
CopyToUnchecked(denseTarget, existingData); CopyToUnchecked(denseTarget, existingData);
return; return;
} }
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>; if (target is SparseCompressedRowMatrixStorage<T> sparseTarget)
if (sparseTarget != null)
{ {
CopyToUnchecked(sparseTarget, existingData); CopyToUnchecked(sparseTarget, existingData);
return; return;
@ -327,15 +317,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
ExistingData existingData) ExistingData existingData)
{ {
var denseTarget = target as DenseColumnMajorMatrixStorage<T>; if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
if (denseTarget != null)
{ {
CopySubMatrixToUnchecked(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, existingData); CopySubMatrixToUnchecked(denseTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, existingData);
return; return;
} }
var diagonalTarget = target as DiagonalMatrixStorage<T>; if (target is DiagonalMatrixStorage<T> diagonalTarget)
if (diagonalTarget != null)
{ {
CopySubMatrixToUnchecked(diagonalTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); CopySubMatrixToUnchecked(diagonalTarget, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount);
return; 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) 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 (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
if (denseOther != null)
{ {
TOther[] otherData = denseOther.Data; TOther[] otherData = denseOther.Data;
int k = 0; int k = 0;
@ -644,8 +631,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null; return null;
} }
var diagonalOther = other as DiagonalMatrixStorage<TOther>; if (other is DiagonalMatrixStorage<TOther> diagonalOther)
if (diagonalOther != null)
{ {
TOther[] otherData = diagonalOther.Data; TOther[] otherData = diagonalOther.Data;
for (int i = 0; i < Data.Length; i++) for (int i = 0; i < Data.Length; i++)
@ -666,8 +652,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null; return null;
} }
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>; if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
if (sparseOther != null)
{ {
int[] otherRowPointers = sparseOther.RowPointers; int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices; int[] otherColumnIndices = sparseOther.ColumnIndices;
@ -772,8 +757,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
var processZeros = zeros == Zeros.Include || !Zero.Equals(f(Zero)); var processZeros = zeros == Zeros.Include || !Zero.Equals(f(Zero));
var diagonalTarget = target as DiagonalMatrixStorage<TU>; if (target is DiagonalMatrixStorage<TU> diagonalTarget)
if (diagonalTarget != null)
{ {
if (processZeros) if (processZeros)
{ {
@ -821,8 +805,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
var processZeros = zeros == Zeros.Include || !Zero.Equals(f(0, 1, Zero)); var processZeros = zeros == Zeros.Include || !Zero.Equals(f(0, 1, Zero));
var diagonalTarget = target as DiagonalMatrixStorage<TU>; if (target is DiagonalMatrixStorage<TU> diagonalTarget)
if (diagonalTarget != null)
{ {
if (processZeros) if (processZeros)
{ {
@ -870,15 +853,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
Zeros zeros, ExistingData existingData) Zeros zeros, ExistingData existingData)
{ {
var diagonalTarget = target as DiagonalMatrixStorage<TU>; if (target is DiagonalMatrixStorage<TU> diagonalTarget)
if (diagonalTarget != null)
{ {
MapSubMatrixIndexedToUnchecked(diagonalTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros); MapSubMatrixIndexedToUnchecked(diagonalTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros);
return; return;
} }
var denseTarget = target as DenseColumnMajorMatrixStorage<TU>; if (target is DenseColumnMajorMatrixStorage<TU> denseTarget)
if (denseTarget != null)
{ {
MapSubMatrixIndexedToUnchecked(denseTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros, existingData); MapSubMatrixIndexedToUnchecked(denseTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros, existingData);
return; 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) 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 (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
if (denseOther != null)
{ {
TOther[] otherData = denseOther.Data; TOther[] otherData = denseOther.Data;
int k = 0; int k = 0;
@ -1113,8 +1093,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state; return state;
} }
var diagonalOther = other as DiagonalMatrixStorage<TOther>; if (other is DiagonalMatrixStorage<TOther> diagonalOther)
if (diagonalOther != null)
{ {
TOther[] otherData = diagonalOther.Data; TOther[] otherData = diagonalOther.Data;
for (int i = 0; i < Data.Length; i++) for (int i = 0; i < Data.Length; i++)
@ -1136,8 +1115,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state; return state;
} }
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>; if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
if (sparseOther != null)
{ {
int[] otherRowPointers = sparseOther.RowPointers; int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices; 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. /// Gets the number of non zero elements in the matrix.
/// </summary> /// </summary>
/// <value>The number of non zero elements.</value> /// <value>The number of non zero elements.</value>
public int ValueCount public int ValueCount => RowPointers[RowCount];
{
get { return RowPointers[RowCount]; }
}
internal SparseCompressedRowMatrixStorage(int rows, int columns) internal SparseCompressedRowMatrixStorage(int rows, int columns)
: base(rows, columns) : base(rows, columns)
@ -85,19 +82,13 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
/// <summary> /// <summary>
/// True if the matrix storage format is dense. /// True if the matrix storage format is dense.
/// </summary> /// </summary>
public override bool IsDense public override bool IsDense => false;
{
get { return false; }
}
/// <summary> /// <summary>
/// True if all fields of this matrix can be set to any value. /// True if all fields of this matrix can be set to any value.
/// False if some fields are fixed, like on a diagonal matrix. /// False if some fields are fixed, like on a diagonal matrix.
/// </summary> /// </summary>
public override bool IsFullyMutable public override bool IsFullyMutable => true;
{
get { return true; }
}
/// <summary> /// <summary>
/// True if the specified field can be set to any value. /// 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) internal override void CopyToUnchecked(MatrixStorage<T> target, ExistingData existingData)
{ {
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>; if (target is SparseCompressedRowMatrixStorage<T> sparseTarget)
if (sparseTarget != null)
{ {
CopyToUnchecked(sparseTarget); CopyToUnchecked(sparseTarget);
return; return;
} }
var denseTarget = target as DenseColumnMajorMatrixStorage<T>; if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
if (denseTarget != null)
{ {
CopyToUnchecked(denseTarget, existingData); CopyToUnchecked(denseTarget, existingData);
return; return;
@ -923,8 +912,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
throw new ArgumentNullException(nameof(target)); throw new ArgumentNullException(nameof(target));
} }
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>; if (target is SparseCompressedRowMatrixStorage<T> sparseTarget)
if (sparseTarget != null)
{ {
CopySubMatrixToUnchecked(sparseTarget, CopySubMatrixToUnchecked(sparseTarget,
sourceRowIndex, targetRowIndex, rowCount, sourceRowIndex, targetRowIndex, rowCount,
@ -1046,8 +1034,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return; return;
} }
var targetSparse = target as SparseVectorStorage<T>; if (target is SparseVectorStorage<T> targetSparse)
if (targetSparse != null)
{ {
if ((sourceColumnIndex == 0) && (targetColumnIndex == 0) && (columnCount == ColumnCount) && (ColumnCount == targetSparse.Length)) 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) internal override void TransposeToUnchecked(MatrixStorage<T> target, ExistingData existingData)
{ {
var sparseTarget = target as SparseCompressedRowMatrixStorage<T>; if (target is SparseCompressedRowMatrixStorage<T> sparseTarget)
if (sparseTarget != null)
{ {
TransposeToUnchecked(sparseTarget); TransposeToUnchecked(sparseTarget);
return; return;
} }
var denseTarget = target as DenseColumnMajorMatrixStorage<T>; if (target is DenseColumnMajorMatrixStorage<T> denseTarget)
if (denseTarget != null)
{ {
TransposeToUnchecked(denseTarget, existingData); TransposeToUnchecked(denseTarget, existingData);
return; 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) 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 (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
if (denseOther != null)
{ {
TOther[] otherData = denseOther.Data; TOther[] otherData = denseOther.Data;
int k = 0; int k = 0;
@ -1478,8 +1462,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null; return null;
} }
var diagonalOther = other as DiagonalMatrixStorage<TOther>; if (other is DiagonalMatrixStorage<TOther> diagonalOther)
if (diagonalOther != null)
{ {
TOther[] otherData = diagonalOther.Data; TOther[] otherData = diagonalOther.Data;
TOther otherZero = BuilderInstance<TOther>.Matrix.Zero; TOther otherZero = BuilderInstance<TOther>.Matrix.Zero;
@ -1537,8 +1520,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return null; return null;
} }
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>; if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
if (sparseOther != null)
{ {
int[] otherRowPointers = sparseOther.RowPointers; int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices; int[] otherColumnIndices = sparseOther.ColumnIndices;
@ -1714,8 +1696,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
{ {
var processZeros = zeros == Zeros.Include || !Zero.Equals(f(Zero)); var processZeros = zeros == Zeros.Include || !Zero.Equals(f(Zero));
var sparseTarget = target as SparseCompressedRowMatrixStorage<TU>; if (target is SparseCompressedRowMatrixStorage<TU> sparseTarget)
if (sparseTarget != null)
{ {
var newRowPointers = sparseTarget.RowPointers; var newRowPointers = sparseTarget.RowPointers;
var newColumnIndices = new List<int>(ColumnIndices.Length); 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 processZeros = zeros == Zeros.Include || !Zero.Equals(f(0, 1, Zero));
var sparseTarget = target as SparseCompressedRowMatrixStorage<TU>; if (target is SparseCompressedRowMatrixStorage<TU> sparseTarget)
if (sparseTarget != null)
{ {
var newRowPointers = sparseTarget.RowPointers; var newRowPointers = sparseTarget.RowPointers;
var newColumnIndices = new List<int>(ColumnIndices.Length); var newColumnIndices = new List<int>(ColumnIndices.Length);
@ -1903,8 +1883,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
int sourceColumnIndex, int targetColumnIndex, int columnCount, int sourceColumnIndex, int targetColumnIndex, int columnCount,
Zeros zeros, ExistingData existingData) Zeros zeros, ExistingData existingData)
{ {
var sparseTarget = target as SparseCompressedRowMatrixStorage<TU>; if (target is SparseCompressedRowMatrixStorage<TU> sparseTarget)
if (sparseTarget != null)
{ {
MapSubMatrixIndexedToUnchecked(sparseTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros, existingData); MapSubMatrixIndexedToUnchecked(sparseTarget, f, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount, zeros, existingData);
return; 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) 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 (other is DenseColumnMajorMatrixStorage<TOther> denseOther)
if (denseOther != null)
{ {
TOther[] otherData = denseOther.Data; TOther[] otherData = denseOther.Data;
int k = 0; int k = 0;
@ -2219,8 +2197,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state; return state;
} }
var diagonalOther = other as DiagonalMatrixStorage<TOther>; if (other is DiagonalMatrixStorage<TOther> diagonalOther)
if (diagonalOther != null)
{ {
TOther[] otherData = diagonalOther.Data; TOther[] otherData = diagonalOther.Data;
TOther otherZero = BuilderInstance<TOther>.Matrix.Zero; TOther otherZero = BuilderInstance<TOther>.Matrix.Zero;
@ -2266,8 +2243,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage
return state; return state;
} }
var sparseOther = other as SparseCompressedRowMatrixStorage<TOther>; if (other is SparseCompressedRowMatrixStorage<TOther> sparseOther)
if (sparseOther != null)
{ {
int[] otherRowPointers = sparseOther.RowPointers; int[] otherRowPointers = sparseOther.RowPointers;
int[] otherColumnIndices = sparseOther.ColumnIndices; int[] otherColumnIndices = sparseOther.ColumnIndices;

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

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

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

@ -62,8 +62,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </returns> /// </returns>
public sealed override bool Equals(object obj) public sealed override bool Equals(object obj)
{ {
var other = obj as Vector<T>; return obj is Vector<T> other && Storage.Equals(other.Storage);
return other != null && Storage.Equals(other.Storage);
} }
/// <summary> /// <summary>
@ -112,10 +111,7 @@ namespace MathNet.Numerics.LinearAlgebra
throw new NotSupportedException(); throw new NotSupportedException();
} }
bool ICollection<T>.IsReadOnly bool ICollection<T>.IsReadOnly => false;
{
get { return false; }
}
void ICollection<T>.Add(T item) 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); Storage.CopySubVectorTo(new DenseVectorStorage<T>(array.Length, array), 0, arrayIndex, Count);
} }
bool IList.IsReadOnly bool IList.IsReadOnly => false;
{
get { return false; }
}
bool IList.IsFixedSize bool IList.IsFixedSize => true;
{
get { return true; }
}
object IList.this[int index] object IList.this[int index]
{ {
get { return Storage[index]; } get => Storage[index];
set { Storage[index] = (T) value; } set => Storage[index] = (T) value;
} }
int IList.IndexOf(object value) int IList.IndexOf(object value)
@ -204,15 +194,9 @@ namespace MathNet.Numerics.LinearAlgebra
throw new NotSupportedException(); throw new NotSupportedException();
} }
bool ICollection.IsSynchronized bool ICollection.IsSynchronized => false;
{
get { return false; }
}
object ICollection.SyncRoot object ICollection.SyncRoot => Storage;
{
get { return Storage; }
}
void ICollection.CopyTo(Array array, int index) void ICollection.CopyTo(Array array, int index)
{ {
@ -255,7 +239,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// </summary> /// </summary>
public virtual string ToTypeString() 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) 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); 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) 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); 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); 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) 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); 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) 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); 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) 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); 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 // Argument validation in base class
} }
protected override ExitCondition WolfeExitCondition protected override ExitCondition WolfeExitCondition => ExitCondition.StrongWolfeCriteria;
{
get { return ExitCondition.StrongWolfeCriteria; }
}
protected override bool WolfeCondition(double stepDd, double initialDd) 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 // Validation in base class
} }
protected override ExitCondition WolfeExitCondition protected override ExitCondition WolfeExitCondition => ExitCondition.WeakWolfeCriteria;
{
get { return ExitCondition.WeakWolfeCriteria; }
}
protected override bool WolfeCondition(double stepDd, double initialDd) protected override bool WolfeCondition(double stepDd, double initialDd)
{ {
@ -67,7 +64,7 @@ namespace MathNet.Numerics.Optimization.LineSearch
{ {
if (!IsFinite(eval.Value)) 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)) 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 System;
using MathNet.Numerics.LinearAlgebra; using MathNet.Numerics.LinearAlgebra;
using static System.FormattableString;
namespace MathNet.Numerics.Optimization.LineSearch 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) public WolfeLineSearch(double c1, double c2, double parameterTolerance, int maxIterations = 10)
{ {
if (c1 <= 0) 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) 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) 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; C1 = c1;
C2 = c2; C2 = c2;
@ -125,12 +126,12 @@ namespace MathNet.Numerics.Optimization.LineSearch
if (ii == MaximumIterations && Double.IsPositiveInfinity(upperBound)) 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) 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); 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 class MinimizationResult
{ {
public Vector<double> MinimizingPoint { get { return FunctionInfoAtMinimum.Point; } } public Vector<double> MinimizingPoint => FunctionInfoAtMinimum.Point;
public IObjectiveFunction FunctionInfoAtMinimum { get; private set; } public IObjectiveFunction FunctionInfoAtMinimum { get; }
public int Iterations { get; private set; } public int Iterations { get; }
public ExitCondition ReasonForExit { get; private set; } public ExitCondition ReasonForExit { get; }
public MinimizationResult(IObjectiveFunction functionInfo, int iterations, ExitCondition reasonForExit) 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 // check to see if we have exceeded our alloted number of evaluations
if (evaluationCount >= maximumIterations) 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]); objectiveFunction.EvaluateAt(vertices[errorProfile.LowestIndex]);

2
src/Numerics/Optimization/NewtonMinimizer.cs

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

6
src/Numerics/Optimization/NonlinearMinimizerBase.cs

@ -19,8 +19,8 @@ namespace MathNet.Numerics.Optimization
/// <summary> /// <summary>
/// The stopping threshold for infinity norm of the gradient. /// The stopping threshold for infinity norm of the gradient.
/// </summary> /// </summary>
public static double GradientTolerance { get; set; } public static double GradientTolerance { get; set; }
/// <summary> /// <summary>
/// The maximum number of iterations. /// The maximum number of iterations.
/// </summary> /// </summary>
@ -41,7 +41,7 @@ namespace MathNet.Numerics.Optimization
/// </summary> /// </summary>
public static Vector<double> Scales { get; private set; } 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) 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(); EvaluateGradient();
return _gradient; return _gradient;
} }
protected set { _gradient = value; } protected set => _gradient = value;
} }
public Matrix<double> Hessian public Matrix<double> Hessian => throw new NotImplementedException();
{
get
{
throw new NotImplementedException();
}
}
public bool IsGradientSupported public bool IsGradientSupported => true;
{
get
{
return true;
}
}
public bool IsHessianSupported public bool IsHessianSupported => false;
{
get
{
return false;
}
}
public Vector<double> Point { get; protected set; } public Vector<double> Point { get; protected set; }
@ -145,7 +127,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public IObjectiveFunction CreateNew() 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; return tmp;
} }
@ -159,7 +141,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
public IObjectiveFunction Fork() 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(), Point = Point?.Clone(),
GradientEvaluated = GradientEvaluated, GradientEvaluated = GradientEvaluated,

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

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

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

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

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

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

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

@ -89,10 +89,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
Hessian = null; Hessian = null;
} }
public Vector<double> Point public Vector<double> Point => _point;
{
get { return _point; }
}
public double Value 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. Matrix<double> jacobianValue; // the Jacobian matrix.
Vector<double> gradientValue; // the Gradient vector. Vector<double> gradientValue; // the Gradient vector.
Matrix<double> hessianValue; // the Hessian matrix. Matrix<double> hessianValue; // the Hessian matrix.
#endregion Private Variables #endregion Private Variables
#region Public Variables #region Public Variables
@ -37,7 +37,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
/// Set or get the values of the observations. /// Set or get the values of the observations.
/// </summary> /// </summary>
public Vector<double> ObservedY { get; private set; } public Vector<double> ObservedY { get; private set; }
/// <summary> /// <summary>
/// Set or get the values of the weights for the observations. /// Set or get the values of the weights for the observations.
/// </summary> /// </summary>
@ -52,12 +52,12 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
/// <summary> /// <summary>
/// Get the number of observations. /// Get the number of observations.
/// </summary> /// </summary>
public int NumberOfObservations { get { return (ObservedY == null) ? 0 : ObservedY.Count; } } public int NumberOfObservations => ObservedY?.Count ?? 0;
/// <summary> /// <summary>
/// Get the number of unknown parameters. /// Get the number of unknown parameters.
/// </summary> /// </summary>
public int NumberOfParameters { get { return (Point == null) ? 0 : Point.Count; } } public int NumberOfParameters => Point?.Count ?? 0;
/// <summary> /// <summary>
/// Get the degree of freedom /// Get the degree of freedom
@ -123,7 +123,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
/// <summary> /// <summary>
/// Set or get the values of the parameters. /// Set or get the values of the parameters.
/// </summary> /// </summary>
public Vector<double> Point { get { return coefficients; } } public Vector<double> Point => coefficients;
/// <summary> /// <summary>
/// Get the y-values of the fitted model that correspond to the independent values. /// 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 IsGradientSupported => true;
public bool IsHessianSupported { get { return true; } } public bool IsHessianSupported => true;
/// <summary> /// <summary>
/// Set observed data to fit. /// Set observed data to fit.
@ -235,7 +235,7 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
throw new ArgumentNullException("initialGuess"); throw new ArgumentNullException("initialGuess");
} }
coefficients = initialGuess; coefficients = initialGuess;
if (isFixed != null && isFixed.Count != initialGuess.Count) if (isFixed != null && isFixed.Count != initialGuess.Count)
{ {
throw new ArgumentException("The isFixed can't have different size from the initial guess."); 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 // approximated Hessian, H = J'WJ + ∑LRiHi ~ J'WJ near the minimum
hessianValue = jacobianValue.Transpose() * jacobianValue; hessianValue = jacobianValue.Transpose() * jacobianValue;
} }
private Matrix<double> NumericalJacobian(Vector<double> parameters, Vector<double> currentValues, int accuracyOrder = 2) private Matrix<double> NumericalJacobian(Vector<double> parameters, Vector<double> currentValues, int accuracyOrder = 2)
{ {
const double sqrtEpsilon = 1.4901161193847656250E-8; // sqrt(machineEpsilon) const double sqrtEpsilon = 1.4901161193847656250E-8; // sqrt(machineEpsilon)
Matrix<double> derivertives = Matrix<double>.Build.Dense(NumberOfObservations, NumberOfParameters); Matrix<double> derivertives = Matrix<double>.Build.Dense(NumberOfObservations, NumberOfParameters);
var d = 0.000003 * parameters.PointwiseAbs().PointwiseMaximum(sqrtEpsilon); 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; return _secondDerivative.Value;
} }
public double Point { get { return _point; } } public double Point => _point;
public double Value { get { return _value ?? SetValue(); } } public double Value => _value ?? SetValue();
public double Derivative { get { return _derivative ?? SetDerivative(); } } public double Derivative => _derivative ?? SetDerivative();
public double SecondDerivative { get { return _secondDerivative ?? SetSecondDerivative(); } } public double SecondDerivative => _secondDerivative ?? SetSecondDerivative();
} }
internal class ScalarObjectiveFunction : IScalarObjectiveFunction internal class ScalarObjectiveFunction : IScalarObjectiveFunction
{ {
public Func<double, double> Objective { get; private set; } public Func<double, double> Objective { get; }
public Func<double, double> Derivative { get; private set; } public Func<double, double> Derivative { get; }
public Func<double, double> SecondDerivative { get; private set; } public Func<double, double> SecondDerivative { get; }
public ScalarObjectiveFunction(Func<double, double> objective) public ScalarObjectiveFunction(Func<double, double> objective)
{ {
@ -96,15 +96,9 @@ namespace MathNet.Numerics.Optimization.ObjectiveFunctions
SecondDerivative = secondDerivative; SecondDerivative = secondDerivative;
} }
public bool IsDerivativeSupported public bool IsDerivativeSupported => Derivative != null;
{
get { return Derivative != null; }
}
public bool IsSecondDerivativeSupported public bool IsSecondDerivativeSupported => SecondDerivative != null;
{
get { return SecondDerivative != null; }
}
public IScalarObjectiveFunctionEvaluation Evaluate(double point) 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 Point { get; }
public double Value { get; } public double Value { get; }
public double Derivative public double Derivative => throw new NotSupportedException();
{
get { throw new NotSupportedException(); }
}
public double SecondDerivative public double SecondDerivative => throw new NotSupportedException();
{
get { throw new NotSupportedException(); }
}
} }
internal class ScalarValueObjectiveFunction : IScalarObjectiveFunction internal class ScalarValueObjectiveFunction : IScalarObjectiveFunction
{ {
public Func<double, double> Objective { get; private set; } public Func<double, double> Objective { get; }
public ScalarValueObjectiveFunction(Func<double, double> objective) public ScalarValueObjectiveFunction(Func<double, double> objective)
{ {
Objective = objective; Objective = objective;
} }
public bool IsDerivativeSupported public bool IsDerivativeSupported => false;
{
get { return false; }
}
public bool IsSecondDerivativeSupported public bool IsSecondDerivativeSupported => false;
{
get { return false; }
}
public IScalarObjectiveFunctionEvaluation Evaluate(double point) public IScalarObjectiveFunctionEvaluation Evaluate(double point)
{ {

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

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

8
src/Numerics/Optimization/ScalarMinimizationResult.cs

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

13
src/Numerics/Permutation.cs

@ -69,23 +69,14 @@ namespace MathNet.Numerics
/// <summary> /// <summary>
/// Gets the number of elements this permutation is over. /// Gets the number of elements this permutation is over.
/// </summary> /// </summary>
public int Dimension public int Dimension => _indices.Length;
{
get { return _indices.Length; }
}
/// <summary> /// <summary>
/// Computes where <paramref name="idx"/> permutes too. /// Computes where <paramref name="idx"/> permutes too.
/// </summary> /// </summary>
/// <param name="idx">The index to permute from.</param> /// <param name="idx">The index to permute from.</param>
/// <returns>The index which is permuted to.</returns> /// <returns>The index which is permuted to.</returns>
public int this[int idx] public int this[int idx] => _indices[idx];
{
get
{
return _indices[idx];
}
}
/// <summary> /// <summary>
/// Computes the inverse of the permutation. /// 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. /// Name of the native DLL.
/// </summary> /// </summary>
const string _DllName = "MathNet.Numerics.CUDA.dll"; 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)] [DllImport(_DllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
internal static extern int query_capability(int capability); internal static extern int query_capability(int capability);

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

Loading…
Cancel
Save