Browse Source

LA: diagonal matrix-vector Multiply LeftMultiply/TransposeThisAndMultiply

pull/184/head
Christoph Ruegg 13 years ago
parent
commit
6754045a56
  1. 210
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  2. 210
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  3. 210
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  4. 4
      src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs
  5. 210
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

210
src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs

@ -327,6 +327,36 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Multiplies this matrix with a vector and places the results into the result vector.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<Complex> rightSide, Vector<Complex> result)
{
var d = Math.Min(ColumnCount, RowCount);
if (d < RowCount)
{
result.ClearSubVector(ColumnCount, RowCount - ColumnCount);
}
if (d == ColumnCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex>;
var denseResult = result.Storage as DenseVectorStorage<Complex>;
if (denseOther != null && denseResult != null)
{
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i]*rightSide.At(i));
}
}
/// <summary>
/// Multiplies this matrix with another matrix and places the results into the result matrix.
/// </summary>
@ -372,131 +402,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
base.DoMultiply(other, result);
}
/// <summary>
/// Multiplies this matrix with a vector and places the results into the result matrix.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <strong>result.Count != this.RowCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.ColumnCount != <paramref name="rightSide"/>.Count</strong>.</exception>
public override void Multiply(Vector<Complex> rightSide, Vector<Complex> result)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (ColumnCount != rightSide.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, rightSide, "rightSide");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (RowCount != result.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
if (ReferenceEquals(rightSide, result))
{
var tmp = result.CreateVector(result.Count);
Multiply(rightSide, tmp);
tmp.CopyTo(result);
}
else
{
// Clear the result vector
result.Clear();
// Multiply the elements in the vector with the corresponding diagonal element in this.
for (var r = 0; r < _data.Length; r++)
{
result[r] = _data[r] * rightSide[r];
}
}
}
/// <summary>
/// Left multiply a matrix with a vector ( = vector * matrix ) and place the result in the result vector.
/// </summary>
/// <param name="leftSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <strong>result.Count != this.ColumnCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.RowCount != <paramref name="leftSide"/>.Count</strong>.</exception>
public override void LeftMultiply(Vector<Complex> leftSide, Vector<Complex> result)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (RowCount != leftSide.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, leftSide, "leftSide");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (ColumnCount != result.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
if (ReferenceEquals(leftSide, result))
{
var tmp = result.CreateVector(result.Count);
LeftMultiply(leftSide, tmp);
tmp.CopyTo(result);
}
else
{
// Clear the result vector
result.Clear();
// Multiply the elements in the vector with the corresponding diagonal element in this.
for (var r = 0; r < _data.Length; r++)
{
result[r] = _data[r] * leftSide[r];
}
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>
/// <returns>The determinant of this matrix.</returns>
public override Complex Determinant()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
return _data.Aggregate(Complex.One, (current, t) => current * t);
}
/// <summary>
/// Returns the elements of the diagonal in a <see cref="DenseVector"/>.
/// </summary>
/// <returns>The elements of the diagonal.</returns>
/// <remarks>For non-square matrices, the method returns Min(Rows, Columns) elements where
/// i == j (i is the row index, and j is the column index).</remarks>
public override Vector<Complex> Diagonal()
{
return new DenseVector(_data).Clone();
}
/// <summary>
/// Multiplies this matrix with transpose of another matrix and places the results into the result matrix.
/// </summary>
@ -586,6 +491,61 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
base.DoTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with a vector and places the results into the result vector.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Vector<Complex> rightSide, Vector<Complex> result)
{
var d = Math.Min(ColumnCount, RowCount);
if (d < ColumnCount)
{
result.ClearSubVector(RowCount, ColumnCount - RowCount);
}
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex>;
var denseResult = result.Storage as DenseVectorStorage<Complex>;
if (denseOther != null && denseResult != null)
{
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i]*rightSide.At(i));
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>
/// <returns>The determinant of this matrix.</returns>
public override Complex Determinant()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
return _data.Aggregate(Complex.One, (current, t) => current * t);
}
/// <summary>
/// Returns the elements of the diagonal in a <see cref="DenseVector"/>.
/// </summary>
/// <returns>The elements of the diagonal.</returns>
/// <remarks>For non-square matrices, the method returns Min(Rows, Columns) elements where
/// i == j (i is the row index, and j is the column index).</remarks>
public override Vector<Complex> Diagonal()
{
return new DenseVector(_data).Clone();
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>

210
src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs

@ -320,6 +320,36 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Multiplies this matrix with a vector and places the results into the result vector.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<Complex32> rightSide, Vector<Complex32> result)
{
var d = Math.Min(ColumnCount, RowCount);
if (d < RowCount)
{
result.ClearSubVector(ColumnCount, RowCount - ColumnCount);
}
if (d == ColumnCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex32>;
var denseResult = result.Storage as DenseVectorStorage<Complex32>;
if (denseOther != null && denseResult != null)
{
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i]*rightSide.At(i));
}
}
/// <summary>
/// Multiplies this matrix with another matrix and places the results into the result matrix.
/// </summary>
@ -365,131 +395,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
base.DoMultiply(other, result);
}
/// <summary>
/// Multiplies this matrix with a vector and places the results into the result matrix.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <strong>result.Count != this.RowCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.ColumnCount != <paramref name="rightSide"/>.Count</strong>.</exception>
public override void Multiply(Vector<Complex32> rightSide, Vector<Complex32> result)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (ColumnCount != rightSide.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, rightSide, "rightSide");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (RowCount != result.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
if (ReferenceEquals(rightSide, result))
{
var tmp = result.CreateVector(result.Count);
Multiply(rightSide, tmp);
tmp.CopyTo(result);
}
else
{
// Clear the result vector
result.Clear();
// Multiply the elements in the vector with the corresponding diagonal element in this.
for (var r = 0; r < _data.Length; r++)
{
result[r] = _data[r] * rightSide[r];
}
}
}
/// <summary>
/// Left multiply a matrix with a vector ( = vector * matrix ) and place the result in the result vector.
/// </summary>
/// <param name="leftSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <strong>result.Count != this.ColumnCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.RowCount != <paramref name="leftSide"/>.Count</strong>.</exception>
public override void LeftMultiply(Vector<Complex32> leftSide, Vector<Complex32> result)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (RowCount != leftSide.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, leftSide, "leftSide");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (ColumnCount != result.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
if (ReferenceEquals(leftSide, result))
{
var tmp = result.CreateVector(result.Count);
LeftMultiply(leftSide, tmp);
tmp.CopyTo(result);
}
else
{
// Clear the result vector
result.Clear();
// Multiply the elements in the vector with the corresponding diagonal element in this.
for (var r = 0; r < _data.Length; r++)
{
result[r] = _data[r] * leftSide[r];
}
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>
/// <returns>The determinant of this matrix.</returns>
public override Complex32 Determinant()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
return _data.Aggregate(Complex32.One, (current, t) => current * t);
}
/// <summary>
/// Returns the elements of the diagonal in a <see cref="DenseVector"/>.
/// </summary>
/// <returns>The elements of the diagonal.</returns>
/// <remarks>For non-square matrices, the method returns Min(Rows, Columns) elements where
/// i == j (i is the row index, and j is the column index).</remarks>
public override Vector<Complex32> Diagonal()
{
return new DenseVector(_data).Clone();
}
/// <summary>
/// Multiplies this matrix with transpose of another matrix and places the results into the result matrix.
/// </summary>
@ -579,6 +484,61 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
base.DoTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with a vector and places the results into the result vector.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Vector<Complex32> rightSide, Vector<Complex32> result)
{
var d = Math.Min(ColumnCount, RowCount);
if (d < ColumnCount)
{
result.ClearSubVector(RowCount, ColumnCount - RowCount);
}
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<Complex32>;
var denseResult = result.Storage as DenseVectorStorage<Complex32>;
if (denseOther != null && denseResult != null)
{
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i]*rightSide.At(i));
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>
/// <returns>The determinant of this matrix.</returns>
public override Complex32 Determinant()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
return _data.Aggregate(Complex32.One, (current, t) => current * t);
}
/// <summary>
/// Returns the elements of the diagonal in a <see cref="DenseVector"/>.
/// </summary>
/// <returns>The elements of the diagonal.</returns>
/// <remarks>For non-square matrices, the method returns Min(Rows, Columns) elements where
/// i == j (i is the row index, and j is the column index).</remarks>
public override Vector<Complex32> Diagonal()
{
return new DenseVector(_data).Clone();
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>

210
src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs

@ -321,6 +321,36 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Multiplies this matrix with a vector and places the results into the result vector.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<double> rightSide, Vector<double> result)
{
var d = Math.Min(ColumnCount, RowCount);
if (d < RowCount)
{
result.ClearSubVector(ColumnCount, RowCount - ColumnCount);
}
if (d == ColumnCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<double>;
var denseResult = result.Storage as DenseVectorStorage<double>;
if (denseOther != null && denseResult != null)
{
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i]*rightSide.At(i));
}
}
/// <summary>
/// Multiplies this matrix with another matrix and places the results into the result matrix.
/// </summary>
@ -366,131 +396,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
base.DoMultiply(other, result);
}
/// <summary>
/// Multiplies this matrix with a vector and places the results into the result matrix.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <strong>result.Count != this.RowCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.ColumnCount != <paramref name="rightSide"/>.Count</strong>.</exception>
public override void Multiply(Vector<double> rightSide, Vector<double> result)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (ColumnCount != rightSide.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, rightSide);
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (RowCount != result.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
if (ReferenceEquals(rightSide, result))
{
var tmp = result.CreateVector(result.Count);
Multiply(rightSide, tmp);
tmp.CopyTo(result);
}
else
{
// Clear the result vector
result.Clear();
// Multiply the elements in the vector with the corresponding diagonal element in this.
for (var r = 0; r < _data.Length; r++)
{
result[r] = _data[r] * rightSide[r];
}
}
}
/// <summary>
/// Left multiply a matrix with a vector ( = vector * matrix ) and place the result in the result vector.
/// </summary>
/// <param name="leftSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <strong>result.Count != this.ColumnCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.RowCount != <paramref name="leftSide"/>.Count</strong>.</exception>
public override void LeftMultiply(Vector<double> leftSide, Vector<double> result)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (RowCount != leftSide.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, leftSide, "leftSide");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (ColumnCount != result.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
if (ReferenceEquals(leftSide, result))
{
var tmp = result.CreateVector(result.Count);
LeftMultiply(leftSide, tmp);
tmp.CopyTo(result);
}
else
{
// Clear the result vector
result.Clear();
// Multiply the elements in the vector with the corresponding diagonal element in this.
for (var r = 0; r < _data.Length; r++)
{
result[r] = _data[r] * leftSide[r];
}
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>
/// <returns>The determinant of this matrix.</returns>
public override double Determinant()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
return _data.Aggregate(1.0, (current, t) => current * t);
}
/// <summary>
/// Returns the elements of the diagonal in a <see cref="DenseVector"/>.
/// </summary>
/// <returns>The elements of the diagonal.</returns>
/// <remarks>For non-square matrices, the method returns Min(Rows, Columns) elements where
/// i == j (i is the row index, and j is the column index).</remarks>
public override Vector<double> Diagonal()
{
return new DenseVector(_data).Clone();
}
/// <summary>
/// Multiplies this matrix with transpose of another matrix and places the results into the result matrix.
/// </summary>
@ -580,6 +485,61 @@ namespace MathNet.Numerics.LinearAlgebra.Double
base.DoTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with a vector and places the results into the result vector.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Vector<double> rightSide, Vector<double> result)
{
var d = Math.Min(ColumnCount, RowCount);
if (d < ColumnCount)
{
result.ClearSubVector(RowCount, ColumnCount - RowCount);
}
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<double>;
var denseResult = result.Storage as DenseVectorStorage<double>;
if (denseOther != null && denseResult != null)
{
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i]*rightSide.At(i));
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>
/// <returns>The determinant of this matrix.</returns>
public override double Determinant()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
return _data.Aggregate(1.0, (current, t) => current * t);
}
/// <summary>
/// Returns the elements of the diagonal in a <see cref="DenseVector"/>.
/// </summary>
/// <returns>The elements of the diagonal.</returns>
/// <remarks>For non-square matrices, the method returns Min(Rows, Columns) elements where
/// i == j (i is the row index, and j is the column index).</remarks>
public override Vector<double> Diagonal()
{
return new DenseVector(_data).Clone();
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>

4
src/Numerics/LinearAlgebra/Matrix.Arithmetic.cs

@ -543,7 +543,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentException">If <strong>result.Count != this.RowCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.ColumnCount != <paramref name="rightSide"/>.Count</strong>.</exception>
public virtual void Multiply(Vector<T> rightSide, Vector<T> result)
public void Multiply(Vector<T> rightSide, Vector<T> result)
{
if (ColumnCount != rightSide.Count)
{
@ -592,7 +592,7 @@ namespace MathNet.Numerics.LinearAlgebra
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentException">If <strong>result.Count != this.ColumnCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.RowCount != <paramref name="leftSide"/>.Count</strong>.</exception>
public virtual void LeftMultiply(Vector<T> leftSide, Vector<T> result)
public void LeftMultiply(Vector<T> leftSide, Vector<T> result)
{
if (RowCount != leftSide.Count)
{

210
src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs

@ -321,6 +321,36 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Multiplies this matrix with a vector and places the results into the result vector.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoMultiply(Vector<float> rightSide, Vector<float> result)
{
var d = Math.Min(ColumnCount, RowCount);
if (d < RowCount)
{
result.ClearSubVector(ColumnCount, RowCount - ColumnCount);
}
if (d == ColumnCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<float>;
var denseResult = result.Storage as DenseVectorStorage<float>;
if (denseOther != null && denseResult != null)
{
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i]*rightSide.At(i));
}
}
/// <summary>
/// Multiplies this matrix with another matrix and places the results into the result matrix.
/// </summary>
@ -366,131 +396,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
base.DoMultiply(other, result);
}
/// <summary>
/// Multiplies this matrix with a vector and places the results into the result matrix.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If <paramref name="rightSide"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If <paramref name="result"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <strong>result.Count != this.RowCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.ColumnCount != <paramref name="rightSide"/>.Count</strong>.</exception>
public override void Multiply(Vector<float> rightSide, Vector<float> result)
{
if (rightSide == null)
{
throw new ArgumentNullException("rightSide");
}
if (ColumnCount != rightSide.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, rightSide, "rightSide");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (RowCount != result.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
if (ReferenceEquals(rightSide, result))
{
var tmp = result.CreateVector(result.Count);
Multiply(rightSide, tmp);
tmp.CopyTo(result);
}
else
{
// Clear the result vector
result.Clear();
// Multiply the elements in the vector with the corresponding diagonal element in this.
for (var r = 0; r < _data.Length; r++)
{
result[r] = _data[r] * rightSide[r];
}
}
}
/// <summary>
/// Left multiply a matrix with a vector ( = vector * matrix ) and place the result in the result vector.
/// </summary>
/// <param name="leftSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
/// <exception cref="ArgumentNullException">If <paramref name="leftSide"/> is <see langword="null" />.</exception>
/// <exception cref="ArgumentNullException">If the result matrix is <see langword="null" />.</exception>
/// <exception cref="ArgumentException">If <strong>result.Count != this.ColumnCount</strong>.</exception>
/// <exception cref="ArgumentException">If <strong>this.RowCount != <paramref name="leftSide"/>.Count</strong>.</exception>
public override void LeftMultiply(Vector<float> leftSide, Vector<float> result)
{
if (leftSide == null)
{
throw new ArgumentNullException("leftSide");
}
if (RowCount != leftSide.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, leftSide, "leftSide");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (ColumnCount != result.Count)
{
throw DimensionsDontMatch<ArgumentException>(this, result, "result");
}
if (ReferenceEquals(leftSide, result))
{
var tmp = result.CreateVector(result.Count);
LeftMultiply(leftSide, tmp);
tmp.CopyTo(result);
}
else
{
// Clear the result vector
result.Clear();
// Multiply the elements in the vector with the corresponding diagonal element in this.
for (var r = 0; r < _data.Length; r++)
{
result[r] = _data[r] * leftSide[r];
}
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>
/// <returns>The determinant of this matrix.</returns>
public override float Determinant()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
return _data.Aggregate(1.0f, (current, t) => current * t);
}
/// <summary>
/// Returns the elements of the diagonal in a <see cref="DenseVector"/>.
/// </summary>
/// <returns>The elements of the diagonal.</returns>
/// <remarks>For non-square matrices, the method returns Min(Rows, Columns) elements where
/// i == j (i is the row index, and j is the column index).</remarks>
public override Vector<float> Diagonal()
{
return new DenseVector(_data).Clone();
}
/// <summary>
/// Multiplies this matrix with transpose of another matrix and places the results into the result matrix.
/// </summary>
@ -580,6 +485,61 @@ namespace MathNet.Numerics.LinearAlgebra.Single
base.DoTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with a vector and places the results into the result vector.
/// </summary>
/// <param name="rightSide">The vector to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoTransposeThisAndMultiply(Vector<float> rightSide, Vector<float> result)
{
var d = Math.Min(ColumnCount, RowCount);
if (d < ColumnCount)
{
result.ClearSubVector(RowCount, ColumnCount - RowCount);
}
if (d == RowCount)
{
var denseOther = rightSide.Storage as DenseVectorStorage<float>;
var denseResult = result.Storage as DenseVectorStorage<float>;
if (denseOther != null && denseResult != null)
{
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(_data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i]*rightSide.At(i));
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>
/// <returns>The determinant of this matrix.</returns>
public override float Determinant()
{
if (RowCount != ColumnCount)
{
throw new ArgumentException(Resources.ArgumentMatrixSquare);
}
return _data.Aggregate(1.0f, (current, t) => current * t);
}
/// <summary>
/// Returns the elements of the diagonal in a <see cref="DenseVector"/>.
/// </summary>
/// <returns>The elements of the diagonal.</returns>
/// <remarks>For non-square matrices, the method returns Min(Rows, Columns) elements where
/// i == j (i is the row index, and j is the column index).</remarks>
public override Vector<float> Diagonal()
{
return new DenseVector(_data).Clone();
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>

Loading…
Cancel
Save