//
// 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using MathNet.Numerics.LinearAlgebra.Storage;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra
{
[DebuggerDisplay("Vector {Count}")]
public abstract partial class Vector
{
///
/// 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 bool Equals(Vector other)
{
return other != null && Storage.Equals(other.Storage);
}
///
/// Determines whether the specified is equal to this instance.
///
/// The to compare with this instance.
///
/// true if the specified is equal to this instance; otherwise, false.
///
public override sealed bool Equals(object obj)
{
var other = obj as Vector;
return other != null && Storage.Equals(other.Storage);
}
///
/// Returns a hash code for this instance.
///
///
/// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
///
public override sealed int GetHashCode()
{
return Storage.GetHashCode();
}
#if !PORTABLE
///
/// Creates a new object that is a copy of the current instance.
///
///
/// A new object that is a copy of this instance.
///
object ICloneable.Clone()
{
return Clone();
}
#endif
int IList.IndexOf(T item)
{
for (int i = 0; i < Count; ++i)
{
if (At(i).Equals(item))
return i;
}
return -1;
}
void IList.Insert(int index, T item)
{
throw new NotSupportedException();
}
void IList.RemoveAt(int index)
{
throw new NotSupportedException();
}
bool ICollection.IsReadOnly
{
get { return false; }
}
void ICollection.Add(T item)
{
throw new NotSupportedException();
}
bool ICollection.Remove(T item)
{
throw new NotSupportedException();
}
bool ICollection.Contains(T item)
{
// ReSharper disable once LoopCanBeConvertedToQuery
foreach (var x in this)
{
if (x.Equals(item))
return true;
}
return false;
}
void ICollection.CopyTo(T[] array, int arrayIndex)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
Storage.CopySubVectorTo(new DenseVectorStorage(array.Length, array), 0, arrayIndex, Count);
}
bool IList.IsReadOnly
{
get { return false; }
}
bool IList.IsFixedSize
{
get { return true; }
}
object IList.this[int index]
{
get { return Storage[index]; }
set { Storage[index] = (T) value; }
}
int IList.IndexOf(object value)
{
if (!(value is T))
{
return -1;
}
return ((IList) this).IndexOf((T) value);
}
bool IList.Contains(object value)
{
if (!(value is T))
{
return false;
}
return ((ICollection) this).Contains((T) value);
}
void IList.Insert(int index, object value)
{
throw new NotSupportedException();
}
int IList.Add(object value)
{
throw new NotSupportedException();
}
void IList.Remove(object value)
{
throw new NotSupportedException();
}
void IList.RemoveAt(int index)
{
throw new NotSupportedException();
}
bool ICollection.IsSynchronized
{
get { return false; }
}
object ICollection.SyncRoot
{
get { return Storage; }
}
void ICollection.CopyTo(Array array, int index)
{
if (array == null)
{
throw new ArgumentNullException("array");
}
if (array.Rank != 1)
{
throw new ArgumentException(Resources.ArgumentSingleDimensionArray, "array");
}
Storage.CopySubVectorTo(new DenseVectorStorage(array.Length, (T[]) array), 0, index, Count);
}
///
/// Returns an enumerator that iterates through the collection.
///
///
/// A that can be used to iterate through the collection.
///
IEnumerator IEnumerable.GetEnumerator()
{
return Enumerate().GetEnumerator();
}
///
/// Returns an enumerator that iterates through a collection.
///
///
/// An object that can be used to iterate through the collection.
///
IEnumerator IEnumerable.GetEnumerator()
{
return Enumerate().GetEnumerator();
}
///
/// Returns a that describes the type, dimensions and shape of this vector.
///
public virtual string ToTypeString()
{
return string.Format("{0} {1}-{2}", GetType().Name, Count, typeof (T).Name);
}
///
/// Returns a that represents the content of this vector, row by row.
///
public string ToVectorString(int maxLines, int maxPerLine, IFormatProvider provider = null)
{
return ToVectorString(maxLines, maxPerLine, 12, "G6", provider);
}
///
/// Returns a that represents the content of this vector, row by row.
///
public string ToVectorString(int maxLines, int maxPerLine, int padding, string format = null, IFormatProvider provider = null)
{
int fullLines = Count/maxPerLine;
int lastLine = Count%maxPerLine;
bool incomplete = false;
if (fullLines > maxLines || fullLines == maxLines && lastLine > 0)
{
fullLines = maxLines - 1;
lastLine = maxPerLine - 1;
incomplete = true;
}
const string separator = " ";
string pdots = "...".PadLeft(padding);
if (format == null)
{
format = "G8";
}
var stringBuilder = new StringBuilder();
var iterator = Enumerate().GetEnumerator();
for (var line = 0; line < fullLines; line++)
{
if (line > 0)
{
stringBuilder.Append(Environment.NewLine);
}
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
for (var column = 1; column < maxPerLine; column++)
{
stringBuilder.Append(separator);
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
}
}
if (lastLine > 0)
{
if (fullLines > 0)
{
stringBuilder.Append(Environment.NewLine);
}
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
for (var column = 1; column < lastLine; column++)
{
stringBuilder.Append(separator);
iterator.MoveNext();
stringBuilder.Append(iterator.Current.ToString(format, provider).PadLeft(padding));
}
if (incomplete)
{
stringBuilder.Append(separator);
stringBuilder.Append(pdots);
}
}
return stringBuilder.ToString();
}
///
/// Returns a that summarizes this vector.
///
public string ToString(int maxLines, int maxPerLine, IFormatProvider provider = null)
{
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(maxLines, maxPerLine, provider));
}
///
/// Returns a that summarizes this vector.
/// The maximum number of cells can be configured in the class.
///
public override sealed string ToString()
{
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(Control.MaxToStringRows, Control.MaxToStringColumns));
}
///
/// Returns a that summarizes this vector.
/// The maximum number of cells can be configured in the class.
/// The format string is ignored.
///
public string ToString(string format, IFormatProvider formatProvider)
{
return string.Concat(ToTypeString(), Environment.NewLine, ToVectorString(Control.MaxToStringRows, Control.MaxToStringColumns, formatProvider));
}
}
}