Browse Source

LA: concrete conjugate and negate

optimization-3
Christoph Ruegg 13 years ago
parent
commit
10f8e22bd3
  1. 50
      src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs
  2. 48
      src/Numerics/LinearAlgebra/Complex/DenseVector.cs
  3. 170
      src/Numerics/LinearAlgebra/Complex/DiagonalMatrix.cs
  4. 20
      src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs
  5. 84
      src/Numerics/LinearAlgebra/Complex/SparseVector.cs
  6. 50
      src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs
  7. 48
      src/Numerics/LinearAlgebra/Complex32/DenseVector.cs
  8. 170
      src/Numerics/LinearAlgebra/Complex32/DiagonalMatrix.cs
  9. 20
      src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs
  10. 84
      src/Numerics/LinearAlgebra/Complex32/SparseVector.cs
  11. 34
      src/Numerics/LinearAlgebra/Double/DenseMatrix.cs
  12. 14
      src/Numerics/LinearAlgebra/Double/DenseVector.cs
  13. 20
      src/Numerics/LinearAlgebra/Double/DiagonalMatrix.cs
  14. 2
      src/Numerics/LinearAlgebra/Double/Matrix.cs
  15. 20
      src/Numerics/LinearAlgebra/Double/SparseMatrix.cs
  16. 39
      src/Numerics/LinearAlgebra/Double/SparseVector.cs
  17. 34
      src/Numerics/LinearAlgebra/Single/DenseMatrix.cs
  18. 14
      src/Numerics/LinearAlgebra/Single/DenseVector.cs
  19. 20
      src/Numerics/LinearAlgebra/Single/DiagonalMatrix.cs
  20. 2
      src/Numerics/LinearAlgebra/Single/Matrix.cs
  21. 20
      src/Numerics/LinearAlgebra/Single/SparseMatrix.cs
  22. 37
      src/Numerics/LinearAlgebra/Single/SparseVector.cs
  23. 7
      src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs
  24. 31
      src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs
  25. 31
      src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs
  26. 18
      src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs
  27. 18
      src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs

50
src/Numerics/LinearAlgebra/Complex/DenseMatrix.cs

@ -464,6 +464,38 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return ret;
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
{
Control.LinearAlgebraProvider.ScaleArray(-1, _values, denseResult._values);
return;
}
base.DoNegate(result);
}
/// <summary>
/// Complex conjugates each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
{
Control.LinearAlgebraProvider.ConjugateArray(_values, denseResult._values);
return;
}
base.DoConjugate(result);
}
/// <summary>
/// Add a scalar to each element of the matrix and stores the result in the result vector.
/// </summary>
@ -943,24 +975,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
base.DoConjugateTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result)
{
var denseResult = result as DenseMatrix;
if (denseResult == null)
{
base.DoNegate(result);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-1, _values, denseResult._values);
}
}
/// <summary>
/// Divides each element of the matrix by a scalar and places results into the result matrix.
/// </summary>

48
src/Numerics/LinearAlgebra/Complex/DenseVector.cs

@ -357,11 +357,26 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (denseResult == null)
{
base.DoNegate(result);
return;
}
else
Control.LinearAlgebraProvider.ScaleArray(-Complex.One, _values, denseResult.Values);
}
/// <summary>
/// Conjugates vector and save result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex> result)
{
var resultDense = result as DenseVector;
if (resultDense == null)
{
Control.LinearAlgebraProvider.ScaleArray(-Complex.One, _values, denseResult.Values);
base.DoConjugate(result);
return;
}
Control.LinearAlgebraProvider.ConjugateArray(_values, resultDense._values);
}
/// <summary>
@ -376,11 +391,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (denseResult == null)
{
base.DoMultiply(scalar, result);
return;
}
else
{
Control.LinearAlgebraProvider.ScaleArray(scalar, _values, denseResult.Values);
}
Control.LinearAlgebraProvider.ScaleArray(scalar, _values, denseResult.Values);
}
/// <summary>
@ -842,27 +856,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
#endregion
/// <summary>
/// Conjugates vector and save result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex> result)
{
var resultDense = result as DenseVector;
if (resultDense == null)
{
base.DoConjugate(result);
return;
}
CommonParallel.For(0, _length, 4096, (a, b) =>
{
for (int i = a; i < b; i++)
{
resultDense._values[i] = _values[i].Conjugate();
}
});
}
}
}

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

@ -194,6 +194,46 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
i => new Complex(distribution.Sample(), distribution.Sample())));
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
{
Control.LinearAlgebraProvider.ScaleArray(-1, _data, diagResult._data);
return;
}
result.Clear();
for (var i = 0; i < _data.Length; i++)
{
result.At(i, i, -_data[i]);
}
}
/// <summary>
/// Complex conjugates each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
{
Control.LinearAlgebraProvider.ConjugateArray(_data, diagResult._data);
return;
}
result.Clear();
for (var i = 0; i < _data.Length; i++)
{
result.At(i, i, _data[i].Conjugate());
}
}
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>
@ -442,6 +482,52 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
base.DoTransposeAndMultiply(other, result);
}
/// <summary>
/// Multiplies this matrix with the conjugate transpose of another matrix and places the results into the result matrix.
/// </summary>
/// <param name="other">The matrix to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{
var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length];
Array.Copy(_data, thisDataCopy, (diagonalResult._data.Length > _data.Length) ? _data.Length : diagonalResult._data.Length);
Array.Copy(diagonalOther._data, otherDataCopy, (diagonalResult._data.Length > diagonalOther._data.Length) ? diagonalOther._data.Length : diagonalResult._data.Length);
// TODO: merge/MulByConj
Control.LinearAlgebraProvider.ConjugateArray(otherDataCopy, otherDataCopy);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(thisDataCopy, otherDataCopy, diagonalResult._data);
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null)
{
var dense = denseOther.Data;
var diagonal = _data;
var d = Math.Min(denseOther.ColumnCount, RowCount);
if (d < RowCount)
{
result.ClearSubMatrix(denseOther.ColumnCount, RowCount - denseOther.ColumnCount, 0, denseOther.RowCount);
}
int index = 0;
for (int j = 0; j < d; j++)
{
for (int i = 0; i < denseOther.RowCount; i++)
{
result.At(j, i, dense[index].Conjugate()*diagonal[j]);
index++;
}
}
return;
}
base.DoConjugateTransposeAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with another matrix and places the results into the result matrix.
/// </summary>
@ -487,6 +573,58 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
base.DoTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with another matrix and places the results into the result matrix.
/// </summary>
/// <param name="other">The matrix to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex> other, Matrix<Complex> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{
var thisDataCopy = new Complex[diagonalResult._data.Length];
var otherDataCopy = new Complex[diagonalResult._data.Length];
Array.Copy(_data, thisDataCopy, (diagonalResult._data.Length > _data.Length) ? _data.Length : diagonalResult._data.Length);
Array.Copy(diagonalOther._data, otherDataCopy, (diagonalResult._data.Length > diagonalOther._data.Length) ? diagonalOther._data.Length : diagonalResult._data.Length);
// TODO: merge/MulByConj
Control.LinearAlgebraProvider.ConjugateArray(thisDataCopy, thisDataCopy);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(thisDataCopy, otherDataCopy, diagonalResult._data);
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex>;
if (denseOther != null)
{
var dense = denseOther.Data;
var conjugateDiagonal = new Complex[_data.Length];
for (int i = 0; i < _data.Length; i++)
{
conjugateDiagonal[i] = _data[i].Conjugate();
}
var d = Math.Min(denseOther.RowCount, ColumnCount);
if (d < ColumnCount)
{
result.ClearSubMatrix(denseOther.RowCount, ColumnCount - denseOther.RowCount, 0, denseOther.ColumnCount);
}
int index = 0;
for (int i = 0; i < denseOther.ColumnCount; i++)
{
for (int j = 0; j < d; j++)
{
result.At(j, i, dense[index]*conjugateDiagonal[j]);
index++;
}
index += (denseOther.RowCount - d);
}
return;
}
base.DoConjugateTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with a vector and places the results into the result vector.
/// </summary>
@ -517,6 +655,38 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Multiplies the conjugate 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 DoConjugateTransposeThisAndMultiply(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)
{
// TODO: merge/MulByConj
Control.LinearAlgebraProvider.ConjugateArray(_data, denseResult.Data);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(denseResult.Data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i].Conjugate()*rightSide.At(i));
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>

20
src/Numerics/LinearAlgebra/Complex/SparseMatrix.cs

@ -638,6 +638,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result)
{
CopyTo(result);
DoMultiply(-1, result);
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>
@ -1037,16 +1047,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex> result)
{
CopyTo(result);
DoMultiply(-1, result);
}
/// <summary>
/// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix.
/// </summary>

84
src/Numerics/LinearAlgebra/Complex/SparseVector.cs

@ -133,44 +133,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
return new SparseVector(SparseVectorStorage<Complex>.OfInit(length, init));
}
/// <summary>
/// Conjugates vector and save result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex> result)
{
if (ReferenceEquals(this, result))
{
var tmp = Build.SameAs(this);
DoConjugate(tmp);
tmp.CopyTo(result);
}
var targetSparse = result as SparseVector;
if (targetSparse == null)
{
base.DoConjugate(result);
return;
}
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
targetSparse._storage.Values = new Complex[_storage.ValueCount];
targetSparse._storage.Indices = new int[_storage.ValueCount];
targetSparse._storage.ValueCount = _storage.ValueCount;
if (_storage.ValueCount != 0)
{
CommonParallel.For(0, _storage.ValueCount, (a, b) =>
{
for (int i = a; i < b; i++)
{
targetSparse._storage.Values[i] = _storage.Values[i].Conjugate();
}
});
Buffer.BlockCopy(_storage.Indices, 0, targetSparse._storage.Indices, 0, _storage.ValueCount*Constants.SizeOfInt);
}
}
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// Warning, the new 'sparse vector' with a non-zero scalar added to it will be a 100% filled
@ -196,7 +158,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
if (ReferenceEquals(this, result))
{
//populate a new vector with the scalar
//populate a new vector with the scalar
var vnonZeroValues = new Complex[Count];
var vnonZeroIndices = new int[Count];
for (int index = 0; index < Count; index++)
@ -213,7 +175,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
vnonZeroValues[indices[j]] = values[j] + scalar;
}
//assign this vectors arrary to the new arrays.
//assign this vectors arrary to the new arrays.
_storage.Values = vnonZeroValues;
_storage.Indices = vnonZeroIndices;
_storage.ValueCount = Count;
@ -427,19 +389,47 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
{
result.At(_storage.Indices[index], -_storage.Values[index]);
}
return;
}
else
if (!ReferenceEquals(this, result))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount*Constants.SizeOfInt);
sparseResult._storage.Values = new Complex[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ScaleArray(-Complex.One, sparseResult._storage.Values, sparseResult._storage.Values);
}
/// <summary>
/// Conjugates vector and save result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex> result)
{
var sparseResult = result as SparseVector;
if (sparseResult != null)
{
if (!ReferenceEquals(this, result))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount*Constants.SizeOfInt);
sparseResult._storage.Values = new Complex[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ScaleArray(-Complex.One, sparseResult._storage.Values, sparseResult._storage.Values);
Control.LinearAlgebraProvider.ConjugateArray(sparseResult._storage.Values, sparseResult._storage.Values);
return;
}
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], _storage.Values[index].Conjugate());
}
}
@ -547,7 +537,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="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>
@ -669,7 +659,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>
/// <returns>The index of absolute minimum element.</returns>
/// <returns>The index of absolute minimum element.</returns>
public override int AbsoluteMinimumIndex()
{
if (_storage.ValueCount == 0)
@ -810,8 +800,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex
/// <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>
/// <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 Matrix<Complex> OuterProduct(SparseVector u, SparseVector v)
{
if (u == null)

50
src/Numerics/LinearAlgebra/Complex32/DenseMatrix.cs

@ -423,6 +423,38 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, _rowCount, _columnCount, _values);
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
{
Control.LinearAlgebraProvider.ScaleArray(-1, _values, denseResult._values);
return;
}
base.DoNegate(result);
}
/// <summary>
/// Complex conjugates each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex32> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
{
Control.LinearAlgebraProvider.ConjugateArray(_values, denseResult._values);
return;
}
base.DoConjugate(result);
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>
@ -940,24 +972,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
base.DoConjugateTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result)
{
var denseResult = result as DenseMatrix;
if (denseResult == null)
{
base.DoNegate(result);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-1, _values, denseResult._values);
}
}
/// <summary>
/// Divides each element of the matrix by a scalar and places results into the result matrix.
/// </summary>

48
src/Numerics/LinearAlgebra/Complex32/DenseVector.cs

@ -352,11 +352,26 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (denseResult == null)
{
base.DoNegate(result);
return;
}
else
Control.LinearAlgebraProvider.ScaleArray(-Complex32.One, _values, denseResult.Values);
}
/// <summary>
/// Conjugates vector and save result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex32> result)
{
var resultDense = result as DenseVector;
if (resultDense == null)
{
Control.LinearAlgebraProvider.ScaleArray(-Complex32.One, _values, denseResult.Values);
base.DoConjugate(result);
return;
}
Control.LinearAlgebraProvider.ConjugateArray(_values, resultDense._values);
}
/// <summary>
@ -371,11 +386,10 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (denseResult == null)
{
base.DoMultiply(scalar, result);
return;
}
else
{
Control.LinearAlgebraProvider.ScaleArray(scalar, _values, denseResult.Values);
}
Control.LinearAlgebraProvider.ScaleArray(scalar, _values, denseResult.Values);
}
/// <summary>
@ -837,27 +851,5 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
#endregion
/// <summary>
/// Conjugates vector and save result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex32> result)
{
var resultDense = result as DenseVector;
if (resultDense == null)
{
base.DoConjugate(result);
return;
}
CommonParallel.For(0, _length, 4096, (a, b) =>
{
for (int i = a; i < b; i++)
{
resultDense._values[i] = _values[i].Conjugate();
}
});
}
}
}

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

@ -189,6 +189,46 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
i => new Complex32((float) distribution.Sample(), (float) distribution.Sample())));
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
{
Control.LinearAlgebraProvider.ScaleArray(-1, _data, diagResult._data);
return;
}
result.Clear();
for (var i = 0; i < _data.Length; i++)
{
result.At(i, i, -_data[i]);
}
}
/// <summary>
/// Complex conjugates each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<Complex32> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
{
Control.LinearAlgebraProvider.ConjugateArray(_data, diagResult._data);
return;
}
result.Clear();
for (var i = 0; i < _data.Length; i++)
{
result.At(i, i, _data[i].Conjugate());
}
}
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>
@ -436,6 +476,52 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
base.DoTransposeAndMultiply(other, result);
}
/// <summary>
/// Multiplies this matrix with the conjugate transpose of another matrix and places the results into the result matrix.
/// </summary>
/// <param name="other">The matrix to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{
var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length];
Array.Copy(_data, thisDataCopy, (diagonalResult._data.Length > _data.Length) ? _data.Length : diagonalResult._data.Length);
Array.Copy(diagonalOther._data, otherDataCopy, (diagonalResult._data.Length > diagonalOther._data.Length) ? diagonalOther._data.Length : diagonalResult._data.Length);
// TODO: merge/MulByConj
Control.LinearAlgebraProvider.ConjugateArray(otherDataCopy, otherDataCopy);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(thisDataCopy, otherDataCopy, diagonalResult._data);
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null)
{
var dense = denseOther.Data;
var diagonal = _data;
var d = Math.Min(denseOther.ColumnCount, RowCount);
if (d < RowCount)
{
result.ClearSubMatrix(denseOther.ColumnCount, RowCount - denseOther.ColumnCount, 0, denseOther.RowCount);
}
int index = 0;
for (int j = 0; j < d; j++)
{
for (int i = 0; i < denseOther.RowCount; i++)
{
result.At(j, i, dense[index].Conjugate()*diagonal[j]);
index++;
}
}
return;
}
base.DoConjugateTransposeAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with another matrix and places the results into the result matrix.
/// </summary>
@ -481,6 +567,58 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
base.DoTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with another matrix and places the results into the result matrix.
/// </summary>
/// <param name="other">The matrix to multiply with.</param>
/// <param name="result">The result of the multiplication.</param>
protected override void DoConjugateTransposeThisAndMultiply(Matrix<Complex32> other, Matrix<Complex32> result)
{
var diagonalOther = other as DiagonalMatrix;
var diagonalResult = result as DiagonalMatrix;
if (diagonalOther != null && diagonalResult != null)
{
var thisDataCopy = new Complex32[diagonalResult._data.Length];
var otherDataCopy = new Complex32[diagonalResult._data.Length];
Array.Copy(_data, thisDataCopy, (diagonalResult._data.Length > _data.Length) ? _data.Length : diagonalResult._data.Length);
Array.Copy(diagonalOther._data, otherDataCopy, (diagonalResult._data.Length > diagonalOther._data.Length) ? diagonalOther._data.Length : diagonalResult._data.Length);
// TODO: merge/MulByConj
Control.LinearAlgebraProvider.ConjugateArray(thisDataCopy, thisDataCopy);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(thisDataCopy, otherDataCopy, diagonalResult._data);
return;
}
var denseOther = other.Storage as DenseColumnMajorMatrixStorage<Complex32>;
if (denseOther != null)
{
var dense = denseOther.Data;
var conjugateDiagonal = new Complex32[_data.Length];
for (int i = 0; i < _data.Length; i++)
{
conjugateDiagonal[i] = _data[i].Conjugate();
}
var d = Math.Min(denseOther.RowCount, ColumnCount);
if (d < ColumnCount)
{
result.ClearSubMatrix(denseOther.RowCount, ColumnCount - denseOther.RowCount, 0, denseOther.ColumnCount);
}
int index = 0;
for (int i = 0; i < denseOther.ColumnCount; i++)
{
for (int j = 0; j < d; j++)
{
result.At(j, i, dense[index]*conjugateDiagonal[j]);
index++;
}
index += (denseOther.RowCount - d);
}
return;
}
base.DoConjugateTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Multiplies the transpose of this matrix with a vector and places the results into the result vector.
/// </summary>
@ -511,6 +649,38 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Multiplies the conjugate 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 DoConjugateTransposeThisAndMultiply(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)
{
// TODO: merge/MulByConj
Control.LinearAlgebraProvider.ConjugateArray(_data, denseResult.Data);
Control.LinearAlgebraProvider.PointWiseMultiplyArrays(denseResult.Data, denseOther.Data, denseResult.Data);
return;
}
}
for (var i = 0; i < d; i++)
{
result.At(i, _data[i].Conjugate()*rightSide.At(i));
}
}
/// <summary>
/// Computes the determinant of this matrix.
/// </summary>

20
src/Numerics/LinearAlgebra/Complex32/SparseMatrix.cs

@ -633,6 +633,16 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result)
{
CopyTo(result);
DoMultiply(-1, result);
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>
@ -1031,16 +1041,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<Complex32> result)
{
CopyTo(result);
DoMultiply(-1, result);
}
/// <summary>
/// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix.
/// </summary>

84
src/Numerics/LinearAlgebra/Complex32/SparseVector.cs

@ -128,44 +128,6 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
return new SparseVector(SparseVectorStorage<Complex32>.OfInit(length, init));
}
/// <summary>
/// Conjugates vector and save result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex32> result)
{
if (ReferenceEquals(this, result))
{
var tmp = Build.SameAs(this);
DoConjugate(tmp);
tmp.CopyTo(result);
}
var targetSparse = result as SparseVector;
if (targetSparse == null)
{
base.DoConjugate(result);
return;
}
// Lets copy only needed data. Portion of needed data is determined by NonZerosCount value
targetSparse._storage.Values = new Complex32[_storage.ValueCount];
targetSparse._storage.Indices = new int[_storage.ValueCount];
targetSparse._storage.ValueCount = _storage.ValueCount;
if (_storage.ValueCount != 0)
{
CommonParallel.For(0, _storage.ValueCount, (a, b) =>
{
for (int i = a; i < b; i++)
{
targetSparse._storage.Values[i] = _storage.Values[i].Conjugate();
}
});
Buffer.BlockCopy(_storage.Indices, 0, targetSparse._storage.Indices, 0, _storage.ValueCount*Constants.SizeOfInt);
}
}
/// <summary>
/// Adds a scalar to each element of the vector and stores the result in the result vector.
/// Warning, the new 'sparse vector' with a non-zero scalar added to it will be a 100% filled
@ -191,7 +153,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
if (ReferenceEquals(this, result))
{
//populate a new vector with the scalar
//populate a new vector with the scalar
var vnonZeroValues = new Complex32[Count];
var vnonZeroIndices = new int[Count];
for (int index = 0; index < Count; index++)
@ -208,7 +170,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
vnonZeroValues[indices[j]] = values[j] + scalar;
}
//assign this vectors arrary to the new arrays.
//assign this vectors arrary to the new arrays.
_storage.Values = vnonZeroValues;
_storage.Indices = vnonZeroIndices;
_storage.ValueCount = Count;
@ -422,19 +384,47 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
{
result.At(_storage.Indices[index], -_storage.Values[index]);
}
return;
}
else
if (!ReferenceEquals(this, result))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount*Constants.SizeOfInt);
sparseResult._storage.Values = new Complex32[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ScaleArray(-Complex32.One, sparseResult._storage.Values, sparseResult._storage.Values);
}
/// <summary>
/// Conjugates vector and save result to <paramref name="result"/>
/// </summary>
/// <param name="result">Target vector</param>
protected override void DoConjugate(Vector<Complex32> result)
{
var sparseResult = result as SparseVector;
if (sparseResult != null)
{
if (!ReferenceEquals(this, result))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount*Constants.SizeOfInt);
sparseResult._storage.Values = new Complex32[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ScaleArray(-Complex32.One, sparseResult._storage.Values, sparseResult._storage.Values);
Control.LinearAlgebraProvider.ConjugateArray(sparseResult._storage.Values, sparseResult._storage.Values);
return;
}
result.Clear();
for (var index = 0; index < _storage.ValueCount; index++)
{
result.At(_storage.Indices[index], _storage.Values[index].Conjugate());
}
}
@ -542,7 +532,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="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>
@ -664,7 +654,7 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>
/// <returns>The index of absolute minimum element.</returns>
/// <returns>The index of absolute minimum element.</returns>
public override int AbsoluteMinimumIndex()
{
if (_storage.ValueCount == 0)
@ -805,8 +795,8 @@ namespace MathNet.Numerics.LinearAlgebra.Complex32
/// <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>
/// <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 Matrix<Complex32> /*SparseMatrix*/ OuterProduct(SparseVector u, SparseVector v)
{
if (u == null)

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

@ -420,6 +420,22 @@ namespace MathNet.Numerics.LinearAlgebra.Double
return Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, _rowCount, _columnCount, _values);
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
{
Control.LinearAlgebraProvider.ScaleArray(-1, _values, denseResult._values);
return;
}
base.DoNegate(result);
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>
@ -784,24 +800,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
base.DoTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result)
{
var denseResult = result as DenseMatrix;
if (denseResult == null)
{
base.DoNegate(result);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-1, _values, denseResult._values);
}
}
/// <summary>
/// Divides each element of the matrix by a scalar and places results into the result matrix.
/// </summary>

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

@ -362,11 +362,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseResult == null)
{
base.DoNegate(result);
return;
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-1.0d, _values, denseResult.Values);
}
Control.LinearAlgebraProvider.ScaleArray(-1.0d, _values, denseResult.Values);
}
/// <summary>
@ -381,11 +380,10 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (denseResult == null)
{
base.DoMultiply(scalar, result);
return;
}
else
{
Control.LinearAlgebraProvider.ScaleArray(scalar, _values, denseResult.Values);
}
Control.LinearAlgebraProvider.ScaleArray(scalar, _values, denseResult.Values);
}
/// <summary>

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

@ -188,6 +188,26 @@ namespace MathNet.Numerics.LinearAlgebra.Double
i => distribution.Sample()));
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
{
Control.LinearAlgebraProvider.ScaleArray(-1, _data, diagResult._data);
return;
}
result.Clear();
for (var i = 0; i < _data.Length; i++)
{
result.At(i, i, -_data[i]);
}
}
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>

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

@ -365,7 +365,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// Complex conjugates each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<double> result)
protected override sealed void DoConjugate(Matrix<double> result)
{
if (ReferenceEquals(this, result))
{

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

@ -631,6 +631,16 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result)
{
CopyTo(result);
DoMultiply(-1, result);
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>
@ -1031,16 +1041,6 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<double> result)
{
CopyTo(result);
DoMultiply(-1, result);
}
/// <summary>
/// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix.
/// </summary>

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

@ -145,7 +145,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
if (!ReferenceEquals(this, result))
{
CopyTo(result);
CopyTo(result);
}
return;
@ -153,7 +153,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
if (ReferenceEquals(this, result))
{
//populate a new vector with the scalar
//populate a new vector with the scalar
var vnonZeroValues = new double[Count];
var vnonZeroIndices = new int[Count];
for (int index = 0; index < Count; index++)
@ -384,20 +384,19 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
result.At(_storage.Indices[index], -_storage.Values[index]);
}
return;
}
else
{
if (!ReferenceEquals(this, result))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
sparseResult._storage.Values = new double[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ScaleArray(-1.0d, sparseResult._storage.Values, sparseResult._storage.Values);
if (!ReferenceEquals(this, result))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
sparseResult._storage.Values = new double[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ScaleArray(-1.0d, sparseResult._storage.Values, sparseResult._storage.Values);
}
/// <summary>
@ -503,7 +502,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="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>
@ -625,7 +624,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>
/// <returns>The index of absolute minimum element.</returns>
/// <returns>The index of absolute minimum element.</returns>
public override int AbsoluteMinimumIndex()
{
if (_storage.ValueCount == 0)
@ -652,7 +651,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Returns the index of the absolute maximum element.
/// </summary>
/// <returns>The index of absolute maximum element.</returns>
/// <returns>The index of absolute maximum element.</returns>
public override int MaximumIndex()
{
if (_storage.ValueCount == 0)
@ -677,7 +676,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <summary>
/// Returns the index of the minimum element.
/// </summary>
/// <returns>The index of minimum element.</returns>
/// <returns>The index of minimum element.</returns>
public override int MinimumIndex()
{
if (_storage.ValueCount == 0)
@ -773,7 +772,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
{
for (var i = 0; i < _storage.ValueCount; i++)
{
_storage.Values[i] *= _storage.Values[i];
_storage.Values[i] *= _storage.Values[i];
}
}
else
@ -816,8 +815,8 @@ namespace MathNet.Numerics.LinearAlgebra.Double
/// <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>
/// <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 Matrix<double> OuterProduct(SparseVector u, SparseVector v)
{
if (u == null)

34
src/Numerics/LinearAlgebra/Single/DenseMatrix.cs

@ -420,6 +420,22 @@ namespace MathNet.Numerics.LinearAlgebra.Single
return Control.LinearAlgebraProvider.MatrixNorm(Norm.FrobeniusNorm, _rowCount, _columnCount, _values);
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result)
{
var denseResult = result as DenseMatrix;
if (denseResult != null)
{
Control.LinearAlgebraProvider.ScaleArray(-1, _values, denseResult._values);
return;
}
base.DoNegate(result);
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>
@ -784,24 +800,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
base.DoTransposeThisAndMultiply(other, result);
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result)
{
var denseResult = result as DenseMatrix;
if (denseResult == null)
{
base.DoNegate(result);
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-1, _values, denseResult._values);
}
}
/// <summary>
/// Divides each element of the matrix by a scalar and places results into the result matrix.
/// </summary>

14
src/Numerics/LinearAlgebra/Single/DenseVector.cs

@ -351,11 +351,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (denseResult == null)
{
base.DoNegate(result);
return;
}
else
{
Control.LinearAlgebraProvider.ScaleArray(-1.0f, _values, denseResult.Values);
}
Control.LinearAlgebraProvider.ScaleArray(-1.0f, _values, denseResult.Values);
}
/// <summary>
@ -370,11 +369,10 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (denseResult == null)
{
base.DoMultiply(scalar, result);
return;
}
else
{
Control.LinearAlgebraProvider.ScaleArray(scalar, _values, denseResult.Values);
}
Control.LinearAlgebraProvider.ScaleArray(scalar, _values, denseResult.Values);
}
/// <summary>

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

@ -188,6 +188,26 @@ namespace MathNet.Numerics.LinearAlgebra.Single
i => (float) distribution.Sample()));
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result)
{
var diagResult = result as DiagonalMatrix;
if (diagResult != null)
{
Control.LinearAlgebraProvider.ScaleArray(-1, _data, diagResult._data);
return;
}
result.Clear();
for (var i = 0; i < _data.Length; i++)
{
result.At(i, i, -_data[i]);
}
}
/// <summary>
/// Adds another matrix to this matrix.
/// </summary>

2
src/Numerics/LinearAlgebra/Single/Matrix.cs

@ -397,7 +397,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// Complex conjugates each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the conjugation.</param>
protected override void DoConjugate(Matrix<float> result)
protected override sealed void DoConjugate(Matrix<float> result)
{
if (ReferenceEquals(this, result))
{

20
src/Numerics/LinearAlgebra/Single/SparseMatrix.cs

@ -631,6 +631,16 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result)
{
CopyTo(result);
DoMultiply(-1, result);
}
/// <summary>
/// Returns the transpose of this matrix.
/// </summary>
@ -1038,16 +1048,6 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
}
/// <summary>
/// Negate each element of this matrix and place the results into the result matrix.
/// </summary>
/// <param name="result">The result of the negation.</param>
protected override void DoNegate(Matrix<float> result)
{
CopyTo(result);
DoMultiply(-1, result);
}
/// <summary>
/// Pointwise multiplies this matrix with another matrix and stores the result into the result matrix.
/// </summary>

37
src/Numerics/LinearAlgebra/Single/SparseVector.cs

@ -153,7 +153,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
if (ReferenceEquals(this, result))
{
//populate a new vector with the scalar
//populate a new vector with the scalar
var vnonZeroValues = new float[Count];
var vnonZeroIndices = new int[Count];
for (int index = 0; index < Count; index++)
@ -170,7 +170,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
vnonZeroValues[indices[j]] = values[j] + scalar;
}
//assign this vectors arrary to the new arrays.
//assign this vectors arrary to the new arrays.
_storage.Values = vnonZeroValues;
_storage.Indices = vnonZeroIndices;
_storage.ValueCount = Count;
@ -385,20 +385,19 @@ namespace MathNet.Numerics.LinearAlgebra.Single
{
result.At(_storage.Indices[index], -_storage.Values[index]);
}
return;
}
else
{
if (!ReferenceEquals(this, result))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
sparseResult._storage.Values = new float[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ScaleArray(-1.0f, sparseResult._storage.Values, sparseResult._storage.Values);
if (!ReferenceEquals(this, result))
{
sparseResult._storage.ValueCount = _storage.ValueCount;
sparseResult._storage.Indices = new int[_storage.ValueCount];
Buffer.BlockCopy(_storage.Indices, 0, sparseResult._storage.Indices, 0, _storage.ValueCount * Constants.SizeOfInt);
sparseResult._storage.Values = new float[_storage.ValueCount];
Array.Copy(_storage.Values, sparseResult._storage.Values, _storage.ValueCount);
}
Control.LinearAlgebraProvider.ScaleArray(-1.0f, sparseResult._storage.Values, sparseResult._storage.Values);
}
/// <summary>
@ -504,7 +503,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
}
/// <summary>
/// Returns a <strong>Vector</strong> containing the negated values of <paramref name="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>
@ -626,7 +625,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <summary>
/// Returns the index of the absolute minimum element.
/// </summary>
/// <returns>The index of absolute minimum element.</returns>
/// <returns>The index of absolute minimum element.</returns>
public override int AbsoluteMinimumIndex()
{
if (_storage.ValueCount == 0)
@ -653,7 +652,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <summary>
/// Returns the index of the absolute maximum element.
/// </summary>
/// <returns>The index of absolute maximum element.</returns>
/// <returns>The index of absolute maximum element.</returns>
public override int MaximumIndex()
{
if (_storage.ValueCount == 0)
@ -678,7 +677,7 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <summary>
/// Returns the index of the minimum element.
/// </summary>
/// <returns>The index of minimum element.</returns>
/// <returns>The index of minimum element.</returns>
public override int MinimumIndex()
{
if (_storage.ValueCount == 0)
@ -817,8 +816,8 @@ namespace MathNet.Numerics.LinearAlgebra.Single
/// <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>
/// <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 Matrix<float> OuterProduct(SparseVector u, SparseVector v)
{
if (u == null)

7
src/Numerics/Providers/LinearAlgebra/ILinearAlgebraProvider.cs

@ -131,6 +131,13 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
/// <remarks>This is similar to the SCAL BLAS routine.</remarks>
void ScaleArray(T alpha, T[] x, T[] result);
/// <summary>
/// Conjugates an array. Can be used to conjugate a vector and a matrix.
/// </summary>
/// <param name="x">The values to conjugate.</param>
/// <param name="result">This result of the conjugation.</param>
void ConjugateArray(T[] x, T[] result);
/// <summary>
/// Computes the dot product of x and y.
/// </summary>

31
src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex.cs

@ -166,6 +166,37 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
}
}
/// <summary>
/// Conjugates an array. Can be used to conjugate a vector and a matrix.
/// </summary>
/// <param name="x">The values to conjugate.</param>
/// <param name="result">This result of the conjugation.</param>
public virtual void ConjugateArray(Complex[] x, Complex[] result)
{
if (x == null)
{
throw new ArgumentNullException("x");
}
if (Control.ParallelizeOperation(x.Length))
{
CommonParallel.For(0, x.Length, 4096, (a, b) =>
{
for (int i = a; i < b; i++)
{
result[i] = x[i].Conjugate();
}
});
}
else
{
for (var index = 0; index < x.Length; index++)
{
result[index] = x[index].Conjugate();
}
}
}
/// <summary>
/// Computes the dot product of x and y.
/// </summary>

31
src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Complex32.cs

@ -163,6 +163,37 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
}
}
/// <summary>
/// Conjugates an array. Can be used to conjugate a vector and a matrix.
/// </summary>
/// <param name="x">The values to conjugate.</param>
/// <param name="result">This result of the conjugation.</param>
public virtual void ConjugateArray(Complex32[] x, Complex32[] result)
{
if (x == null)
{
throw new ArgumentNullException("x");
}
if (Control.ParallelizeOperation(x.Length))
{
CommonParallel.For(0, x.Length, 4096, (a, b) =>
{
for (int i = a; i < b; i++)
{
result[i] = x[i].Conjugate();
}
});
}
else
{
for (var index = 0; index < x.Length; index++)
{
result[index] = x[index].Conjugate();
}
}
}
/// <summary>
/// Computes the dot product of x and y.
/// </summary>

18
src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Double.cs

@ -160,6 +160,24 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
}
}
/// <summary>
/// Conjugates an array. Can be used to conjugate a vector and a matrix.
/// </summary>
/// <param name="x">The values to conjugate.</param>
/// <param name="result">This result of the conjugation.</param>
public virtual void ConjugateArray(double[] x, double[] result)
{
if (x == null)
{
throw new ArgumentNullException("x");
}
if (!ReferenceEquals(x, result))
{
x.CopyTo(result, 0);
}
}
/// <summary>
/// Computes the dot product of x and y.
/// </summary>

18
src/Numerics/Providers/LinearAlgebra/ManagedLinearAlgebraProvider.Single.cs

@ -160,6 +160,24 @@ namespace MathNet.Numerics.Providers.LinearAlgebra
}
}
/// <summary>
/// Conjugates an array. Can be used to conjugate a vector and a matrix.
/// </summary>
/// <param name="x">The values to conjugate.</param>
/// <param name="result">This result of the conjugation.</param>
public virtual void ConjugateArray(float[] x, float[] result)
{
if (x == null)
{
throw new ArgumentNullException("x");
}
if (!ReferenceEquals(x, result))
{
x.CopyTo(result, 0);
}
}
/// <summary>
/// Computes the dot product of x and y.
/// </summary>

Loading…
Cancel
Save