using System; using MathNet.Numerics.Properties; namespace MathNet.Numerics.LinearAlgebra.Storage { internal class DenseColumnMajorMatrixStorage : MatrixStorage where T : struct, IEquatable, IFormattable { // [ruegg] public fields are OK here public readonly T[] Data; internal DenseColumnMajorMatrixStorage(int rows, int columns) : base(rows, columns) { Data = new T[rows * columns]; } internal DenseColumnMajorMatrixStorage(int rows, int columns, T[] data) : base(rows, columns) { Data = data; } /// /// Retrieves the requested element without range checking. /// public override T At(int row, int column) { return Data[(column * RowCount) + row]; } /// /// Sets the element without range checking. /// public override void At(int row, int column, T value) { Data[(column * RowCount) + row] = value; } public override void Clear() { Array.Clear(Data, 0, Data.Length); } public void CopyTo(MatrixStorage target, bool skipClearing = false) { var denseTarget = target as DenseColumnMajorMatrixStorage; if (denseTarget != null) { CopyTo(denseTarget); return; } // FALL BACK if (ReferenceEquals(this, target)) { return; } if (target == null) { throw new ArgumentNullException("target"); } if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) { var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); throw new ArgumentException(message, "target"); } for (int j = 0, offset = 0; j < ColumnCount; j++, offset += RowCount) { for (int i = 0; i < RowCount; i++) { target.At(i, j, Data[i + offset]); } } } public void CopyTo(DenseColumnMajorMatrixStorage target) { if (ReferenceEquals(this, target)) { return; } if (target == null) { throw new ArgumentNullException("target"); } if (RowCount != target.RowCount || ColumnCount != target.ColumnCount) { var message = string.Format(Resources.ArgumentMatrixDimensions2, RowCount + "x" + ColumnCount, target.RowCount + "x" + target.ColumnCount); throw new ArgumentException(message, "target"); } //Buffer.BlockCopy(Data, 0, target.Data, 0, Data.Length * System.Runtime.InteropServices.Marshal.SizeOf(typeof(T))); Array.Copy(Data, 0, target.Data, 0, Data.Length); } public void CopySubMatrixTo(DenseColumnMajorMatrixStorage target, int sourceRowIndex, int targetRowIndex, int rowCount, int sourceColumnIndex, int targetColumnIndex, int columnCount) { if (target == null) { throw new ArgumentNullException("target"); } if (ReferenceEquals(this, target)) { throw new NotSupportedException(); } ValidateSubMatrixRange(target, sourceRowIndex, targetRowIndex, rowCount, sourceColumnIndex, targetColumnIndex, columnCount); for (int j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnIndex + columnCount; j++, jj++) { //Buffer.BlockCopy(Data, j*RowCount + sourceRowIndex, target.Data, jj*target.RowCount + targetRowIndex, rowCount * System.Runtime.InteropServices.Marshal.SizeOf(typeof(T))); Array.Copy(Data, j*RowCount + sourceRowIndex, target.Data, jj*target.RowCount + targetRowIndex, rowCount); } } } }