///
/// 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 System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.CompilerServices;
using ILNumerics.Storage;
using ILNumerics.Misc;
using ILNumerics.Exceptions;
namespace ILNumerics {
///
/// Rectangular array, used as input parameter only
///
/// Inner type. This will mostly be a system numeric type or a
/// complex floating point type.
/// This class extends the primary ILArray by optimizing its behavior for the case when used as an
/// input parameter to functions.When writing your own function all input parameters should be of type ILInArray.
/// Similary, all return types should be of type and all "out" parameters of type .
/// Other than being used to transfer arguments into functions, ILInArray should not be used.
///
///
///
///
[Serializable]
public sealed partial class ILInArray : ILDenseArray {
private static readonly bool s_isTempArray = true;
#region constructors
///
/// create new ILInArray, specify (dense) storage
///
///
internal ILInArray(ILDenseStorage storage)
: base(storage, s_isTempArray) {
}
///
/// create new ILInArray, specify dimensions
///
///
internal ILInArray(ILSize dimensions)
: base(new ILDenseStorage(dimensions), s_isTempArray) {
}
///
/// create new ILInArray from System.Array
///
/// System.Array
/// dimension specifier
internal ILInArray(ElementType[] elements, ILSize dimensions) :
base(new ILDenseStorage(elements, dimensions), s_isTempArray) {
}
///
/// create new ILInArray from System.Array
///
/// System.Array
/// dimension specifier
internal ILInArray(ElementType[] elements, params int[] dimensions) :
base(new ILDenseStorage(elements, new ILSize(dimensions)), s_isTempArray) {
}
///
/// create new ILInArray from System.Array
///
/// variable length System.Array
internal ILInArray(params ElementType[] elements) :
base(new ILDenseStorage(elements, new ILSize(1, elements.Length)), s_isTempArray) {
}
#endregion
#region implicit cast operators
#region constructional operators
///
/// Implicitly convert scalar to array of size 1x1 (scalar).
///
/// System type of size scalar
/// New ILInArray of type ILInArray ]]> of size 1x1
/// holding the only element with value of val.
///
public static implicit operator ILInArray (ElementType val) {
ILInArray ret = new ILInArray(
new ILDenseStorage(
new ElementType[1] {val},
new ILSize(1,1)));
return ret;
}
///
/// Implicitly cast one dimensional System.Array to ILNumerics array (vector)
///
/// 1-dimensional system array, arbitrary type
/// ILNumerics array of same element type as elements of A.
/// Row vector. If A is null: empty array.
/// The System.Array A will directly be used for the new ILNumerics array!
/// No copy will be done! Make sure, not to reference A after this conversion!
public static implicit operator ILInArray (ElementType[] A) {
if (A == null) {
return null;
//ILDenseStorage dS = new ILDenseStorage(new ElementType[0], ILSize.Empty00);
//return new ILInArray(dS);
}
ILArray ret;
if (Settings.CreateRowVectorsByDefault)
ret = new ILInArray(A, 1, A.Length);
else
ret = new ILInArray(A, A.Length, 1);
if (Settings.AllowInArrayAssignments)
ILScope.Context.RegisterArray(ret);
return ret;
}
///
/// Implicitly convert n-dimensional System.Array to ILNumerics array
///
/// Arbitrarily sized System.Array
/// If A is null: empty array. Else: new ILNumerics array of the same size as A
/// The inner type of input array must match the requested type
/// . The resulting ILInArray will reflect all dimensions of
/// A. Elements of A will get copied to elements of output array (shallow copy).
/// if type of input does not match
/// ElementType
public static implicit operator ILInArray (Array A) {
if (A == null) {
return null;
}
if (A.Length == 0) {
return new ILInArray(ILSize.Empty00);
}
if (A.GetType().GetElementType() != typeof(ElementType))
throw new ILCastException("inner type of System.Array must match");
int[] dims = new int[A.Rank];
ElementType[] retArr = ILMemoryPool.Pool.New(A.Length);
int posArr = 0;
for (int i = 0; i < dims.Length; i++) {
dims[i] = A.GetLength(dims.Length - i - 1);
}
if (dims.Length == 1 && Settings.CreateRowVectorsByDefault) {
dims = new int[2] { 1, dims[0] };
}
foreach (ElementType item in A)
retArr[posArr++] = item;
ILInArray ret = new ILInArray(retArr,dims);
if (Settings.AllowInArrayAssignments)
ILScope.Context.RegisterArray(ret);
return ret;
}
///
/// Implicitly cast two dimensional System.Array to ILNumerics array
///
/// 2-dimensional System.Array
/// If A is null: empty array. ILNumerics array of same size and type as A otherwise.
public static implicit operator ILInArray(ElementType[,] A) {
if (A == null) {
return null;
}
if (A.Length == 0) {
return new ILInArray(ILSize.Empty00);
}
int[] dims = new int[2];
ElementType[] retArr = ILMemoryPool.Pool.New(A.Length);
int posArr = 0;
for (int i = 0; i < 2; i++) {
dims[i] = A.GetLength(dims.Length - i - 1);
}
foreach (ElementType item in A)
retArr[posArr++] = item;
ILInArray ret = new ILInArray(retArr, dims);
if (Settings.AllowInArrayAssignments)
ILScope.Context.RegisterArray(ret);
return ret;
}
///
/// Implicitly casts three dimensional System.Array to ILNumerics array
///
/// 3-dimensional System.Array
/// If A is null: empty array. ILNumerics array of same size and type as A otherwise.
public static implicit operator ILInArray(ElementType[,,] A) {
if (A == null) {
return null;
}
if (A.Length == 0) {
return new ILInArray(ILSize.Empty00);
}
int[] dims = new int[3];
ElementType[] retArr = ILMemoryPool.Pool.New(A.Length);
int posArr = 0;
for (int i = 0; i < 3; i++) {
dims[i] = A.GetLength(dims.Length - i - 1);
}
foreach (ElementType item in A)
retArr[posArr++] = item;
ILInArray ret = new ILInArray(retArr, dims);
if (Settings.AllowInArrayAssignments)
ILScope.Context.RegisterArray(ret);
return ret;
}
///
/// Implicitly cast ILInArray to ILRetCell
///
/// Input array
/// 1x1 cell holding the array
public static implicit operator ILRetCell(ILInArray array) {
using (ILScope.Enter(array)) {
ILRetCell ret = new ILRetCell(ILSize.Scalar1_1, array.Storage);
ret.Storage.FromImplicitCast = true;
return ret;
}
}
#endregion
#region conversional operators
///
/// Convert temporary to input parameter array
///
/// Temp array
/// Input parameter array, will survive the current scope only
public static implicit operator ILInArray(ILRetArray array) {
if (object.Equals(array, null))
return null;
ILInArray ret = new ILInArray(array.GiveStorageAwayOrClone());
if (Settings.AllowInArrayAssignments)
ILScope.Context.RegisterArray(ret);
return ret;
}
///
/// Convert persistent to input parameter array
///
/// Persistent array
/// Input parameter array, will survive the current scope only
public static implicit operator ILInArray(ILArray array) {
if (object.Equals(array, null))
return null;
ILInArray ret = new ILInArray(new ILDenseStorage(
array.Storage.GetDataArray(), array.Size));
if (Settings.AllowInArrayAssignments)
ILScope.Context.RegisterArray(ret);
return ret;
}
///
/// Convert output paramter array to input parameter array
///
/// Output parameter array
/// Input parameter array, will survive the current scope only
public static implicit operator ILInArray(ILOutArray array) {
if (object.Equals(array, null))
return null;
ILInArray ret = new ILInArray(new ILDenseStorage(
array.Storage.GetDataArray(), array.Size));
if (Settings.AllowInArrayAssignments)
ILScope.Context.RegisterArray(ret);
return ret;
}
#endregion
#endregion
#region indexer + memory management
///
/// Subarray creation/ manipulation/ deletion
///
/// Range specification, defining the size of the subarray
/// Subarray as copy of this array
public ILRetArray this[params ILBaseArray[] range] {
get {
using (ILScope.Enter(this))
using (ILScope.Enter(range))
return new ILRetArray(Storage.Subarray(range));
}
}
internal override void LeaveScope() {
m_scopeCounter--;
if (m_scopeCounter <= 0) {
Dispose();
}
}
#endregion
}
}