Browse Source

merged Andriy's sparse optimizations

a little clean up
la-knuth
Marcus Cuda 16 years ago
parent
commit
c62ba47988
  1. 62
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  2. 202
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  3. 149
      src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs
  4. 384
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  5. 583
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  6. 309
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  7. 204
      src/Numerics/LinearAlgebra/Double/Vector.cs
  8. 6
      src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs
  9. 12
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

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

@ -44,7 +44,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(int order) public DenseMatrix(int order)
: base(order) : base(order)
{ {
this.Data = new double[order * order]; Data = new double[order * order];
} }
/// <summary> /// <summary>
@ -59,7 +59,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(int rows, int columns) public DenseMatrix(int rows, int columns)
: base(rows, columns) : base(rows, columns)
{ {
this.Data = new double[rows * columns]; Data = new double[rows * columns];
} }
/// <summary> /// <summary>
@ -75,10 +75,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(int rows, int columns, double value) public DenseMatrix(int rows, int columns, double value)
: base(rows, columns) : base(rows, columns)
{ {
this.Data = new double[rows * columns]; Data = new double[rows * columns];
for (var i = 0; i < this.Data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
this.Data[i] = value; Data[i] = value;
} }
} }
@ -92,7 +92,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseMatrix(int rows, int columns, double[] array) public DenseMatrix(int rows, int columns, double[] array)
: base(rows, columns) : base(rows, columns)
{ {
this.Data = array; Data = array;
} }
/// <summary> /// <summary>
@ -105,12 +105,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
var rows = array.GetLength(0); var rows = array.GetLength(0);
var columns = array.GetLength(1); var columns = array.GetLength(1);
this.Data = new double[rows * columns]; Data = new double[rows * columns];
for (var i = 0; i < rows; i++) for (var i = 0; i < rows; i++)
{ {
for (var j = 0; j < columns; j++) for (var j = 0; j < columns; j++)
{ {
this.Data[(j * rows) + i] = array[i, j]; Data[(j * rows) + i] = array[i, j];
} }
} }
} }
@ -168,7 +168,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public override double At(int row, int column) public override double At(int row, int column)
{ {
return this.Data[(column * this.RowCount) + row]; return Data[(column * RowCount) + row];
} }
/// <summary> /// <summary>
@ -185,7 +185,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </param> /// </param>
public override void At(int row, int column, double value) public override void At(int row, int column, double value)
{ {
this.Data[(column * this.RowCount) + row] = value; Data[(column * RowCount) + row] = value;
} }
/// <summary> /// <summary>
@ -193,7 +193,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary> /// </summary>
public override void Clear() public override void Clear()
{ {
Array.Clear(this.Data, 0, this.Data.Length); Array.Clear(Data, 0, Data.Length);
} }
/// <summary> /// <summary>
@ -202,13 +202,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The transpose of this matrix.</returns> /// <returns>The transpose of this matrix.</returns>
public override Matrix Transpose() public override Matrix Transpose()
{ {
var ret = new DenseMatrix(this.ColumnCount, this.RowCount); var ret = new DenseMatrix(ColumnCount, RowCount);
for (var j = 0; j < this.ColumnCount; j++) for (var j = 0; j < ColumnCount; j++)
{ {
var index = j * this.RowCount; var index = j * RowCount;
for (var i = 0; i < this.RowCount; i++) for (var i = 0; i < RowCount; i++)
{ {
ret.Data[(i * this.ColumnCount) + j] = this.Data[index + i]; ret.Data[(i * ColumnCount) + j] = Data[index + i];
} }
} }
@ -232,7 +232,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.Add(m); Add(m);
} }
} }
@ -249,12 +249,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{ {
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
} }
Control.LinearAlgebraProvider.AddArrays(this.Data, other.Data, this.Data); Control.LinearAlgebraProvider.AddArrays(Data, other.Data, Data);
} }
/// <summary> /// <summary>
@ -272,7 +272,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.Subtract(m); Subtract(m);
} }
} }
@ -289,12 +289,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{ {
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
} }
Control.LinearAlgebraProvider.SubtractArrays(this.Data, other.Data, this.Data); Control.LinearAlgebraProvider.SubtractArrays(Data, other.Data, Data);
} }
/// <summary> /// <summary>
@ -303,7 +303,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <param name="scalar">The scalar to multiply with.</param> /// <param name="scalar">The scalar to multiply with.</param>
public override void Multiply(double scalar) public override void Multiply(double scalar)
{ {
Control.LinearAlgebraProvider.ScaleArray(scalar, this.Data); Control.LinearAlgebraProvider.ScaleArray(scalar, Data);
} }
/// <summary> /// <summary>
@ -327,12 +327,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.ColumnCount != other.RowCount) if (ColumnCount != other.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions); throw new ArgumentException(Resources.ArgumentMatrixDimensions);
} }
if (result.RowCount != this.RowCount || result.ColumnCount != other.ColumnCount) if (result.RowCount != RowCount || result.ColumnCount != other.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions); throw new ArgumentException(Resources.ArgumentMatrixDimensions);
} }
@ -347,9 +347,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
else else
{ {
Control.LinearAlgebraProvider.MatrixMultiply( Control.LinearAlgebraProvider.MatrixMultiply(
this.Data, Data,
this.RowCount, RowCount,
this.ColumnCount, ColumnCount,
m.Data, m.Data,
m.RowCount, m.RowCount,
m.ColumnCount, m.ColumnCount,
@ -371,7 +371,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.ColumnCount != other.RowCount) if (ColumnCount != other.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions); throw new ArgumentException(Resources.ArgumentMatrixDimensions);
} }
@ -382,7 +382,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return base.Multiply(other); return base.Multiply(other);
} }
var result = (DenseMatrix)this.CreateMatrix(this.RowCount, other.ColumnCount); var result = (DenseMatrix)CreateMatrix(RowCount, other.ColumnCount);
Multiply(other, result); Multiply(other, result);
return result; return result;
} }
@ -440,4 +440,4 @@ namespace MathNet.Numerics.LinearAlgebra.Double
#endregion #endregion
} }
} }

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

@ -51,7 +51,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseVector(int size) public DenseVector(int size)
: base(size) : base(size)
{ {
this.Data = new double[size]; Data = new double[size];
} }
/// <summary> /// <summary>
@ -70,9 +70,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseVector(int size, double value) public DenseVector(int size, double value)
: this(size) : this(size)
{ {
for (var index = 0; index < this.Data.Length; index++) for (var index = 0; index < Data.Length; index++)
{ {
this.Data[index] = value; Data[index] = value;
} }
} }
@ -91,12 +91,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
CommonParallel.For( CommonParallel.For(
0, 0,
this.Data.Length, Data.Length,
index => this[index] = other[index]); index => this[index] = other[index]);
} }
else else
{ {
Buffer.BlockCopy(vector.Data, 0, this.Data, 0, this.Data.Length * Constants.SizeOfDouble); Buffer.BlockCopy(vector.Data, 0, Data, 0, Data.Length * Constants.SizeOfDouble);
} }
} }
@ -110,7 +110,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseVector(DenseVector other) public DenseVector(DenseVector other)
: this(other.Count) : this(other.Count)
{ {
Buffer.BlockCopy(other.Data, 0, this.Data, 0, this.Data.Length * Constants.SizeOfDouble); Buffer.BlockCopy(other.Data, 0, Data, 0, Data.Length * Constants.SizeOfDouble);
} }
/// <summary> /// <summary>
@ -121,7 +121,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// changes to the vector will also change the array.</remarks> /// changes to the vector will also change the array.</remarks>
public DenseVector(double[] array) : base(array.Length) public DenseVector(double[] array) : base(array.Length)
{ {
this.Data = array; Data = array;
} }
/// <summary> /// <summary>
@ -158,7 +158,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary> /// </summary>
/// <param name="array">The array to bind to the <c>DenseVector</c> object.</param> /// <param name="array">The array to bind to the <c>DenseVector</c> object.</param>
/// <returns> /// <returns>
/// A DenseVector whose values are bound to the given array. /// A <c>DenseVector</c> whose values are bound to the given array.
/// </returns> /// </returns>
public static implicit operator DenseVector(double[] array) public static implicit operator DenseVector(double[] array)
{ {
@ -176,10 +176,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a column matrix.</returns> /// <returns>This vector as a column matrix.</returns>
public override Matrix ToColumnMatrix() public override Matrix ToColumnMatrix()
{ {
var matrix = new DenseMatrix(this.Count, 1); var matrix = new DenseMatrix(Count, 1);
for (var i = 0; i < this.Data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
matrix[i, 0] = this.Data[i]; matrix[i, 0] = Data[i];
} }
return matrix; return matrix;
@ -191,10 +191,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a row matrix.</returns> /// <returns>This vector as a row matrix.</returns>
public override Matrix ToRowMatrix() public override Matrix ToRowMatrix()
{ {
var matrix = new DenseMatrix(1, this.Count); var matrix = new DenseMatrix(1, Count);
for (var i = 0; i < this.Data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
matrix[0, i] = this.Data[i]; matrix[0, i] = Data[i];
} }
return matrix; return matrix;
@ -209,12 +209,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
get get
{ {
return this.Data[index]; return Data[index];
} }
set set
{ {
this.Data[index] = value; Data[index] = value;
} }
} }
@ -270,7 +270,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("target"); throw new ArgumentNullException("target");
} }
if (this.Count != target.Count) if (Count != target.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
} }
@ -285,12 +285,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
CommonParallel.For( CommonParallel.For(
0, 0,
this.Data.Length, Data.Length,
index => target[index] = this.Data[index]); index => target[index] = Data[index]);
} }
else else
{ {
Buffer.BlockCopy(this.Data, 0, otherVector.Data, 0, this.Data.Length * Constants.SizeOfDouble); Buffer.BlockCopy(Data, 0, otherVector.Data, 0, Data.Length * Constants.SizeOfDouble);
} }
} }
@ -307,8 +307,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For( CommonParallel.For(
0, 0,
this.Data.Length, Data.Length,
index => this.Data[index] += scalar); index => Data[index] += scalar);
} }
/// <summary> /// <summary>
@ -325,12 +325,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
this.CopyTo(result); CopyTo(result);
result.Add(scalar); result.Add(scalar);
} }
@ -347,7 +347,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
@ -360,7 +360,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
Control.LinearAlgebraProvider.AddVectorToScaledVector(this.Data, 1.0, denseVector.Data); Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, 1.0, denseVector.Data);
} }
} }
@ -380,12 +380,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -398,7 +398,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.Add(other); result.Add(other);
} }
} }
@ -463,8 +463,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For( CommonParallel.For(
0, 0,
this.Data.Length, Data.Length,
index => this.Data[index] -= scalar); index => Data[index] -= scalar);
} }
/// <summary> /// <summary>
@ -481,12 +481,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
this.CopyTo(result); CopyTo(result);
result.Subtract(scalar); result.Subtract(scalar);
} }
@ -503,7 +503,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
@ -516,7 +516,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
Control.LinearAlgebraProvider.AddVectorToScaledVector(this.Data, -1.0, denseVector.Data); Control.LinearAlgebraProvider.AddVectorToScaledVector(Data, -1.0, denseVector.Data);
} }
} }
@ -536,12 +536,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -554,7 +554,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.Subtract(other); result.Subtract(other);
} }
} }
@ -612,11 +612,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <remarks>Added as an alternative to the unary negation operator.</remarks> /// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector Negate() public override Vector Negate()
{ {
var result = new DenseVector(this.Count); var result = new DenseVector(Count);
CommonParallel.For( CommonParallel.For(
0, 0,
this.Data.Length, Data.Length,
index => result[index] = -this.Data[index]); index => result[index] = -Data[index]);
return result; return result;
} }
@ -632,7 +632,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
Control.LinearAlgebraProvider.ScaleArray(scalar, this.Data); Control.LinearAlgebraProvider.ScaleArray(scalar, Data);
} }
/// <summary> /// <summary>
@ -649,7 +649,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
@ -661,7 +661,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return base.DotProduct(other); return base.DotProduct(other);
} }
return Control.LinearAlgebraProvider.DotProduct(this.Data, denseVector.Data); return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
} }
/// <summary> /// <summary>
@ -756,10 +756,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override int AbsoluteMinimumIndex() public override int AbsoluteMinimumIndex()
{ {
var index = 0; var index = 0;
var min = Math.Abs(this.Data[index]); var min = Math.Abs(Data[index]);
for (var i = 1; i < this.Count; i++) for (var i = 1; i < Count; i++)
{ {
var test = Math.Abs(this.Data[i]); var test = Math.Abs(Data[i]);
if (test < min) if (test < min)
{ {
index = i; index = i;
@ -776,7 +776,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The value of the absolute minimum element.</returns> /// <returns>The value of the absolute minimum element.</returns>
public override double AbsoluteMinimum() public override double AbsoluteMinimum()
{ {
return Math.Abs(this.Data[this.AbsoluteMinimumIndex()]); return Math.Abs(Data[AbsoluteMinimumIndex()]);
} }
/// <summary> /// <summary>
@ -785,7 +785,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The value of the absolute maximum element.</returns> /// <returns>The value of the absolute maximum element.</returns>
public override double AbsoluteMaximum() public override double AbsoluteMaximum()
{ {
return Math.Abs(this.Data[this.AbsoluteMaximumIndex()]); return Math.Abs(Data[AbsoluteMaximumIndex()]);
} }
/// <summary> /// <summary>
@ -795,10 +795,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override int AbsoluteMaximumIndex() public override int AbsoluteMaximumIndex()
{ {
var index = 0; var index = 0;
var max = Math.Abs(this.Data[index]); var max = Math.Abs(Data[index]);
for (var i = 1; i < this.Count; i++) for (var i = 1; i < Count; i++)
{ {
var test = Math.Abs(this.Data[i]); var test = Math.Abs(Data[i]);
if (test > max) if (test > max)
{ {
index = i; index = i;
@ -822,7 +822,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception> /// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public override Vector SubVector(int index, int length) public override Vector SubVector(int index, int length)
{ {
if (index < 0 || index >= this.Count) if (index < 0 || index >= Count)
{ {
throw new ArgumentOutOfRangeException("index"); throw new ArgumentOutOfRangeException("index");
} }
@ -832,7 +832,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("length"); throw new ArgumentOutOfRangeException("length");
} }
if (index + length > this.Count) if (index + length > Count)
{ {
throw new ArgumentOutOfRangeException("length"); throw new ArgumentOutOfRangeException("length");
} }
@ -842,7 +842,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For( CommonParallel.For(
index, index,
index + length, index + length,
i => result.Data[i - index] = this.Data[i]); i => result.Data[i - index] = Data[i]);
return result; return result;
} }
@ -859,7 +859,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("values"); throw new ArgumentNullException("values");
} }
if (values.Length != this.Count) if (values.Length != Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values");
} }
@ -867,7 +867,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For( CommonParallel.For(
0, 0,
values.Length, values.Length,
i => this.Data[i] = values[i]); i => Data[i] = values[i]);
} }
/// <summary> /// <summary>
@ -877,13 +877,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override int MaximumIndex() public override int MaximumIndex()
{ {
var index = 0; var index = 0;
var max = this.Data[0]; var max = Data[0];
for (var i = 1; i < this.Count; i++) for (var i = 1; i < Count; i++)
{ {
if (max < this.Data[i]) if (max < Data[i])
{ {
index = i; index = i;
max = this.Data[i]; max = Data[i];
} }
} }
@ -897,13 +897,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override int MinimumIndex() public override int MinimumIndex()
{ {
var index = 0; var index = 0;
var min = this.Data[0]; var min = Data[0];
for (var i = 1; i < this.Count; i++) for (var i = 1; i < Count; i++)
{ {
if (min > this.Data[i]) if (min > Data[i])
{ {
index = i; index = i;
min = this.Data[i]; min = Data[i];
} }
} }
@ -917,9 +917,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double Sum() public override double Sum()
{ {
double result = 0; double result = 0;
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
result += this.Data[i]; result += Data[i];
} }
return result; return result;
@ -932,9 +932,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double SumMagnitudes() public override double SumMagnitudes()
{ {
double result = 0; double result = 0;
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
result += Math.Abs(this.Data[i]); result += Math.Abs(Data[i]);
} }
return result; return result;
@ -953,7 +953,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
@ -968,7 +968,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => this[index] *= other[index]); index => this[index] *= other[index]);
} }
} }
@ -994,12 +994,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -1007,12 +1007,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{ {
var tmp = result.CreateVector(result.Count); var tmp = result.CreateVector(result.Count);
this.PointwiseMultiply(other, tmp); PointwiseMultiply(other, tmp);
tmp.CopyTo(result); tmp.CopyTo(result);
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.PointwiseMultiply(other); result.PointwiseMultiply(other);
} }
} }
@ -1030,7 +1030,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
@ -1045,7 +1045,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => this[index] /= other[index]); index => this[index] /= other[index]);
} }
} }
@ -1071,12 +1071,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -1084,12 +1084,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{ {
var tmp = result.CreateVector(result.Count); var tmp = result.CreateVector(result.Count);
this.PointwiseDivide(other, tmp); PointwiseDivide(other, tmp);
tmp.CopyTo(result); tmp.CopyTo(result);
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.PointwiseDivide(other); result.PointwiseDivide(other);
} }
} }
@ -1116,11 +1116,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var matrix = new DenseMatrix(u.Count, v.Count); var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For( CommonParallel.For(
0, 0,
u.Count, u.Count,
i => i =>
{ {
for (int j = 0; j < v.Count; j++) for (var j = 0; j < v.Count; j++)
{ {
matrix.At(i, j, u.Data[i] * v.Data[j]); matrix.At(i, j, u.Data[i] * v.Data[j]);
} }
@ -1145,7 +1145,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
} }
var v = (DenseVector)this.CreateVector(length); var v = (DenseVector)CreateVector(length);
for (var index = 0; index < v.Data.Length; index++) for (var index = 0; index < v.Data.Length; index++)
{ {
v.Data[index] = randomDistribution.Sample(); v.Data[index] = randomDistribution.Sample();
@ -1171,7 +1171,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
} }
var v = (DenseVector)this.CreateVector(length); var v = (DenseVector)CreateVector(length);
for (var index = 0; index < v.Data.Length; index++) for (var index = 0; index < v.Data.Length; index++)
{ {
v.Data[index] = randomDistribution.Sample(); v.Data[index] = randomDistribution.Sample();
@ -1203,9 +1203,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
var sum = 0.0; var sum = 0.0;
for (var i = 0; i < this.Data.Length; i++) for (var i = 0; i < Data.Length; i++)
{ {
sum = SpecialFunctions.Hypotenuse(sum, this.Data[i]); sum = SpecialFunctions.Hypotenuse(sum, Data[i]);
} }
return sum; return sum;
@ -1219,8 +1219,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
return CommonParallel.Aggregate( return CommonParallel.Aggregate(
0, 0,
this.Count, Count,
index => Math.Abs(this.Data[index])); index => Math.Abs(Data[index]));
} }
/// <summary> /// <summary>
@ -1237,18 +1237,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (1 == p) if (1 == p)
{ {
return this.Norm1(); return Norm1();
} }
if (2 == p) if (2 == p)
{ {
return this.Norm(); return Norm();
} }
var sum = CommonParallel.Aggregate( var sum = CommonParallel.Aggregate(
0, 0,
this.Count, Count,
index => Math.Pow(Math.Abs(this.Data[index]), p)); index => Math.Pow(Math.Abs(Data[index]), p));
return Math.Pow(sum, 1.0 / p); return Math.Pow(sum, 1.0 / p);
} }
@ -1261,8 +1261,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
return CommonParallel.Select( return CommonParallel.Select(
0, 0,
this.Count, Count,
(index, localData) => localData = Math.Max(localData, Math.Abs(this.Data[index])), (index, localData) => localData = Math.Max(localData, Math.Abs(Data[index])),
Math.Max); Math.Max);
} }
@ -1426,5 +1426,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
#endregion #endregion
/// <summary>
/// Resets all values to zero.
/// </summary>
public override void Clear()
{
Array.Clear(Data, 0, Data.Length);
}
} }
} }

149
src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs

@ -49,19 +49,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{ {
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
} }
CommonParallel.For( CommonParallel.For(
0, 0,
this.RowCount, RowCount,
i => i =>
{ {
for (var j = 0; j < this.ColumnCount; j++) for (var j = 0; j < ColumnCount; j++)
{ {
this.At(i, j, this.At(i, j) + other.At(i, j)); At(i, j, At(i, j) + other.At(i, j));
} }
}); });
} }
@ -79,19 +79,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount) if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{ {
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions); throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
} }
CommonParallel.For( CommonParallel.For(
0, 0,
this.RowCount, RowCount,
i => i =>
{ {
for (var j = 0; j < this.ColumnCount; j++) for (var j = 0; j < ColumnCount; j++)
{ {
this.At(i, j, this.At(i, j) - other.At(i, j)); At(i, j, At(i, j) - other.At(i, j));
} }
}); });
} }
@ -109,12 +109,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For( CommonParallel.For(
0, 0,
this.RowCount, RowCount,
i => i =>
{ {
for (var j = 0; j < this.ColumnCount; j++) for (var j = 0; j < ColumnCount; j++)
{ {
this.At(i, j, this.At(i, j) * scalar); At(i, j, At(i, j) * scalar);
} }
}); });
} }
@ -133,17 +133,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (result.RowCount != this.RowCount) if (result.RowCount != RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "result"); throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "result");
} }
if (result.ColumnCount != this.ColumnCount) if (result.ColumnCount != ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension, "result"); throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension, "result");
} }
this.CopyTo(result); CopyTo(result);
result.Multiply(scalar); result.Multiply(scalar);
} }
@ -156,7 +156,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <c>this.ColumnCount != rightSide.Count</c>.</exception> /// <exception cref="ArgumentException">If <c>this.ColumnCount != rightSide.Count</c>.</exception>
public virtual Vector Multiply(Vector rightSide) public virtual Vector Multiply(Vector rightSide)
{ {
var ret = this.CreateVector(this.RowCount); var ret = CreateVector(RowCount);
Multiply(rightSide, ret); Multiply(rightSide, ret);
return ret; return ret;
} }
@ -177,7 +177,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("rightSide"); throw new ArgumentNullException("rightSide");
} }
if (this.ColumnCount != rightSide.Count) if (ColumnCount != rightSide.Count)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "rightSide"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "rightSide");
} }
@ -187,7 +187,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.RowCount != result.Count) if (RowCount != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
@ -202,13 +202,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
CommonParallel.For( CommonParallel.For(
0, 0,
this.RowCount, RowCount,
i => i =>
{ {
double s = 0; double s = 0;
for (var j = 0; j != this.ColumnCount; j++) for (var j = 0; j != ColumnCount; j++)
{ {
s += this.At(i, j) * rightSide[j]; s += At(i, j) * rightSide[j];
} }
result[i] = s; result[i] = s;
@ -225,8 +225,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <strong>this.RowCount != <paramref name="leftSide"/>.Count</strong>.</exception> /// <exception cref="ArgumentException">If <strong>this.RowCount != <paramref name="leftSide"/>.Count</strong>.</exception>
public virtual Vector LeftMultiply(Vector leftSide) public virtual Vector LeftMultiply(Vector leftSide)
{ {
var ret = this.CreateVector(this.ColumnCount); var ret = CreateVector(ColumnCount);
this.LeftMultiply(leftSide, ret); LeftMultiply(leftSide, ret);
return ret; return ret;
} }
@ -246,7 +246,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("leftSide"); throw new ArgumentNullException("leftSide");
} }
if (this.RowCount != leftSide.Count) if (RowCount != leftSide.Count)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "leftSide"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "leftSide");
} }
@ -256,7 +256,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.ColumnCount != result.Count) if (ColumnCount != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
@ -264,20 +264,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(leftSide, result)) if (ReferenceEquals(leftSide, result))
{ {
var tmp = result.CreateVector(result.Count); var tmp = result.CreateVector(result.Count);
this.LeftMultiply(leftSide, tmp); LeftMultiply(leftSide, tmp);
tmp.CopyTo(result); tmp.CopyTo(result);
} }
else else
{ {
CommonParallel.For( CommonParallel.For(
0, 0,
this.RowCount, RowCount,
j => j =>
{ {
double s = 0; double s = 0;
for (var i = 0; i != leftSide.Count; i++) for (var i = 0; i != leftSide.Count; i++)
{ {
s += leftSide[i] * this.At(i, j); s += leftSide[i] * At(i, j);
} }
result[j] = s; result[j] = s;
@ -306,12 +306,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.ColumnCount != other.RowCount) if (ColumnCount != other.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions); throw new ArgumentException(Resources.ArgumentMatrixDimensions);
} }
if (result.RowCount != this.RowCount || result.ColumnCount != other.ColumnCount) if (result.RowCount != RowCount || result.ColumnCount != other.ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions); throw new ArgumentException(Resources.ArgumentMatrixDimensions);
} }
@ -326,15 +326,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
CommonParallel.For( CommonParallel.For(
0, 0,
this.RowCount, RowCount,
j => j =>
{ {
for (var i = 0; i != other.ColumnCount; i++) for (var i = 0; i != other.ColumnCount; i++)
{ {
double s = 0; double s = 0;
for (var l = 0; l < this.ColumnCount; l++) for (var l = 0; l < ColumnCount; l++)
{ {
s += this.At(j, l) * other.At(l, i); s += At(j, l) * other.At(l, i);
} }
result.At(j, i, s); result.At(j, i, s);
@ -357,12 +357,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.ColumnCount != other.RowCount) if (ColumnCount != other.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions); throw new ArgumentException(Resources.ArgumentMatrixDimensions);
} }
var result = this.CreateMatrix(this.RowCount, other.ColumnCount); var result = CreateMatrix(RowCount, other.ColumnCount);
Multiply(other, result); Multiply(other, result);
return result; return result;
} }
@ -372,7 +372,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary> /// </summary>
public virtual void Negate() public virtual void Negate()
{ {
this.Multiply(-1); Multiply(-1);
} }
/// <summary> /// <summary>
@ -388,12 +388,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (result.RowCount != this.RowCount || result.ColumnCount != this.ColumnCount) if (result.RowCount != RowCount || result.ColumnCount != ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions); throw new ArgumentException(Resources.ArgumentMatrixDimensions);
} }
this.CopyTo(result); CopyTo(result);
result.Negate(); result.Negate();
} }
@ -614,13 +614,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) if (ColumnCount != other.ColumnCount || RowCount != other.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "other"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "other");
} }
var result = this.CreateMatrix(this.RowCount, this.ColumnCount); var result = CreateMatrix(RowCount, ColumnCount);
this.PointwiseMultiply(other, result); PointwiseMultiply(other, result);
return result; return result;
} }
@ -645,24 +645,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) if (ColumnCount != other.ColumnCount || RowCount != other.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
if (this.ColumnCount != result.ColumnCount || this.RowCount != result.RowCount) if (ColumnCount != result.ColumnCount || RowCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
CommonParallel.For( CommonParallel.For(
0, 0,
this.ColumnCount, ColumnCount,
j => j =>
{ {
for (var i = 0; i < this.RowCount; i++) for (var i = 0; i < RowCount; i++)
{ {
result.At(i, j, this.At(i, j) * other.At(i, j)); result.At(i, j, At(i, j) * other.At(i, j));
} }
}); });
} }
@ -681,13 +681,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) if (ColumnCount != other.ColumnCount || RowCount != other.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "other"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "other");
} }
var result = this.CreateMatrix(this.RowCount, this.ColumnCount); var result = CreateMatrix(RowCount, ColumnCount);
this.PointwiseDivide(other, result); PointwiseDivide(other, result);
return result; return result;
} }
@ -712,24 +712,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount) if (ColumnCount != other.ColumnCount || RowCount != other.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
if (this.ColumnCount != result.ColumnCount || this.RowCount != result.RowCount) if (ColumnCount != result.ColumnCount || RowCount != result.RowCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
CommonParallel.For( CommonParallel.For(
0, 0,
this.ColumnCount, ColumnCount,
j => j =>
{ {
for (var i = 0; i < this.RowCount; i++) for (var i = 0; i < RowCount; i++)
{ {
result.At(i, j, this.At(i, j) / other.At(i, j)); result.At(i, j, At(i, j) / other.At(i, j));
} }
}); });
} }
@ -757,10 +757,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns"); throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns");
} }
var matrix = this.CreateMatrix(numberOfRows, numberOfColumns); var matrix = CreateMatrix(numberOfRows, numberOfColumns);
CommonParallel.For( CommonParallel.For(
0, 0,
this.ColumnCount, ColumnCount,
j => j =>
{ {
for (var i = 0; i < matrix.RowCount; i++) for (var i = 0; i < matrix.RowCount; i++)
@ -795,10 +795,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns"); throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns");
} }
var matrix = this.CreateMatrix(numberOfRows, numberOfColumns); var matrix = CreateMatrix(numberOfRows, numberOfColumns);
CommonParallel.For( CommonParallel.For(
0, 0,
this.ColumnCount, ColumnCount,
j => j =>
{ {
for (var i = 0; i < matrix.RowCount; i++) for (var i = 0; i < matrix.RowCount; i++)
@ -817,12 +817,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If the matrix is not square</exception> /// <exception cref="ArgumentException">If the matrix is not square</exception>
public virtual double Trace() public virtual double Trace()
{ {
if (this.RowCount != this.ColumnCount) if (RowCount != ColumnCount)
{ {
throw new ArgumentException(Resources.ArgumentMatrixSquare); throw new ArgumentException(Resources.ArgumentMatrixSquare);
} }
return CommonParallel.Aggregate(0, this.RowCount, i => this[i, i]); return CommonParallel.Aggregate(0, RowCount, i => this[i, i]);
} }
/// <summary> /// <summary>
@ -863,8 +863,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
var result = this.CreateMatrix(this.RowCount * other.RowCount, this.ColumnCount * other.ColumnCount); var result = CreateMatrix(RowCount * other.RowCount, ColumnCount * other.ColumnCount);
this.KroneckerProduct(other, result); KroneckerProduct(other, result);
return result; return result;
} }
@ -889,19 +889,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (result.RowCount != (this.RowCount * other.RowCount) || result.ColumnCount != (this.ColumnCount * other.ColumnCount)) if (result.RowCount != (RowCount * other.RowCount) || result.ColumnCount != (ColumnCount * other.ColumnCount))
{ {
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result"); throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
} }
CommonParallel.For( CommonParallel.For(
0, 0,
this.ColumnCount, ColumnCount,
j => j =>
{ {
for (var i = 0; i < this.RowCount; i++) for (var i = 0; i < RowCount; i++)
{ {
result.SetSubMatrix(i * other.RowCount, other.RowCount, j * other.ColumnCount, other.ColumnCount, this.At(i, j) * other); result.SetSubMatrix(i * other.RowCount, other.RowCount, j * other.ColumnCount, other.ColumnCount, At(i, j) * other);
} }
}); });
} }
@ -919,15 +919,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("p", Resources.ArgumentMustBePositive); throw new ArgumentOutOfRangeException("p", Resources.ArgumentMustBePositive);
} }
var ret = this.Clone(); var ret = Clone();
CommonParallel.For( CommonParallel.For(
0, 0,
this.ColumnCount, ColumnCount,
i => i =>
{ {
var coli = this.Column(i); var coli = Column(i);
var norm = coli.NormP(p); var norm = coli.NormP(p);
for (var j = 0; j < this.RowCount; j++) for (var j = 0; j < RowCount; j++)
{ {
ret[j, i] = coli[j] / norm; ret[j, i] = coli[j] / norm;
} }
@ -948,21 +948,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("p", Resources.ArgumentMustBePositive); throw new ArgumentOutOfRangeException("p", Resources.ArgumentMustBePositive);
} }
var ret = this.Clone(); var ret = Clone();
CommonParallel.For( CommonParallel.For(
0, 0,
this.ColumnCount, ColumnCount,
j => j =>
{ {
var rowj = this.Row(j); var rowj = Row(j);
var norm = rowj.NormP(p); var norm = rowj.NormP(p);
for (var i = 0; i < this.RowCount; i++) for (var i = 0; i < RowCount; i++)
{ {
ret[i, j] = rowj[j] / norm; ret[i, j] = rowj[j] / norm;
} }
}); });
return ret; return ret;
} }
} }
} }

384
src/Numerics/LinearAlgebra/Double/Matrix.cs

File diff suppressed because it is too large

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

File diff suppressed because it is too large

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

@ -40,9 +40,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public class SparseVector : Vector public class SparseVector : Vector
{ {
/// <summary> /// <summary>
/// Lock ojbect for the indexer. /// Lock object for the indexer.
/// </summary> /// </summary>
private readonly object lockObject = new object(); private readonly object _lockObject = new object();
/// <summary> /// <summary>
/// Gets the vector's internal data. The array containing the actual values; only the non-zero values are stored. /// Gets the vector's internal data. The array containing the actual values; only the non-zero values are stored.
@ -96,22 +96,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
if (value == 0.0) if (value == 0.0)
{ {
// Skip adding values // Skip adding values
return; return;
} }
// We already know that this vector is "full", let's allocate all needed memory // We already know that this vector is "full", let's allocate all needed memory
this._nonZeroValues = new double[size]; _nonZeroValues = new double[size];
this._nonZeroIndices = new int[size]; _nonZeroIndices = new int[size];
this.NonZerosCount = size; NonZerosCount = size;
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => index =>
{ {
this._nonZeroValues[index] = value; _nonZeroValues[index] = value;
this._nonZeroIndices[index] = index; _nonZeroIndices[index] = index;
}); });
} }
@ -134,13 +134,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this._nonZeroValues = new double[vector.NonZerosCount]; _nonZeroValues = new double[vector.NonZerosCount];
this._nonZeroIndices = new int[vector.NonZerosCount]; _nonZeroIndices = new int[vector.NonZerosCount];
this.NonZerosCount = vector.NonZerosCount; NonZerosCount = vector.NonZerosCount;
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
Buffer.BlockCopy(vector._nonZeroValues, 0, this._nonZeroValues, 0, vector.NonZerosCount * Constants.SizeOfDouble); Buffer.BlockCopy(vector._nonZeroValues, 0, _nonZeroValues, 0, vector.NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(vector._nonZeroIndices, 0, this._nonZeroIndices, 0, vector.NonZerosCount * Constants.SizeOfInt); Buffer.BlockCopy(vector._nonZeroIndices, 0, _nonZeroIndices, 0, vector.NonZerosCount * Constants.SizeOfInt);
} }
} }
@ -154,12 +154,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public SparseVector(SparseVector other) : this(other.Count) public SparseVector(SparseVector other) : this(other.Count)
{ {
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
this._nonZeroValues = new double[other.NonZerosCount]; _nonZeroValues = new double[other.NonZerosCount];
this._nonZeroIndices = new int[other.NonZerosCount]; _nonZeroIndices = new int[other.NonZerosCount];
this.NonZerosCount = other.NonZerosCount; NonZerosCount = other.NonZerosCount;
Buffer.BlockCopy(other._nonZeroValues, 0, this._nonZeroValues, 0, other.NonZerosCount * Constants.SizeOfDouble); Buffer.BlockCopy(other._nonZeroValues, 0, _nonZeroValues, 0, other.NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(other._nonZeroIndices, 0, this._nonZeroIndices, 0, other.NonZerosCount * Constants.SizeOfInt); Buffer.BlockCopy(other._nonZeroIndices, 0, _nonZeroIndices, 0, other.NonZerosCount * Constants.SizeOfInt);
} }
/// <summary> /// <summary>
@ -183,10 +183,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a column matrix.</returns> /// <returns>This vector as a column matrix.</returns>
public override Matrix ToColumnMatrix() public override Matrix ToColumnMatrix()
{ {
var matrix = new SparseMatrix(this.Count, 1); var matrix = new SparseMatrix(Count, 1);
for (var i = 0; i < this.NonZerosCount; i++) for (var i = 0; i < NonZerosCount; i++)
{ {
matrix[this._nonZeroIndices[i], 0] = this._nonZeroValues[i]; matrix[_nonZeroIndices[i], 0] = _nonZeroValues[i];
} }
return matrix; return matrix;
@ -198,10 +198,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a row matrix.</returns> /// <returns>This vector as a row matrix.</returns>
public override Matrix ToRowMatrix() public override Matrix ToRowMatrix()
{ {
var matrix = new SparseMatrix(1, this.Count); var matrix = new SparseMatrix(1, Count);
for (var i = 0; i < this.NonZerosCount; i++) for (var i = 0; i < NonZerosCount; i++)
{ {
matrix[0, this._nonZeroIndices[i]] = this._nonZeroValues[i]; matrix[0, _nonZeroIndices[i]] = _nonZeroValues[i];
} }
return matrix; return matrix;
@ -217,18 +217,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double
get get
{ {
// If index is out of bounds // If index is out of bounds
if ((index < 0) || (index >= this.Count)) if ((index < 0) || (index >= Count))
{ {
throw new IndexOutOfRangeException(); throw new IndexOutOfRangeException();
} }
lock (this.lockObject) lock (_lockObject)
{ {
// Search if item idex exists in NonZeroIndices array in range "0 - real nonzero values count" // Search if item idex exists in NonZeroIndices array in range "0 - real nonzero values count"
var itemIndex = Array.BinarySearch(this._nonZeroIndices, 0, this.NonZerosCount, index); var itemIndex = Array.BinarySearch(_nonZeroIndices, 0, NonZerosCount, index);
if (itemIndex >= 0) if (itemIndex >= 0)
{ {
return this._nonZeroValues[itemIndex]; return _nonZeroValues[itemIndex];
} }
} }
@ -238,14 +238,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
set set
{ {
// If index is out of bounds // If index is out of bounds
if ((index < 0) || (index >= this.Count)) if ((index < 0) || (index >= Count))
{ {
throw new IndexOutOfRangeException(); throw new IndexOutOfRangeException();
} }
lock (this.lockObject) lock (_lockObject)
{ {
this.SetValue(index, value); SetValue(index, value);
} }
} }
} }
@ -284,11 +284,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
/// <summary> /// <summary>
/// Clears this instance. /// Resets all values to zero.
/// </summary> /// </summary>
public void Clear() public override void Clear()
{ {
this.NonZerosCount = 0; NonZerosCount = 0;
} }
/// <summary> /// <summary>
@ -310,7 +310,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("target"); throw new ArgumentNullException("target");
} }
if (this.Count != target.Count) if (Count != target.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
} }
@ -325,33 +325,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => target[index] = this[index]); index => target[index] = this[index]);
} }
else else
{ {
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value // Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
otherVector._nonZeroValues = new double[this.NonZerosCount]; otherVector._nonZeroValues = new double[NonZerosCount];
otherVector._nonZeroIndices = new int[this.NonZerosCount]; otherVector._nonZeroIndices = new int[NonZerosCount];
otherVector.NonZerosCount = this.NonZerosCount; otherVector.NonZerosCount = NonZerosCount;
Buffer.BlockCopy(this._nonZeroValues, 0, otherVector._nonZeroValues, 0, this.NonZerosCount * Constants.SizeOfDouble); Buffer.BlockCopy(_nonZeroValues, 0, otherVector._nonZeroValues, 0, NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(this._nonZeroIndices, 0, otherVector._nonZeroIndices, 0, this.NonZerosCount * Constants.SizeOfInt); Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
} }
} }
#region Operators and supplementary functions #region Operators and supplementary functions
// NOTE: There are no operators as:
// public static implicit operator SparseVector(double[] array)
// and
// public static implicit operator double[](SparseVector vector)
// as it is in DenseVector. Because when creating vector from double[] values are copied to internal storage and if user wants
// to get double[] he should call SparseVector.ToArray(), then double[] will be generated and returned to a user.\
// In DenseVector implementation reference to double[] is assigned to interanl storage when casting from double[] and returned
// when casting to double[]
/// <summary> /// <summary>
/// Adds a scalar to each element of the vector. /// Adds a scalar to each element of the vector.
/// </summary> /// </summary>
@ -363,7 +353,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
this[i] += scalar; this[i] += scalar;
} }
@ -383,12 +373,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
this.CopyTo(result); CopyTo(result);
result.Add(scalar); result.Add(scalar);
} }
@ -405,7 +395,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
@ -418,7 +408,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.AddScaledSparceVector(1.0, sparseVector); AddScaledSparceVector(1.0, sparseVector);
} }
} }
@ -434,7 +424,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
@ -453,21 +443,21 @@ namespace MathNet.Numerics.LinearAlgebra.Double
// To avoid such problem lets change values in internal storage of "this" // To avoid such problem lets change values in internal storage of "this"
if (alpha == 1.0) if (alpha == 1.0)
{ {
for (var i = 0; i < this.NonZerosCount; i++) for (var i = 0; i < NonZerosCount; i++)
{ {
this._nonZeroValues[i] += this._nonZeroValues[i]; _nonZeroValues[i] += _nonZeroValues[i];
} }
} }
else if (alpha == -1.0) else if (alpha == -1.0)
{ {
this.Clear(); // Vector is subtracted from itself Clear(); // Vector is subtracted from itself
return; return;
} }
else else
{ {
for (var i = 0; i < this.NonZerosCount; i++) for (var i = 0; i < NonZerosCount; i++)
{ {
this._nonZeroValues[i] += alpha * this._nonZeroValues[i]; _nonZeroValues[i] += alpha * _nonZeroValues[i];
} }
} }
} }
@ -507,12 +497,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -525,7 +515,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.Add(other); result.Add(other);
} }
} }
@ -588,7 +578,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
this[i] -= scalar; this[i] -= scalar;
} }
@ -608,12 +598,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
this.CopyTo(result); CopyTo(result);
result.Subtract(scalar); result.Subtract(scalar);
} }
@ -630,7 +620,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
@ -643,7 +633,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.AddScaledSparceVector(-1.0, sparseVector); AddScaledSparceVector(-1.0, sparseVector);
} }
} }
@ -663,12 +653,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -681,7 +671,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.Subtract(other); result.Subtract(other);
} }
} }
@ -739,19 +729,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <remarks>Added as an alternative to the unary negation operator.</remarks> /// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector Negate() public override Vector Negate()
{ {
var result = new SparseVector(this.Count) var result = new SparseVector(Count)
{ {
_nonZeroValues = new double[this.NonZerosCount], _nonZeroValues = new double[NonZerosCount],
_nonZeroIndices = new int[this.NonZerosCount], _nonZeroIndices = new int[NonZerosCount],
NonZerosCount = this.NonZerosCount NonZerosCount = NonZerosCount
}; };
Buffer.BlockCopy(this._nonZeroIndices, 0, result._nonZeroIndices, 0, this.NonZerosCount * Constants.SizeOfInt); Buffer.BlockCopy(_nonZeroIndices, 0, result._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
CommonParallel.For( CommonParallel.For(
0, 0,
this.NonZerosCount, NonZerosCount,
index => result._nonZeroValues[index] = -this._nonZeroValues[index]); index => result._nonZeroValues[index] = -_nonZeroValues[index]);
return result; return result;
} }
@ -769,11 +759,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (scalar == 0) if (scalar == 0)
{ {
this.Clear(); // Set array empty Clear(); // Set array empty
return; return;
} }
Control.LinearAlgebraProvider.ScaleArray(scalar, this._nonZeroValues); Control.LinearAlgebraProvider.ScaleArray(scalar, _nonZeroValues);
} }
/// <summary> /// <summary>
@ -790,7 +780,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
@ -798,9 +788,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
double result = 0; double result = 0;
// base implementation iterates though all elements, but we need only take non-zeros // base implementation iterates though all elements, but we need only take non-zeros
for (var i = 0; i < this.NonZerosCount; i++) for (var i = 0; i < NonZerosCount; i++)
{ {
result += this._nonZeroValues[i] * other[this._nonZeroIndices[i]]; result += _nonZeroValues[i] * other[_nonZeroIndices[i]];
} }
return result; return result;
@ -897,17 +887,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The index of absolute minimum element.</returns> /// <returns>The index of absolute minimum element.</returns>
public override int AbsoluteMinimumIndex() public override int AbsoluteMinimumIndex()
{ {
if (this.NonZerosCount == 0) if (NonZerosCount == 0)
{ {
// No non-zero elements. Return 0 // No non-zero elements. Return 0
return 0; return 0;
} }
var index = 0; var index = 0;
var min = Math.Abs(this._nonZeroValues[index]); var min = Math.Abs(_nonZeroValues[index]);
for (var i = 1; i < this.NonZerosCount; i++) for (var i = 1; i < NonZerosCount; i++)
{ {
var test = Math.Abs(this._nonZeroValues[i]); var test = Math.Abs(_nonZeroValues[i]);
if (test < min) if (test < min)
{ {
index = i; index = i;
@ -915,7 +905,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
} }
return this._nonZeroIndices[index]; return _nonZeroIndices[index];
} }
/// <summary> /// <summary>
@ -931,7 +921,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception> /// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public override Vector SubVector(int index, int length) public override Vector SubVector(int index, int length)
{ {
if (index < 0 || index >= this.Count) if (index < 0 || index >= Count)
{ {
throw new ArgumentOutOfRangeException("index"); throw new ArgumentOutOfRangeException("index");
} }
@ -941,7 +931,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("length"); throw new ArgumentOutOfRangeException("length");
} }
if (index + length > this.Count) if (index + length > Count)
{ {
throw new ArgumentOutOfRangeException("length"); throw new ArgumentOutOfRangeException("length");
} }
@ -968,7 +958,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("values"); throw new ArgumentNullException("values");
} }
if (values.Length != this.Count) if (values.Length != Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values");
} }
@ -985,23 +975,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The index of absolute maximum element.</returns> /// <returns>The index of absolute maximum element.</returns>
public override int MaximumIndex() public override int MaximumIndex()
{ {
if (this.NonZerosCount == 0) if (NonZerosCount == 0)
{ {
return 0; return 0;
} }
var index = 0; var index = 0;
var max = this._nonZeroValues[0]; var max = _nonZeroValues[0];
for (var i = 1; i < this.NonZerosCount; i++) for (var i = 1; i < NonZerosCount; i++)
{ {
if (max < this._nonZeroValues[i]) if (max < _nonZeroValues[i])
{ {
index = i; index = i;
max = this._nonZeroValues[i]; max = _nonZeroValues[i];
} }
} }
return this._nonZeroIndices[index]; return _nonZeroIndices[index];
} }
/// <summary> /// <summary>
@ -1010,23 +1000,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The index of minimum element.</returns> /// <returns>The index of minimum element.</returns>
public override int MinimumIndex() public override int MinimumIndex()
{ {
if (this.NonZerosCount == 0) if (NonZerosCount == 0)
{ {
return 0; return 0;
} }
var index = 0; var index = 0;
var min = this._nonZeroValues[0]; var min = _nonZeroValues[0];
for (var i = 1; i < this.NonZerosCount; i++) for (var i = 1; i < NonZerosCount; i++)
{ {
if (min > this._nonZeroValues[i]) if (min > _nonZeroValues[i])
{ {
index = i; index = i;
min = this._nonZeroValues[i]; min = _nonZeroValues[i];
} }
} }
return this._nonZeroIndices[index]; return _nonZeroIndices[index];
} }
/// <summary> /// <summary>
@ -1036,9 +1026,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double Sum() public override double Sum()
{ {
double result = 0; double result = 0;
for (var i = 0; i < this.NonZerosCount; i++) for (var i = 0; i < NonZerosCount; i++)
{ {
result += this._nonZeroValues[i]; result += _nonZeroValues[i];
} }
return result; return result;
@ -1051,9 +1041,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double SumMagnitudes() public override double SumMagnitudes()
{ {
double result = 0; double result = 0;
for (var i = 0; i < this.NonZerosCount; i++) for (var i = 0; i < NonZerosCount; i++)
{ {
result += Math.Abs(this._nonZeroValues[i]); result += Math.Abs(_nonZeroValues[i]);
} }
return result; return result;
@ -1072,13 +1062,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
// We cannot iterate using NonZeroCount because the value may be changed (if multiply by 0) // We cannot iterate using NonZeroCount because the value may be changed (if multiply by 0)
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
this[i] *= other[i]; this[i] *= other[i];
} }
@ -1105,12 +1095,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -1118,12 +1108,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{ {
var tmp = result.CreateVector(result.Count); var tmp = result.CreateVector(result.Count);
this.PointwiseMultiply(other, tmp); PointwiseMultiply(other, tmp);
tmp.CopyTo(result); tmp.CopyTo(result);
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.PointwiseMultiply(other); result.PointwiseMultiply(other);
} }
} }
@ -1141,15 +1131,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
// base implementation iterates though all elements, but we need only take non-zeros // base implementation iterates though all elements, but we need only take non-zeros
for (var i = 0; i < this.NonZerosCount; i++) for (var i = 0; i < NonZerosCount; i++)
{ {
this[this._nonZeroIndices[i]] /= other[this._nonZeroIndices[i]]; this[_nonZeroIndices[i]] /= other[_nonZeroIndices[i]];
} }
} }
@ -1174,12 +1164,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -1187,12 +1177,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{ {
var tmp = result.CreateVector(result.Count); var tmp = result.CreateVector(result.Count);
this.PointwiseDivide(other, tmp); PointwiseDivide(other, tmp);
tmp.CopyTo(result); tmp.CopyTo(result);
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.PointwiseDivide(other); result.PointwiseDivide(other);
} }
} }
@ -1249,7 +1239,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
} }
var v = (SparseVector)this.CreateVector(length); var v = (SparseVector)CreateVector(length);
for (var index = 0; index < v.Count; index++) for (var index = 0; index < v.Count; index++)
{ {
v[index] = randomDistribution.Sample(); v[index] = randomDistribution.Sample();
@ -1275,7 +1265,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
} }
var v = (SparseVector)this.CreateVector(length); var v = (SparseVector)CreateVector(length);
for (var index = 0; index < v.Count; index++) for (var index = 0; index < v.Count; index++)
{ {
v[index] = randomDistribution.Sample(); v[index] = randomDistribution.Sample();
@ -1315,8 +1305,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var sum = CommonParallel.Aggregate( var sum = CommonParallel.Aggregate(
0, 0,
this.NonZerosCount, NonZerosCount,
index => Math.Pow(Math.Abs(this._nonZeroValues[index]), p)); index => Math.Pow(Math.Abs(_nonZeroValues[index]), p));
return Math.Pow(sum, 1.0 / p); return Math.Pow(sum, 1.0 / p);
} }
@ -1327,7 +1317,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>Scalar <c>ret = max(abs(this[i]))</c></returns> /// <returns>Scalar <c>ret = max(abs(this[i]))</c></returns>
public override double NormInfinity() public override double NormInfinity()
{ {
return CommonParallel.Select(0, this.NonZerosCount, (index, localData) => localData = Math.Max(localData, Math.Abs(this._nonZeroValues[index])), Math.Max); return CommonParallel.Select(0, NonZerosCount, (index, localData) => localData = Math.Max(localData, Math.Abs(_nonZeroValues[index])), Math.Max);
} }
#endregion #endregion
@ -1500,7 +1490,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
private void SetValue(int index, double value) private void SetValue(int index, double value)
{ {
// Search if "index" already exists in range "0 - real nonzero values count" // Search if "index" already exists in range "0 - real nonzero values count"
var itemIndex = Array.BinarySearch(this._nonZeroIndices, 0, this.NonZerosCount, index); var itemIndex = Array.BinarySearch(_nonZeroIndices, 0, NonZerosCount, index);
if (itemIndex >= 0) if (itemIndex >= 0)
{ {
@ -1508,56 +1498,61 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (value == 0.0) if (value == 0.0)
{ {
// Value is zero. Let's delete it from Values and Indices array // Value is zero. Let's delete it from Values and Indices array
for (var i = itemIndex + 1; i < this.NonZerosCount; i++) for (var i = itemIndex + 1; i < NonZerosCount; i++)
{ {
this._nonZeroValues[i - 1] = this._nonZeroValues[i]; _nonZeroValues[i - 1] = _nonZeroValues[i];
this._nonZeroIndices[i - 1] = this._nonZeroIndices[i]; _nonZeroIndices[i - 1] = _nonZeroIndices[i];
} }
this.NonZerosCount -= 1; NonZerosCount -= 1;
// Check if the storage needs to be shrink. This is reasonable to do if // Check if the storage needs to be shrink. This is reasonable to do if
// there are a lot of non-zero elements and storage is two times bigger // there are a lot of non-zero elements and storage is two times bigger
if ((this.NonZerosCount > 1024) && (this.NonZerosCount < this._nonZeroIndices.Length / 2)) if ((NonZerosCount > 1024) && (NonZerosCount < _nonZeroIndices.Length / 2))
{ {
Array.Resize(ref this._nonZeroValues, this.NonZerosCount); Array.Resize(ref _nonZeroValues, NonZerosCount);
Array.Resize(ref this._nonZeroIndices, this.NonZerosCount); Array.Resize(ref _nonZeroIndices, NonZerosCount);
} }
} }
else else
{ {
this._nonZeroValues[itemIndex] = value; _nonZeroValues[itemIndex] = value;
} }
} }
else else
{ {
if (value == 0.0)
{
return;
}
itemIndex = ~itemIndex; // Index where to put new value itemIndex = ~itemIndex; // Index where to put new value
// Check if the storage needs to be increased // Check if the storage needs to be increased
if ((this.NonZerosCount == this._nonZeroValues.Length) && (this.NonZerosCount < this.Count)) if ((NonZerosCount == _nonZeroValues.Length) && (NonZerosCount < Count))
{ {
// Value and Indices arrays are completely full so we increase the size // Value and Indices arrays are completely full so we increase the size
var size = Math.Min(this._nonZeroValues.Length + this.GrowthSize(), this.Count); var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count);
Array.Resize(ref this._nonZeroValues, size); Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref this._nonZeroIndices, size); Array.Resize(ref _nonZeroIndices, size);
} }
// Move all values (with an position larger than index) in the value array // Move all values (with an position larger than index) in the value array
// to the next position // to the next position
// move all values (with an position larger than index) in the columIndices // move all values (with an position larger than index) in the columIndices
// array to the next position // array to the next position
for (var i = this.NonZerosCount - 1; i > itemIndex - 1; i--) for (var i = NonZerosCount - 1; i > itemIndex - 1; i--)
{ {
this._nonZeroValues[i + 1] = this._nonZeroValues[i]; _nonZeroValues[i + 1] = _nonZeroValues[i];
this._nonZeroIndices[i + 1] = this._nonZeroIndices[i]; _nonZeroIndices[i + 1] = _nonZeroIndices[i];
} }
// Add the value and the column index // Add the value and the column index
this._nonZeroValues[itemIndex] = value; _nonZeroValues[itemIndex] = value;
this._nonZeroIndices[itemIndex] = index; _nonZeroIndices[itemIndex] = index;
// increase the number of non-zero numbers by one // increase the number of non-zero numbers by one
this.NonZerosCount += 1; NonZerosCount += 1;
} }
} }
@ -1569,19 +1564,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
private int GrowthSize() private int GrowthSize()
{ {
int delta; int delta;
if (this._nonZeroValues.Length > 1024) if (_nonZeroValues.Length > 1024)
{ {
delta = this._nonZeroValues.Length / 4; delta = _nonZeroValues.Length / 4;
} }
else else
{ {
if (this._nonZeroValues.Length > 256) if (_nonZeroValues.Length > 256)
{ {
delta = 512; delta = 512;
} }
else else
{ {
delta = this._nonZeroValues.Length > 64 ? 128 : 32; delta = _nonZeroValues.Length > 64 ? 128 : 32;
} }
} }
@ -1612,15 +1607,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return true; return true;
} }
if ((this.Count != sparseVector.Count) || (this.NonZerosCount != sparseVector.NonZerosCount)) if ((Count != sparseVector.Count) || (NonZerosCount != sparseVector.NonZerosCount))
{ {
return false; return false;
} }
// If all else fails, perform element wise comparison. // If all else fails, perform element wise comparison.
for (var index = 0; index < this.NonZerosCount; index++) for (var index = 0; index < NonZerosCount; index++)
{ {
if (!this._nonZeroValues[index].AlmostEqual(sparseVector._nonZeroValues[index]) || (this._nonZeroIndices[index] != sparseVector._nonZeroIndices[index])) if (!_nonZeroValues[index].AlmostEqual(sparseVector._nonZeroValues[index]) || (_nonZeroIndices[index] != sparseVector._nonZeroIndices[index]))
{ {
return false; return false;
} }
@ -1637,14 +1632,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public override int GetHashCode() public override int GetHashCode()
{ {
var hashNum = Math.Min(this.NonZerosCount, 20); var hashNum = Math.Min(NonZerosCount, 20);
long hash = 0; long hash = 0;
for (var i = 0; i < hashNum; i++) for (var i = 0; i < hashNum; i++)
{ {
#if SILVERLIGHT #if SILVERLIGHT
hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i]); hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i]);
#else #else
hash ^= BitConverter.DoubleToInt64Bits(this._nonZeroValues[i]); hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i]);
#endif #endif
} }
@ -1653,4 +1648,4 @@ namespace MathNet.Numerics.LinearAlgebra.Double
#endregion #endregion
} }
} }

204
src/Numerics/LinearAlgebra/Double/Vector.cs

@ -62,7 +62,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "size"); throw new ArgumentException(Resources.ArgumentMustBePositive, "size");
} }
this.Count = size; Count = size;
} }
/// <summary> /// <summary>
@ -129,7 +129,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => this[index] += scalar); index => this[index] += scalar);
} }
@ -155,14 +155,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
if (!ReferenceEquals(this, result)) if (!ReferenceEquals(this, result))
{ {
this.CopyTo(result); CopyTo(result);
} }
result.Add(scalar); result.Add(scalar);
@ -201,14 +201,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => this[index] += other[index]); index => this[index] += other[index]);
} }
@ -240,7 +240,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -253,7 +253,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.Add(other); result.Add(other);
} }
} }
@ -273,7 +273,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => this[index] -= scalar); index => this[index] -= scalar);
} }
@ -299,14 +299,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
if (!ReferenceEquals(this, result)) if (!ReferenceEquals(this, result))
{ {
this.CopyTo(result); CopyTo(result);
} }
result.Subtract(scalar); result.Subtract(scalar);
@ -345,14 +345,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => this[index] -= other[index]); index => this[index] -= other[index]);
} }
@ -384,7 +384,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -397,7 +397,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.Subtract(other); result.Subtract(other);
} }
} }
@ -417,7 +417,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => this[index] *= scalar); index => this[index] *= scalar);
} }
@ -443,14 +443,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
if (!ReferenceEquals(this, result)) if (!ReferenceEquals(this, result))
{ {
this.CopyTo(result); CopyTo(result);
} }
result.Multiply(scalar); result.Multiply(scalar);
@ -478,13 +478,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
var dot = 0.0; var dot = 0.0;
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
dot += this[i] * other[i]; dot += this[i] * other[i];
} }
@ -505,7 +505,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return; return;
} }
this.Multiply(1.0 / scalar); Multiply(1.0 / scalar);
} }
/// <summary> /// <summary>
@ -530,14 +530,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result"); throw new ArgumentNullException("result");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
if (!ReferenceEquals(this, result)) if (!ReferenceEquals(this, result))
{ {
this.CopyTo(result); CopyTo(result);
} }
result.Multiply(1.0 / scalar); result.Multiply(1.0 / scalar);
@ -556,14 +556,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => this[index] *= other[index]); index => this[index] *= other[index]);
} }
@ -588,12 +588,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -601,12 +601,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{ {
var tmp = result.CreateVector(result.Count); var tmp = result.CreateVector(result.Count);
this.PointwiseMultiply(other, tmp); PointwiseMultiply(other, tmp);
tmp.CopyTo(result); tmp.CopyTo(result);
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.PointwiseMultiply(other); result.PointwiseMultiply(other);
} }
} }
@ -624,14 +624,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => this[index] /= other[index]); index => this[index] /= other[index]);
} }
@ -656,12 +656,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other"); throw new ArgumentNullException("other");
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
} }
if (this.Count != result.Count) if (Count != result.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
} }
@ -669,12 +669,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result)) if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{ {
var tmp = result.CreateVector(result.Count); var tmp = result.CreateVector(result.Count);
this.PointwiseDivide(other, tmp); PointwiseDivide(other, tmp);
tmp.CopyTo(result); tmp.CopyTo(result);
} }
else else
{ {
this.CopyTo(result); CopyTo(result);
result.PointwiseDivide(other); result.PointwiseDivide(other);
} }
} }
@ -701,11 +701,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var matrix = new DenseMatrix(u.Count, v.Count); var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For( CommonParallel.For(
0, 0,
u.Count, u.Count,
i => i =>
{ {
for (int j = 0; j < v.Count; j++) for (var j = 0; j < v.Count; j++)
{ {
matrix.At(i, j, u[i] * v[j]); matrix.At(i, j, u[i] * v[j]);
} }
@ -722,7 +722,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// A vector with n-random elements distributed according /// A vector with n-random elements distributed according
/// to the specified random distribution. /// to the specified random distribution.
/// </returns> /// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non poisitive<see langword="null" />.</exception> /// <exception cref="ArgumentException">If the n vector is non-positive.</exception>
public virtual Vector Random(int length, IContinuousDistribution randomDistribution) public virtual Vector Random(int length, IContinuousDistribution randomDistribution)
{ {
if (length < 1) if (length < 1)
@ -730,7 +730,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
} }
var v = this.CreateVector(length); var v = CreateVector(length);
for (var index = 0; index < v.Count; index++) for (var index = 0; index < v.Count; index++)
{ {
v[index] = randomDistribution.Sample(); v[index] = randomDistribution.Sample();
@ -748,7 +748,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// A vector with n-random elements distributed according /// A vector with n-random elements distributed according
/// to the specified random distribution. /// to the specified random distribution.
/// </returns> /// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non poisitive<see langword="null" />.</exception> /// <exception cref="ArgumentException">If the n vector is not positive.</exception>
public virtual Vector Random(int length, IDiscreteDistribution randomDistribution) public virtual Vector Random(int length, IDiscreteDistribution randomDistribution)
{ {
if (length < 1) if (length < 1)
@ -756,7 +756,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "length"); throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
} }
var v = this.CreateVector(length); var v = CreateVector(length);
for (var index = 0; index < v.Count; index++) for (var index = 0; index < v.Count; index++)
{ {
v[index] = randomDistribution.Sample(); v[index] = randomDistribution.Sample();
@ -784,7 +784,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The value of the absolute minimum element.</returns> /// <returns>The value of the absolute minimum element.</returns>
public virtual double AbsoluteMinimum() public virtual double AbsoluteMinimum()
{ {
return Math.Abs(this[this.AbsoluteMinimumIndex()]); return Math.Abs(this[AbsoluteMinimumIndex()]);
} }
/// <summary> /// <summary>
@ -795,7 +795,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
var index = 0; var index = 0;
var min = Math.Abs(this[index]); var min = Math.Abs(this[index]);
for (var i = 1; i < this.Count; i++) for (var i = 1; i < Count; i++)
{ {
var test = Math.Abs(this[i]); var test = Math.Abs(this[i]);
if (test < min) if (test < min)
@ -814,7 +814,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The value of the absolute maximum element.</returns> /// <returns>The value of the absolute maximum element.</returns>
public virtual double AbsoluteMaximum() public virtual double AbsoluteMaximum()
{ {
return Math.Abs(this[this.AbsoluteMaximumIndex()]); return Math.Abs(this[AbsoluteMaximumIndex()]);
} }
/// <summary> /// <summary>
@ -825,7 +825,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
var index = 0; var index = 0;
var max = Math.Abs(this[index]); var max = Math.Abs(this[index]);
for (var i = 1; i < this.Count; i++) for (var i = 1; i < Count; i++)
{ {
var test = Math.Abs(this[i]); var test = Math.Abs(this[i]);
if (test > max) if (test > max)
@ -844,7 +844,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The value of maximum element.</returns> /// <returns>The value of maximum element.</returns>
public virtual double Maximum() public virtual double Maximum()
{ {
return this[this.MaximumIndex()]; return this[MaximumIndex()];
} }
/// <summary> /// <summary>
@ -855,7 +855,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
var index = 0; var index = 0;
var max = this[0]; var max = this[0];
for (var i = 1; i < this.Count; i++) for (var i = 1; i < Count; i++)
{ {
if (max < this[i]) if (max < this[i])
{ {
@ -873,7 +873,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The value of the minimum element.</returns> /// <returns>The value of the minimum element.</returns>
public virtual double Minimum() public virtual double Minimum()
{ {
return this[this.MinimumIndex()]; return this[MinimumIndex()];
} }
/// <summary> /// <summary>
@ -884,7 +884,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{ {
var index = 0; var index = 0;
var min = this[0]; var min = this[0];
for (var i = 1; i < this.Count; i++) for (var i = 1; i < Count; i++)
{ {
if (min > this[i]) if (min > this[i])
{ {
@ -903,7 +903,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public virtual double Sum() public virtual double Sum()
{ {
double result = 0; double result = 0;
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
result += this[i]; result += this[i];
} }
@ -918,7 +918,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public virtual double SumMagnitudes() public virtual double SumMagnitudes()
{ {
double result = 0; double result = 0;
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
result += Math.Abs(this[i]); result += Math.Abs(this[i]);
} }
@ -931,7 +931,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
#region Arithmetic Operator Overloading #region Arithmetic Operator Overloading
/// <summary> /// <summary>
/// Returns a <strong>Vector</strong> containing the same values of rightSide. /// Returns a <strong>Vector</strong> containing the same values of <paramref name="rightSide"/>.
/// </summary> /// </summary>
/// <remarks>This method is included for completeness.</remarks> /// <remarks>This method is included for completeness.</remarks>
/// <param name="rightSide">The vector to get the values from.</param> /// <param name="rightSide">The vector to get the values from.</param>
@ -978,7 +978,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
/// <summary> /// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of rightSide. /// Returns a <strong>Vector</strong> containing the negated values of <paramref name="rightSide"/>.
/// </summary> /// </summary>
/// <param name="rightSide">The vector to get the values from.</param> /// <param name="rightSide">The vector to get the values from.</param>
/// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns> /// <returns>A vector containing the negated values as <paramref name="rightSide"/>.</returns>
@ -1116,22 +1116,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Euclidean Norm also known as 2-Norm. /// Euclidean Norm also known as 2-Norm.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// Scalar ret = sqrt(sum(this[i]^2)) /// <c>Scalar ret = sqrt(sum(this[i]^2))</c>
/// </returns> /// </returns>
public virtual double Norm() public virtual double Norm()
{ {
return this.NormP(2); return NormP(2);
} }
/// <summary> /// <summary>
/// Squared Euclidean 2-Norm. /// Squared Euclidean 2-Norm.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// Scalar ret = sum(this[i]^2) /// <c>Scalar ret = sum(this[i]^2)</c>
/// </returns> /// </returns>
public virtual double SquaredNorm() public virtual double SquaredNorm()
{ {
var norm = this.Norm(); var norm = Norm();
return norm * norm; return norm * norm;
} }
@ -1139,11 +1139,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// 1-Norm also known as Manhattan Norm or Taxicab Norm. /// 1-Norm also known as Manhattan Norm or Taxicab Norm.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// Scalar ret = sum(abs(this[i])) /// <c>Scalar ret = sum(abs(this[i]))</c>
/// </returns> /// </returns>
public virtual double Norm1() public virtual double Norm1()
{ {
return this.NormP(1); return NormP(1);
} }
/// <summary> /// <summary>
@ -1153,7 +1153,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// The p value. /// The p value.
/// </param> /// </param>
/// <returns> /// <returns>
/// Scalar ret = (sum(abs(this[i])^p))^(1/p) /// <c>Scalar ret = (sum(abs(this[i])^p))^(1/p)</c>
/// </returns> /// </returns>
public virtual double NormP(int p) public virtual double NormP(int p)
{ {
@ -1164,7 +1164,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var sum = CommonParallel.Aggregate( var sum = CommonParallel.Aggregate(
0, 0,
this.Count, Count,
index => Math.Pow(Math.Abs(this[index]), p)); index => Math.Pow(Math.Abs(this[index]), p));
return Math.Pow(sum, 1.0 / p); return Math.Pow(sum, 1.0 / p);
@ -1174,27 +1174,27 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Infinity Norm. /// Infinity Norm.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// Scalar ret = max(abs(this[i])) /// <c>Scalar ret = max(abs(this[i]))</c>
/// </returns> /// </returns>
public virtual double NormInfinity() public virtual double NormInfinity()
{ {
return CommonParallel.Select( return CommonParallel.Select(
0, 0,
this.Count, Count,
(index, localData) => localData = Math.Max(localData, Math.Abs(this[index])), (index, localData) => localData = Math.Max(localData, Math.Abs(this[index])),
Math.Max); Math.Max);
} }
/// <summary> /// <summary>
/// Normalizes this vector to a unit vector with respect to the Eucliden 2-Norm. /// Normalizes this vector to a unit vector with respect to the Euclidean 2-Norm.
/// </summary> /// </summary>
/// <returns> /// <returns>
/// This vector normalized to a unit vector with respect to the Eucliden 2-Norm. /// This vector normalized to a unit vector with respect to the Euclidean 2-Norm.
/// </returns> /// </returns>
public virtual Vector Normalize() public virtual Vector Normalize()
{ {
var norm = this.Norm(); var norm = Norm();
var clone = this.Clone(); var clone = Clone();
if (norm == 0.0) if (norm == 0.0)
{ {
return clone; return clone;
@ -1216,8 +1216,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public Vector Clone() public Vector Clone()
{ {
var retrunVector = this.CreateVector(this.Count); var retrunVector = CreateVector(Count);
this.CopyTo(retrunVector); CopyTo(retrunVector);
return retrunVector; return retrunVector;
} }
@ -1240,7 +1240,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("target"); throw new ArgumentNullException("target");
} }
if (this.Count != target.Count) if (Count != target.Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
} }
@ -1252,7 +1252,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For( CommonParallel.For(
0, 0,
this.Count, Count,
index => target[index] = this[index]); index => target[index] = this[index]);
} }
@ -1278,12 +1278,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("destination"); throw new ArgumentNullException("destination");
} }
if (offset >= this.Count) if (offset >= Count)
{ {
throw new ArgumentOutOfRangeException("offset"); throw new ArgumentOutOfRangeException("offset");
} }
if (offset + count > this.Count) if (offset + count > Count)
{ {
throw new ArgumentOutOfRangeException("count"); throw new ArgumentOutOfRangeException("count");
} }
@ -1301,7 +1301,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, destination)) if (ReferenceEquals(this, destination))
{ {
var tmpVector = destination.CreateVector(destination.Count); var tmpVector = destination.CreateVector(destination.Count);
this.CopyTo(tmpVector); CopyTo(tmpVector);
CommonParallel.For( CommonParallel.For(
0, 0,
@ -1325,7 +1325,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public virtual double[] ToArray() public virtual double[] ToArray()
{ {
var ret = new double[this.Count]; var ret = new double[Count];
for (var i = 0; i < ret.Length; i++) for (var i = 0; i < ret.Length; i++)
{ {
ret[i] = this[i]; ret[i] = this[i];
@ -1342,8 +1342,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public virtual Matrix ToColumnMatrix() public virtual Matrix ToColumnMatrix()
{ {
var matrix = this.CreateMatrix(this.Count, 1); var matrix = CreateMatrix(Count, 1);
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
matrix[i, 0] = this[i]; matrix[i, 0] = this[i];
} }
@ -1359,8 +1359,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public virtual Matrix ToRowMatrix() public virtual Matrix ToRowMatrix()
{ {
var matrix = this.CreateMatrix(1, this.Count); var matrix = CreateMatrix(1, Count);
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
matrix[0, i] = this[i]; matrix[0, i] = this[i];
} }
@ -1381,7 +1381,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception> /// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public virtual Vector SubVector(int index, int length) public virtual Vector SubVector(int index, int length)
{ {
if (index < 0 || index >= this.Count) if (index < 0 || index >= Count)
{ {
throw new ArgumentOutOfRangeException("index"); throw new ArgumentOutOfRangeException("index");
} }
@ -1391,12 +1391,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("length"); throw new ArgumentOutOfRangeException("length");
} }
if (index + length > this.Count) if (index + length > Count)
{ {
throw new ArgumentOutOfRangeException("index"); throw new ArgumentOutOfRangeException("index");
} }
var result = this.CreateVector(length); var result = CreateVector(length);
CommonParallel.For( CommonParallel.For(
index, index,
@ -1418,7 +1418,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("values"); throw new ArgumentNullException("values");
} }
if (values.Length != this.Count) if (values.Length != Count)
{ {
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values"); throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values");
} }
@ -1445,7 +1445,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
object ICloneable.Clone() object ICloneable.Clone()
{ {
return this.Clone(); return Clone();
} }
#endregion #endregion
@ -1462,7 +1462,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
IEnumerator IEnumerable.GetEnumerator() IEnumerator IEnumerable.GetEnumerator()
{ {
return this.GetEnumerator(); return GetEnumerator();
} }
#endregion #endregion
@ -1477,7 +1477,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public virtual IEnumerator<double> GetEnumerator() public virtual IEnumerator<double> GetEnumerator()
{ {
for (var index = 0; index < this.Count; index++) for (var index = 0; index < Count; index++)
{ {
yield return this[index]; yield return this[index];
} }
@ -1501,7 +1501,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </remarks> /// </remarks>
public virtual IEnumerable<KeyValuePair<int, double>> GetIndexedEnumerator() public virtual IEnumerable<KeyValuePair<int, double>> GetIndexedEnumerator()
{ {
for (var i = 0; i < this.Count; i++) for (var i = 0; i < Count; i++)
{ {
yield return new KeyValuePair<int, double>(i, this[i]); yield return new KeyValuePair<int, double>(i, this[i]);
} }
@ -1516,7 +1516,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// An object to compare with this object. /// An object to compare with this object.
/// </param> /// </param>
/// <returns> /// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false. /// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
/// </returns> /// </returns>
public bool Equals(Vector other) public bool Equals(Vector other)
{ {
@ -1526,7 +1526,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return false; return false;
} }
if (this.Count != other.Count) if (Count != other.Count)
{ {
return false; return false;
} }
@ -1538,7 +1538,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
} }
// If all else fails, perform element wise comparison. // If all else fails, perform element wise comparison.
for (var index = 0; index < this.Count; index++) for (var index = 0; index < Count; index++)
{ {
if (!this[index].AlmostEqual(other[index])) if (!this[index].AlmostEqual(other[index]))
{ {
@ -1564,7 +1564,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public string ToString(IFormatProvider formatProvider) public string ToString(IFormatProvider formatProvider)
{ {
return this.ToString(null, formatProvider); return ToString(null, formatProvider);
} }
/// <summary> /// <summary>
@ -1582,10 +1582,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public string ToString(string format, IFormatProvider formatProvider) public string ToString(string format, IFormatProvider formatProvider)
{ {
var stringBuilder = new StringBuilder(); var stringBuilder = new StringBuilder();
for (var index = 0; index < this.Count; index++) for (var index = 0; index < Count; index++)
{ {
stringBuilder.Append(this[index].ToString(format, formatProvider)); stringBuilder.Append(this[index].ToString(format, formatProvider));
if (index != this.Count - 1) if (index != Count - 1)
{ {
stringBuilder.Append(formatProvider.GetTextInfo().ListSeparator); stringBuilder.Append(formatProvider.GetTextInfo().ListSeparator);
} }
@ -1611,7 +1611,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public override bool Equals(object obj) public override bool Equals(object obj)
{ {
return this.Equals(obj as Vector); return Equals(obj as Vector);
} }
/// <summary> /// <summary>
@ -1622,7 +1622,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public override int GetHashCode() public override int GetHashCode()
{ {
var hashNum = Math.Min(this.Count, 20); var hashNum = Math.Min(Count, 20);
long hash = 0; long hash = 0;
for (var i = 0; i < hashNum; i++) for (var i = 0; i < hashNum; i++)
{ {
@ -1644,9 +1644,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns> /// </returns>
public override string ToString() public override string ToString()
{ {
return this.ToString(null, null); return ToString(null, null);
} }
#endregion #endregion
/// <summary>
/// Resets all values to zero.
/// </summary>
public virtual void Clear()
{
CommonParallel.For(0, Count, index => this[index] = 0);
}
} }
} }

6
src/UnitTests/LinearAlgebraTests/Double/SparseVectorTest.cs

@ -61,15 +61,10 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
System.Array.Copy(_data, data, _data.Length); System.Array.Copy(_data, data, _data.Length);
var vector = new SparseVector(data); var vector = new SparseVector(data);
//Assert.AreSame(data, vector.ToArray()); There is no way to cast SparseVector to double[], so "vector.ToArray()" and "array" have different references
for (var i = 0; i < data.Length; i++) for (var i = 0; i < data.Length; i++)
{ {
Assert.AreEqual(data[i], vector[i]); Assert.AreEqual(data[i], vector[i]);
} }
// vector and data are different instances actually
//vector[0] = 100.0;
//Assert.AreEqual(100.0, data[0]);
} }
[Test] [Test]
@ -139,7 +134,6 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
var vector = new SparseVector(_data); var vector = new SparseVector(_data);
var array = vector.ToArray(); var array = vector.ToArray();
Assert.IsInstanceOfType(typeof(double[]), array); Assert.IsInstanceOfType(typeof(double[]), array);
//Assert.AreSame(vector.ToArray(), array); There is no way to cast SparseVector to double[], so "vector.ToArray()" and "array" have different references
Assert.AreElementsEqual(vector, array); Assert.AreElementsEqual(vector, array);
} }

12
src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

@ -421,6 +421,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
vector = vector.Random(-2, new ContinuousUniform()); vector = vector.Random(-2, new ContinuousUniform());
} }
[Test]
public void CanClearVector()
{
double[] testData = { -20, -10, 10, 20, 30, };
var vector = CreateVector(testData);
vector.Clear();
foreach (var element in vector)
{
Assert.AreEqual(0.0, element);
}
}
protected abstract Vector CreateVector(int size); protected abstract Vector CreateVector(int size);
protected abstract Vector CreateVector(IList<double> data); protected abstract Vector CreateVector(IList<double> data);
} }

Loading…
Cancel
Save