/// <summary>
 /// Subarray access for get/set/removal
 /// </summary>
 /// <param name="slices">Array of IronPython Slice objects</param>
 /// <returns>Reference pointing to the elements of this array specified by range. If used for removal:
 /// the array will be changed to a referencing array having the selected parts removed.</returns>
 public ILArray <BaseT> this[params IronPython.Runtime.Slice[] slices]
 {
     get
     {
         return((ILArray <BaseT>)SubarrayPython((object[])slices));
     }
     set
     {
         if (Object.ReferenceEquals(value, null) || value.IsEmpty)
         {
             ILDimension oldDim  = m_dimensions;
             ILDimension newdim  = null;
             int         dimIdx  = -1;
             int[]       indices = null;
             ExtractRemovalParameter((object[])slices, out dimIdx, out indices, out newdim);
             if (indices.Length == 0)
             {
                 return;
             }
             Reshape(newdim);
             try {
                 Remove(dimIdx, indices);
             } catch (ILException e) {
                 // rollback:
                 Reshape(oldDim);
                 throw e;
             }
         }
         else
         {
             SetRange(new ILRange(m_dimensions, RangeSide.Left, (object[])slices), value);
         }
     }
 }
        /// <summary>
        /// output information about this ILLogicalArray
        /// </summary>
        /// <param name="s">predefined string, to be used as prefix for output</param>
        /// <param name="maxLength">number of characters in lines of output</param>
        /// <returns>string representation of this ILLogicalArray</returns>
        public override String ToString(String s, int maxLength)
        {
            StringBuilder sb = new StringBuilder(s);
            // print dimension string
            ILDimension dim = Dimensions;

            sb.Append(String.Format("\r\nILLogicalArray: '{0}'\r\n",
                                    (Name.Length > 0) ? Name : this.GetHashCode().ToString()));
            sb.Append("[");
            for (int t = 0; t < dim.NumberOfDimensions; t++)
            {
                sb.Append(dim[t]);
                if (t < dim.NumberOfDimensions - 1)
                {
                    sb.Append(",");
                }
            }
            sb.Append("] ");
            // status flags
            if (IsReference)
            {
                sb.Append("Ref(" + GetNumberOfReferences() + ") ");
            }
            else
            {
                sb.Append("Phys. ");
            }
            sb.Append(ValuesToString(maxLength));
            return(sb.ToString());
        }
        public ILArray(IronPython.Runtime.List list)
        {
            List <int> size = new List <int>();
            Type       type = typeof(double);

            PythonListHelper.GetListDimensionsAndType(ref size, list, ref type);
            if (!PythonListHelper.CheckList(ref size, list, 0, ref type))
            {
                throw new ArgumentException("List is not rectangular or not of constant type!");
            }
            if (type != typeof(BaseT))
            {
                throw new ArgumentException("Array type does not match list type!");
            }
            try
            {
                m_dimensions = new ILDimension(true, size.ToArray());
                bool dummy;
                m_data = ILMemoryPool.Pool.New <BaseT>(m_dimensions.NumberOfElements, true, out dummy);
                m_name = "";
                IncreaseReference();
            }
            catch (Exception e)
            {
                throw new Exception("Error creating new ILArray object.", e);
            }
            int[] indices = new int[size.Count];
            FillILArrayFromList <BaseT>(ref size, list, 0, ref type, ref m_data, 0, 1);
        }
        /*!HC:TYPELIST:
         * <hycalper>
         * <type>
         * <source	locate="after">
         * inArr1
         * </source>
         * <destination>byte</destination>
         * <destination>complex</destination>
         * <destination>complex</destination>
         * <destination>double</destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  inCls1
         * </source>
         * <destination><![CDATA[ILArray<byte>]]></destination>
         * <destination><![CDATA[ILArray<complex>]]></destination>
         * <destination><![CDATA[ILArray<complex>]]></destination>
         * <destination><![CDATA[ILArray<double>]]></destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  outCls1
         * </source>
         * <destination><![CDATA[ILArray<byte>]]></destination>
         * <destination><![CDATA[ILArray<complex>]]></destination>
         * <destination><![CDATA[ILArray<double>]]></destination>
         * <destination><![CDATA[ILArray<complex>]]></destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  outArr1
         * </source>
         * <destination>byte</destination>
         * <destination>complex</destination>
         * <destination>double</destination>
         * <destination>complex</destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  delegate_unary
         * </source>
         * <destination>ILByteFunctionByte</destination>
         * <destination>ILComplexFunctionComplex</destination>
         * <destination>ILDoubleFunctionComplex</destination>
         * <destination>ILComplexFunctionDouble</destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  unaryop
         * </source>
         * <destination>ByteOperatorByte</destination>
         * <destination>ComplexOperatorComplex</destination>
         * <destination>DoubleOperatorComplex</destination>
         * <destination>ComplexOperatorDouble</destination>
         * </type>
         * </hycalper>
         */
        /// <summary>
        /// Applys the function (delegate) given to all elements of the storage
        /// </summary>
        /// <param name="inArray">storage array to apply the function to</param>
        /// <param name="operation">operation to apply to the elements of inArray. This
        /// acts like a function pointer.</param>
        /// <returns>new <![CDATA[ /*!HC:outCls1*/ ILArray<double> ]]> with result of operation</returns>
        /// <remarks> the values of inArray will not be altered.</remarks>
        private static /*!HC:outCls1*/ ILArray <double> /*!HC:unaryop*/ DoubleOperatorDouble(/*!HC:inCls1*/ ILArray <double> inArray,
                                                                                             /*!HC:delegate_unary*/ ILDoubleFunctionDouble operation)
        {
            ILDimension inDim = inArray.Dimensions;

            /*!HC:outArr1*/ double [] retDblArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            retDblArr = new /*!HC:outArr1*/ double [newLength];
            int leadDimLen = inDim [0];

            unsafe
            {
                fixed(/*!HC:outArr1*/ double *pOutArr = retDblArr)
                fixed(/*!HC:inArr1*/ double *pInArr = inArray.m_data)
                {
                    /*!HC:outArr1*/ double *lastElement = pOutArr + retDblArr.Length;
                    /*!HC:outArr1*/ double *tmpOut      = pOutArr;
                    /*!HC:inArr1*/ double * tmpIn       = pInArr;

                    while (tmpOut < lastElement)
                    {
                        *tmpOut++ = operation(*tmpIn++);
                    }
                }
            }

            return(new /*!HC:outCls1*/ ILArray <double> (retDblArr, inDim.ToIntArray()));
        }
        /// <summary>
        /// normal random distributed n-dimensional array elements
        /// </summary>
        /// <param name="dimensions">int array or single int paramters specifying
        /// dimensions for new array to be created</param>
        /// <returns>n-dimensional array filled with random numbers.</returns>
        /// <remarks>the elements lay within the range 0.0 ... 1.0 and are choosen to be normally
        /// distributed.</remarks>
        public static ILArray <double> randn(params int[] dimensions)
        {
            ILDimension dim;

            if (dimensions.Length == 1)
            {
                dim = new ILDimension(dimensions[0], dimensions[0]);
            }
            else
            {
                dim = new ILDimension(dimensions);
            }
            if (m_nrandomGenerator == null)
            {
                m_nrandomGenerator = new ILNRandom(Environment.TickCount);
            }
            double[] data = ILMemoryPool.Pool.New <double>(dim.NumberOfElements);
            unsafe
            {
                fixed(double *pRetArray = data)
                {
                    double *pCurIdx      = pRetArray;
                    double *pLastElement = pCurIdx + dim.NumberOfElements;

                    while (pCurIdx < pLastElement)
                    {
                        *pCurIdx++ = m_nrandomGenerator.NextDouble();
                    }
                }
            }
            return(new ILArray <double>(data, dim));
        }
        /// <summary>
        /// create physical copy from this physical array and shift dimensions
        /// </summary>
        /// <param name="indices">sequential indices. may be of any size</param>
        /// <param name="shift">number of dimensions to shift the result</param>
        /// <returns>physical copy of elements addressed by indices.shape of shifted indices</returns>
        private ILArray <BaseT> CreatePhysicalSubarrayFromPhysicalSequentialShifted(ILArray <byte> indices, int shift)
        {
            if (shift < 0)
            {
                shift = 0;
            }
            if (shift > indices.m_dimensions.NumberOfDimensions)
            {
                shift %= indices.m_dimensions.NumberOfDimensions;
            }
            ILDimension dim    = indices.m_dimensions.GetShifted(shift);
            int         outLen = dim.NumberOfElements;

            BaseT[] outdata = ILMemoryPool.Pool.New <BaseT> (outLen--);
            int     outPos = 0, outInc = dim.SequentialIndexDistance(dim.NumberOfDimensions - shift);

            byte [] indData = indices.m_data;
            for (int i = 0; i < outLen; i++)
            {
                outdata[outPos] = m_data[(int)indData[i]];
                outPos         += outInc;
                if (outPos > outLen)
                {
                    outPos %= outLen;
                }
            }
            outdata[outLen] = m_data[(int)indData[outLen]];

            return(new ILArray <BaseT>(outdata, dim));
        }
        /// <summary>
        /// Applys the function (delegate) given to all elements of the storage
        /// </summary>
        /// <param name="inArray">storage array to apply the function to</param>
        /// <param name="operation">operation to apply to the elements of inArray. This
        /// acts like a function pointer.</param>
        /// <returns>new <![CDATA[  ILArray<complex> ]]> with result of operation</returns>
        /// <remarks> the values of inArray will not be altered.</remarks>
        private static ILArray <complex> ComplexOperatorComplex(ILArray <complex> inArray,
                                                                ILComplexFunctionComplex operation)
        {
            ILDimension inDim = inArray.Dimensions;

            complex [] retDblArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            retDblArr = new  complex [newLength];
            int leadDimLen = inDim [0];

            unsafe
            {
                fixed(complex *pOutArr = retDblArr)
                fixed(complex * pInArr = inArray.m_data)
                {
                    complex *lastElement = pOutArr + retDblArr.Length;
                    complex *tmpOut      = pOutArr;
                    complex *tmpIn       = pInArr;

                    while (tmpOut < lastElement)
                    {
                        *tmpOut++ = operation(*tmpIn++);
                    }
                }
            }

            return(new  ILArray <complex> (retDblArr, inDim.ToIntArray()));
        }
        /// <summary>
        /// Applys the function (delegate) given to all elements of the storage
        /// </summary>
        /// <param name="inArray">storage array to apply the function to</param>
        /// <param name="operation">operation to apply to the elements of inArray. This
        /// acts like a function pointer.</param>
        /// <returns>new <![CDATA[  ILArray<byte> ]]> with result of operation</returns>
        /// <remarks> the values of inArray will not be altered.</remarks>
        private static ILArray <byte> ByteOperatorByte(ILArray <byte> inArray,
                                                       ILByteFunctionByte operation)
        {
            ILDimension inDim = inArray.Dimensions;

            byte [] retDblArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            retDblArr = new  byte [newLength];
            int leadDimLen = inDim [0];

            unsafe
            {
                fixed(byte *pOutArr = retDblArr)
                fixed(byte *pInArr = inArray.m_data)
                {
                    byte *lastElement = pOutArr + retDblArr.Length;
                    byte *tmpOut      = pOutArr;
                    byte *tmpIn       = pInArr;

                    while (tmpOut < lastElement)
                    {
                        *tmpOut++ = operation(*tmpIn++);
                    }
                }
            }

            return(new  ILArray <byte> (retDblArr, inDim.ToIntArray()));
        }
 /// <summary>
 /// Constructor creating ILLogicalArray, predefined storage (fast version)
 /// </summary>
 /// <param name="data">predefined storage elements. The array will directly be used
 /// as underlying storage. No copy will be made! </param>
 /// <param name="dimension">Dimensions specification.</param>
 /// <param name="nonZeroCount">number of nonzero elements in <paramref name="data"/>.
 /// Providing this parameter prevents from counting the 'true' elements (again). </param>
 public ILLogicalArray(byte[] data, ILDimension dimension, int nonZeroCount)
     : base(data, dimension)
 {
     if (nonZeroCount < 0)
     {
         throw new ILNumerics.Exceptions.ILArgumentException("invalid number of non-zero-elements given!");
     }
     m_numberNonZero = nonZeroCount;
 }
Beispiel #10
0
        /// <summary>
        /// Create N-dimensional array with elements counting from 1.
        /// </summary>
        /// <param name="dimensions">Variable int array with dimension specification</param>
        /// <returns>Array with elements counting from 1 ... dimensions.NumberOfElements</returns>
        /// <remarks>This function may be used for the convenient creation of arrays for testing purposes.</remarks>
        public static ILArray <double> counter(params int[] dimensions)
        {
            ILDimension dimRet = new ILDimension(dimensions);

            double [] retArr = ILMemoryPool.Pool.New <double>(dimRet.NumberOfElements);
            for (int i = 0; i < retArr.Length;)
            {
                retArr[i] = ++i;
            }
            return(new ILArray <double>(retArr, dimRet));
        }
Beispiel #11
0
        /// <summary>
        /// Dimension represented by this ILIndexOffset
        /// </summary>
        /// <returns>Dimension object</returns>
        /// <remarks>This function will introduce a performance penalty,
        /// since the dimension object will get reconstructed every time called.
        /// One should therefore cache the dimension externally, if needed.</remarks>
        public ILDimension GetDimensions()
        {
            ILDimension ret;

            int[] dims = new int[m_idxOffset.Length];
            for (int d = m_idxOffset.Length; d-- > 0;)
            {
                dims[d] = m_idxOffset[d].Length;
            }
            ret = new ILDimension(dims);
            return(ret);
        }
        /// <summary>
        /// Create ILArray of specified size and type
        /// </summary>
        /// <param name="data">preallocated array with data. The array will
        /// directly be used for storage. No copy will be done.</param>
        /// <param name="dimensions">
        /// dimension specification. The ILDimension given must not be null and will
        /// directly be used for the new object! No copy will be made for it!
        /// </param>
        /// <remarks>The size parameter may not be null or empty!
        /// An exception will be thrown in this case. The dimensions will be trimmed
        /// before processing (removing non singleton dimensions from the end).
        /// Depending on the requested size an ILArray of the specified type and
        /// dimension will be created.
        /// </remarks>
        public ILArray(BaseT[] data, ILDimension dimensions)
        {
            System.Diagnostics.Debug.Assert(dimensions != null, "ILArray|(construct): " +
                                            "dimension must be specified!");

            m_dimensions = dimensions.Trim();
            if (data.Length != m_dimensions.NumberOfElements)
            {
                throw new ILArgumentException("ILArray.construct: Data array specifier must match "
                                              + "size of dimensions!");
            }
            m_data = data;
        }
        /// <summary>
        /// Create array innitialized with ones
        /// </summary>
        /// <param name="type">Numeric type specification. One value out of the types listed in the <see cred="ILNumerics.NumericType"/>
        /// enum.</param>
        /// <param name="dimensions">Dimension specification. At least one dimension must be specified.</param>
        /// <returns>ILArray&lt;BaseT&gt; of inner type corresponding to <paramref name="type"/> argument.</returns>
        /// <remarks>The array returned may be casted to the actual type accordingly afterwards.
        /// <para>
        /// <list type="number">
        /// <listheader>The following types are supported: </listheader>
        /// <item>Double</item>
        /// <item>Single</item>
        /// <item>Complex</item>
        /// <item>FComplex</item>
        /// <item>Byte</item>
        /// <item>Char</item>
        /// <item>Int16</item>
        /// <item>Int32</item>
        /// <item>Int64</item>
        /// <item>UInt16</item>
        /// <item>UInt32</item>
        /// <item>UInt64</item>
        /// </list>
        /// </para>
        /// </remarks>
        public static ILBaseArray ones(NumericType type, params int[] dimensions)
        {
            if (dimensions.Length < 1)
            {
                throw new ILArgumentException("ones: invalid dimension specified!");
            }
            switch (type)
            {
            case NumericType.Double:
                return(ones(dimensions));

            case NumericType.Complex:
                ILDimension dim = new ILDimension(dimensions);
                unsafe {
                    complex[] data = null;
                    data = new complex[dim.NumberOfElements];
                    fixed(complex *pD = data)
                    {
                        complex *pStart = pD;
                        complex *pEnd   = pD + data.Length;

                        while (pEnd > pStart)
                        {
                            *(--pEnd) = new complex(1.0, 1.0);
                        }
                    }

                    return(new ILArray <complex>(data, dimensions));
                }

            case NumericType.Byte:
                dim = new ILDimension(dimensions);
                unsafe {
                    byte[] data = null;
                    data = new byte[dim.NumberOfElements];
                    fixed(byte *pD = data)
                    {
                        byte *pStart = pD;
                        byte *pEnd   = pD + data.Length;

                        while (pEnd > pStart)
                        {
                            *(--pEnd) = 1;
                        }
                    }

                    return(new ILArray <byte>(data, dimensions));
                }
            }
            return(null);
        }
Beispiel #14
0
        /// <summary>
        /// invert elements of A, return result as out argument
        /// </summary>
        /// <param name="A"></param>
        /// <param name="outArray"></param>
        public static void /*!HC:HCFuncName*/ invert(/*!HC:inCls1*/ ILArray <double> A, /*!HC:inCls2*/ ILArray <double> outArray)
        {
            #region array + array
            ILDimension inDim = A.Dimensions;
            if (!inDim.IsSameSize(outArray.Dimensions))
            {
                throw new ILDimensionMismatchException();
            }
            int leadDim = 0, leadDimLen = inDim [0];
            // walk along the longest dimension (for performance reasons)
            for (int i = 1; i < inDim.NumberOfDimensions; i++)
            {
                if (leadDimLen < inDim [i])
                {
                    leadDimLen = inDim [i];
                    leadDim    = i;
                }
            }
            unsafe
            {
                fixed(/*!HC:inArr1*/ double *inA1 = A.m_data)
                fixed(/*!HC:inArr2*/ double *inA2 = outArray.m_data)
                {
                    /*!HC:inArr1*/ double *pInA1 = inA1;
                    /*!HC:inArr2*/ double *pInA2 = inA2;
                    int c = 0;
                    /*!HC:inArr2*/ double *outEnd = inA2 + outArray.m_data.Length;

                    if (A.IsReference)
                    {
                        while (pInA2 < outEnd)    //HC07
                        /*!HC:HCCompute07*/
                        {
                            *pInA2++ = /*!HC:outCast*/ /**/ (*(pInA1 + A.getBaseIndex(c++)) /*!HC:HCoperation*/ * -1.0);
                        }
                    }
                    else
                    {
                        while (pInA2 < outEnd)    //HC11
                        /*!HC:HCCompute11*/
                        {
                            *pInA2++ = /*!HC:outCast*/ /**/ (*pInA1++ /*HC:HCoperation*/ *(-1.0));
                        }
                    }
                }
                #endregion array + array
            }
            return;
        }
Beispiel #15
0
        /// <summary>
        /// invert elements of A, return result as out argument
        /// </summary>
        /// <param name="A"></param>
        /// <param name="outArray"></param>
        public static void  invert(ILArray <complex> A, ILArray <complex> outArray)
        {
            #region array + array
            ILDimension inDim = A.Dimensions;
            if (!inDim.IsSameSize(outArray.Dimensions))
            {
                throw new ILDimensionMismatchException();
            }
            int leadDim = 0, leadDimLen = inDim [0];
            // walk along the longest dimension (for performance reasons)
            for (int i = 1; i < inDim.NumberOfDimensions; i++)
            {
                if (leadDimLen < inDim [i])
                {
                    leadDimLen = inDim [i];
                    leadDim    = i;
                }
            }
            unsafe
            {
                fixed(complex *inA1 = A.m_data)
                fixed(complex * inA2 = outArray.m_data)
                {
                    complex *pInA1  = inA1;
                    complex *pInA2  = inA2;
                    int      c      = 0;
                    complex *outEnd = inA2 + outArray.m_data.Length;

                    if (A.IsReference)
                    {
                        while (pInA2 < outEnd)    //HC07

                        {
                            *pInA2++ = (*(pInA1 + A.getBaseIndex(c++)) * (-1.0));
                        }
                    }
                    else
                    {
                        while (pInA2 < outEnd)    //HC11

                        {
                            *pInA2++ = (*pInA1++ /*HC:*/ *(-1.0));
                        }
                    }
                }
                #endregion array + array
            }
            return;
        }
 /// <summary>
 ///		Create ILArray of type object and given name and size
 /// </summary>
 /// <param name="size">
 /// Variable length int array specifying the number and size of dimensions
 /// to be created.
 /// </param>
 /// <remarks>The size parameter may not be null or an empty array!
 /// An Exception will be thrown in this case. The dimensions will be trimmed
 /// before processing (removing non singleton dimensions from the end).
 /// Depending on the requested size an ILArray of the specified
 /// dimension will be created.
 /// </remarks>
 public ILArray(params int[] size)
 {
     if (size == null || size.Length == 0)
     {
         throw new ArgumentException("ILArray|(construct): dimension specification needed!");
     }
     try {
         m_dimensions = new ILDimension(true, size);
         bool dummy;
         m_data = ILMemoryPool.Pool.New <BaseT>(m_dimensions.NumberOfElements, true, out dummy);
         m_name = "";
     } catch (Exception e) {
         throw new Exception("Error creating new ILArray object.", e);
     }
 }
        /// <summary>
        /// create physical copy from this physical array
        /// </summary>
        /// <param name="indices">sequential indices. may be of any size</param>
        /// <returns>physical copy of elements addressed by indices.shape of indices</returns>
        private ILArray <BaseT> CreatePhysicalSubarrayFromPhysicalSequential(ILArray <byte> indices)
        {
            ILDimension dim    = indices.m_dimensions;
            int         outLen = dim.NumberOfElements;

            BaseT[] outdata = ILMemoryPool.Pool.New <BaseT> (outLen);

            byte [] indData = indices.m_data;
            for (int i = 0; i < outLen; i++)
            {
                outdata[i] = m_data[(int)indData[i]];
            }

            return(new ILArray <BaseT>(outdata, dim));
        }
 /// <summary>
 ///	Create ILArray of specified size and type
 /// </summary>
 /// <param name="size">
 /// Variable length integer array specifying the number and size of dimensions
 /// to be created.
 /// </param>
 /// <param name="data">preallocated array with data. The array will
 /// directly be used for storage. No copy will be done.</param>
 /// <remarks>The size parameter may not be null or empty!
 /// An exception will be thrown in this case. The dimensions will be trimmed
 /// before processing (removing non singleton dimensions from the end).
 /// Depending on the requested size an ILArray of the specified type and
 /// dimension will be created.
 /// </remarks>
 public ILArray(BaseT[] data, params int[] size)
 {
     if (size == null || size.Length == 0)
     {
         m_dimensions = new ILDimension(1, data.Length);
     }
     else
     {
         m_dimensions = new ILDimension(true, size);
     }
     if (data.Length != m_dimensions.NumberOfElements)
     {
         throw new ILArgumentException("size of data must match dimensions!");
     }
     m_data = data;
 }
 /// <summary>
 /// create scalar or row vector from values explitely given
 /// </summary>
 /// <param name="vector_elements">elements. </param>
 /// <remarks><para>The elements may are given as comma seperated list or as predefined System.Array
 /// of type 'BaseT'. In this case the System.Array object given will directly be used as storage
 /// for the newly created ILArray.</para>
 /// <para>If vector_elements is null, an empty ILArray will be created.</para></remarks>
 public ILArray(BaseT[] vector_elements)
 {
     if (object.Equals(vector_elements, null))
     {
         m_data       = new BaseT[0];
         m_dimensions = new ILDimension(new int[2] {
             0, 0
         });
     }
     else
     {
         m_data       = vector_elements;
         m_dimensions = new ILDimension(new int[2] {
             1, m_data.Length
         });
     }
 }
Beispiel #20
0
        //////////////////////////////   ONES / ZEROS ... ////////////////////////////////
        public static ILArray <double> Ones(params int[] dimensions)
        {
            ILDimension dim = new ILDimension(dimensions);

            double[]         data = new double[dim.NumberOfElements];
            ILArray <double> ret  = new ILArray <double>(data, dimensions);

            return(Set(ret, 1.0));

            {            /* unsafe {
                          *     fixed(double* pData = data) {
                          *             double* ende = pData + data.Length;
                          *             double* curr = pData;
                          *             while (curr < ende)
                          * curr++ = 1.0;
                          * }*/
            }
        }
Beispiel #21
0
        /// <summary>
        /// random n-dimensional array elements
        /// </summary>
        /// <param name="dimensions">int array or single int paramters specifying
        /// dimensions for new array to be created</param>
        /// <returns>n-dimensional array filled with random numbers.</returns>
        /// <remarks>the elements lay within the range 0.0 ... 1.0 and are uniformly
        /// distributed.</remarks>
        public static ILArray <double> Rand(params int[] dimensions)
        {
            ILDimension dim = new ILDimension(dimensions);

            double[] data = new double[dim.NumberOfElements];
            unsafe
            {
                fixed(double *pRetArray = data)
                {
                    double *pCurIdx      = pRetArray;
                    double *pLastElement = pCurIdx + dim.NumberOfElements;
                    Random  r            = new Random((int)System.Environment.TickCount);

                    while (pCurIdx < pLastElement)
                    {
                        *pCurIdx++ = r.NextDouble();
                    }
                }
            }
            return(new ILArray <double>(data, dimensions));
        }
Beispiel #22
0
        /// <summary>
        /// Create N-dimensional array with elements counting from 1 and specified interval.
        /// </summary>
        /// <param name="start">Initial value.</param>
        /// <param name="increment">Increment for each element.</param>
        /// <param name="dimensions">Variable int array with dimension specification.</param>
        /// <returns>Array with elements counting from 1 ... dimensions.NumberOfElements.</returns>
        /// <remarks><para>Counter is a fast alternative to the creation of arrays via <see cref="ILNumerics.BuiltInFunctions.ILMath.ones(int[])"/>
        /// or <see cref="ILNumerics.BuiltInFunctions.ILMath.zeros(int[])"/> with subsequent modification.
        /// Counter is more general. It can create arrays of all constants (zeros, ones, twos ...) if <paramref name="increment"/> is 0, regularly
        /// incrementing elements if <paramref name="increment"/> is positive or negative.</para>
        /// This function may also be used for the convenient creation of arrays for testing purposes.
        /// <para>Keep in mind: in order to distingush this function from the overloaded version <see cref="ILNumerics.BuiltInFunctions.ILMath.counter(int[])"/>
        /// you need to specify parameters <paramref name="start"/> and <paramref name="increments"/> explicitly as double value:</para>
        /// <example><code>
        /// // This will create elements counting from 1...24:
        /// <![CDATA[ILArray<double>]]> A = ILMath.counter(4,3,2);
        /// // This will create elements counting from 1...48 with intervals of 2.0:
        /// <![CDATA[ILArray<double>]]> A = ILMath.counter(1.0,2.0,4,3,2);
        /// // ... but this will (by mistake) call the wrong function:
        /// <![CDATA[ILArray<double>]]> A = ILMath.counter(1,2,4,3,2);
        /// // ... and therefore create an array of size [1,2,4,3,2] with elements counting from one!
        /// </code></example>  </remarks>
        public static ILArray <double> counter(double start, double increment, params int[] dimensions)
        {
            ILDimension dimRet = new ILDimension(dimensions);

            double [] retArr = ILMemoryPool.Pool.New <double>(dimRet.NumberOfElements);
            double    val    = start;

            unsafe
            {
                fixed(double *pRetArr = retArr)
                {
                    double *pRetArray = pRetArr;
                    double *pEnd      = pRetArr + retArr.Length;

                    while (pRetArray < pEnd)
                    {
                        *pRetArray++ = val;
                        val += increment;
                    }
                }
            }
            return(new ILArray <double>(retArr, dimRet));
        }
Beispiel #23
0
// DO NOT EDIT INSIDE THIS REGION !! CHANGES WILL BE LOST !!
        /// <summary>
        /// invert elements of A, return result as out argument
        /// </summary>
        /// <param name="A"></param>
        /// <param name="outArray"></param>
        public static void  invert(ILArray <byte> A, ILArray <byte> outArray)
        {
            #region array + array
            ILDimension inDim = A.Dimensions;
            if (!inDim.IsSameSize(outArray.Dimensions))
            {
                throw new ILDimensionMismatchException();
            }
            int leadDim = 0, leadDimLen = inDim [0];
            // walk along the longest dimension (for performance reasons)
            for (int i = 1; i < inDim.NumberOfDimensions; i++)
            {
                if (leadDimLen < inDim [i])
                {
                    leadDimLen = inDim [i];
                    leadDim    = i;
                }
            }
            unsafe
            {
                fixed(byte *inA1 = A.m_data)
                fixed(byte *inA2 = outArray.m_data)
                {
                    byte *pInA1  = inA1;
                    byte *pInA2  = inA2;
                    byte *outEnd = inA2 + outArray.m_data.Length;

                    while (pInA2 < outEnd)    //HC11

                    {
                        *pInA2++ = (byte)(*pInA1++ /*HC:*/ *(-1));
                    }
                }
                #endregion array + array
            }
            return;
        }
Beispiel #24
0
// DO NOT EDIT INSIDE THIS REGION !! CHANGES WILL BE LOST !!

        /// <summary>
        /// operate on elements of both storages by the given function -> relational operations
        /// </summary>
        /// <param name="inArray1">First storage array</param>
        /// <param name="inArray2">Second storage array</param>
        /// <param name="operation">operation to apply to the elements of inArray. This
        /// acts like a function pointer.</param>
        /// <returns><![CDATA[  ILArray<complex> ]]> with result of operation for corresponding
        /// elements of both arrays.</returns>
        /// <remarks>The values of inArray1 nor inArray2 will not be altered.The dimensions
        /// of both arrays must match.</remarks>
        private static ILArray <complex> ComplexOperatorComplexComplex(ILArray <complex> inArray1, ILArray <complex> inArray2,
                                                                       ILComplexFunctionComplexComplex operation)
        {
            ILDimension inDim = inArray1.Dimensions;

            if (!inDim.IsSameSize(inArray2.Dimensions))
            {
                throw new ILDimensionMismatchException();
            }
            complex [] retSystemArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            retSystemArr = new  complex [newLength];
            int leadDimLen = inDim [0];

            // physical -> pointer arithmetic
            unsafe
            {
                fixed(complex *pInArr1 = inArray1.m_data)
                fixed(complex * pInArr2 = inArray2.m_data)
                fixed(complex * pOutArr = retSystemArr)
                {
                    complex *poutarr = pOutArr;
                    complex *poutend = poutarr + newLength;
                    complex *pIn1    = pInArr1;
                    complex *pIn2    = pInArr2;

                    while (poutarr < poutend)
                    {
                        *poutarr++ = operation(*pIn1++, *pIn2++);
                    }
                }
            }

            return(new  ILArray <complex> (retSystemArr, inDim.ToIntArray()));
        }
Beispiel #25
0
        /// <summary>
        /// operate on elements of both storages by the given function -> relational operations
        /// </summary>
        /// <param name="inArray1">First storage array</param>
        /// <param name="inArray2">Second storage array</param>
        /// <param name="operation">operation to apply to the elements of inArray. This
        /// acts like a function pointer.</param>
        /// <returns><![CDATA[  ILArray<byte> ]]> with result of operation for corresponding
        /// elements of both arrays.</returns>
        /// <remarks>The values of inArray1 nor inArray2 will not be altered.The dimensions
        /// of both arrays must match.</remarks>
        private static ILArray <byte> ByteOperatorByteByte(ILArray <byte> inArray1, ILArray <byte> inArray2,
                                                           ILByteFunctionByteByte operation)
        {
            ILDimension inDim = inArray1.Dimensions;

            if (!inDim.IsSameSize(inArray2.Dimensions))
            {
                throw new ILDimensionMismatchException();
            }
            byte [] retSystemArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            retSystemArr = new  byte [newLength];
            int leadDimLen = inDim [0];

            // physical -> pointer arithmetic
            unsafe
            {
                fixed(byte *pInArr1 = inArray1.m_data)
                fixed(byte *pInArr2 = inArray2.m_data)
                fixed(byte *pOutArr = retSystemArr)
                {
                    byte *poutarr = pOutArr;
                    byte *poutend = poutarr + newLength;
                    byte *pIn1    = pInArr1;
                    byte *pIn2    = pInArr2;

                    while (poutarr < poutend)
                    {
                        *poutarr++ = operation(*pIn1++, *pIn2++);
                    }
                }
            }

            return(new  ILArray <byte> (retSystemArr, inDim.ToIntArray()));
        }
Beispiel #26
0
        /*!HC:TYPELIST:
         * <hycalper>
         * <type>
         * <source	locate="after">
         * inArr1
         * </source>
         * <destination>byte</destination>
         * <destination>complex</destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  inArr2
         * </source>
         * <destination>byte</destination>
         * <destination>complex</destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  inCls1
         * </source>
         * <destination><![CDATA[ILArray<byte>]]></destination>
         * <destination><![CDATA[ILArray<complex>]]></destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  inCls2
         * </source>
         * <destination><![CDATA[ILArray<byte>]]></destination>
         * <destination><![CDATA[ILArray<complex>]]></destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  outCls1
         * </source>
         * <destination><![CDATA[ILArray<byte>]]></destination>
         * <destination><![CDATA[ILArray<complex>]]></destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  outArr1
         * </source>
         * <destination>byte</destination>
         * <destination>complex</destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  delegate_binary
         * </source>
         * <destination>ILByteFunctionByteByte</destination>
         * <destination>ILComplexFunctionComplexComplex</destination>
         * </type>
         * <type>
         * <source	locate="after">
         *  hcfunctionName
         * </source>
         * <destination>ByteOperatorByteByte</destination>
         * <destination>ComplexOperatorComplexComplex</destination>
         * </type>
         * </hycalper>
         */
        /// <summary>
        /// operate on elements of both storages by the given function -> relational operations
        /// </summary>
        /// <param name="inArray1">First storage array</param>
        /// <param name="inArray2">Second storage array</param>
        /// <param name="operation">operation to apply to the elements of inArray. This
        /// acts like a function pointer.</param>
        /// <returns><![CDATA[ /*!HC:outCls1*/ ILArray<double> ]]> with result of operation for corresponding
        /// elements of both arrays.</returns>
        /// <remarks>The values of inArray1 nor inArray2 will not be altered.The dimensions
        /// of both arrays must match.</remarks>
        private static /*!HC:outCls1*/ ILArray <double> /*!HC:hcfunctionName*/ DoubleOperatorDoubleDouble(/*!HC:inCls1*/ ILArray <double> inArray1, /*!HC:inCls2*/ ILArray <double> inArray2,
                                                                                                          /*!HC:delegate_binary*/ ILDoubleFunctionDoubleDouble operation)
        {
            ILDimension inDim = inArray1.Dimensions;

            if (!inDim.IsSameSize(inArray2.Dimensions))
            {
                throw new ILDimensionMismatchException();
            }
            /*!HC:outArr1*/ double [] retSystemArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            retSystemArr = new /*!HC:outArr1*/ double [newLength];
            int leadDimLen = inDim [0];

            // physical -> pointer arithmetic
            unsafe
            {
                fixed(/*!HC:inArr1*/ double *pInArr1 = inArray1.m_data)
                fixed(/*!HC:inArr2*/ double *pInArr2  = inArray2.m_data)
                fixed(/*!HC:outArr1*/ double *pOutArr = retSystemArr)
                {
                    /*!HC:outArr1*/ double *poutarr = pOutArr;
                    /*!HC:outArr1*/ double *poutend = poutarr + newLength;
                    /*!HC:inArr1*/ double * pIn1    = pInArr1;
                    /*!HC:inArr2*/ double * pIn2    = pInArr2;

                    while (poutarr < poutend)
                    {
                        *poutarr++ = operation(*pIn1++, *pIn2++);
                    }
                }
            }

            return(new /*!HC:outCls1*/ ILArray <double> (retSystemArr, inDim.ToIntArray()));
        }
Beispiel #27
0
        /// <summary>Exponential of array elements </summary>
        /// <param name="A">input array</param>
        /// <returns>Exponential of array elements</returns>
        /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
        /// <para>The array returned will be a dense array.</para></remarks>
        public static ILArray <double> exp(ILArray <double> A)
        {
            if (A.IsEmpty)
            {
                return(ILArray <double> .empty(A.Dimensions));
            }
            ILDimension inDim = A.Dimensions;

            double [] retDblArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            //retDblArr = new  double [newLength];
            retDblArr = ILMemoryPool.Pool.New <double> (newLength);
            int leadDimLen = inDim [0];

            // physical -> pointer arithmetic
            unsafe
            {
                fixed(double *pOutArr = retDblArr)
                fixed(double *pInArr = A.m_data)
                {
                    double *lastElement = pOutArr + retDblArr.Length;
                    double *tmpOut      = pOutArr;
                    double *tmpIn       = pInArr;

                    while (tmpOut < lastElement)   // HC02

                    {
                        *tmpOut++ = Math.Exp(*tmpIn++);
                    }
                }
            }

            return(new  ILArray <double> (retDblArr, inDim));
        }
Beispiel #28
0
        /// <summary>Finds finite value elements</summary>
        /// <param name="A">input array</param>
        /// <returns>Logical array with 1 if the corresponding elements of input array is finite, 0 else.</returns>
        /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
        /// <para>The array returned will be a dense array.</para></remarks>
        public static ILLogicalArray  isfinite(ILArray <complex> A)
        {
            if (A.IsEmpty)
            {
                return(ILLogicalArray.empty(A.Dimensions));
            }
            ILDimension inDim = A.Dimensions;

            byte [] retDblArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            //retDblArr = new  byte [newLength];
            retDblArr = ILMemoryPool.Pool.New <byte> (newLength);
            int leadDimLen = inDim [0];

            // physical -> pointer arithmetic
            unsafe
            {
                fixed(byte *pOutArr = retDblArr)
                fixed(complex * pInArr = A.m_data)
                {
                    byte *   lastElement = pOutArr + retDblArr.Length;
                    byte *   tmpOut      = pOutArr;
                    complex *tmpIn       = pInArr;

                    while (tmpOut < lastElement)   // HC02

                    {
                        *tmpOut++ = complex.IsFinite(*tmpIn++)  ?(byte)1:(byte)0;
                    }
                }
            }

            return(new  ILLogicalArray(retDblArr, inDim));
        }
Beispiel #29
0
// DO NOT EDIT INSIDE THIS REGION !! CHANGES WILL BE LOST !!
        /// <summary>Hyperbolic tangent of array elements</summary>
        /// <param name="A">input array</param>
        /// <returns>Hyperbolic tangent of array elements</returns>
        /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
        /// <para>The array returned will be a dense array.</para></remarks>
        public static ILArray <complex> tanh(ILArray <complex> A)
        {
            if (A.IsEmpty)
            {
                return(ILArray <complex> .empty(A.Dimensions));
            }
            ILDimension inDim = A.Dimensions;

            complex [] retDblArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            //retDblArr = new  complex [newLength];
            retDblArr = ILMemoryPool.Pool.New <complex> (newLength);
            int leadDimLen = inDim [0];

            // physical -> pointer arithmetic
            unsafe
            {
                fixed(complex *pOutArr = retDblArr)
                fixed(complex * pInArr = A.m_data)
                {
                    complex *lastElement = pOutArr + retDblArr.Length;
                    complex *tmpOut      = pOutArr;
                    complex *tmpIn       = pInArr;

                    while (tmpOut < lastElement)   // HC02

                    {
                        *tmpOut++ = complex.Tanh(*tmpIn++);
                    }
                }
            }

            return(new  ILArray <complex> (retDblArr, inDim));
        }
Beispiel #30
0
        /// <summary>
        /// Sinus of array elements
        /// </summary>
        /// <param name="A">input array</param>
        /// <returns>Sinus of elements from input array</returns>
        /// <remarks><para>If the input array is empty, an empty array will be returned.</para>
        /// <para>The array returned will be a dense array.</para></remarks>
        public static /*!HC:outCls*/ ILArray <double> /*!HC:HCfuncname*/ sin(/*!HC:inCls1*/ ILArray <double> A)
        {
            if (A.IsEmpty)
            {
                return /*!HC:outCls*/ (ILArray <double> .empty(A.Dimensions));
            }
            ILDimension inDim = A.Dimensions;

            /*!HC:outArr*/ double [] retDblArr;
            // build ILDimension
            int newLength = inDim.NumberOfElements;

            //retDblArr = new /*!HC:outArr*/ double [newLength];
            retDblArr = ILMemoryPool.Pool.New </*!HC:outArr*/ double> (newLength);
            int leadDimLen = inDim [0];

            // physical -> pointer arithmetic
            unsafe
            {
                fixed(/*!HC:outArr*/ double *pOutArr = retDblArr)
                fixed(/*!HC:inArr1*/ double *pInArr = A.m_data)
                {
                    /*!HC:outArr*/ double *lastElement = pOutArr + retDblArr.Length;
                    /*!HC:outArr*/ double *tmpOut      = pOutArr;
                    /*!HC:inArr1*/ double *tmpIn       = pInArr;

                    while (tmpOut < lastElement)   // HC02
                    /*!HC:HCCompute02*/
                    {
                        *tmpOut++ = /*!HC:HCoperation*/ Math.Sin(*tmpIn++) /*!HC:HCpostOp*/;
                    }
                }
            }

            return(new /*!HC:outCls*/ ILArray <double> (retDblArr, inDim));
        }