// // Math.NET Numerics, part of the Math.NET Project // http://numerics.mathdotnet.com // http://github.com/mathnet/mathnet-numerics // http://mathnetnumerics.codeplex.com // // Copyright (c) 2009-2013 Math.NET // // Permission is hereby granted, free of charge, to any person // obtaining a copy of this software and associated documentation // files (the "Software"), to deal in the Software without // restriction, including without limitation the rights to use, // copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the // Software is furnished to do so, subject to the following // conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR // OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Collections.Generic; using MathNet.Numerics.Properties; namespace MathNet.Numerics.LinearAlgebra.Storage { [Serializable] public abstract partial class VectorStorage : IEquatable> where T : struct, IEquatable, IFormattable { // [ruegg] public fields are OK here protected static readonly T Zero = BuilderInstance.Vector.Zero; public readonly int Length; protected VectorStorage(int length) { if (length <= 0) { throw new ArgumentOutOfRangeException("length", Resources.ArgumentMustBePositive); } Length = length; } /// /// True if the vector storage format is dense. /// public abstract bool IsDense { get; } /// /// Gets or sets the value at the given index, with range checking. /// /// /// The index of the element. /// /// The value to get or set. /// This method is ranged checked. and /// to get and set values without range checking. public T this[int index] { get { ValidateRange(index); return At(index); } set { ValidateRange(index); At(index, value); } } /// /// Retrieves the requested element without range checking. /// /// The index of the element. /// The requested element. /// Not range-checked. public abstract T At(int index); /// /// Sets the element without range checking. /// /// The index of the element. /// The value to set the element to. /// WARNING: This method is not thread safe. Use "lock" with it and be sure to avoid deadlocks. public abstract void At(int index, T value); public virtual void Clear() { for (var i = 0; i < Length; i++) { At(i, Zero); } } public virtual void Clear(int index, int count) { for (var i = index; i < index + count; i++) { At(i, Zero); } } /// /// Indicates whether the current object is equal to another object of the same type. /// /// /// An object to compare with this object. /// /// /// true if the current object is equal to the parameter; otherwise, false. /// public virtual bool Equals(VectorStorage other) { // Reject equality when the argument is null or has a different shape. if (other == null) { return false; } if (Length != other.Length) { return false; } // Accept if the argument is the same object as this. if (ReferenceEquals(this, other)) { return true; } // If all else fails, perform element wise comparison. for (var index = 0; index < Length; index++) { if (!At(index).Equals(other.At(index))) { return false; } } return true; } /// /// Determines whether the specified is equal to the current . /// /// /// true if the specified is equal to the current ; otherwise, false. /// /// The to compare with the current . public override sealed bool Equals(object obj) { return Equals(obj as VectorStorage); } /// /// Serves as a hash function for a particular type. /// /// /// A hash code for the current . /// public override int GetHashCode() { var hashNum = Math.Min(Length, 25); int hash = 17; unchecked { for (var i = 0; i < hashNum; i++) { hash = hash*31 + At(i).GetHashCode(); } } return hash; } // VECTOR COPY public void CopyTo(VectorStorage target, bool skipClearing = false) { if (target == null) { throw new ArgumentNullException("target"); } if (ReferenceEquals(this, target)) { return; } if (Length != target.Length) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); } CopyToUnchecked(target, skipClearing); } internal virtual void CopyToUnchecked(VectorStorage target, bool skipClearing = false) { for (int i = 0; i < Length; i++) { target.At(i, At(i)); } } // ROW COPY public void CopyToRow(MatrixStorage target, int rowIndex, bool skipClearing = false) { if (target == null) { throw new ArgumentNullException("target"); } if (Length != target.ColumnCount) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); } ValidateRowRange(target, rowIndex); CopyToRowUnchecked(target, rowIndex, skipClearing); } internal virtual void CopyToRowUnchecked(MatrixStorage target, int rowIndex, bool skipClearing = false) { for (int j = 0; j < Length; j++) { target.At(rowIndex, j, At(j)); } } // COLUMN COPY public void CopyToColumn(MatrixStorage target, int columnIndex, bool skipClearing = false) { if (target == null) { throw new ArgumentNullException("target"); } if (Length != target.RowCount) { throw new ArgumentException(Resources.ArgumentVectorsSameLength, "target"); } ValidateColumnRange(target, columnIndex); CopyToColumnUnchecked(target, columnIndex, skipClearing); } internal virtual void CopyToColumnUnchecked(MatrixStorage target, int columnIndex, bool skipClearing = false) { for (int i = 0; i < Length; i++) { target.At(i, columnIndex, At(i)); } } // SUB-VECTOR COPY public void CopySubVectorTo(VectorStorage target, int sourceIndex, int targetIndex, int count, bool skipClearing = false) { if (target == null) { throw new ArgumentNullException("target"); } ValidateSubVectorRange(target, sourceIndex, targetIndex, count); CopySubVectorToUnchecked(target, sourceIndex, targetIndex, count, skipClearing); } internal virtual void CopySubVectorToUnchecked(VectorStorage target, int sourceIndex, int targetIndex, int count, bool skipClearing = false) { if (ReferenceEquals(this, target)) { var tmp = new T[count]; for (int i = 0; i < tmp.Length; i++) { tmp[i] = At(i + sourceIndex); } for (int i = 0; i < tmp.Length; i++) { At(i + targetIndex, tmp[i]); } return; } for (int i = sourceIndex, ii = targetIndex; i < sourceIndex + count; i++, ii++) { target.At(ii, At(i)); } } // SUB-ROW COPY public void CopyToSubRow(MatrixStorage target, int rowIndex, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing = false) { if (target == null) { throw new ArgumentNullException("target"); } ValidateSubRowRange(target, rowIndex, sourceColumnIndex, targetColumnIndex, columnCount); CopyToSubRowUnchecked(target, rowIndex, sourceColumnIndex, targetColumnIndex, columnCount, skipClearing); } internal virtual void CopyToSubRowUnchecked(MatrixStorage target, int rowIndex, int sourceColumnIndex, int targetColumnIndex, int columnCount, bool skipClearing = false) { for (int j = sourceColumnIndex, jj = targetColumnIndex; j < sourceColumnIndex + columnCount; j++, jj++) { target.At(rowIndex, jj, At(j)); } } // SUB-COLUMN COPY public void CopyToSubColumn(MatrixStorage target, int columnIndex, int sourceRowIndex, int targetRowIndex, int rowCount, bool skipClearing = false) { if (target == null) { throw new ArgumentNullException("target"); } ValidateSubColumnRange(target, columnIndex, sourceRowIndex, targetRowIndex, rowCount); CopyToSubColumnUnchecked(target, columnIndex, sourceRowIndex, targetRowIndex, rowCount, skipClearing); } internal virtual void CopyToSubColumnUnchecked(MatrixStorage target, int columnIndex, int sourceRowIndex, int targetRowIndex, int rowCount, bool skipClearing = false) { for (int i = sourceRowIndex, ii = targetRowIndex; i < sourceRowIndex + rowCount; i++, ii++) { target.At(ii, columnIndex, At(i)); } } // ENUMERATION public virtual IEnumerable Enumerate() { for (var i = 0; i < Length; i++) { yield return At(i); } } public virtual IEnumerable> EnumerateIndexed() { for (var i = 0; i < Length; i++) { yield return new Tuple(i, At(i)); } } public virtual IEnumerable EnumerateNonZero() { for (var i = 0; i < Length; i++) { var x = At(i); if (!Zero.Equals(x)) { yield return x; } } } public virtual IEnumerable> EnumerateNonZeroIndexed() { for (var i = 0; i < Length; i++) { var x = At(i); if (!Zero.Equals(x)) { yield return new Tuple(i, x); } } } // FUNCTIONAL COMBINATORS public virtual void MapInplace(Func f, bool forceMapZeros = false) { for (int i = 0; i < Length; i++) { At(i, f(At(i))); } } public virtual void MapIndexedInplace(Func f, bool forceMapZeros = false) { for (int i = 0; i < Length; i++) { At(i, f(i, At(i))); } } } }