|
|
|
@ -287,12 +287,83 @@ namespace MathNet.Numerics.LinearAlgebra.Storage |
|
|
|
MapInplace(x => x, Zeros.AllowSkip); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Indicates whether the current object is equal to another object of the same type.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="other">
|
|
|
|
/// An object to compare with this object.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
|
|
|
|
/// </returns>
|
|
|
|
public override bool Equals(MatrixStorage<T> other) |
|
|
|
{ |
|
|
|
// Reject equality when the argument is null or has a different shape.
|
|
|
|
if (other == null || ColumnCount != other.ColumnCount || RowCount != other.RowCount) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// Accept if the argument is the same object as this.
|
|
|
|
if (ReferenceEquals(this, other)) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
var sparse = other as SparseCompressedRowMatrixStorage<T>; |
|
|
|
if (sparse == null) |
|
|
|
{ |
|
|
|
return base.Equals(other); |
|
|
|
} |
|
|
|
|
|
|
|
if (ValueCount != sparse.ValueCount) |
|
|
|
{ |
|
|
|
// TODO: this is only correct if normalized
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// If all else fails, perform element wise comparison.
|
|
|
|
for (var index = 0; index < ValueCount; index++) |
|
|
|
{ |
|
|
|
// TODO: AlmostEquals
|
|
|
|
if (!Values[index].Equals(sparse.Values[index]) || ColumnIndices[index] != sparse.ColumnIndices[index]) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a hash code for this instance.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
|
|
|
|
/// </returns>
|
|
|
|
public override int GetHashCode() |
|
|
|
{ |
|
|
|
var values = Values; |
|
|
|
var hashNum = Math.Min(ValueCount, 25); |
|
|
|
int hash = 17; |
|
|
|
unchecked |
|
|
|
{ |
|
|
|
for (var i = 0; i < hashNum; i++) |
|
|
|
{ |
|
|
|
hash = hash*31 + values[i].GetHashCode(); |
|
|
|
} |
|
|
|
} |
|
|
|
return hash; |
|
|
|
} |
|
|
|
|
|
|
|
// CLEARING
|
|
|
|
|
|
|
|
public override void Clear() |
|
|
|
{ |
|
|
|
Array.Clear(RowPointers, 0, RowPointers.Length); |
|
|
|
} |
|
|
|
|
|
|
|
public override void Clear(int rowIndex, int rowCount, int columnIndex, int columnCount) |
|
|
|
internal override void ClearUnchecked(int rowIndex, int rowCount, int columnIndex, int columnCount) |
|
|
|
{ |
|
|
|
if (rowIndex == 0 && columnIndex == 0 && rowCount == RowCount && columnCount == ColumnCount) |
|
|
|
{ |
|
|
|
@ -346,7 +417,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public override void ClearRows(int[] rowIndices) |
|
|
|
internal override void ClearRowsUnchecked(int[] rowIndices) |
|
|
|
{ |
|
|
|
var rows = new bool[RowCount]; |
|
|
|
for (int i = 0; i < rowIndices.Length; i++) |
|
|
|
@ -356,7 +427,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage |
|
|
|
MapIndexedInplace((i, j, x) => rows[i] ? Zero : x, Zeros.AllowSkip); |
|
|
|
} |
|
|
|
|
|
|
|
public override void ClearColumns(int[] columnIndices) |
|
|
|
internal override void ClearColumnsUnchecked(int[] columnIndices) |
|
|
|
{ |
|
|
|
var columns = new bool[ColumnCount]; |
|
|
|
for (int i = 0; i < columnIndices.Length; i++) |
|
|
|
@ -366,75 +437,6 @@ namespace MathNet.Numerics.LinearAlgebra.Storage |
|
|
|
MapIndexedInplace((i, j, x) => columns[j] ? Zero : x, Zeros.AllowSkip); |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Indicates whether the current object is equal to another object of the same type.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="other">
|
|
|
|
/// An object to compare with this object.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// <c>true</c> if the current object is equal to the <paramref name="other"/> parameter; otherwise, <c>false</c>.
|
|
|
|
/// </returns>
|
|
|
|
public override bool Equals(MatrixStorage<T> other) |
|
|
|
{ |
|
|
|
// Reject equality when the argument is null or has a different shape.
|
|
|
|
if (other == null || ColumnCount != other.ColumnCount || RowCount != other.RowCount) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// Accept if the argument is the same object as this.
|
|
|
|
if (ReferenceEquals(this, other)) |
|
|
|
{ |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
var sparse = other as SparseCompressedRowMatrixStorage<T>; |
|
|
|
if (sparse == null) |
|
|
|
{ |
|
|
|
return base.Equals(other); |
|
|
|
} |
|
|
|
|
|
|
|
if (ValueCount != sparse.ValueCount) |
|
|
|
{ |
|
|
|
// TODO: this is only correct if normalized
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
// If all else fails, perform element wise comparison.
|
|
|
|
for (var index = 0; index < ValueCount; index++) |
|
|
|
{ |
|
|
|
// TODO: AlmostEquals
|
|
|
|
if (!Values[index].Equals(sparse.Values[index]) || ColumnIndices[index] != sparse.ColumnIndices[index]) |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Returns a hash code for this instance.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>
|
|
|
|
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
|
|
|
|
/// </returns>
|
|
|
|
public override int GetHashCode() |
|
|
|
{ |
|
|
|
var values = Values; |
|
|
|
var hashNum = Math.Min(ValueCount, 25); |
|
|
|
int hash = 17; |
|
|
|
unchecked |
|
|
|
{ |
|
|
|
for (var i = 0; i < hashNum; i++) |
|
|
|
{ |
|
|
|
hash = hash*31 + values[i].GetHashCode(); |
|
|
|
} |
|
|
|
} |
|
|
|
return hash; |
|
|
|
} |
|
|
|
|
|
|
|
// INITIALIZATION
|
|
|
|
|
|
|
|
public static SparseCompressedRowMatrixStorage<T> OfMatrix(MatrixStorage<T> matrix) |
|
|
|
@ -959,7 +961,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage |
|
|
|
|
|
|
|
if (existingData == ExistingData.Clear) |
|
|
|
{ |
|
|
|
target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); |
|
|
|
target.ClearUnchecked(targetRowIndex, rowCount, targetColumnIndex, columnCount); |
|
|
|
} |
|
|
|
|
|
|
|
for (int i = sourceRowIndex, row = 0; i < sourceRowIndex + rowCount; i++, row++) |
|
|
|
@ -1029,7 +1031,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage |
|
|
|
|
|
|
|
if (existingData == ExistingData.Clear) |
|
|
|
{ |
|
|
|
target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); |
|
|
|
target.ClearUnchecked(targetRowIndex, rowCount, targetColumnIndex, columnCount); |
|
|
|
} |
|
|
|
|
|
|
|
// NOTE: potential for more efficient implementation
|
|
|
|
@ -1642,7 +1644,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage |
|
|
|
var processZeros = zeros == Zeros.Include || !Zero.Equals(f(0, 1, Zero)); |
|
|
|
if (existingData == ExistingData.Clear && !processZeros) |
|
|
|
{ |
|
|
|
target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); |
|
|
|
target.ClearUnchecked(targetRowIndex, rowCount, targetColumnIndex, columnCount); |
|
|
|
} |
|
|
|
|
|
|
|
if (processZeros) |
|
|
|
@ -1699,7 +1701,7 @@ namespace MathNet.Numerics.LinearAlgebra.Storage |
|
|
|
var processZeros = zeros == Zeros.Include || !Zero.Equals(f(0, 1, Zero)); |
|
|
|
if (existingData == ExistingData.Clear && !processZeros) |
|
|
|
{ |
|
|
|
target.Clear(targetRowIndex, rowCount, targetColumnIndex, columnCount); |
|
|
|
target.ClearUnchecked(targetRowIndex, rowCount, targetColumnIndex, columnCount); |
|
|
|
} |
|
|
|
|
|
|
|
var rowOffset = targetRowIndex - sourceRowIndex; |
|
|
|
|