/// /// This file is part of ILNumerics Community Edition. /// /// ILNumerics Community Edition - high performance computing for applications. /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net /// /// ILNumerics Community Edition is free software: you can redistribute it and/or modify /// it under the terms of the GNU General Public License version 3 as published by /// the Free Software Foundation. /// /// ILNumerics Community Edition is distributed in the hope that it will be useful, /// but WITHOUT ANY WARRANTY; without even the implied warranty of /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /// GNU General Public License for more details. /// /// You should have received a copy of the GNU General Public License /// along with ILNumerics Community Edition. See the file License.txt in the root /// of your distribution package. If not, see . /// /// In addition this software uses the following components and/or licenses: /// /// ================================================================================= /// The Open Toolkit Library License /// /// Copyright (c) 2006 - 2009 the Open Toolkit library. /// /// 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. /// /// ================================================================================= /// using System; using ILNumerics; using ILNumerics.Misc; using ILNumerics.Storage; using ILNumerics.Native; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.IO; using System.Text; using System.Collections.Generic; using System.Collections; namespace ILNumerics { /// /// Base type for all arrays in ILNumerics /// /// All numerical arrays derive from ILBaseArray. ILBaseArrays itself /// cannot be instantiated. Currently, only ILArray]]> exist, which describe /// a rectangular array as full (solid) or referencing array. There are plans to /// extend the collection of derived types to encompass triangular, diagonal and sparse arrays. /// [Serializable] public abstract class ILBaseArray : IDisposable { #region attributes /// /// Name of this array /// protected String m_name = ""; internal ILStorage m_storage; internal int m_scopeCounter = 0; internal bool m_isTempArray; #endregion #region properties internal int ScopeID { get { return m_scopeCounter; } } internal bool IsDisposed { get { return m_storage == null || m_storage.IsDisposed; } } internal ILStorage Storage { get { return m_storage; } } /// /// Size descriptor specification /// public virtual ILSize Size { get { return Storage.Size; } } /// /// [deprecated] Use 'Size' as size descriptor! /// [Obsolete("Use 'Size' instead!")] public ILSize Dimensions { get { return Size; } } /// /// Size descriptor shortcut /// public virtual ILSize S { get { return Storage.Size; } } /// /// [deprecated] Use 'S' as size descriptor! /// [Obsolete("Use S instead!")] public virtual ILSize D { get { return S; } } /// /// Length of the longest dimension of this instance /// /// This property is readonly. public virtual int Length { get { return Size.Longest; } } /// /// Gets the name of this array or sets it /// public String Name { get { return m_name; } set { m_name = value; } } /// /// Test if this instance is a scalar /// /// This attribute is readonly. It returns: Size.NumberOfElements == 1. public virtual bool IsScalar { get { return Size.NumberOfElements == 1; } } /// /// Test if this instance is a matrix /// /// In order for an array to be a matrix the number of non singleton /// dimensions must equal 2. This attribute is readonly. public virtual bool IsMatrix { get { if (Size.NumberOfDimensions == 2) return true; return (Size.Squeeze().NumberOfDimensions == 2); } } /// /// Test if this array is a vector /// /// In order for an array to be a vector the number of non singleton /// dimensions must equal 1. Keep in mind that all ILArrays have at least 2 dimensions. Therefore /// it is not sufficient to test for the number of dimensions, but to take the number of /// non singleton dimensions into account. This attribute is readonly. public virtual bool IsVector { get { return (Size[0] == 1 || Size[1] == 1) && Size.NumberOfDimensions == 2; } } /// /// Test if this array instance is a row vector /// public virtual bool IsRowVector { get { return Size[0] == 1 && Size.NumberOfDimensions == 2; } } /// /// Test if this array instance is a column vector /// public virtual bool IsColumnVector { get { return Size[1] == 1 && Size.NumberOfDimensions == 2;; } } /// /// Test if this instance is an empty array (number of elements stored = 0) /// public virtual bool IsEmpty { get { return Size.NumberOfElements == 0; } } #endregion #region constructors internal ILBaseArray(ILStorage storage, bool isTempArray) { m_storage = storage; m_isTempArray = isTempArray; } /// /// Implicit cast from scalar of typeof(a) to ILRetArray<typeof(A)> /// /// Input scalar /// A ILRetArray of same type as and size 1x1 public static implicit operator ILBaseArray(double a) { return new ILRetArray(new double[] { a }, ILSize.Scalar1_1); } /// /// Implicit cast from scalar of typeof(A) to ILRetArray<typeof(A)> /// /// Input scalar /// A ILRetArray of same type as ans size 1x1 public static implicit operator ILBaseArray(complex a) { return new ILRetArray(new complex[] { a, 0 }, ILSize.Scalar1_1); } /// /// Implicit cast from scalar of typeof(A) to ILRetArray<typeof(A)> /// /// Input scalar /// A ILRetArray of same type as ans size 1x1 public static implicit operator ILBaseArray(fcomplex a) { return new ILRetArray(new fcomplex[] { a, 0 }, ILSize.Scalar1_1); } /// /// Implicit cast from scalar of typeof(A) to ILRetArray<typeof(A)> /// /// Input scalar /// A ILRetArray of same type as and size 1x1 public static implicit operator ILBaseArray(string s) { return new ILRetArray(new string[] { s }, ILSize.Scalar1_1); } //public static implicit operator ILBaseArray(string[] arr) { // return new ILRetArray(new string[] { String.Join(";", arr) }, ILSize.Scalar1_1); //} /// /// Implicit cast from scalar of typeof(A) to ILRetArray<typeof(A)> /// /// Input scalar /// A ILRetArray of same type as ans size 1x1 public static implicit operator ILBaseArray(double[] a) { if (a == null || a.Length == 0) { return new ILRetArray(ILSize.Empty00); } ILCell ret = new ILCell(1,a.Length); for (int i = 0; i < a.Length; i++) { ret.SetValue(new ILRetArray(new double[] { a[i] }, ILSize.Scalar1_1),0,i); } //ret.SetValue("int[]",1,0); return ret; } //public static implicit operator ILBaseArray(int[] a) { // if (a == null || a.Length == 0) { // return new ILRetArray(ILSize.Empty00); // } // ILCell ret = new ILCell(1,a.Length); // for (int i = 0; i < a.Length; i++) { // ret.SetValue(new ILRetArray(new int[] { a[i] }, ILSize.Scalar1_1),0,i); // } // //ret.SetValue("int[]",1,0); // return ret; //} #endregion #region virtual /// /// Compare elements and shape of this array with another array /// /// Other array /// true if shape and element values of both arrays match, false otherwise /// 'Equals' accepts two vectors even if the orientations do not match. Therefore, a row vector /// with the same element values than another column vector would be considered equal to each other. public override bool Equals(object A) { ILBaseArray baseArray = A as ILBaseArray; if (baseArray != null) { return Storage.Equals(baseArray.Storage); } if (IsScalar && A is System.ValueType) { return String.Equals(A.ToString(),Storage.GetValue(0).ToString()); } return false; } /// /// Generate a hash code based on the current arrays values /// /// Hash code /// The hashcode is created by taking the values currently stored in the array into account. /// Therefore, the function iterates over all elements in the array - which makes it somehow an expensive /// operation. Take this into account, if you consider using large arrays in collections like dictionaries /// or hashtables, which make great use of hash codes. public override int GetHashCode() { return Storage.GetHashCode(); } internal virtual bool EnterScope() { if (m_scopeCounter < 2) { m_scopeCounter++; return true; } return false; } internal virtual void LeaveScope() { if (--m_scopeCounter <= 0) { Dispose(); } } #endregion #region abstract interface /// /// Clone this array (shallow) /// /// ILBaseArray as new representation of this storages data. /// The object returned will be of the same size than this array. /// This this is a 'shallow' copy only! I.e., if elements are copied only. If they are /// references to any objects, those objects are not replicated. internal abstract ILBaseArray Clone(); /// /// Determine if this array is of complex inner type. /// public abstract bool IsComplex { get; } /// /// Determine if this array is of numeric inner type. /// public abstract bool IsNumeric { get; } /// /// Print values of this instance to a stream. /// /// Stream to write the values into. /// Format string to be used for output. See for a specification /// of valid formating expressions. This flag is only used, when 'method' is set to 'Serial'. /// A constant out of . Specifies the way /// the values will be serialized. /// If method 'Formatted' is used, any occurences of NewLine character(s) /// will be replaced from the format string before applying to the elements. This is done to /// prevent the format from breaking the 'page' style for the output. /// If 'method' is set to 'Matlab', the array will be written as Matfile version 5.0. No compression will be used. The internal 'Name' property will be used as /// the array name for writing. This array instance will be the only array in the mat file. If you want to write several arrays bundled into one mat file, use the MatFile class to /// create a collection of arrays and write the MatFile to stream. public abstract void ToStream(Stream outStream, string format, ILArrayStreamSerializationFlags method); /// /// Convert to string /// /// String representation of content public override string ToString() { return ToString(0); } /// /// Convert to string with limited length /// /// Maximal length of returned string; set to 0 to not limit result /// String representation of content public virtual string ToString(int maxLength) { string ret = ShortInfo(); if (m_storage.Size.NumberOfElements > 1) ret += Environment.NewLine + Storage.ValuesToString(maxLength); return ret; } /// /// Short textual summary of this instance, used for debug output /// /// String representation of type and size /// The type of elements and the size of the array are displayed. If the array /// is scalar, its value is displayed next to the type. public virtual String ShortInfo() { return Storage.ShortInfo(); } /// /// Dispose this array and all its content /// public void Dispose() { if (IsDisposed) return; if (Storage != null) { Storage.Dispose(); } } #endregion } }