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

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

@ -51,7 +51,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseVector(int size)
: base(size)
{
this.Data = new double[size];
Data = new double[size];
}
/// <summary>
@ -70,9 +70,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public DenseVector(int size, double value)
: 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(
0,
this.Data.Length,
Data.Length,
index => this[index] = other[index]);
}
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)
: 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>
@ -121,7 +121,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// changes to the vector will also change the array.</remarks>
public DenseVector(double[] array) : base(array.Length)
{
this.Data = array;
Data = array;
}
/// <summary>
@ -158,7 +158,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary>
/// <param name="array">The array to bind to the <c>DenseVector</c> object.</param>
/// <returns>
/// A DenseVector whose values are bound to the given array.
/// A <c>DenseVector</c> whose values are bound to the given array.
/// </returns>
public static implicit operator DenseVector(double[] array)
{
@ -176,10 +176,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a column matrix.</returns>
public override Matrix ToColumnMatrix()
{
var matrix = new DenseMatrix(this.Count, 1);
for (var i = 0; i < this.Data.Length; i++)
var matrix = new DenseMatrix(Count, 1);
for (var i = 0; i < Data.Length; i++)
{
matrix[i, 0] = this.Data[i];
matrix[i, 0] = Data[i];
}
return matrix;
@ -191,10 +191,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a row matrix.</returns>
public override Matrix ToRowMatrix()
{
var matrix = new DenseMatrix(1, this.Count);
for (var i = 0; i < this.Data.Length; i++)
var matrix = new DenseMatrix(1, Count);
for (var i = 0; i < Data.Length; i++)
{
matrix[0, i] = this.Data[i];
matrix[0, i] = Data[i];
}
return matrix;
@ -209,12 +209,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
get
{
return this.Data[index];
return Data[index];
}
set
{
this.Data[index] = value;
Data[index] = value;
}
}
@ -270,7 +270,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("target");
}
if (this.Count != target.Count)
if (Count != target.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
@ -285,12 +285,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
CommonParallel.For(
0,
this.Data.Length,
index => target[index] = this.Data[index]);
Data.Length,
index => target[index] = Data[index]);
}
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(
0,
this.Data.Length,
index => this.Data[index] += scalar);
Data.Length,
index => Data[index] += scalar);
}
/// <summary>
@ -325,12 +325,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
this.CopyTo(result);
CopyTo(result);
result.Add(scalar);
}
@ -347,7 +347,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
@ -360,7 +360,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
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");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
@ -398,7 +398,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this.CopyTo(result);
CopyTo(result);
result.Add(other);
}
}
@ -463,8 +463,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For(
0,
this.Data.Length,
index => this.Data[index] -= scalar);
Data.Length,
index => Data[index] -= scalar);
}
/// <summary>
@ -481,12 +481,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
this.CopyTo(result);
CopyTo(result);
result.Subtract(scalar);
}
@ -503,7 +503,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
@ -516,7 +516,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
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");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
@ -554,7 +554,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this.CopyTo(result);
CopyTo(result);
result.Subtract(other);
}
}
@ -612,11 +612,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector Negate()
{
var result = new DenseVector(this.Count);
var result = new DenseVector(Count);
CommonParallel.For(
0,
this.Data.Length,
index => result[index] = -this.Data[index]);
Data.Length,
index => result[index] = -Data[index]);
return result;
}
@ -632,7 +632,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
Control.LinearAlgebraProvider.ScaleArray(scalar, this.Data);
Control.LinearAlgebraProvider.ScaleArray(scalar, Data);
}
/// <summary>
@ -649,7 +649,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
@ -661,7 +661,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return base.DotProduct(other);
}
return Control.LinearAlgebraProvider.DotProduct(this.Data, denseVector.Data);
return Control.LinearAlgebraProvider.DotProduct(Data, denseVector.Data);
}
/// <summary>
@ -756,10 +756,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override int AbsoluteMinimumIndex()
{
var index = 0;
var min = Math.Abs(this.Data[index]);
for (var i = 1; i < this.Count; i++)
var min = Math.Abs(Data[index]);
for (var i = 1; i < Count; i++)
{
var test = Math.Abs(this.Data[i]);
var test = Math.Abs(Data[i]);
if (test < min)
{
index = i;
@ -776,7 +776,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The value of the absolute minimum element.</returns>
public override double AbsoluteMinimum()
{
return Math.Abs(this.Data[this.AbsoluteMinimumIndex()]);
return Math.Abs(Data[AbsoluteMinimumIndex()]);
}
/// <summary>
@ -785,7 +785,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The value of the absolute maximum element.</returns>
public override double AbsoluteMaximum()
{
return Math.Abs(this.Data[this.AbsoluteMaximumIndex()]);
return Math.Abs(Data[AbsoluteMaximumIndex()]);
}
/// <summary>
@ -795,10 +795,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override int AbsoluteMaximumIndex()
{
var index = 0;
var max = Math.Abs(this.Data[index]);
for (var i = 1; i < this.Count; i++)
var max = Math.Abs(Data[index]);
for (var i = 1; i < Count; i++)
{
var test = Math.Abs(this.Data[i]);
var test = Math.Abs(Data[i]);
if (test > max)
{
index = i;
@ -822,7 +822,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public override Vector SubVector(int index, int length)
{
if (index < 0 || index >= this.Count)
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException("index");
}
@ -832,7 +832,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("length");
}
if (index + length > this.Count)
if (index + length > Count)
{
throw new ArgumentOutOfRangeException("length");
}
@ -842,7 +842,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For(
index,
index + length,
i => result.Data[i - index] = this.Data[i]);
i => result.Data[i - index] = Data[i]);
return result;
}
@ -859,7 +859,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("values");
}
if (values.Length != this.Count)
if (values.Length != Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values");
}
@ -867,7 +867,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
CommonParallel.For(
0,
values.Length,
i => this.Data[i] = values[i]);
i => Data[i] = values[i]);
}
/// <summary>
@ -877,13 +877,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override int MaximumIndex()
{
var index = 0;
var max = this.Data[0];
for (var i = 1; i < this.Count; i++)
var max = Data[0];
for (var i = 1; i < Count; i++)
{
if (max < this.Data[i])
if (max < Data[i])
{
index = i;
max = this.Data[i];
max = Data[i];
}
}
@ -897,13 +897,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override int MinimumIndex()
{
var index = 0;
var min = this.Data[0];
for (var i = 1; i < this.Count; i++)
var min = Data[0];
for (var i = 1; i < Count; i++)
{
if (min > this.Data[i])
if (min > Data[i])
{
index = i;
min = this.Data[i];
min = Data[i];
}
}
@ -917,9 +917,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double Sum()
{
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;
@ -932,9 +932,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double SumMagnitudes()
{
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;
@ -953,7 +953,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
@ -968,7 +968,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
CommonParallel.For(
0,
this.Count,
Count,
index => this[index] *= other[index]);
}
}
@ -994,12 +994,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
@ -1007,12 +1007,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointwiseMultiply(other, tmp);
PointwiseMultiply(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
CopyTo(result);
result.PointwiseMultiply(other);
}
}
@ -1030,7 +1030,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
@ -1045,7 +1045,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
CommonParallel.For(
0,
this.Count,
Count,
index => this[index] /= other[index]);
}
}
@ -1071,12 +1071,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
@ -1084,12 +1084,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointwiseDivide(other, tmp);
PointwiseDivide(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
CopyTo(result);
result.PointwiseDivide(other);
}
}
@ -1116,11 +1116,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For(
0,
u.Count,
0,
u.Count,
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]);
}
@ -1145,7 +1145,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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++)
{
v.Data[index] = randomDistribution.Sample();
@ -1171,7 +1171,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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++)
{
v.Data[index] = randomDistribution.Sample();
@ -1203,9 +1203,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
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;
@ -1219,8 +1219,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
return CommonParallel.Aggregate(
0,
this.Count,
index => Math.Abs(this.Data[index]));
Count,
index => Math.Abs(Data[index]));
}
/// <summary>
@ -1237,18 +1237,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (1 == p)
{
return this.Norm1();
return Norm1();
}
if (2 == p)
{
return this.Norm();
return Norm();
}
var sum = CommonParallel.Aggregate(
0,
this.Count,
index => Math.Pow(Math.Abs(this.Data[index]), p));
Count,
index => Math.Pow(Math.Abs(Data[index]), p));
return Math.Pow(sum, 1.0 / p);
}
@ -1261,8 +1261,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
return CommonParallel.Select(
0,
this.Count,
(index, localData) => localData = Math.Max(localData, Math.Abs(this.Data[index])),
Count,
(index, localData) => localData = Math.Max(localData, Math.Abs(Data[index])),
Math.Max);
}
@ -1426,5 +1426,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
#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");
}
if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount)
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
}
CommonParallel.For(
0,
this.RowCount,
RowCount,
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");
}
if (other.RowCount != this.RowCount || other.ColumnCount != this.ColumnCount)
if (other.RowCount != RowCount || other.ColumnCount != ColumnCount)
{
throw new ArgumentOutOfRangeException(Resources.ArgumentMatrixDimensions);
}
CommonParallel.For(
0,
this.RowCount,
RowCount,
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(
0,
this.RowCount,
RowCount,
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");
}
if (result.RowCount != this.RowCount)
if (result.RowCount != RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameRowDimension, "result");
}
if (result.ColumnCount != this.ColumnCount)
if (result.ColumnCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension, "result");
}
this.CopyTo(result);
CopyTo(result);
result.Multiply(scalar);
}
@ -156,7 +156,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <c>this.ColumnCount != rightSide.Count</c>.</exception>
public virtual Vector Multiply(Vector rightSide)
{
var ret = this.CreateVector(this.RowCount);
var ret = CreateVector(RowCount);
Multiply(rightSide, ret);
return ret;
}
@ -177,7 +177,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("rightSide");
}
if (this.ColumnCount != rightSide.Count)
if (ColumnCount != rightSide.Count)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "rightSide");
}
@ -187,7 +187,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result");
}
if (this.RowCount != result.Count)
if (RowCount != result.Count)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
@ -202,13 +202,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
CommonParallel.For(
0,
this.RowCount,
RowCount,
i =>
{
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;
@ -225,8 +225,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <strong>this.RowCount != <paramref name="leftSide"/>.Count</strong>.</exception>
public virtual Vector LeftMultiply(Vector leftSide)
{
var ret = this.CreateVector(this.ColumnCount);
this.LeftMultiply(leftSide, ret);
var ret = CreateVector(ColumnCount);
LeftMultiply(leftSide, ret);
return ret;
}
@ -246,7 +246,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("leftSide");
}
if (this.RowCount != leftSide.Count)
if (RowCount != leftSide.Count)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "leftSide");
}
@ -256,7 +256,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result");
}
if (this.ColumnCount != result.Count)
if (ColumnCount != result.Count)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
@ -264,20 +264,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(leftSide, result))
{
var tmp = result.CreateVector(result.Count);
this.LeftMultiply(leftSide, tmp);
LeftMultiply(leftSide, tmp);
tmp.CopyTo(result);
}
else
{
CommonParallel.For(
0,
this.RowCount,
RowCount,
j =>
{
double s = 0;
for (var i = 0; i != leftSide.Count; i++)
{
s += leftSide[i] * this.At(i, j);
s += leftSide[i] * At(i, j);
}
result[j] = s;
@ -306,12 +306,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result");
}
if (this.ColumnCount != other.RowCount)
if (ColumnCount != other.RowCount)
{
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);
}
@ -326,15 +326,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
CommonParallel.For(
0,
this.RowCount,
RowCount,
j =>
{
for (var i = 0; i != other.ColumnCount; i++)
{
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);
@ -357,12 +357,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.ColumnCount != other.RowCount)
if (ColumnCount != other.RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions);
}
var result = this.CreateMatrix(this.RowCount, other.ColumnCount);
var result = CreateMatrix(RowCount, other.ColumnCount);
Multiply(other, result);
return result;
}
@ -372,7 +372,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </summary>
public virtual void Negate()
{
this.Multiply(-1);
Multiply(-1);
}
/// <summary>
@ -388,12 +388,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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);
}
this.CopyTo(result);
CopyTo(result);
result.Negate();
}
@ -614,13 +614,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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");
}
var result = this.CreateMatrix(this.RowCount, this.ColumnCount);
this.PointwiseMultiply(other, result);
var result = CreateMatrix(RowCount, ColumnCount);
PointwiseMultiply(other, result);
return result;
}
@ -645,24 +645,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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");
}
if (this.ColumnCount != result.ColumnCount || this.RowCount != result.RowCount)
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
CommonParallel.For(
0,
this.ColumnCount,
ColumnCount,
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");
}
if (this.ColumnCount != other.ColumnCount || this.RowCount != other.RowCount)
if (ColumnCount != other.ColumnCount || RowCount != other.RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "other");
}
var result = this.CreateMatrix(this.RowCount, this.ColumnCount);
this.PointwiseDivide(other, result);
var result = CreateMatrix(RowCount, ColumnCount);
PointwiseDivide(other, result);
return result;
}
@ -712,24 +712,24 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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");
}
if (this.ColumnCount != result.ColumnCount || this.RowCount != result.RowCount)
if (ColumnCount != result.ColumnCount || RowCount != result.RowCount)
{
throw new ArgumentException(Resources.ArgumentMatrixDimensions, "result");
}
CommonParallel.For(
0,
this.ColumnCount,
ColumnCount,
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");
}
var matrix = this.CreateMatrix(numberOfRows, numberOfColumns);
var matrix = CreateMatrix(numberOfRows, numberOfColumns);
CommonParallel.For(
0,
this.ColumnCount,
ColumnCount,
j =>
{
for (var i = 0; i < matrix.RowCount; i++)
@ -795,10 +795,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMustBePositive, "numberOfColumns");
}
var matrix = this.CreateMatrix(numberOfRows, numberOfColumns);
var matrix = CreateMatrix(numberOfRows, numberOfColumns);
CommonParallel.For(
0,
this.ColumnCount,
ColumnCount,
j =>
{
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>
public virtual double Trace()
{
if (this.RowCount != this.ColumnCount)
if (RowCount != ColumnCount)
{
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>
@ -863,8 +863,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
var result = this.CreateMatrix(this.RowCount * other.RowCount, this.ColumnCount * other.ColumnCount);
this.KroneckerProduct(other, result);
var result = CreateMatrix(RowCount * other.RowCount, ColumnCount * other.ColumnCount);
KroneckerProduct(other, result);
return result;
}
@ -889,19 +889,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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");
}
CommonParallel.For(
0,
this.ColumnCount,
ColumnCount,
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);
}
var ret = this.Clone();
var ret = Clone();
CommonParallel.For(
0,
this.ColumnCount,
ColumnCount,
i =>
{
var coli = this.Column(i);
var coli = Column(i);
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;
}
@ -948,21 +948,20 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("p", Resources.ArgumentMustBePositive);
}
var ret = this.Clone();
var ret = Clone();
CommonParallel.For(
0,
this.ColumnCount,
ColumnCount,
j =>
{
var rowj = this.Row(j);
var rowj = Row(j);
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;
}
});
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
{
/// <summary>
/// Lock ojbect for the indexer.
/// Lock object for the indexer.
/// </summary>
private readonly object lockObject = new object();
private readonly object _lockObject = new object();
/// <summary>
/// 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)
{
// Skip adding values
// Skip adding values
return;
}
// We already know that this vector is "full", let's allocate all needed memory
this._nonZeroValues = new double[size];
this._nonZeroIndices = new int[size];
this.NonZerosCount = size;
_nonZeroValues = new double[size];
_nonZeroIndices = new int[size];
NonZerosCount = size;
CommonParallel.For(
0,
this.Count,
Count,
index =>
{
this._nonZeroValues[index] = value;
this._nonZeroIndices[index] = index;
_nonZeroValues[index] = value;
_nonZeroIndices[index] = index;
});
}
@ -134,13 +134,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this._nonZeroValues = new double[vector.NonZerosCount];
this._nonZeroIndices = new int[vector.NonZerosCount];
this.NonZerosCount = vector.NonZerosCount;
_nonZeroValues = new double[vector.NonZerosCount];
_nonZeroIndices = new int[vector.NonZerosCount];
NonZerosCount = vector.NonZerosCount;
// 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._nonZeroIndices, 0, this._nonZeroIndices, 0, vector.NonZerosCount * Constants.SizeOfInt);
Buffer.BlockCopy(vector._nonZeroValues, 0, _nonZeroValues, 0, vector.NonZerosCount * Constants.SizeOfDouble);
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)
{
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
this._nonZeroValues = new double[other.NonZerosCount];
this._nonZeroIndices = new int[other.NonZerosCount];
this.NonZerosCount = other.NonZerosCount;
_nonZeroValues = new double[other.NonZerosCount];
_nonZeroIndices = new int[other.NonZerosCount];
NonZerosCount = other.NonZerosCount;
Buffer.BlockCopy(other._nonZeroValues, 0, this._nonZeroValues, 0, other.NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(other._nonZeroIndices, 0, this._nonZeroIndices, 0, other.NonZerosCount * Constants.SizeOfInt);
Buffer.BlockCopy(other._nonZeroValues, 0, _nonZeroValues, 0, other.NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(other._nonZeroIndices, 0, _nonZeroIndices, 0, other.NonZerosCount * Constants.SizeOfInt);
}
/// <summary>
@ -183,10 +183,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a column matrix.</returns>
public override Matrix ToColumnMatrix()
{
var matrix = new SparseMatrix(this.Count, 1);
for (var i = 0; i < this.NonZerosCount; i++)
var matrix = new SparseMatrix(Count, 1);
for (var i = 0; i < NonZerosCount; i++)
{
matrix[this._nonZeroIndices[i], 0] = this._nonZeroValues[i];
matrix[_nonZeroIndices[i], 0] = _nonZeroValues[i];
}
return matrix;
@ -198,10 +198,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>This vector as a row matrix.</returns>
public override Matrix ToRowMatrix()
{
var matrix = new SparseMatrix(1, this.Count);
for (var i = 0; i < this.NonZerosCount; i++)
var matrix = new SparseMatrix(1, Count);
for (var i = 0; i < NonZerosCount; i++)
{
matrix[0, this._nonZeroIndices[i]] = this._nonZeroValues[i];
matrix[0, _nonZeroIndices[i]] = _nonZeroValues[i];
}
return matrix;
@ -217,18 +217,18 @@ namespace MathNet.Numerics.LinearAlgebra.Double
get
{
// If index is out of bounds
if ((index < 0) || (index >= this.Count))
if ((index < 0) || (index >= Count))
{
throw new IndexOutOfRangeException();
}
lock (this.lockObject)
lock (_lockObject)
{
// 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)
{
return this._nonZeroValues[itemIndex];
return _nonZeroValues[itemIndex];
}
}
@ -238,14 +238,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
set
{
// If index is out of bounds
if ((index < 0) || (index >= this.Count))
if ((index < 0) || (index >= Count))
{
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>
/// Clears this instance.
/// Resets all values to zero.
/// </summary>
public void Clear()
public override void Clear()
{
this.NonZerosCount = 0;
NonZerosCount = 0;
}
/// <summary>
@ -310,7 +310,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("target");
}
if (this.Count != target.Count)
if (Count != target.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target");
}
@ -325,33 +325,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
CommonParallel.For(
0,
this.Count,
Count,
index => target[index] = this[index]);
}
else
{
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
otherVector._nonZeroValues = new double[this.NonZerosCount];
otherVector._nonZeroIndices = new int[this.NonZerosCount];
otherVector.NonZerosCount = this.NonZerosCount;
otherVector._nonZeroValues = new double[NonZerosCount];
otherVector._nonZeroIndices = new int[NonZerosCount];
otherVector.NonZerosCount = NonZerosCount;
Buffer.BlockCopy(this._nonZeroValues, 0, otherVector._nonZeroValues, 0, this.NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(this._nonZeroIndices, 0, otherVector._nonZeroIndices, 0, this.NonZerosCount * Constants.SizeOfInt);
Buffer.BlockCopy(_nonZeroValues, 0, otherVector._nonZeroValues, 0, NonZerosCount * Constants.SizeOfDouble);
Buffer.BlockCopy(_nonZeroIndices, 0, otherVector._nonZeroIndices, 0, NonZerosCount * Constants.SizeOfInt);
}
}
#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>
/// Adds a scalar to each element of the vector.
/// </summary>
@ -363,7 +353,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
for (var i = 0; i < this.Count; i++)
for (var i = 0; i < Count; i++)
{
this[i] += scalar;
}
@ -383,12 +373,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
this.CopyTo(result);
CopyTo(result);
result.Add(scalar);
}
@ -405,7 +395,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
@ -418,7 +408,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this.AddScaledSparceVector(1.0, sparseVector);
AddScaledSparceVector(1.0, sparseVector);
}
}
@ -434,7 +424,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
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"
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)
{
this.Clear(); // Vector is subtracted from itself
Clear(); // Vector is subtracted from itself
return;
}
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");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
@ -525,7 +515,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this.CopyTo(result);
CopyTo(result);
result.Add(other);
}
}
@ -588,7 +578,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return;
}
for (var i = 0; i < this.Count; i++)
for (var i = 0; i < Count; i++)
{
this[i] -= scalar;
}
@ -608,12 +598,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
this.CopyTo(result);
CopyTo(result);
result.Subtract(scalar);
}
@ -630,7 +620,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
@ -643,7 +633,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this.AddScaledSparceVector(-1.0, sparseVector);
AddScaledSparceVector(-1.0, sparseVector);
}
}
@ -663,12 +653,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("result");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
@ -681,7 +671,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
else
{
this.CopyTo(result);
CopyTo(result);
result.Subtract(other);
}
}
@ -739,19 +729,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <remarks>Added as an alternative to the unary negation operator.</remarks>
public override Vector Negate()
{
var result = new SparseVector(this.Count)
var result = new SparseVector(Count)
{
_nonZeroValues = new double[this.NonZerosCount],
_nonZeroIndices = new int[this.NonZerosCount],
NonZerosCount = this.NonZerosCount
_nonZeroValues = new double[NonZerosCount],
_nonZeroIndices = new int[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(
0,
this.NonZerosCount,
index => result._nonZeroValues[index] = -this._nonZeroValues[index]);
NonZerosCount,
index => result._nonZeroValues[index] = -_nonZeroValues[index]);
return result;
}
@ -769,11 +759,11 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (scalar == 0)
{
this.Clear(); // Set array empty
Clear(); // Set array empty
return;
}
Control.LinearAlgebraProvider.ScaleArray(scalar, this._nonZeroValues);
Control.LinearAlgebraProvider.ScaleArray(scalar, _nonZeroValues);
}
/// <summary>
@ -790,7 +780,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
@ -798,9 +788,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
double result = 0;
// 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;
@ -897,17 +887,17 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The index of absolute minimum element.</returns>
public override int AbsoluteMinimumIndex()
{
if (this.NonZerosCount == 0)
if (NonZerosCount == 0)
{
// No non-zero elements. Return 0
return 0;
}
var index = 0;
var min = Math.Abs(this._nonZeroValues[index]);
for (var i = 1; i < this.NonZerosCount; i++)
var min = Math.Abs(_nonZeroValues[index]);
for (var i = 1; i < NonZerosCount; i++)
{
var test = Math.Abs(this._nonZeroValues[i]);
var test = Math.Abs(_nonZeroValues[i]);
if (test < min)
{
index = i;
@ -915,7 +905,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
return this._nonZeroIndices[index];
return _nonZeroIndices[index];
}
/// <summary>
@ -931,7 +921,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public override Vector SubVector(int index, int length)
{
if (index < 0 || index >= this.Count)
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException("index");
}
@ -941,7 +931,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentOutOfRangeException("length");
}
if (index + length > this.Count)
if (index + length > Count)
{
throw new ArgumentOutOfRangeException("length");
}
@ -968,7 +958,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("values");
}
if (values.Length != this.Count)
if (values.Length != Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "values");
}
@ -985,23 +975,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The index of absolute maximum element.</returns>
public override int MaximumIndex()
{
if (this.NonZerosCount == 0)
if (NonZerosCount == 0)
{
return 0;
}
var index = 0;
var max = this._nonZeroValues[0];
for (var i = 1; i < this.NonZerosCount; i++)
var max = _nonZeroValues[0];
for (var i = 1; i < NonZerosCount; i++)
{
if (max < this._nonZeroValues[i])
if (max < _nonZeroValues[i])
{
index = i;
max = this._nonZeroValues[i];
max = _nonZeroValues[i];
}
}
return this._nonZeroIndices[index];
return _nonZeroIndices[index];
}
/// <summary>
@ -1010,23 +1000,23 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <returns>The index of minimum element.</returns>
public override int MinimumIndex()
{
if (this.NonZerosCount == 0)
if (NonZerosCount == 0)
{
return 0;
}
var index = 0;
var min = this._nonZeroValues[0];
for (var i = 1; i < this.NonZerosCount; i++)
var min = _nonZeroValues[0];
for (var i = 1; i < NonZerosCount; i++)
{
if (min > this._nonZeroValues[i])
if (min > _nonZeroValues[i])
{
index = i;
min = this._nonZeroValues[i];
min = _nonZeroValues[i];
}
}
return this._nonZeroIndices[index];
return _nonZeroIndices[index];
}
/// <summary>
@ -1036,9 +1026,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double Sum()
{
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;
@ -1051,9 +1041,9 @@ namespace MathNet.Numerics.LinearAlgebra.Double
public override double SumMagnitudes()
{
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;
@ -1072,13 +1062,13 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
// 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];
}
@ -1105,12 +1095,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
@ -1118,12 +1108,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointwiseMultiply(other, tmp);
PointwiseMultiply(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
CopyTo(result);
result.PointwiseMultiply(other);
}
}
@ -1141,15 +1131,15 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentNullException("other");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
// 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");
}
if (this.Count != other.Count)
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (this.Count != result.Count)
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
@ -1187,12 +1177,12 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result) || ReferenceEquals(other, result))
{
var tmp = result.CreateVector(result.Count);
this.PointwiseDivide(other, tmp);
PointwiseDivide(other, tmp);
tmp.CopyTo(result);
}
else
{
this.CopyTo(result);
CopyTo(result);
result.PointwiseDivide(other);
}
}
@ -1249,7 +1239,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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++)
{
v[index] = randomDistribution.Sample();
@ -1275,7 +1265,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
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++)
{
v[index] = randomDistribution.Sample();
@ -1315,8 +1305,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
var sum = CommonParallel.Aggregate(
0,
this.NonZerosCount,
index => Math.Pow(Math.Abs(this._nonZeroValues[index]), p));
NonZerosCount,
index => Math.Pow(Math.Abs(_nonZeroValues[index]), 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>
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
@ -1500,7 +1490,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
private void SetValue(int index, double value)
{
// 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)
{
@ -1508,56 +1498,61 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (value == 0.0)
{
// 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];
this._nonZeroIndices[i - 1] = this._nonZeroIndices[i];
_nonZeroValues[i - 1] = _nonZeroValues[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
// 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 this._nonZeroIndices, this.NonZerosCount);
Array.Resize(ref _nonZeroValues, NonZerosCount);
Array.Resize(ref _nonZeroIndices, NonZerosCount);
}
}
else
{
this._nonZeroValues[itemIndex] = value;
_nonZeroValues[itemIndex] = value;
}
}
else
{
if (value == 0.0)
{
return;
}
itemIndex = ~itemIndex; // Index where to put new value
// 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
var size = Math.Min(this._nonZeroValues.Length + this.GrowthSize(), this.Count);
Array.Resize(ref this._nonZeroValues, size);
Array.Resize(ref this._nonZeroIndices, size);
var size = Math.Min(_nonZeroValues.Length + GrowthSize(), Count);
Array.Resize(ref _nonZeroValues, size);
Array.Resize(ref _nonZeroIndices, size);
}
// Move all values (with an position larger than index) in the value array
// to the next position
// move all values (with an position larger than index) in the columIndices
// 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];
this._nonZeroIndices[i + 1] = this._nonZeroIndices[i];
_nonZeroValues[i + 1] = _nonZeroValues[i];
_nonZeroIndices[i + 1] = _nonZeroIndices[i];
}
// Add the value and the column index
this._nonZeroValues[itemIndex] = value;
this._nonZeroIndices[itemIndex] = index;
_nonZeroValues[itemIndex] = value;
_nonZeroIndices[itemIndex] = index;
// 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()
{
int delta;
if (this._nonZeroValues.Length > 1024)
if (_nonZeroValues.Length > 1024)
{
delta = this._nonZeroValues.Length / 4;
delta = _nonZeroValues.Length / 4;
}
else
{
if (this._nonZeroValues.Length > 256)
if (_nonZeroValues.Length > 256)
{
delta = 512;
}
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;
}
if ((this.Count != sparseVector.Count) || (this.NonZerosCount != sparseVector.NonZerosCount))
if ((Count != sparseVector.Count) || (NonZerosCount != sparseVector.NonZerosCount))
{
return false;
}
// 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;
}
@ -1637,14 +1632,14 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// </returns>
public override int GetHashCode()
{
var hashNum = Math.Min(this.NonZerosCount, 20);
var hashNum = Math.Min(NonZerosCount, 20);
long hash = 0;
for (var i = 0; i < hashNum; i++)
{
#if SILVERLIGHT
hash ^= Precision.DoubleToInt64Bits(this._nonZeroValues[i]);
#else
hash ^= BitConverter.DoubleToInt64Bits(this._nonZeroValues[i]);
hash ^= BitConverter.DoubleToInt64Bits(_nonZeroValues[i]);
#endif
}
@ -1653,4 +1648,4 @@ namespace MathNet.Numerics.LinearAlgebra.Double
#endregion
}
}
}

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

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

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

@ -421,6 +421,18 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
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(IList<double> data);
}

Loading…
Cancel
Save