Browse Source

Added:

AbsoluteMinimum()
AbsoluteMinimumIndex()
GetSubVector() - rename SubVector()
Maximum()
MaximumIndex()
Minimum()
MimumIndex()
Sum()
SumMagnitudes()
SetValues()
DyadicProduct()
Random()
TensorMultiply()
la-knuth
Hani Medhat 17 years ago
committed by Marcus Cuda
parent
commit
f213e9988b
  1. BIN
      src/FSharpUnitTests100602(1).vsp
  2. BIN
      src/FSharpUnitTests100602(2).vsp
  3. BIN
      src/FSharpUnitTests100602.vsp
  4. 504
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  5. 123
      src/Numerics/LinearAlgebra/Double/Matrix.Arithmetic.cs
  6. 1
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  7. 400
      src/Numerics/LinearAlgebra/Double/Vector.cs
  8. 6
      src/Numerics/Settings.StyleCop
  9. 399
      src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs
  10. 302
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs
  11. 28
      src/UnitTests/LinearAlgebraTests/Double/VectorTests.cs

BIN
src/FSharpUnitTests100602(1).vsp

Binary file not shown.

BIN
src/FSharpUnitTests100602(2).vsp

Binary file not shown.

BIN
src/FSharpUnitTests100602.vsp

Binary file not shown.

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

@ -752,6 +752,510 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return ret;
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>
/// <returns>The value of the absolute minimum element.</returns>
public override double AbsoluteMinimum()
{
return Math.Abs(this.Data[AbsoluteMinimumIndex()]);
}
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>
/// <returns>The index of absolute minimum element.</returns>
public override int AbsoluteMinimumIndex()
{
int index = 0;
double min = System.Math.Abs(this.Data[index]);
for (int i = 1; i < Count; i++)
{
double test = System.Math.Abs(this.Data[i]);
if (test < min)
{
index = i;
min = test;
}
}
return index;
}
/// <summary>
/// Creates a vector containing specified elements.
/// </summary>
/// <param name="index">The first element to begin copying from.</param>
/// <param name="length">The number of elements to copy.</param>
/// <returns>A vector containing a copy of the specified elements.</returns>
/// <exception cref="ArgumentOutOfRangeException"><list><item>If <paramref name="index"/> is not positive or
/// greater than or equal to the size of the vector.</item>
/// <item>If <paramref name="index"/> + <paramref name="length"/> is greater than or equal to the size of the vector.</item>
/// </list></exception>
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public override Vector SubVector(int index, int length)
{
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException("start");
}
if (length <= 0)
{
throw new ArgumentOutOfRangeException("length");
}
if (index + length > Count)
{
throw new ArgumentOutOfRangeException("start");
}
DenseVector result = new DenseVector(length);
CommonParallel.For(
index,
index + length,
i => result.Data[i - index] = this.Data[i]);
return result;
}
/// <summary>
/// Set the values of this vector to the given values.
/// </summary>
/// <param name="values">The array containing the values to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="values"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <paramref name="values"/> is not the same size as this vector.</exception>
public override void SetValues(double[] values)
{
if (values == null)
{
throw new ArgumentNullException("values");
}
if (values.Length != Count)
{
throw new ArgumentException("values", Resources.ArgumentVectorsSameLength);
}
CommonParallel.For(
0,
values.Length,
i => this.Data[i] = values[i]);
}
/// <summary>
/// Returns the value of maximum element.
/// </summary>
/// <returns>The value of maximum element.</returns>
public override double Maximum()
{
return this.Data[MaximumIndex()];
}
/// <summary>
/// Returns the index of the absolute maximum element.
/// </summary>
/// <returns>The index of absolute maximum element.</returns>
public override int MaximumIndex()
{
int index = 0;
double max = this.Data[0];
for (int i = 1; i < Count; i++)
{
if (max < this.Data[i])
{
index = i;
max = this.Data[i];
}
}
return index;
}
/// <summary>
/// Returns the value of the minimum element.
/// </summary>
/// <returns>The value of the minimum element.</returns>
public override double Minimum()
{
return this.Data[MinimumIndex()];
}
/// <summary>
/// Returns the index of the minimum element.
/// </summary>
/// <returns>The index of minimum element.</returns>
public override int MinimumIndex()
{
int index = 0;
double min = this.Data[0];
for (int i = 1; i < Count; i++)
{
if (min > this.Data[i])
{
index = i;
min = this.Data[i];
}
}
return index;
}
/// <summary>
/// Computes the sum of the vector's elements.
/// </summary>
/// <returns>The sum of the vector's elements.</returns>
public override double Sum()
{
double result = 0;
for (int i = 0; i < Count; i++)
{
result += this.Data[i];
}
return result;
}
/// <summary>
/// Computes the sum of the absolute value of the vector's elements.
/// </summary>
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public override double SumMagnitudes()
{
double result = 0;
for (int i = 0; i < Count; i++)
{
result += Math.Abs(this.Data[i]);
}
return result;
}
/// <summary>
/// Pointwise multiplies this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise multiplication of this vector and <paramref name="other"/>.</returns>
public virtual DenseVector PointWiseMultiply(DenseVector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
DenseVector result = new DenseVector(Count);
PointWiseMultiply(other, result);
return result;
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise multiply with this one.</param>
/// <param name="result">The vector to store the result of the pointwise multiplication.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void PointWiseMultiply(DenseVector other, DenseVector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CommonParallel.For(
0,
this.Count,
index => result.Data[index] = this.Data[index] * other.Data[index]);
}
/// <summary>
/// Pointwise add this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise add with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise addition of this vector and <paramref name="other"/>.</returns>
public virtual DenseVector PointWiseAdd(DenseVector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
DenseVector result = new DenseVector(Count);
PointWiseAdd(other, result);
return result;
}
/// <summary>
/// Pointwise add this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise add with this one.</param>
/// <param name="result">The vector to store the result of the pointwise addition.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void PointWiseAdd(DenseVector other, DenseVector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CommonParallel.For(
0,
this.Count,
index => result.Data[index] = this.Data[index] + other.Data[index]);
}
/// <summary>
/// Pointwise subtarct this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise subtract from this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise subtraction of this vector and <paramref name="other"/>.</returns>
public virtual DenseVector PointWiseSubtract(DenseVector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
DenseVector result = new DenseVector(Count);
PointWiseSubtract(other, result);
return result;
}
/// <summary>
/// Pointwise subtract this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise subtract from this one.</param>
/// <param name="result">The vector to store the result of the pointwise subtraction.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void PointWiseSubtract(DenseVector other, DenseVector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CommonParallel.For(
0,
this.Count,
index => result.Data[index] = this.Data[index] - other.Data[index]);
}
/// <summary>
/// Pointwise divide this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise division of this vector and <paramref name="other"/>.</returns>
public virtual DenseVector PointWiseDivide(DenseVector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
DenseVector result = new DenseVector(Count);
PointWiseDivide(other, result);
return result;
}
/// <summary>
/// Pointwise divide this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <param name="result">The vector to store the result of the pointwise division.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void PointWiseDivide(DenseVector other, DenseVector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CommonParallel.For(
0,
this.Count,
index => result.Data[index] = this.Data[index] / other.Data[index]);
}
/// <summary>
/// Dyadic product of two vectors
/// </summary>
/// <param name="u">First vector</param>
/// <param name="v">Second vector</param>
/// <returns>Matrix M[i,j] = u[i]*v[j] </returns>
/// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception>
public static DenseMatrix DyadicProduct(DenseVector u, DenseVector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
DenseMatrix matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For(
0,
u.Count,
i => CommonParallel.For(0, v.Count, j => matrix.At(i, j, u.Data[i] * v.Data[j])));
return matrix;
}
/// <summary>
/// Generates vector with random elements
/// </summary>
/// <param name="n">Dimensionality of vector.</param>
/// <returns>
/// An n-dimensional vector with uniformly distributed
/// random elements in <c>[0, 1)</c> interval.
/// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non poisitive<see langword="null" />.</exception>
public new virtual DenseVector Random(int n)
{
// use any class which inherits the AbstractRandomNumberGenerator
return Random(n, new MathNet.Numerics.Random.SystemCryptoRandomNumberGenerator());
}
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="n">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// 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>
public new virtual DenseVector Random(int n, MathNet.Numerics.Random.AbstractRandomNumberGenerator randomDistribution)
{
if (n < 0)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "n");
}
DenseVector v = new DenseVector(n);
CommonParallel.For(
0,
v.Count,
index => v.Data[index] = randomDistribution.NextDouble());
return v;
}
/// <summary>
/// Tensor Product (Dyadic) of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="DyadicProduct"/>
public Matrix TensorMultiply(DenseVector v)
{
return DyadicProduct(this, v);
}
#region Vector Norms
/// <summary>

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

@ -652,26 +652,26 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException(Resources.ArgumentMatrixSameColumnDimension);
}
CommonParallel.For(
0,
this.RowCount,
i =>
{
for (int j = 0; j < this.ColumnCount; j++)
{
result.At(i, j, At(i, j));
}
});
CommonParallel.For(
0,
right.RowCount,
i =>
{
for (int j = 0; j < right.ColumnCount; j++)
{
result.At(i, j + ColumnCount, right.At(i, j));
}
CommonParallel.Invoke(
() =>
{
CommonParallel.For(
0,
this.RowCount,
i =>
{
CommonParallel.For(0, this.ColumnCount, j => result.At(i, j, At(i, j)));
});
},
() =>
{
CommonParallel.For(
0,
right.RowCount,
i =>
{
CommonParallel.For(0, right.ColumnCount, j => result.At(i, j + ColumnCount, right.At(i, j)));
});
});
}
@ -727,27 +727,27 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
throw new ArgumentException("result", Resources.ArgumentMatrixDimensions);
}
CommonParallel.For(
0,
this.RowCount,
i =>
CommonParallel.Invoke(
() =>
{
for (int j = 0; j < this.ColumnCount; j++)
{
result.At(i, j, At(i, j));
}
});
CommonParallel.For(
0,
lower.RowCount,
i =>
CommonParallel.For(
0,
this.RowCount,
i =>
{
CommonParallel.For(0, this.ColumnCount, j => result.At(i, j, At(i, j)));
});
},
() =>
{
for (int j = 0; j < lower.ColumnCount; j++)
{
result.At(i + RowCount, j, lower.At(i, j));
}
CommonParallel.For(
0,
lower.RowCount,
i =>
{
CommonParallel.For(0, lower.ColumnCount, j => result.At(i + RowCount, j, lower.At(i, j)));
});
});
}
@ -817,27 +817,36 @@ namespace MathNet.Numerics.LinearAlgebra.Double
throw new ArgumentException("result", Resources.ArgumentMatrixDimensions);
}
CommonParallel.For(
0,
this.RowCount,
i =>
CommonParallel.Invoke(
() =>
{
for (var j = 0; j < this.ColumnCount; j++)
{
result.At(i, j, At(i, j));
}
CommonParallel.For(
0,
this.RowCount,
i =>
{
CommonParallel.For(0, this.ColumnCount, j => result.At(i, j, At(i, j)));
});
},
() =>
{
CommonParallel.For(
0,
lower.RowCount,
i =>
{
CommonParallel.For(0, lower.ColumnCount, j => result.At(i + RowCount, j + ColumnCount, lower.At(i, j)));
});
});
}
CommonParallel.For(
0,
lower.RowCount,
i =>
{
for (var j = 0; j < lower.ColumnCount; j++)
{
result.At(i + RowCount, j + ColumnCount, lower.At(i, j));
}
});
/// <summary>
/// Calculates the rank of the matrix
/// </summary>
/// <returns>effective numerical rank, obtained from SVD</returns>
public virtual int Rank()
{
throw new NotImplementedException();
}
}
}

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

@ -654,6 +654,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
ret.At(j, i, At(i, j));
}
}
return ret;
}
}

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

@ -609,6 +609,270 @@ namespace MathNet.Numerics.LinearAlgebra.Double
index => result[index] = this[index] * other[index]);
}
/// <summary>
/// Pointwise add this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise add with this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise addition of this vector and <paramref name="other"/>.</returns>
public virtual Vector PointWiseAdd(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
Vector result = CreateVector(Count);
PointWiseAdd(other, result);
return result;
}
/// <summary>
/// Pointwise add this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise add with this one.</param>
/// <param name="result">The vector to store the result of the pointwise addition.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void PointWiseAdd(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CommonParallel.For(
0,
this.Count,
index => result[index] = this[index] + other[index]);
}
/// <summary>
/// Pointwise subtarct this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise subtract from this one.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise subtraction of this vector and <paramref name="other"/>.</returns>
public virtual Vector PointWiseSubtract(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
Vector result = CreateVector(Count);
PointWiseSubtract(other, result);
return result;
}
/// <summary>
/// Pointwise subtract this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise subtract from this one.</param>
/// <param name="result">The vector to store the result of the pointwise subtraction.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void PointWiseSubtract(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CommonParallel.For(
0,
this.Count,
index => result[index] = this[index] - other[index]);
}
/// <summary>
/// Pointwise divide this vector with another vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <returns>A new vector that is the pointwise division of this vector and <paramref name="other"/>.</returns>
public virtual Vector PointWiseDivide(Vector other)
{
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
Vector result = CreateVector(Count);
PointWiseDivide(other, result);
return result;
}
/// <summary>
/// Pointwise divide this vector with another vector and stores the result into the result vector.
/// </summary>
/// <param name="other">The vector to pointwise divide this one by.</param>
/// <param name="result">The vector to store the result of the pointwise division.</param>
/// <exception cref="ArgumentNullException">If the other vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="other"/> are not the same size.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public virtual void PointWiseDivide(Vector other, Vector result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (other == null)
{
throw new ArgumentNullException("other");
}
if (Count != other.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "other");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
CommonParallel.For(
0,
this.Count,
index => result[index] = this[index] / other[index]);
}
/// <summary>
/// Dyadic product of two vectors
/// </summary>
/// <param name="u">First vector</param>
/// <param name="v">Second vector</param>
/// <returns>Matrix M[i,j] = u[i]*v[j] </returns>
/// <exception cref="ArgumentNullException">If the u vector is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the v vector is <see langword="null" />.</exception>
public static DenseMatrix DyadicProduct(Vector u, Vector v)
{
if (u == null)
{
throw new ArgumentNullException("u");
}
if (v == null)
{
throw new ArgumentNullException("v");
}
DenseMatrix matrix = new DenseMatrix(u.Count, v.Count);
CommonParallel.For(
0,
u.Count,
i => CommonParallel.For(0, v.Count, j => matrix.At(i, j, u[i] * v[j])));
return matrix;
}
/// <summary>
/// Generates vector with random elements
/// </summary>
/// <param name="length">Dimensionality of vector.</param>
/// <returns>
/// An n-dimensional vector with uniformly distributed
/// random elements in <c>[0, 1)</c> interval.
/// </returns>
/// <exception cref="ArgumentNullException">If the n vector is non poisitive<see langword="null" />.</exception>
public virtual Vector Random(int length)
{
// Just use any class which inherits the AbstractRandomNumberGenerator
return Random(length, new MathNet.Numerics.Random.SystemCryptoRandomNumberGenerator());
}
/// <summary>
/// Generates a vector with random elements
/// </summary>
/// <param name="length">Number of elements in the vector.</param>
/// <param name="randomDistribution">Continuous Random Distribution or Source</param>
/// <returns>
/// 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>
public virtual Vector Random(int length, MathNet.Numerics.Random.AbstractRandomNumberGenerator randomDistribution)
{
if (length < 0)
{
throw new ArgumentException(Resources.ArgumentMustBePositive, "length");
}
Vector v = CreateVector(length);
CommonParallel.For(
0,
v.Count,
index => v[index] = randomDistribution.NextDouble());
return v;
}
/// <summary>
/// Tensor Product (Dyadic) of this and another vector.
/// </summary>
/// <param name="v">The vector to operate on.</param>
/// <returns>
/// Matrix M[i,j] = this[i] * v[j].
/// </returns>
/// <seealso cref="DyadicProduct"/>
public Matrix TensorMultiply(Vector v)
{
return DyadicProduct(this, v);
}
/// <summary>
/// Returns the value of the absolute minimum element.
/// </summary>
@ -635,10 +899,97 @@ namespace MathNet.Numerics.LinearAlgebra.Double
min = test;
}
}
return index;
}
/// <summary>
/// Returns the value of maximum element.
/// </summary>
/// <returns>The value of maximum element.</returns>
public virtual double Maximum()
{
return this[MaximumIndex()];
}
/// <summary>
/// Returns the index of the absolute maximum element.
/// </summary>
/// <returns>The index of absolute maximum element.</returns>
public virtual int MaximumIndex()
{
int index = 0;
double max = this[0];
for (int i = 1; i < Count; i++)
{
if (max < this[i])
{
index = i;
max = this[i];
}
}
return index;
}
/// <summary>
/// Returns the value of the minimum element.
/// </summary>
/// <returns>The value of the minimum element.</returns>
public virtual double Minimum()
{
return this[MinimumIndex()];
}
/// <summary>
/// Returns the index of the minimum element.
/// </summary>
/// <returns>The index of minimum element.</returns>
public virtual int MinimumIndex()
{
int index = 0;
double min = this[0];
for (int i = 1; i < Count; i++)
{
if (min > this[i])
{
index = i;
min = this[i];
}
}
return index;
}
/// <summary>
/// Computes the sum of the vector's elements.
/// </summary>
/// <returns>The sum of the vector's elements.</returns>
public virtual double Sum()
{
double result = 0;
for (int i = 0; i < Count; i++)
{
result += this[i];
}
return result;
}
/// <summary>
/// Computes the sum of the absolute value of the vector's elements.
/// </summary>
/// <returns>The sum of the absolute value of the vector's elements.</returns>
public virtual double SumMagnitudes()
{
double result = 0;
for (int i = 0; i < Count; i++)
{
result += Math.Abs(this[i]);
}
return result;
}
#endregion
#region Arithmetic Operator Overloading
@ -1110,32 +1461,63 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Creates a vector containing specified elements.
/// </summary>
/// <param name="start">The first element to begin copying from.</param>
/// <param name="index">The first element to begin copying from.</param>
/// <param name="length">The number of elements to copy.</param>
/// <returns>A vector containing a copy of the specified elements.</returns>
/// <exception cref="ArgumentOutOfRangeException"><list><item>If <paramref name="start"/> is not positive or
/// <exception cref="ArgumentOutOfRangeException"><list><item>If <paramref name="index"/> is not positive or
/// greater than or equal to the size of the vector.</item>
/// <item>If <paramref name="start"/> + <paramref name="length"/> is greater than or equal to the size of the vector.</item>
/// <item>If <paramref name="index"/> + <paramref name="length"/> is greater than or equal to the size of the vector.</item>
/// </list></exception>
/// <exception cref="ArgumentException">If <paramref name="length"/> is not positive.</exception>
public virtual Vector SubVector(int start, int length)
public virtual Vector SubVector(int index, int length)
{
if (start < 0 || start >= Count)
if (index < 0 || index >= Count)
{
throw new ArgumentOutOfRangeException("start");
}
if (start + length > Count)
if (length <= 0)
{
throw new ArgumentOutOfRangeException("length");
}
if (index + length > Count)
{
throw new ArgumentOutOfRangeException("start");
}
Vector result = CreateVector(length);
CommonParallel.For(start,
start + length, index => result[index-start] = this[index]);
CommonParallel.For(
index,
index + length,
i => result[i - index] = this[i]);
return result;
}
/// <summary>
/// Set the values of this vector to the given values.
/// </summary>
/// <param name="values">The array containing the values to use.</param>
/// <exception cref="ArgumentNullException">If <paramref name="values"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <paramref name="values"/> is not the same size as this vector.</exception>
public virtual void SetValues(double[] values)
{
if (values == null)
{
throw new ArgumentNullException("values");
}
if (values.Length != Count)
{
throw new ArgumentException("values", Resources.ArgumentVectorsSameLength);
}
CommonParallel.For(
0,
values.Length,
i => this[i] = values[i]);
}
#endregion
#region Implemented Interfaces

6
src/Numerics/Settings.StyleCop

@ -0,0 +1,6 @@
<StyleCopSettings Version="4.3">
<GlobalSettings>
<StringProperty Name="LinkedSettingsFile">..\Settings.StyleCop</StringProperty>
<StringProperty Name="MergeSettingsFiles">Linked</StringProperty>
</GlobalSettings>
</StyleCopSettings>

399
src/UnitTests/LinearAlgebraTests/Double/DenseVectorTests.cs

@ -33,6 +33,7 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
using System.Collections.Generic;
using MbUnit.Framework;
using LinearAlgebra.Double;
using System;
public class DenseVectorTests : VectorTests
{
@ -254,22 +255,400 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
[Test]
[MultipleAsserts]
public void CanCopyToNonDenseVector()
public void CanFindAbsoluteMinimumIndexInDenseVector()
{
var vector = new DenseVector(_data);
var other = new UserDefinedVector(_data.Length);
vector.CopyTo(other, 1,1,2);
DenseVector source = new DenseVector(_data);
int expected = 0;
int actual = source.AbsoluteMinimumIndex();
Assert.AreEqual(expected, actual);
}
Assert.AreEqual(vector[1], other[1]);
Assert.AreEqual(vector[2], other[2]);
vector.CopyTo(other);
[Test]
public void CanFindAbsoluteMinimumInDenseVector()
{
DenseVector source = new DenseVector(_data);
double expected = 1;
double actual = source.AbsoluteMinimum();
Assert.AreEqual(expected, actual);
for(var i = 0; i < vector.Count; i++)
}
[Test]
[Row(0, 5)]
[Row(2, 2)]
[Row(1, 4)]
[Row(6, 10, ExpectedException = typeof(ArgumentOutOfRangeException))]
[Row(1, 10, ExpectedException = typeof(ArgumentOutOfRangeException))]
[Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))]
public void CanCalculateSubVector(int index, int length)
{
DenseVector vector = new DenseVector(_data);
Vector sub = vector.SubVector(index, length);
Assert.AreEqual(length, sub.Count);
for (int i = 0; i < length; i++)
{
Assert.AreEqual(vector[i], other[i]);
Assert.AreEqual(vector[i + index], sub[i]);
}
}
[Test]
public void CanFindMaximumIndexInDenseVector()
{
DenseVector vector = new DenseVector(_data);
int expected = 4;
int actual = vector.MaximumIndex();
Assert.AreEqual(expected, actual);
}
[Test]
public void CanFindMaximumInDenseVector()
{
DenseVector vector = new DenseVector(_data);
double expected = 5;
double actual = vector.Maximum();
Assert.AreEqual(expected, actual);
}
[Test]
public void CanFindMinimumIndexOfDenseVector()
{
DenseVector vector = new DenseVector(_data);
int expected = 0;
int actual = vector.MinimumIndex();
Assert.AreEqual(expected, actual);
}
[Test]
public void CanFindMinimumOfDenseVector()
{
DenseVector vector = new DenseVector(_data);
double expected = 1;
double actual = vector.Minimum();
Assert.AreEqual(expected, actual);
}
[Test]
public void SumOfDenseVector()
{
double[] testData = { -20, -10, 10, 20, 30, };
DenseVector vector = new DenseVector(testData);
double actual = vector.Sum();
double expected = 30;
Assert.AreEqual(expected, actual);
}
[Test]
public void SumMagnitudesOfDenseVector()
{
double[] testData = { -20, -10, 10, 20, 30, };
DenseVector vector = new DenseVector(testData);
double actual = vector.SumMagnitudes();
double expected = 90;
Assert.AreEqual(expected, actual);
}
[Test]
[ExpectedArgumentNullException]
public new void SetValuesWithNullParameterShouldThrowException()
{
DenseVector vector = new DenseVector(_data);
vector.SetValues(null);
}
[Test]
[ExpectedArgumentException]
public new void SetValuesWithNonEqualDataLengthShouldThrowException()
{
DenseVector vector = new DenseVector(_data.Length + 2);
vector.SetValues(_data);
}
[Test]
public new void PointWiseMultiply()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = new DenseVector(vector1.Count);
vector1.PointWiseMultiply(vector2, result);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] * _data[i], result[i]);
}
}
[Test]
[ExpectedArgumentNullException]
public new void PointWiseMultiplyWithOtherNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = null;
DenseVector result = new DenseVector(vector1.Count);
vector1.PointWiseMultiply(vector2, result);
}
[Test]
[ExpectedArgumentNullException]
public new void PointWiseMultiplyWithResultNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = null;
vector1.PointWiseMultiply(vector2, result);
}
[Test]
[ExpectedArgumentException]
public new void PointWiseMultiplyWithInvalidResultLengthShouldThrowException()
{
Vector vector1 = new DenseVector(_data);
Vector vector2 = new DenseVector(_data);
Vector result = CreateVector(vector1.Count + 1);
vector1.PointWiseMultiply(vector2, result);
}
[Test]
public new void PointWiseMultiplyWithResult()
{
Vector vector1 = new DenseVector(_data);
Vector vector2 = new DenseVector(_data);
Vector result = vector1.PointWiseMultiply(vector2);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] * _data[i], result[i]);
}
}
[Test]
public new void PointWiseAdd()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = new DenseVector(vector1.Count);
vector1.PointWiseAdd(vector2, result);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] + _data[i], result[i]);
}
}
[Test]
[ExpectedArgumentNullException]
public new void PointWiseAddWithOtherNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = null;
DenseVector result = new DenseVector(vector1.Count);
vector1.PointWiseAdd(vector2, result);
}
[Test]
[ExpectedArgumentNullException]
public new void PointWiseAddWithResultNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = null;
vector1.PointWiseAdd(vector2, result);
}
[Test]
[ExpectedArgumentException]
public new void PointWiseAddWithInvalidResultLengthShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = new DenseVector(vector1.Count + 1);
vector1.PointWiseAdd(vector2, result);
}
[Test]
public new void PointWiseAddWithResult()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = vector1.PointWiseAdd(vector2);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] + _data[i], result[i]);
}
}
[Test]
public new void PointWiseSubtract()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = new DenseVector(vector1.Count);
vector1.PointWiseSubtract(vector2, result);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] - _data[i], result[i]);
}
}
[Test]
[ExpectedArgumentNullException]
public new void PointWiseSubtractWithOtherNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = null;
DenseVector result = new DenseVector(vector1.Count);
vector1.PointWiseSubtract(vector2, result);
}
[Test]
[ExpectedArgumentNullException]
public new void PointWiseSubtractWithResultNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
Vector result = null;
vector1.PointWiseSubtract(vector2, result);
}
[Test]
[ExpectedArgumentException]
public new void PointWiseSubtractWithInvalidResultLengthShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = new DenseVector(vector1.Count + 1);
vector1.PointWiseSubtract(vector2, result);
}
[Test]
public new void PointWiseSubtractWithResult()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = vector1.PointWiseSubtract(vector2);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] - _data[i], result[i]);
}
}
[Test]
public new void PointWiseDivide()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = new DenseVector(vector1.Count);
vector1.PointWiseDivide(vector2, result);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] / _data[i], result[i]);
}
}
[Test]
[ExpectedArgumentNullException]
public new void PointWiseDivideWithOtherNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = null;
DenseVector result = new DenseVector(vector1.Count);
vector1.PointWiseDivide(vector2, result);
}
[Test]
[ExpectedArgumentNullException]
public new void PointWiseDivideWithResultNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = null;
vector1.PointWiseDivide(vector2, result);
}
[Test]
[ExpectedArgumentException]
public new void PointWiseDivideWithInvalidResultLengthShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = new DenseVector(vector1.Count + 1);
vector1.PointWiseDivide(vector2, result);
}
[Test]
public new void PointWiseDivideWithResult()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
DenseVector result = vector1.PointWiseDivide(vector2);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] / _data[i], result[i]);
}
}
[Test]
public new void CanCalculateDyadicProduct()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
Matrix m = DenseVector.DyadicProduct(vector1, vector2);
for (int i = 0; i < vector1.Count; i++)
{
for (int j = 0; j < vector2.Count; j++)
{
Assert.AreEqual(m[i, j], vector1[i] * vector2[j]);
}
}
}
[Test]
[ExpectedArgumentNullException]
public new void DyadicProductWithFirstParameterNullShouldThrowException()
{
DenseVector vector1 = null;
DenseVector vector2 = new DenseVector(_data);
DenseVector.DyadicProduct(vector1, vector2);
}
[Test]
[ExpectedArgumentNullException]
public new void DyadicProductWithSecondParameterNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = null;
Vector.DyadicProduct(vector1, vector2);
}
[Test]
public new void CanCalculateTensorMultiply()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = new DenseVector(_data);
Matrix m = vector1.TensorMultiply(vector2);
for (int i = 0; i < vector1.Count; i++)
{
for (int j = 0; j < vector2.Count; j++)
{
Assert.AreEqual(m[i, j], vector1[i] * vector2[j]);
}
}
}
[Test]
[ExpectedArgumentNullException]
public new void TensorMultiplyWithNullParameterNullShouldThrowException()
{
DenseVector vector1 = new DenseVector(_data);
DenseVector vector2 = null;
vector1.TensorMultiply(vector2);
}
[Test]
[ExpectedArgumentException]
public new void RandomWithNumberOfElementsLessThanZeroShouldThrowException()
{
DenseVector vector = new DenseVector(4);
vector = vector.Random(-3);
}
}
}

302
src/UnitTests/LinearAlgebraTests/Double/VectorTests.Arithmetic.cs

@ -782,6 +782,171 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
}
[Test]
public void PointWiseAdd()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = CreateVector(vector1.Count);
vector1.PointWiseAdd(vector2, result);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] + _data[i], result[i]);
}
}
[Test]
[ExpectedArgumentNullException]
public void PointWiseAddWithOtherNullShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = null;
Vector result = CreateVector(vector1.Count);
vector1.PointWiseAdd(vector2, result);
}
[Test]
[ExpectedArgumentNullException]
public void PointWiseAddWithResultNullShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = null;
vector1.PointWiseAdd(vector2, result);
}
[Test]
[ExpectedArgumentException]
public void PointWiseAddWithInvalidResultLengthShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = CreateVector(vector1.Count + 1);
vector1.PointWiseAdd(vector2, result);
}
[Test]
public void PointWiseAddWithResult()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = vector1.PointWiseAdd(vector2);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] + _data[i], result[i]);
}
}
[Test]
public void PointWiseSubtract()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = CreateVector(vector1.Count);
vector1.PointWiseSubtract(vector2, result);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] - _data[i], result[i]);
}
}
[Test]
[ExpectedArgumentNullException]
public void PointWiseSubtractWithOtherNullShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = null;
Vector result = CreateVector(vector1.Count);
vector1.PointWiseSubtract(vector2, result);
}
[Test]
[ExpectedArgumentNullException]
public void PointWiseSubtractWithResultNullShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = null;
vector1.PointWiseSubtract(vector2, result);
}
[Test]
[ExpectedArgumentException]
public void PointWiseSubtractWithInvalidResultLengthShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = CreateVector(vector1.Count + 1);
vector1.PointWiseSubtract(vector2, result);
}
[Test]
public void PointWiseSubtractWithResult()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = vector1.PointWiseSubtract(vector2);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] - _data[i], result[i]);
}
}
[Test]
public void PointWiseDivide()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = CreateVector(vector1.Count);
vector1.PointWiseDivide(vector2, result);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] / _data[i], result[i]);
}
}
[Test]
[ExpectedArgumentNullException]
public void PointWiseDivideWithOtherNullShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = null;
Vector result = CreateVector(vector1.Count);
vector1.PointWiseDivide(vector2, result);
}
[Test]
[ExpectedArgumentNullException]
public void PointWiseDivideWithResultNullShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = null;
vector1.PointWiseDivide(vector2, result);
}
[Test]
[ExpectedArgumentException]
public void PointWiseDivideWithInvalidResultLengthShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = CreateVector(vector1.Count + 1);
vector1.PointWiseDivide(vector2, result);
}
[Test]
public void PointWiseDivideWithResult()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Clone();
Vector result = vector1.PointWiseDivide(vector2);
for (int i = 0; i < vector1.Count; i++)
{
Assert.AreEqual(_data[i] / _data[i], result[i]);
}
}
[Test]
public void CanFindAbsoluteMinimumIndex()
{
@ -801,22 +966,153 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
}
[Test]
public void CanFindMaximumIndex()
{
Vector vector = CreateVector(_data);
int expected = 4;
int actual = vector.MaximumIndex();
Assert.AreEqual(expected, actual);
}
[Test]
public void CanFindMaximum()
{
Vector vector = CreateVector(_data);
double expected = 5;
double actual = vector.Maximum();
Assert.AreEqual(expected, actual);
}
[Test]
public void CanFindMinimumIndex()
{
Vector vector = CreateVector(_data);
int expected = 0;
int actual = vector.MinimumIndex();
Assert.AreEqual(expected, actual);
}
[Test]
public void CanFindMinimum()
{
Vector vector = CreateVector(_data);
double expected = 1;
double actual = vector.Minimum();
Assert.AreEqual(expected, actual);
}
[Test]
[Row(0, 5)]
[Row(2, 2)]
[Row(1, 4)]
[Row(6, 10, ExpectedException = typeof(ArgumentOutOfRangeException))]
[Row(1, 10, ExpectedException = typeof(ArgumentOutOfRangeException))]
public void SubVector(int start, int length)
[Row(1, -10, ExpectedException = typeof(ArgumentOutOfRangeException))]
public void SubVector(int index, int length)
{
Vector vector = CreateVector(_data);
Vector sub = vector.SubVector(start, length);
Vector sub = vector.SubVector(index, length);
Assert.AreEqual(length, sub.Count);
for (int i = 0; i < length; i++)
{
Assert.AreEqual(vector[i + start], sub[i]);
Assert.AreEqual(vector[i + index], sub[i]);
}
}
[Test]
public void Sum()
{
double[] testData = { -20, -10, 10, 20, 30, };
Vector vector = CreateVector(testData);
double actual = vector.Sum();
double expected = 30;
Assert.AreEqual(expected, actual);
}
[Test]
public void SumMagnitudes()
{
double[] testData = { -20, -10, 10, 20, 30, };
Vector vector = CreateVector(testData);
double actual = vector.SumMagnitudes();
double expected = 90;
Assert.AreEqual(expected, actual);
}
[Test]
public void CanCalculateDyadicProduct()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = CreateVector(_data);
Matrix m = Vector.DyadicProduct(vector1,vector2);
for (int i = 0; i < vector1.Count; i++)
{
for (int j = 0; j < vector2.Count; j++)
{
Assert.AreEqual(m[i, j], vector1[i] * vector2[j]);
}
}
}
[Test]
[ExpectedArgumentNullException]
public void DyadicProductWithFirstParameterNullShouldThrowException()
{
Vector vector1 = null;
Vector vector2 = CreateVector(_data);
Vector.DyadicProduct(vector1, vector2);
}
[Test]
[ExpectedArgumentNullException]
public void DyadicProductWithSecondParameterNullShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = null;
Vector.DyadicProduct(vector1, vector2);
}
[Test]
public void CanCalculateTensorMultiply()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = CreateVector(_data);
Matrix m = vector1.TensorMultiply(vector2);
for (int i = 0; i < vector1.Count; i++)
{
for (int j = 0; j < vector2.Count; j++)
{
Assert.AreEqual(m[i, j], vector1[i] * vector2[j]);
}
}
}
[Test]
[ExpectedArgumentNullException]
public void TensorMultiplyWithNullParameterNullShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = null;
vector1.TensorMultiply(vector2);
}
[Test]
[ExpectedArgumentException]
public void RandomWithNumberOfElementsLessThanZeroShouldThrowException()
{
Vector vector1 = CreateVector(_data);
Vector vector2 = vector1.Random(-5);
}
}
}

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

@ -252,9 +252,35 @@ namespace MathNet.Numerics.UnitTests.LinearAlgebraTests.Double
Assert.AreEqual(vector[i], matrix[0, i]);
}
}
[Test]
public void CanSetValues()
{
Vector vector = CreateVector(_data);
vector.SetValues(_data);
for (int i = 0; i < _data.Length; i++)
{
Assert.AreEqual(vector[i], _data[i]);
}
}
protected abstract Vector CreateVector(int size);
[Test]
[ExpectedArgumentNullException]
public void SetValuesWithNullParameterShouldThrowException()
{
Vector vector = CreateVector(_data);
vector.SetValues(null);
}
[Test]
[ExpectedArgumentException]
public void SetValuesWithNonEqualDataLengthShouldThrowException()
{
Vector vector = CreateVector(_data.Length + 2);
vector.SetValues(_data);
}
protected abstract Vector CreateVector(int size);
protected abstract Vector CreateVector(IList<double> data);
}
}
Loading…
Cancel
Save