private bool isSorted(ILArray <double> input, int dim, bool descending) { if (input.IsEmpty || input.IsScalar) { return(true); } ILIterator <double> it = input.CreateIterator(ILIteratorPositions.ILStart, dim); double tmpold = it.Value; double tmpnew = it.Increment(); int dimpos = 1; while (!it.IsAtStart()) { if (++dimpos % input.Dimensions[dim] != 1) { if (descending) { if (tmpnew > tmpold) { return(false); } } else { if (tmpnew < tmpold) { return(false); } } } tmpold = tmpnew; tmpnew = it.Increment(); } return(true); }
/// <summary> /// real part of complex array /// </summary> /// <param name="X">complex input array</param> /// <returns>real part of complex array</returns> public static /*!HC:outCls1*/ ILArray<double> real (/*!HC:inCls1*/ ILArray<complex> X) { int nrX = X.m_dimensions.NumberOfElements; /*!HC:outArr1*/ double [] retArr = new /*!HC:outArr1*/ double [nrX]; /*!HC:outCls1*/ ILArray<double> ret = new /*!HC:outCls1*/ ILArray<double> (retArr,X.m_dimensions); ILIterator</*!HC:inArr1*/ complex > it = X.CreateIterator(ILIteratorPositions.ILEnd,0); for (int i= 0; i < nrX; i++) { retArr[i] = it.Increment().real; } return ret; }
public static BitmapSource ILArrayToBitmapSource(ILArray <double> surface) { // Define parameters used to create the BitmapSource. PixelFormat pf = PixelFormats.Bgr32; int width = surface.Dimensions[0]; int height = surface.Dimensions[1]; int bytes = (pf.BitsPerPixel + 7) / 8; int rawStride = (width * bytes); byte[] rawImage = new byte[rawStride * height]; int index = 0; ColourMap ColourMap = new ColourMap(ColourMapType.Jet, 256); byte[,] cmap = ColourMap.ToByteArray(); double range = surface.MaxValue - surface.MinValue; double min = surface.MinValue; int magnitude = 0; ILArray <int> scaled = (ILArray <int>)ILMath.convert(NumericType.Int32, ILMath.floor((surface - min) * 256.0 / range)); ILIterator <int> iterator = scaled.CreateIterator(); Stopwatch sw = Stopwatch.StartNew(); sw.Reset(); sw.Start(); magnitude = iterator.Value; for (int y = 0; y < height; ++y) { for (int x = 0; x < width; ++x) { if (magnitude == 256) { magnitude = 255; } rawImage[index] = cmap[magnitude, 3]; rawImage[index + 1] = cmap[magnitude, 2]; rawImage[index + 2] = cmap[magnitude, 1]; rawImage[index + 3] = cmap[magnitude, 0]; index += bytes; magnitude = iterator.Increment(); } } sw.Stop(); string result; result = "Elapsed time: " + sw.ElapsedMilliseconds.ToString() + " ms"; // Create a BitmapSource. BitmapSource bitmap = BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, rawStride); return(bitmap); }
// DO NOT EDIT INSIDE THIS REGION !! CHANGES WILL BE LOST !! /// <summary> /// real part of complex array /// </summary> /// <param name="X">complex input array</param> /// <returns>real part of complex array</returns> public static ILArray <float> real(ILArray <fcomplex> X) { int nrX = X.m_dimensions.NumberOfElements; float [] retArr = new float [nrX]; ILArray <float> ret = new ILArray <float> (retArr, X.m_dimensions); ILIterator <fcomplex> it = X.CreateIterator(ILIteratorPositions.ILEnd, 0); for (int i = 0; i < nrX; i++) { retArr[i] = it.Increment().real; } return(ret); }
/// <summary> /// real part of complex array /// </summary> /// <param name="X">complex input array</param> /// <returns>real part of complex array</returns> public static /*!HC:outCls1*/ ILArray <double> real(/*!HC:inCls1*/ ILArray <complex> X) { int nrX = X.m_dimensions.NumberOfElements; /*!HC:outArr1*/ double [] retArr = new /*!HC:outArr1*/ double [nrX]; /*!HC:outCls1*/ ILArray <double> ret = new /*!HC:outCls1*/ ILArray <double> (retArr, X.m_dimensions); ILIterator </*!HC:inArr1*/ complex> it = X.CreateIterator(ILIteratorPositions.ILEnd, 0); for (int i = 0; i < nrX; i++) { retArr[i] = it.Increment().real; } return(ret); }
public static BitmapSource ILArrayToBitmapSourceReversed(ILArray <double> surface, ColourMap colourMap) { // Define parameters used to create the BitmapSource. PixelFormat pf = PixelFormats.Bgr32; int width = surface.Dimensions[0]; int height = surface.Dimensions[1]; int bytes = (pf.BitsPerPixel + 7) / 8; int rawStride = (width * bytes); byte[] rawImage = new byte[rawStride * height]; int index = 0; byte[,] cmap = colourMap.ToByteArray(); int colourMapLength = colourMap.Length; double range = surface.MaxValue - surface.MinValue; double min = surface.MinValue; int magnitude = 0; ILArray <int> scaled = (ILArray <int>)ILMath.convert(NumericType.Int32, ILMath.floor((surface - min) * (double)(colourMapLength - 1) / range)); ILIterator <int> iterator = scaled.CreateIterator(); magnitude = iterator.Value; for (int y = height - 1; y >= 0; --y) { index = y * rawStride; for (int x = 0; x < width; ++x) { rawImage[index] = cmap[magnitude, 3]; rawImage[index + 1] = cmap[magnitude, 2]; rawImage[index + 2] = cmap[magnitude, 1]; rawImage[index + 3] = cmap[magnitude, 0]; index += bytes; magnitude = iterator.Increment(); } } // Create a BitmapSource. BitmapSource bitmap = BitmapSource.Create(width, height, 96, 96, pf, null, rawImage, rawStride); return(bitmap); }
// DO NOT EDIT INSIDE THIS REGION !! CHANGES WILL BE LOST !! /// <summary> /// real part of complex array /// </summary> /// <param name="X">complex input array</param> /// <returns>real part of complex array</returns> public static ILArray<float> real ( ILArray<fcomplex> X) { int nrX = X.m_dimensions.NumberOfElements; float [] retArr = new float [nrX]; ILArray<float> ret = new ILArray<float> (retArr,X.m_dimensions); ILIterator< fcomplex > it = X.CreateIterator(ILIteratorPositions.ILEnd,0); for (int i= 0; i < nrX; i++) { retArr[i] = it.Increment().real; } return ret; }
/// <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>new ILLogicalArray with result of operation for corresponding /// elements of both arrays.</returns> /// <remarks>The values of inArray2 nor inArray2 will not be altered.The dimensions /// of both arrays must match.</remarks> private static ILArray <double> DoubleBinaryDoubleOperator(ILArray <double> inArray1, ILArray <double> inArray2, ILApplyDouble_DoubleDouble operation) { ILDimension inDim = inArray1.Dimensions; if (!inDim.IsSameSize(inArray2.Dimensions)) { throw new Exception("Array dimensions must match."); } double[] retBoolArr; // build ILDimension int newLength = inDim.NumberOfElements; retBoolArr = new double[newLength]; int leadDim = 0; int leadDimLen = inDim[0]; if (inArray1.IsReference || inArray2.IsReference) { // this will most probably be not very fast, but .... :| #region Reference storage // 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; } } ILIterator <double> it1 = inArray1.CreateIterator(ILIteratorPositions.ILEnd, leadDim); ILIterator <double> it2 = inArray2.CreateIterator(ILIteratorPositions.ILEnd, leadDim); unsafe { fixed(double *pOutArr = retBoolArr) { double *poutarr = pOutArr; double *outEnd = poutarr + newLength; while (poutarr < outEnd) { *poutarr++ = operation(it1.Increment(), it2.Increment()); } } } // ============================================================== #endregion } else { // physical -> pointer arithmetic #region physical storage unsafe { fixed(double *pInArr1 = inArray1.m_data, pInArr2 = inArray2.m_data) { fixed(double *pOutArr = retBoolArr) { double *poutarr = pOutArr; double *poutend = poutarr + newLength; double *pIn1 = pInArr1; double *pIn2 = pInArr2; while (poutarr < poutend) { *poutarr++ = operation(*pIn1++, *pIn2++); } } } } #endregion } return(new ILArray <double>(retBoolArr, inDim.ToIntArray())); }
private bool isSorted(ILArray<double> input, int dim, bool descending) { if (input.IsEmpty || input.IsScalar) return true; ILIterator<double> it = input.CreateIterator(ILIteratorPositions.ILStart,dim); double tmpold = it.Value; double tmpnew = it.Increment(); int dimpos = 1; while (!it.IsAtStart()) { if (++dimpos % input.Dimensions[dim] != 1) { if (descending) { if (tmpnew > tmpold) return false; } else { if (tmpnew < tmpold) return false; } } tmpold = tmpnew; tmpnew = it.Increment(); } return true; }
/// <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>new ILLogicalArray with result of operation for corresponding /// elements of both arrays.</returns> /// <remarks>The values of inArray2 nor inArray2 will not be altered.The dimensions /// of both arrays must match.</remarks> private static ILArray<double> DoubleBinaryDoubleOperator(ILArray<double> inArray1, ILArray<double> inArray2, ILApplyDouble_DoubleDouble operation) { ILDimension inDim = inArray1.Dimensions; if (!inDim.IsSameSize(inArray2.Dimensions)) throw new Exception("Array dimensions must match."); double[] retBoolArr; // build ILDimension int newLength = inDim.NumberOfElements; retBoolArr = new double[newLength]; int leadDim = 0; int leadDimLen = inDim[0]; if (inArray1.IsReference || inArray2.IsReference) { // this will most probably be not very fast, but .... :| #region Reference storage // 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; } } ILIterator<double> it1 = inArray1.CreateIterator(ILIteratorPositions.ILEnd, leadDim); ILIterator<double> it2 = inArray2.CreateIterator(ILIteratorPositions.ILEnd, leadDim); unsafe { fixed (double* pOutArr = retBoolArr) { double* poutarr = pOutArr; double* outEnd = poutarr + newLength; while (poutarr < outEnd) { *poutarr++ = operation(it1.Increment(), it2.Increment()); } } } // ============================================================== #endregion } else { // physical -> pointer arithmetic #region physical storage unsafe { fixed (double* pInArr1 = inArray1.m_data, pInArr2 = inArray2.m_data) { fixed (double* pOutArr = retBoolArr) { double* poutarr = pOutArr; double* poutend = poutarr + newLength; double* pIn1 = pInArr1; double* pIn2 = pInArr2; while (poutarr < poutend) *poutarr++ = operation(*pIn1++, *pIn2++); } } } #endregion } return new ILArray<double>(retBoolArr, inDim.ToIntArray()); }
public void Test_setrange() { int errorCode = 0; // success? try { object[] data = new object[120]; for (int i = 120; i-- > 0; ) data[i] = (double)i; ILArray<object> A = new ILArray<object>(data, 5, 4, 1, 3, 2, 1); //ILFullArray A = B.CreateReference(new ILRange("0:2,4;1,3;0;2,1,0,1"), 3); errorCode = 1; // new values double[] vals = new double[12] { 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111 }; object[] valso = new object[12]; for (int i = 0; i < 12; i++) valso[i] = vals[i]; ILArray<object> C = new ILArray<object>(valso, 2, 2, 3); // should replace missing trailing dimension by 0 // inner singleton dimensions should be ok! A["1,3;0,2;0;0,1,2"] = C; errorCode = 2; double[] results = new double[120] {0,100, 2,101, 4, 5, 6, 7, 8, 9, 10, 102, 12,103, 14, 15, 16, 17, 18, 19, 20,104, 22,105, 24, 25, 26, 27, 28, 29, 30,106, 32,107, 34, 35, 36, 37, 38, 39, 40,108, 42,109, 44, 45, 46, 47, 48, 49, 50, 110, 52, 111, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,100, 101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119}; ILIterator<object> I = A.CreateIterator(ILIteratorPositions.ILStart, 0); int count = 0; do { if (((Double)I.Value).CompareTo(results[count++]) != 0) throw new Exception(); I.Increment(); } while (!I.IsAtStart()); // complain if dimensions do not match errorCode = 3; ILArray<object> D = new ILArray<object>(new object[4] { 1, 1, 1, 1 }, 1, 2, 2); try { A["0,1,0,0:2"] = D; throw new Exception("setrange should complain if dimensions do not match!"); } catch (ILArgumentException) { } errorCode = 4; Success("Test_setrange successful."); } catch (Exception e) { Error("Test_setrange failed at step: " + errorCode + " Msg: " + e.Message); } }
public void Test_tmpPerfCount() { double [] data = new double[1000*1000*10]; ILArray<double> A = new ILArray<double>(data, 1000, 1000, 10); ILPerformer p = new ILPerformer(); p.Tic(); for (int i = 0; i < 10000000; i++) { A.getBaseIndex(i); } p.Toc(); Info("1000x1000x10: dense GetBaseIndex needed: " + p.Duration + " ms"); ILIterator<double> it = A.CreateIterator(); p.Tic(); do { it.Increment(); } while(!it.IsAtStart()); p.Toc(); Info("1000x1000x10: dense Iterator needed: " + p.Duration + " ms"); A = (ILArray<double>)A[1,""]; p.Tic(); for (int i = 0; i < 10000000; i++) { A.getBaseIndex(i); } p.Toc(); Info("1000x1000x10: ref. GetBaseIndex needed: " + p.Duration + " ms"); it = A.CreateIterator(); p.Tic(); do { it.Increment(); } while (!it.IsAtStart()); p.Toc(); Info("1000x1000x10: ref Iterator needed: " + p.Duration + " ms"); p.Tic(); }
public void Test_Deserialize() { int errorCode = 0; try { { String[] data = new String[1000]; String filename = "ILFullArray_SerializeTest1.ils"; for (int i = 0; i < data.Length; i++) data[i] = "Datum Nr." + i; ILArray<String> A = new ILArray<String>(data, 10, 20, 5); // full storage, was: A BinaryFormatter bf = new BinaryFormatter(); FileStream fs = new FileStream(filename, FileMode.Open); ILArray<String> ADS = (ILArray<String>)bf.Deserialize(fs); fs.Close(); if (ADS.Dimensions.NumberOfElements != 1000) throw new Exception("Object has wrong size! Expected: 1000, Found: " + ADS.Dimensions.NumberOfElements); ILIterator<String> I1 = A.CreateIterator(ILIteratorPositions.ILStart,0 ); ILIterator<String> I2 = ADS.CreateIterator(ILIteratorPositions.ILStart,0 ); for (int i = 0; i < A.Dimensions.NumberOfElements; i++) { if (((String)I1.Value).CompareTo((String)I2.Value) != 0) throw new Exception("Values did not match! "); } // reference storage, was : AR1 filename = "ILFullArray_SerializeTest2.ils"; fs = new FileStream(filename, FileMode.Open); ILArray<String> AR1DS = (ILArray<String>)bf.Deserialize(fs); fs.Close(); ILArray<String> AR1 = (ILArray<String>)A["0:8;3:18;0:end"]; if (AR1DS.Dimensions.NumberOfElements != AR1.Dimensions.NumberOfElements) throw new Exception("Object has wrong size! Expected: " + AR1.Dimensions.NumberOfElements + ", Found: " + AR1DS.Dimensions.NumberOfElements); I1 = AR1.CreateIterator(ILIteratorPositions.ILStart, 0); I2 = AR1DS.CreateIterator(ILIteratorPositions.ILStart, 0); for (int i = 0; i < AR1.Dimensions.NumberOfElements; i++) { if (((String)I1.Value).CompareTo((String)I2.Value) != 0) throw new Exception("Values did not match! "); } // if reference storage saved effective memory - keep it as reference errorCode = 3; filename = "ILFullArray_SerializeTest3.ils"; fs = new FileStream(filename, FileMode.Open); ILArray<String> AR2DS = (ILArray<String>)bf.Deserialize(fs); fs.Close(); ILArray<String> AR2 = (ILArray<String>)A["0:end,0:end;0:end,0:end;0:end"]; if (AR2DS.Dimensions.NumberOfElements != AR2.Dimensions.NumberOfElements) throw new Exception("Object has wrong size! Expected: " + AR2.Dimensions.NumberOfElements + ", Found: " + AR2DS.Dimensions.NumberOfElements); I1 = AR2.CreateIterator(ILIteratorPositions.ILStart, 0); I2 = AR2DS.CreateIterator(ILIteratorPositions.ILStart, 0); for (int i = 0; i < AR2.Dimensions.NumberOfElements; i++) { if (((String)I1.Value).CompareTo((String)I2.Value) != 0) throw new Exception("Values did not match! "); } // test if small reference would NOT store full data array errorCode = 4; filename = "ILFullArray_SerializeTest4.ils"; fs = new FileStream(filename, FileMode.Open); ILArray<String> AR3DS = (ILArray<String>)bf.Deserialize(fs); fs.Close(); ILArray<String> AR3 = A[3,3,1]; if (AR3DS.m_data.Length != 1 || ((String)AR3DS.m_data[0]).CompareTo("Datum Nr.233") != 0) throw new Exception("Unvalid deserialized scalar storage!"); errorCode = 5; // clean up . delete all test files File.Delete("ILFullArray_SerializeTest1.ils"); File.Delete("ILFullArray_SerializeTest2.ils"); File.Delete("ILFullArray_SerializeTest3.ils"); File.Delete("ILFullArray_SerializeTest4.ils"); } Success("Test_Deserialize successfull"); } catch (SerializationException e) { Error("Test_Deserialize failed at errorCode: " + errorCode + " due: " + e.Message); } catch (Exception e) { Error("Test_Deserialize failed at errorCode: " + errorCode + " due: " + e.Message); } }
public void Test_Iterator_Performance() { int errorCode = 0; // success? try { double[] data = new double[4 * 3 * 2] {0, 1,2,3,4,5,6,7,8,9,10 ,11,12,13,14,15,16,17,18,19,20,21,22,23 }; ILArray<double> A = new ILArray<double>(data, 4,3,2); ILArray<double> Ar = (ILArray<double>)A.GetShifted(2); ILPerformer p = new ILPerformer(); int counter = 0; ILIterator<double> it = A.CreateIterator(ILIteratorPositions.ILEnd, 0); ILIterator<double> it1 = Ar.CreateIterator(ILIteratorPositions.ILEnd, 2); double val = 0.0; do { if (it.Increment() != counter++) throw new Exception("invalid iterator value!"); } while (!it.IsAtEnd()); // shifted (1) physical errorCode = 1; double[] results = new double[24] {0,4,8,12,16,20,1,5,9,13,17,21 ,2,6,10,14,18,22,3,7,11,15,19,23}; it = A.CreateIterator(ILIteratorPositions.ILEnd, 1); counter = 0; do { if (it.Increment() != results[counter]) throw new Exception("invalid phy iterator value!"); if (it1.Increment() != results[counter++]) throw new Exception("invalid ref iterator value!"); } while (!it.IsAtEnd()); results = new double[24] {0,4,8,12,16,20,1,5,9,13,17,21 ,2,6,10,14,18,22,3,7,11,15,19,23}; data = new double[1000 * 1000 * 10]; A = new ILArray<double>(data, 1000, 1000, 10); //// test the IEnumerable way of doing this //errorCode = 1; //p.Tic(); //foreach (double v in A) { // val = v; //} //p.Toc(); //Info("[1000 x 1000 x 10] IEnumarable 'foreach' Iterator: " + p.ToString()); errorCode = 2; // iterate the classic way: val = 2.23; double dura = 0.0, dura1= 0.0; double itIncCounter = 0.0; it = A.CreateIterator(ILIteratorPositions.ILStart, 0); it1 = A.CreateIterator(ILIteratorPositions.ILStart, 1); ILPerformer p1 = new ILPerformer(); do { p1.Tic(); val = it1.Increment(); p1.Toc(); dura1 += p1.Duration; // outer time needed p.Tic(); val = it.Increment(); p.Toc(); //dummy += Environment.TickCount - it.Dummy; // internal time needed dura += p.Duration; // outer time needed itIncCounter++; } while (itIncCounter < data.Length); Info("[1000 x 1000 x 10] ILIterator Phys leadDim: 0 ->" + dura.ToString() + " leadDim: 1 ->" + dura1.ToString() + " incCount:" + itIncCounter); errorCode = 3; ILArray<double> B = (ILArray<double>)A.T; p.Tic(); it = B.CreateIterator(ILIteratorPositions.ILStart, 0); it1 = B.CreateIterator(ILIteratorPositions.ILStart, 1); dura = 0.0; dura1 = 0.0; itIncCounter = 0; do { p1.Tic(); val = it1.Increment(); p1.Toc(); dura1 += p1.Duration; // outer time needed p.Tic(); val = it.Increment(); p.Toc(); //dummy += Environment.TickCount - it.Dummy; // internal time needed dura += p.Duration; // outer time needed itIncCounter++; } while (itIncCounter < data.Length); p.Toc(); Info("[1000 x 1000 x 10] ILIterator - Ref. leadDim: 0 ->" + dura.ToString() + " leadDim: 1 ->" + dura1.ToString() + " incCount:" + itIncCounter); Success("Test_Iterator_Performance successful."); } catch (Exception e) { Error("Test_Iterator_Performance failed at step: " + errorCode + " Msg: " + e.Message); } }