Browse Source

Merge pull request #124 from tibel/master

LA: Add vector operations (scalar/vector and scalar-vector)
pull/125/head
Christoph Ruegg 13 years ago
parent
commit
e411befb40
  1. 13
      src/Numerics/LinearAlgebra/Complex/Vector.cs
  2. 13
      src/Numerics/LinearAlgebra/Complex32/Vector.cs
  3. 13
      src/Numerics/LinearAlgebra/Double/Vector.cs
  4. 8
      src/Numerics/LinearAlgebra/Generic/Matrix.cs
  5. 2
      src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs
  6. 131
      src/Numerics/LinearAlgebra/Generic/Vector.cs
  7. 13
      src/Numerics/LinearAlgebra/Single/Vector.cs

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

@ -146,6 +146,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
DoMultiply(1 / scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <param name="result">The vector to store the result of the division.</param>
protected override void DoDivideByThis(Complex scalar, Vector<Complex> result)
{
for (var index = 0; index < Count; index++)
{
result.At(index, scalar / At(index));
}
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>

13
src/Numerics/LinearAlgebra/Complex32/Vector.cs

@ -146,6 +146,19 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
DoMultiply(1 / scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <param name="result">The vector to store the result of the division.</param>
protected override void DoDivideByThis(Complex32 scalar, Vector<Complex32> result)
{
for (var index = 0; index < Count; index++)
{
result.At(index, scalar / At(index));
}
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>

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

@ -145,6 +145,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
DoMultiply(1 / scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <param name="result">The vector to store the result of the division.</param>
protected override void DoDivideByThis(double scalar, Vector<double> result)
{
for (var index = 0; index < Count; index++)
{
result.At(index, scalar / At(index));
}
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>

8
src/Numerics/LinearAlgebra/Generic/Matrix.cs

@ -110,7 +110,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
{
for (var i = 0; i < columnVectors[j].Count; i++)
{
matrix.At(i, j, columnVectors[j][i]);
matrix.At(i, j, columnVectors[j].At(i));
}
}
@ -149,7 +149,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
{
for (var j = 0; j < rowVectors[i].Count; j++)
{
matrix.At(i, j, rowVectors[i][j]);
matrix.At(i, j, rowVectors[i].At(j));
}
}
@ -640,7 +640,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
for (var i = 0; i < min; i++)
{
diagonal[i] = At(i, i);
diagonal.At(i, At(i, i));
}
return diagonal;
@ -962,7 +962,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
for (var i = 0; i < min; i++)
{
At(i, i, source[i]);
At(i, i, source.At(i));
}
}

2
src/Numerics/LinearAlgebra/Generic/Vector.BCL.cs

@ -96,7 +96,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
{
for (int i = 0; i < Count; ++i)
{
if (this[i].Equals(item))
if (At(i).Equals(item))
return i;
}
return -1;

131
src/Numerics/LinearAlgebra/Generic/Vector.cs

@ -191,6 +191,17 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="result">The vector to store the result of the subtraction.</param>
protected abstract void DoSubtract(T scalar, Vector<T> result);
/// <summary>
/// Subtracts each element of the vector from a scalar and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract from.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
protected virtual void DoSubtractFrom(T scalar, Vector<T> result)
{
DoNegate(result);
result.DoAdd(scalar, result);
}
/// <summary>
/// Subtracts another vector to this vector and stores the result into the result vector.
/// </summary>
@ -219,6 +230,13 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <param name="result">The vector to store the result of the division.</param>
protected abstract void DoDivide(T scalar, Vector<T> result);
/// <summary>
/// Divides a scalar by each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <param name="result">The vector to store the result of the division.</param>
protected abstract void DoDivideByThis(T scalar, Vector<T> result);
/// <summary>
/// Computes the modulus for each element of the vector for the given divisor.
/// </summary>
@ -391,6 +409,40 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
DoSubtract(scalar, result);
}
/// <summary>
/// Subtracts each element of the vector from a scalar.
/// </summary>
/// <param name="scalar">The scalar to subtract from.</param>
/// <returns>A new vector containing the subtraction of the scalar and this vector.</returns>
public Vector<T> SubtractFrom(T scalar)
{
var result = CreateVector(Count);
DoSubtractFrom(scalar, result);
return result;
}
/// <summary>
/// Subtracts each element of the vector from a scalar and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to subtract from.</param>
/// <param name="result">The vector to store the result of the subtraction.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void SubtractFrom(T scalar, Vector<T> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoSubtractFrom(scalar, result);
}
/// <summary>
/// Returns a negated vector.
/// </summary>
@ -623,6 +675,40 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
DoDivide(scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the vector.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <returns>A new vector that is the division of the vector and the scalar.</returns>
public Vector<T> DevideByThis(T scalar)
{
var result = CreateVector(Count);
DoDivideByThis(scalar, result);
return result;
}
/// <summary>
/// Divides a scalar by each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <param name="result">The vector to store the result of the division.</param>
/// <exception cref="ArgumentNullException">If the result vector is <see langword="null"/>.</exception>
/// <exception cref="ArgumentException">If this vector and <paramref name="result"/> are not the same size.</exception>
public void DivideByThis(T scalar, Vector<T> result)
{
if (result == null)
{
throw new ArgumentNullException("result");
}
if (Count != result.Count)
{
throw new ArgumentException(Resources.ArgumentVectorsSameLength, "result");
}
DoDivideByThis(scalar, result);
}
/// <summary>
/// Computes the modulus for each element of the vector for the given divisor.
/// </summary>
@ -795,7 +881,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
for (var i = 0; i < u.Count; i++)
{
matrix.SetRow(i, v.Multiply(u[i]));
matrix.SetRow(i, v.Multiply(u.At(i)));
}
return matrix;
@ -948,9 +1034,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
throw new ArgumentNullException("rightSide");
}
var res = rightSide.Negate();
res.Add(leftSide, res);
return res;
return rightSide.SubtractFrom(leftSide);
}
/// <summary>
@ -1005,6 +1089,23 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return leftSide.DotProduct(rightSide);
}
/// <summary>
/// Divides a scalar with a vector.
/// </summary>
/// <param name="leftSide">The scalar to divide.</param>
/// <param name="rightSide">The vector.</param>
/// <returns>The result of the division.</returns>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
public static Vector<T> operator /(T leftSide, Vector<T> rightSide)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
return rightSide.DevideByThis(leftSide);
}
/// <summary>
/// Divides a vector with a scalar.
/// </summary>
@ -1022,6 +1123,24 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
return leftSide.Divide(rightSide);
}
/// <summary>
/// Pointwise divides two <strong>Vectors</strong>.
/// </summary>
/// <param name="leftSide">The vector to divide.</param>
/// <param name="rightSide">The other vector.</param>
/// <returns>The result of the division.</returns>
/// <exception cref="ArgumentException">If <paramref name="leftSide"/> and <paramref name="rightSide"/> are not the same size.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
public static Vector<T> operator /(Vector<T> leftSide, Vector<T> rightSide)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
return leftSide.PointwiseDivide(rightSide);
}
/// <summary>
/// Computes the modulus of each element of the vector of the given divisor.
/// </summary>
@ -1091,7 +1210,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <returns>The value of maximum element.</returns>
public T Maximum()
{
return this[MaximumIndex()];
return At(MaximumIndex());
}
/// <summary>
@ -1106,7 +1225,7 @@ namespace MathNet.Numerics.LinearAlgebra.Generic
/// <returns>The value of the minimum element.</returns>
public T Minimum()
{
return this[MinimumIndex()];
return At(MinimumIndex());
}
/// <summary>

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

@ -145,6 +145,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
DoMultiply(1 / scalar, result);
}
/// <summary>
/// Divides a scalar by each element of the vector and stores the result in the result vector.
/// </summary>
/// <param name="scalar">The scalar to divide.</param>
/// <param name="result">The vector to store the result of the division.</param>
protected override void DoDivideByThis(float scalar, Vector<float> result)
{
for (var index = 0; index < Count; index++)
{
result.At(index, scalar / At(index));
}
}
/// <summary>
/// Pointwise multiplies this vector with another vector and stores the result into the result vector.
/// </summary>

Loading…
Cancel
Save