[9102] | 1 | ///
|
---|
| 2 | /// This file is part of ILNumerics Community Edition.
|
---|
| 3 | ///
|
---|
| 4 | /// ILNumerics Community Edition - high performance computing for applications.
|
---|
| 5 | /// Copyright (C) 2006 - 2012 Haymo Kutschbach, http://ilnumerics.net
|
---|
| 6 | ///
|
---|
| 7 | /// ILNumerics Community Edition is free software: you can redistribute it and/or modify
|
---|
| 8 | /// it under the terms of the GNU General Public License version 3 as published by
|
---|
| 9 | /// the Free Software Foundation.
|
---|
| 10 | ///
|
---|
| 11 | /// ILNumerics Community Edition is distributed in the hope that it will be useful,
|
---|
| 12 | /// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 13 | /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 14 | /// GNU General Public License for more details.
|
---|
| 15 | ///
|
---|
| 16 | /// You should have received a copy of the GNU General Public License
|
---|
| 17 | /// along with ILNumerics Community Edition. See the file License.txt in the root
|
---|
| 18 | /// of your distribution package. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | ///
|
---|
| 20 | /// In addition this software uses the following components and/or licenses:
|
---|
| 21 | ///
|
---|
| 22 | /// =================================================================================
|
---|
| 23 | /// The Open Toolkit Library License
|
---|
| 24 | ///
|
---|
| 25 | /// Copyright (c) 2006 - 2009 the Open Toolkit library.
|
---|
| 26 | ///
|
---|
| 27 | /// Permission is hereby granted, free of charge, to any person obtaining a copy
|
---|
| 28 | /// of this software and associated documentation files (the "Software"), to deal
|
---|
| 29 | /// in the Software without restriction, including without limitation the rights to
|
---|
| 30 | /// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
---|
| 31 | /// the Software, and to permit persons to whom the Software is furnished to do
|
---|
| 32 | /// so, subject to the following conditions:
|
---|
| 33 | ///
|
---|
| 34 | /// The above copyright notice and this permission notice shall be included in all
|
---|
| 35 | /// copies or substantial portions of the Software.
|
---|
| 36 | ///
|
---|
| 37 | /// =================================================================================
|
---|
| 38 | ///
|
---|
| 39 |
|
---|
| 40 | using System;
|
---|
| 41 | using System.Collections.Generic;
|
---|
| 42 | using System.Text;
|
---|
| 43 | using System.IO;
|
---|
| 44 | using System.Runtime.Serialization;
|
---|
| 45 | using System.Runtime.CompilerServices;
|
---|
| 46 | using ILNumerics.Storage;
|
---|
| 47 | using ILNumerics.Misc;
|
---|
| 48 | using ILNumerics.Exceptions;
|
---|
| 49 |
|
---|
| 50 |
|
---|
| 51 |
|
---|
| 52 | namespace ILNumerics {
|
---|
| 53 | /// <summary>
|
---|
| 54 | /// The main rectangular array to be used in algorithms
|
---|
| 55 | /// </summary>
|
---|
| 56 | /// <typeparam name="ElementType">Inner type. This will mostly be a system numeric type or a
|
---|
| 57 | /// complex floating point type.</typeparam>
|
---|
| 58 | /// <remarks>This class serves as the main rectangular array, holding arbitrary elements (usually numeric types)
|
---|
| 59 | /// in arbitrary dimensions.
|
---|
| 60 | /// <para>Arrays of this type may use any type as generic element. However, common mathematical functions and operators
|
---|
| 61 | /// are defined for a limited number of inner types only. All binary operations (+,-,*,/,<![CDATA[<,>,<=]]>,etc.) are
|
---|
| 62 | /// defined for two arrays with the same <i>numeric type</i>, would it be from the <c>System</c> namespace (<c>double</c>,
|
---|
| 63 | /// <c>int</c>,...) or <c>ILNumerics.complex</c>/ <c>ILNumerics.fcomplex</c>. Most algebraic functions require floating point
|
---|
| 64 | /// types. See the <see cref="ILNumerics.ILMath"/> class for a list of all computational functions.</para>
|
---|
| 65 | /// <para>Arrays are capable of creating flexible <a href="http://ilnumerics.net/$Subarray0.html" target="ILMain">subarrays</a>
|
---|
| 66 | /// and to get altered at runtime. Read about all details of ILNumerics arrays in the
|
---|
| 67 | /// <a href="http://ilnumerics.net/$Arrays.html" target="ILMain">ILNumerics Array documentation</a>.</para>
|
---|
| 68 | /// <para>Arrays of this type are dense arrays. Cloning arrays is done as lazy
|
---|
| 69 | /// copy on write, i.e. clones do only use new memory, if attempting to write on them. Arrays integrate into the memory
|
---|
| 70 | /// management of ILNumerics. Read about the most <a href="http://ilnumerics.net/$GeneralRules.html" target="ILMain">important
|
---|
| 71 | /// simple rules</a>, for using arrays in custom computational functions.</para>
|
---|
| 72 | /// <para>Arrays come with overloaded mathematical operators, allowing for a convenient syntax. A
|
---|
| 73 | /// sophisticated memory management in the back will make sure, that as little memory as needed is used, even in
|
---|
| 74 | /// expressions like: a + c * 2 / abs(sin(c) * -b / log(a)). Here all arrays are of the same size. Evaluating
|
---|
| 75 | /// this expression does only need the memory of twice the size of one array. Memory gets collected and reused
|
---|
| 76 | /// for every subexpression evaluation. Further optimization options exist, as described in
|
---|
| 77 | /// <a href="http://ilnumerics.net/$PerfMemoryOpt.html" target="ILMain">Optimizing Algorithm Performance</a>.</para>
|
---|
| 78 | /// </remarks>
|
---|
| 79 | /// <example><para>A simple example demonstrating some uses of arrays in a very simple application:</para>
|
---|
| 80 | /// <code>using System;
|
---|
| 81 | ///using System.Collections.Generic;
|
---|
| 82 | ///using System.Linq;
|
---|
| 83 | ///using System.Text;
|
---|
| 84 | ///using ILNumerics;
|
---|
| 85 | ///
|
---|
| 86 | ///
|
---|
| 87 | ///namespace ConsoleApplication1 {
|
---|
| 88 | /// class Program : ILMath {
|
---|
| 89 | /// static void Main(string[] args) {
|
---|
| 90 | /// ILArray<double> A = rand(10,20);
|
---|
| 91 | /// ILArray<double> B = A * 30 + 100;
|
---|
| 92 | /// ILLogical C = any(multiply(B,B.T));
|
---|
| 93 | /// Console.Out.Write(-B);
|
---|
| 94 | /// Console.ReadKey();
|
---|
| 95 | /// }
|
---|
| 96 | /// }
|
---|
| 97 | ///}
|
---|
| 98 | ///</code>
|
---|
| 99 | /// </example>
|
---|
| 100 | /// <seealso cref="ILNumerics.ILLogical"/>
|
---|
| 101 | /// <seealso cref="ILNumerics.ILCell"/>
|
---|
| 102 | [Serializable]
|
---|
| 103 | public sealed class ILArray<ElementType> : ILDenseArray<ElementType> {
|
---|
| 104 |
|
---|
| 105 | private static bool s_isTempArray = false;
|
---|
| 106 |
|
---|
| 107 | #region constructors
|
---|
| 108 | /// <summary>
|
---|
| 109 | /// Create new ILArray, specify (dense) storage
|
---|
| 110 | /// </summary>
|
---|
| 111 | /// <param name="storage"></param>
|
---|
| 112 | internal ILArray(ILDenseStorage<ElementType> storage)
|
---|
| 113 | : base(storage, s_isTempArray) {
|
---|
| 114 | }
|
---|
| 115 | /// <summary>
|
---|
| 116 | /// create new ILArray, specify dimensions
|
---|
| 117 | /// </summary>
|
---|
| 118 | /// <param name="dimensions"></param>
|
---|
| 119 | private ILArray(ILSize dimensions)
|
---|
| 120 | : base(new ILDenseStorage<ElementType>(dimensions), s_isTempArray) {
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | /// <summary>
|
---|
| 124 | /// create new ILArray, specify storage and if the new array should be disposed automatically
|
---|
| 125 | /// </summary>
|
---|
| 126 | /// <param name="storage"></param>
|
---|
| 127 | /// <param name="registerForDisposal"></param>
|
---|
| 128 | internal ILArray(ILDenseStorage<ElementType> storage, bool registerForDisposal)
|
---|
| 129 | : base(storage, s_isTempArray) {
|
---|
| 130 | if (registerForDisposal)
|
---|
| 131 | ILScope.Context.RegisterArray(this);
|
---|
| 132 | }
|
---|
| 133 | /// <summary>
|
---|
| 134 | /// create new ILArray, specify dimensions
|
---|
| 135 | /// </summary>
|
---|
| 136 | /// <param name="dimensions"></param>
|
---|
| 137 | /// <param name="registerForDisposal"></param>
|
---|
| 138 | private ILArray(ILSize dimensions, bool registerForDisposal)
|
---|
| 139 | : base(new ILDenseStorage<ElementType>(dimensions), s_isTempArray) {
|
---|
| 140 | if (registerForDisposal)
|
---|
| 141 | ILScope.Context.RegisterArray(this);
|
---|
| 142 | }
|
---|
| 143 | /// <summary>
|
---|
| 144 | /// create new ILArray from System.Array
|
---|
| 145 | /// </summary>
|
---|
| 146 | /// <param name="elements">System.Array</param>
|
---|
| 147 | /// <param name="size">dimension specifier</param>
|
---|
| 148 | internal ILArray(ElementType[] elements, ILSize size) :
|
---|
| 149 | base(new ILDenseStorage<ElementType>(elements, size), s_isTempArray) {
|
---|
| 150 | }
|
---|
| 151 | /// <summary>
|
---|
| 152 | /// create new ILArray from System.Array, optionally register the array for disposal
|
---|
| 153 | /// </summary>
|
---|
| 154 | /// <param name="elements">System.Array</param>
|
---|
| 155 | /// <param name="registerForDisposal">if true, the array will be disposed once the current scope is closed</param>
|
---|
| 156 | /// <param name="size">dimension specifier</param>
|
---|
| 157 | internal ILArray(ElementType[] elements, ILSize size, bool registerForDisposal) :
|
---|
| 158 | base(new ILDenseStorage<ElementType>(elements, size), s_isTempArray) {
|
---|
| 159 | if (registerForDisposal)
|
---|
| 160 | ILScope.Context.RegisterArray(this);
|
---|
| 161 | }
|
---|
| 162 | /// <summary>
|
---|
| 163 | /// create new ILArray from System.Array
|
---|
| 164 | /// </summary>
|
---|
| 165 | /// <param name="elements">System.Array</param>
|
---|
| 166 | /// <param name="size">dimension specifier</param>
|
---|
| 167 | internal ILArray(ElementType[] elements, params int[] size) :
|
---|
| 168 | base(new ILDenseStorage<ElementType>(elements, new ILSize(size)), s_isTempArray) {
|
---|
| 169 | }
|
---|
| 170 | /// <summary>
|
---|
| 171 | /// create new ILArray from System.Array
|
---|
| 172 | /// </summary>
|
---|
| 173 | /// <param name="elements">variable length System.Array</param>
|
---|
| 174 | internal ILArray(params ElementType[] elements) :
|
---|
| 175 | base(new ILDenseStorage<ElementType>(elements, new ILSize(1, elements.Length)), s_isTempArray) {
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 |
|
---|
| 179 | /// <summary>
|
---|
| 180 | /// [deprecated] Create empty array of arbitrary size
|
---|
| 181 | /// </summary>
|
---|
| 182 | /// <param name="size">Dimension sizes</param>
|
---|
| 183 | /// <returns>New empty array</returns>
|
---|
| 184 | /// <remarks>This function is markes as deprecated and only included for compatibility reasons.
|
---|
| 185 | /// It will be removed in a future version. Use <see cref="ILNumerics.ILMath.empty{T}()"/> instead.</remarks>
|
---|
| 186 | [Obsolete("Use ILNumerics.ILMath.empty{T}() instead")]
|
---|
| 187 | public static ILRetArray<ElementType> empty(params int[] size) {
|
---|
| 188 | if (size == null || size.Length == 0)
|
---|
| 189 | return new ILArray<ElementType>(ILSize.Empty00);
|
---|
| 190 | ILArray<ElementType> ret = new ILArray<ElementType>(new ILSize(size));
|
---|
| 191 | if (!ret.IsEmpty)
|
---|
| 192 | throw new ILArgumentException("'size' must specify the size of an empty array");
|
---|
| 193 | return ret;
|
---|
| 194 | }
|
---|
| 195 | /// <summary>
|
---|
| 196 | /// [deprecated] Create empty array of arbitrary size
|
---|
| 197 | /// </summary>
|
---|
| 198 | /// <param name="size">Dimension sizes</param>
|
---|
| 199 | /// <returns>New empty array</returns>
|
---|
| 200 | /// <remarks>This function is markes as deprecated and only included for compatibility reasons.
|
---|
| 201 | /// It will be removed in a future version. Use <see cref="ILNumerics.ILMath.empty{T}()"/> instead.</remarks>
|
---|
| 202 | [Obsolete("Use ILNumerics.ILMath.empty{T}() instead")]
|
---|
| 203 | public static ILRetArray<ElementType> empty(ILSize size) {
|
---|
| 204 | ILArray<ElementType> ret = new ILArray<ElementType>(size);
|
---|
| 205 | if (!ret.IsEmpty)
|
---|
| 206 | throw new ILArgumentException("'size' must specify the size of an empty array");
|
---|
| 207 | return ret;
|
---|
| 208 | }
|
---|
| 209 | #endregion
|
---|
| 210 |
|
---|
| 211 | #region implicit cast operators
|
---|
| 212 |
|
---|
| 213 | #region constructional operators
|
---|
| 214 | /// <summary>
|
---|
| 215 | /// Implicitly convert scalar to array of size 1x1 (scalar).
|
---|
| 216 | /// </summary>
|
---|
| 217 | /// <param name="val">System type of size scalar</param>
|
---|
| 218 | /// <returns>New ILArray of type ILArray <![CDATA[<typeof(val)>]]> of size 1x1
|
---|
| 219 | /// holding the only element with value of val.
|
---|
| 220 | /// </returns>
|
---|
| 221 | public static implicit operator ILArray<ElementType> (ElementType val) {
|
---|
| 222 | ILArray<ElementType> ret = new ILArray<ElementType>(
|
---|
| 223 | new ILDenseStorage<ElementType>(
|
---|
| 224 | new ElementType[1] {val},
|
---|
| 225 | new ILSize(1,1)));
|
---|
| 226 | return ret;
|
---|
| 227 | }
|
---|
| 228 | /// <summary>
|
---|
| 229 | /// Implicitly cast one dimensional System.Array to ILNumerics array (vector)
|
---|
| 230 | /// </summary>
|
---|
| 231 | /// <param name="A">1-dimensional system array, arbitrary type</param>
|
---|
| 232 | /// <returns>ILNumerics array of same element type as elements of A. If A is null: empty array.</returns>
|
---|
| 233 | /// <remarks><para>The System.Array A will directly be used for the new ILNumerics array!
|
---|
| 234 | /// No copy will be done! Make sure, not to reference A after this conversion</para>
|
---|
| 235 | /// <para>The size of the result depends on the global option <c>Settings.CreateRowVectorByDefault</c></para></remarks>
|
---|
| 236 | /// <seealso cref="Settings.CreateRowVectorsByDefault"/>
|
---|
| 237 | public static implicit operator ILArray<ElementType> (ElementType[] A) {
|
---|
| 238 | if (A == null)
|
---|
| 239 | return null;
|
---|
| 240 | ILArray<ElementType> ret;
|
---|
| 241 | if (Settings.CreateRowVectorsByDefault) {
|
---|
| 242 | ret = new ILArray<ElementType>(A, 1, A.Length);
|
---|
| 243 | } else {
|
---|
| 244 | ret = new ILArray<ElementType>(A, A.Length, 1);
|
---|
| 245 | }
|
---|
| 246 | ILScope.Context.RegisterArray(ret);
|
---|
| 247 | return ret;
|
---|
| 248 | }
|
---|
| 249 | /// <summary>
|
---|
| 250 | /// Implicitly convert n-dimensional System.Array to ILNumerics array
|
---|
| 251 | /// </summary>
|
---|
| 252 | /// <param name="A">Arbitrarily sized System.Array</param>
|
---|
| 253 | /// <returns>If A is null: empty array. Else: new ILNumerics array of the same size as A</returns>
|
---|
| 254 | /// <remarks>The inner type of input array <paramref name="A"/> must match the requested type
|
---|
| 255 | /// <typeparamref name="ElementType"/>. The resulting ILArray will reflect all dimensions of
|
---|
| 256 | /// A. Elements of A will get copied to elements of the output array (shallow copy).</remarks>
|
---|
| 257 | /// <seealso cref="Settings.CreateRowVectorsByDefault"/>
|
---|
| 258 | /// <exception cref="ILNumerics.Exceptions.ILCastException">If type of input does not match
|
---|
| 259 | /// ElementType</exception>
|
---|
| 260 | public static implicit operator ILArray<ElementType> (Array A) {
|
---|
| 261 | if (A == null)
|
---|
| 262 | return null;
|
---|
| 263 | if (A.Length == 0) {
|
---|
| 264 | return new ILArray<ElementType>(ILSize.Empty00);
|
---|
| 265 | }
|
---|
| 266 | if (A.GetType().GetElementType() != typeof(ElementType))
|
---|
| 267 | throw new ILCastException("inner type of System.Array must match");
|
---|
| 268 | int [] dims = new int[A.Rank];
|
---|
| 269 | ElementType [] retArr = ILMemoryPool.Pool.New<ElementType>(A.Length);
|
---|
| 270 | int posArr = 0;
|
---|
| 271 | for (int i = 0; i < dims.Length; i++) {
|
---|
| 272 | dims[i] = A.GetLength(dims.Length-i-1);
|
---|
| 273 | }
|
---|
| 274 | if (dims.Length == 1 && Settings.CreateRowVectorsByDefault) {
|
---|
| 275 | dims = new int[2] {1, dims[0]};
|
---|
| 276 | }
|
---|
| 277 | foreach (ElementType item in A)
|
---|
| 278 | retArr[posArr++] = item;
|
---|
| 279 | ILArray<ElementType> ret = new ILArray<ElementType>(retArr,dims);
|
---|
| 280 | ILScope.Context.RegisterArray(ret);
|
---|
| 281 | return ret;
|
---|
| 282 | }
|
---|
| 283 | /// <summary>
|
---|
| 284 | /// Implicitly cast two dimensional System.Array to ILNumerics array
|
---|
| 285 | /// </summary>
|
---|
| 286 | /// <param name="A">2-dimensional System.Array</param>
|
---|
| 287 | /// <returns>If A is null: empty array. ILNumerics array of same size and type as A otherwise.</returns>
|
---|
| 288 | public static implicit operator ILArray<ElementType>(ElementType[,] A) {
|
---|
| 289 | if (A == null)
|
---|
| 290 | return null;
|
---|
| 291 | if (A.Length == 0) {
|
---|
| 292 | return new ILArray<ElementType>(ILSize.Empty00);
|
---|
| 293 | }
|
---|
| 294 | int[] dims = new int[2];
|
---|
| 295 | ElementType[] retArr = ILMemoryPool.Pool.New<ElementType>(A.Length);
|
---|
| 296 | int posArr = 0;
|
---|
| 297 | for (int i = 0; i < 2; i++) {
|
---|
| 298 | dims[i] = A.GetLength(dims.Length - i - 1);
|
---|
| 299 | }
|
---|
| 300 | foreach (ElementType item in A)
|
---|
| 301 | retArr[posArr++] = item;
|
---|
| 302 | ILArray<ElementType> ret = new ILArray<ElementType>(retArr, dims);
|
---|
| 303 | ILScope.Context.RegisterArray(ret);
|
---|
| 304 | return ret;
|
---|
| 305 | }
|
---|
| 306 | /// <summary>
|
---|
| 307 | /// Implicitly cast three dimensional System.Array to ILNumerics array
|
---|
| 308 | /// </summary>
|
---|
| 309 | /// <param name="A">3-dimensional System.Array</param>
|
---|
| 310 | /// <returns>If A is null: empty array. ILNumerics array of same size and type as A otherwise.</returns>
|
---|
| 311 | public static implicit operator ILArray<ElementType>(ElementType[,,] A) {
|
---|
| 312 | if (A == null)
|
---|
| 313 | return null;
|
---|
| 314 | if (A.Length == 0) {
|
---|
| 315 | return new ILArray<ElementType>(ILSize.Empty00);
|
---|
| 316 | }
|
---|
| 317 | int[] dims = new int[3];
|
---|
| 318 | ElementType[] retArr = ILMemoryPool.Pool.New<ElementType>(A.Length);
|
---|
| 319 | int posArr = 0;
|
---|
| 320 | for (int i = 0; i < 3; i++) {
|
---|
| 321 | dims[i] = A.GetLength(dims.Length - i - 1);
|
---|
| 322 | }
|
---|
| 323 | foreach (ElementType item in A)
|
---|
| 324 | retArr[posArr++] = item;
|
---|
| 325 | ILArray<ElementType> ret = new ILArray<ElementType>(retArr, dims);
|
---|
| 326 | ILScope.Context.RegisterArray(ret);
|
---|
| 327 | return ret;
|
---|
| 328 | }
|
---|
| 329 | #endregion
|
---|
| 330 |
|
---|
| 331 | #region conversional operators
|
---|
| 332 | /// <summary>
|
---|
| 333 | /// "Persistence cast" make a steady array out of a temp array
|
---|
| 334 | /// </summary>
|
---|
| 335 | /// <param name="A">Temporary array</param>
|
---|
| 336 | /// <returns>Steady ILArray, will survive multiple usages</returns>
|
---|
| 337 | /// <remarks>This is one of the most important casts in the ILNumerics framework. It changes temporary
|
---|
| 338 | /// arrays from temporary state to ('steady') ILArrays and persistent state. The inner storage is kept and used for
|
---|
| 339 | /// the new array. The new arrays can than get used multiple times in any function. In contrast to
|
---|
| 340 | /// that, ILRetArrays are disposed off after first use.</remarks>
|
---|
| 341 | public static implicit operator ILArray<ElementType>(ILRetArray<ElementType> A) {
|
---|
| 342 | if (object.Equals(A, null))
|
---|
| 343 | return null;
|
---|
| 344 | ILArray<ElementType> ret = new ILArray<ElementType>(A.GiveStorageAwayOrClone());
|
---|
| 345 | ILScope.Context.RegisterArray(ret);
|
---|
| 346 | return ret;
|
---|
| 347 | }
|
---|
| 348 | /// <summary>
|
---|
| 349 | /// "Persistence cast" make a steady array out of an input array
|
---|
| 350 | /// </summary>
|
---|
| 351 | /// <param name="A">Input array</param>
|
---|
| 352 | /// <returns>Steady ILArray, will survive multiple usages</returns>
|
---|
| 353 | /// <remarks>This is one of the most important casts in the ILNumerics framework. It changes temporary
|
---|
| 354 | /// arrays from temporary state to ('steady') ILArrays and persistent state. The inner storage is kept and used for
|
---|
| 355 | /// the new array. The new arrays can than get used multiple times in any function. In contrast to
|
---|
| 356 | /// that, ILRetArrays are disposed off after first use.</remarks>
|
---|
| 357 | public static implicit operator ILArray<ElementType>(ILInArray<ElementType> A) {
|
---|
| 358 | if (object.Equals(A, null))
|
---|
| 359 | return null;
|
---|
| 360 | ILArray < ElementType > ret = new ILArray<ElementType>(new ILDenseStorage<ElementType>(
|
---|
| 361 | A.Storage.GetDataArray(), A.Size));
|
---|
| 362 | ILScope.Context.RegisterArray(ret);
|
---|
| 363 | return ret;
|
---|
| 364 | }
|
---|
| 365 | /// <summary>
|
---|
| 366 | /// "Persistence cast" make a steady array out of an input array
|
---|
| 367 | /// </summary>
|
---|
| 368 | /// <param name="A">Input array</param>
|
---|
| 369 | /// <returns>Steady ILArray, will survive multiple usages</returns>
|
---|
| 370 | /// <remarks>This is one of the most important casts in the ILNumerics framework. It changes temporary
|
---|
| 371 | /// arrays from temporary state to ('steady') ILArrays and persistent state. The inner storage is kept and used for
|
---|
| 372 | /// the new array. The new arrays can than get used multiple times in any function. In contrast to
|
---|
| 373 | /// that, ILRetArrays are disposed off after first use.</remarks>
|
---|
| 374 | public static implicit operator ILArray<ElementType>(ILOutArray<ElementType> A) {
|
---|
| 375 | if (object.Equals(A, null))
|
---|
| 376 | return null;
|
---|
| 377 | ILArray<ElementType> ret = new ILArray<ElementType>(new ILDenseStorage<ElementType>(
|
---|
| 378 | A.Storage.GetDataArray(), A.Size));
|
---|
| 379 | ILScope.Context.RegisterArray(ret);
|
---|
| 380 | return ret;
|
---|
| 381 | }
|
---|
| 382 | /// <summary>
|
---|
| 383 | /// Convert dense array to scalar temporary cell
|
---|
| 384 | /// </summary>
|
---|
| 385 | /// <param name="A">Input array</param>
|
---|
| 386 | /// <returns>Scalar cell having the only element with a clone of array</returns>
|
---|
| 387 | public static implicit operator ILRetCell(ILArray<ElementType> A) {
|
---|
| 388 | using (ILScope.Enter(A)) {
|
---|
| 389 | ILRetCell ret = new ILRetCell(ILSize.Scalar1_1, A.Storage);
|
---|
| 390 | ret.Storage.FromImplicitCast = true;
|
---|
| 391 | return ret;
|
---|
| 392 | }
|
---|
| 393 | }
|
---|
| 394 | #endregion
|
---|
| 395 | #endregion
|
---|
| 396 |
|
---|
| 397 | #region memory management
|
---|
| 398 | /// <summary>
|
---|
| 399 | /// Assign another array to this array variable. This is an optional, yet more efficient alternative to using '='
|
---|
| 400 | /// </summary>
|
---|
| 401 | /// <param name="value">New array</param>
|
---|
| 402 | /// <remarks>By assigning to this property, this array is immediately released to the memory pool and replaced by the new array. In difference to that,
|
---|
| 403 | /// by using the common '=' assignment operator, the array is released only at the time, the current
|
---|
| 404 | /// <see cref="ILNumerics.ILScope.Enter"/> block is left. Therefeore, prefere this method, if a
|
---|
| 405 | /// smaller memory pool is crucial. However, for variables of type <c>ILArray</c>, regular assignments are allowed as well.</remarks>
|
---|
| 406 | /// <seealso cref="ILNumerics.ILOutArray{T}.Assign"/>
|
---|
| 407 | public ILRetArray<ElementType> a {
|
---|
| 408 | set { Assign(value); }
|
---|
| 409 | get { return this.C; }
|
---|
| 410 | }
|
---|
| 411 | /// <summary>
|
---|
| 412 | /// Assign another array to this array variable. This is an optional, yet more efficient alternative to '='
|
---|
| 413 | /// </summary>
|
---|
| 414 | /// <param name="value">New array</param>
|
---|
| 415 | /// <remarks>By using this method, this array is immediately released to the memory pool and replaced by the new array. In difference to that,
|
---|
| 416 | /// by using the common '=' assignment operator, the array is released only at the time, the current
|
---|
| 417 | /// <see cref="ILNumerics.ILScope.Enter"/> block is left. Therefeore, prefere this method, if a
|
---|
| 418 | /// smaller memory pool is crucial.</remarks>
|
---|
| 419 | /// <seealso cref="ILNumerics.ILOutArray{T}.Assign"/>
|
---|
| 420 | public void Assign(ILRetArray<ElementType> value) {
|
---|
| 421 | if (!IsDisposed)
|
---|
| 422 | Storage.Dispose();
|
---|
| 423 | m_storage = value.GiveStorageAwayOrClone();
|
---|
| 424 | //ILScope.Context.RegisterArray(this);
|
---|
| 425 | }
|
---|
| 426 | internal override bool EnterScope() {
|
---|
| 427 | return false;
|
---|
| 428 | }
|
---|
| 429 | #endregion
|
---|
| 430 |
|
---|
| 431 | #region mutability + indexer
|
---|
| 432 | /// <summary>
|
---|
| 433 | /// Set single value to element at index specified
|
---|
| 434 | /// </summary>
|
---|
| 435 | /// <param name="value">New value</param>
|
---|
| 436 | /// <param name="idx">Index of element to be altered</param>
|
---|
| 437 | public void SetValue(ElementType value, params int[] idx) {
|
---|
| 438 | Storage.SetValueTyped(value, idx);
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | /// <summary>
|
---|
| 442 | /// Alter range of this array
|
---|
| 443 | /// </summary>
|
---|
| 444 | /// <param name="value">Array with new values</param>
|
---|
| 445 | /// <param name="range">Range specification</param>
|
---|
| 446 | public void SetRange(ILInArray<ElementType> value, params ILBaseArray[] range) {
|
---|
| 447 | using (ILScope.Enter(value))
|
---|
| 448 | using (ILScope.Enter(range)) {
|
---|
| 449 | if (object.Equals(value, null)) {
|
---|
| 450 | Storage.IndexSubrange(null, range);
|
---|
| 451 | } else {
|
---|
| 452 | Storage.IndexSubrange(value.Storage, range);
|
---|
| 453 | }
|
---|
| 454 | }
|
---|
| 455 | }
|
---|
| 456 | /// <summary>
|
---|
| 457 | /// Subarray creation/ manipulation/ deletion
|
---|
| 458 | /// </summary>
|
---|
| 459 | /// <param name="range">Range specification, defining the size of the subarray</param>
|
---|
| 460 | /// <returns>Subarray as copy of this array</returns>
|
---|
| 461 | public ILRetArray<ElementType> this[params ILBaseArray[] range] {
|
---|
| 462 | get {
|
---|
| 463 | using (ILScope.Enter(range))
|
---|
| 464 | return new ILRetArray<ElementType>(Storage.Subarray(range));
|
---|
| 465 | }
|
---|
| 466 | set {
|
---|
| 467 | SetRange(value, range);
|
---|
| 468 | }
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | #endregion
|
---|
| 472 |
|
---|
| 473 | }
|
---|
| 474 | }
|
---|