private void TestBucketSortArrayMatrixRow() { try { ILArray <string> A = new ILArray <string>(3, 4); A[0, 0] = "abc"; A[0, 1] = "rtu"; A[0, 2] = "sfkw"; A[0, 3] = "lsdkfi"; A[1, 0] = "iowejkc"; A[1, 1] = "cjks"; A[1, 2] = "wokys"; A[1, 3] = "suem,"; A[2, 0] = "fgj"; A[2, 1] = "JKSF"; A[2, 2] = "SEs"; A[2, 3] = "SEFsr"; ILArray <double> ind; ILArray <string> res = ILMath.sort(A, out ind, 0, false); if (!res.Equals(A[ind])) { throw new Exception("invalid indices/values detected"); } ILArray <Int16> indI = ILMath.toint16(ILMath.counter(0.0, 1.0, A.Dimensions.ToIntArray())); res = ILMath.sort(A, ref indI, 0, true, new ILASCIIKeyMapper()); if (!res.Equals(A[indI])) { throw new Exception("invalid indices/values detected"); } Success(" elapsed: " + m_stopwatch.ElapsedMilliseconds + " ms"); } catch (Exception e) { Error(0, e.Message); } }
public void Test_DivideDouble() { int errorCode = 0; // success? try { ILPerformer p = new ILPerformer(); double[] data1 = new double[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; double[] data2 = new double[10] { 11, 12, -13, 14, 15, 16, -17, 18, 19, 110 }; double tmp = ((double)13.0) / ((double)3.0); double[] results = new double[18] { 11.0000, (double)-13.0 / (double)3.0, 15.0 / 5.0, 19.0 / 9.0, 11 , 3.0000, 6.0000, 3.5000, 16.0 / 6.0, 11.0000, 6.0000, 16.0 / 6.0, 11.0000 , -13.0 / 3.0, 3.0000, 19.0 / 9.0, 11.0000, 3.0000 }; ILArray <double> C = new ILArray <double>(data1, 2, 5); ILArray <double> B = new ILArray <double>(data2, 2, 5); ILArray <double> R = new ILArray <double>(results, 6, 3); ILArray <double> BR = (ILArray <double>)B[1, "0:1,0;0,1,2,4,0,2"]; ILArray <double> CR = (ILArray <double>)C[1, "0:1,0;0,1,2,4,0,2"]; C = (ILArray <double>)CR.T.T; B = (ILArray <double>)BR.T.T; C.Detach(); B.Detach(); ILArray <double> A = B / C; errorCode = 1; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } A = B / CR; errorCode = 2; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } A = BR / CR; errorCode = 3; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } A = BR / CR; errorCode = 4; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } Success("Test_DivideDouble successful. (" + p.Toc() + "ms needed)"); } catch (Exception e) { Error("Test_DivideDouble failed at step: " + errorCode + " Msg: " + e.Message); } }
public void Test_AddDouble() { int errorCode = 0; // success? try { ILPerformer p = new ILPerformer(); double[] data1 = new double[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; double[] data2 = new double[10] { 11, 12, -13, 14, 15, 16, -17, 18, 19, 110 }; double[] results = new double[18] { 12, -10, 20, 28, 12, 20, 14, 18, 22, 120, 14, 22, 12, -10 , 20, 28, 12, 20 }; ILArray <double> C = new ILArray <double>(data1, 2, 5); ILArray <double> B = new ILArray <double>(data2, 2, 5); ILArray <double> R = new ILArray <double>(results, 6, 3); ILArray <double> BR = (ILArray <double>)B[1, "0:1,0;0,1,2,4,0,2"]; ILArray <double> CR = (ILArray <double>)C[1, "0:1,0;0,1,2,4,0,2"]; C = CR.R; // creates reference only B = BR.R; C.Detach(); B.Detach(); ILArray <double> A = B + C; errorCode = 1; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } A = B + CR; errorCode = 2; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } A = BR + CR; errorCode = 3; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } A = BR + CR; errorCode = 4; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } Success("Test_AddDouble successful. (" + p.Toc() + "ms needed)"); } catch (Exception e) { Error("Test_AddDouble failed at step: " + errorCode + " Msg: " + e.Message); } }
public void Test_MultiplyElementsDouble() { int errorCode = 0; // success? try { ILPerformer p = new ILPerformer(); double[] data1 = new double[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; double[] data2 = new double[10] { 11, 12, -13, 14, 15, 16, -17, 18, 19, 110 }; double[] results = new double[18] { 11, -39, 75, 171, 11, 75, 24, 56, 96, 1100 , 24, 96, 11, -39, 75, 171, 11, 75 }; ILArray <double> C = new ILArray <double>(data1, 2, 5); ILArray <double> B = new ILArray <double>(data2, 2, 5); ILArray <double> R = new ILArray <double>(results, 6, 3); ILArray <double> BR = (ILArray <double>)B[1, "0:1,0;0,1,2,4,0,2"]; ILArray <double> CR = (ILArray <double>)C[1, "0:1,0;0,1,2,4,0,2"]; C = (ILArray <double>)CR.T.T; B = (ILArray <double>)BR.T.T; C.Detach(); B.Detach(); ILArray <double> A = B * C; errorCode = 1; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } A = B * CR; errorCode = 2; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } A = BR * CR; errorCode = 3; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } A = BR * CR; errorCode = 4; if (!A.Equals(R)) { throw new Exception("Values did not match!"); } Success("Test_MultiplyElementsDouble successful. (" + p.Toc() + "ms needed)"); } catch (Exception e) { Error("Test_MultiplyElementsDouble failed at step: " + errorCode + " Msg: " + e.Message); } }
private void Test_SortSpeed(int length) { try { ILArray <double> ResILN = null, ResCLR = null, A = null; long durILN = 0, durCLR = 0; Misc.ILPerformer p = new ILNumerics.Misc.ILPerformer(); for (int i = 0; i < 10; i++) { A = ILMath.randn(length, 1); p.Tic(); ILMath.sort(A); p.Toc(); durILN += p.Duration; ResCLR = A.C; p.Tic(); Array.Sort(ResCLR.m_data); p.Toc(); durCLR += p.Duration; } ResILN = ILMath.sort(A); if (!ResCLR.Equals(ResILN)) { throw new Exception("invalid values!"); } Info(String.Format("ILNumerics.Net sort {1} needed: {0}ms", durILN / 10, A.Dimensions.ToString())); Info(String.Format("CLR Array.Sort {1} needed: {0}ms", durCLR / 10, A.Dimensions.ToString())); // descending durCLR = 0; durILN = 0; IComparer comparer = new DescComparerDouble(); for (int i = 0; i < 10; i++) { A = ILMath.randn(length, 1); p.Tic(); ResILN = ILMath.sort(A, true); p.Toc(); durILN += p.Duration; ResCLR = A.C; p.Tic(); Array.Sort(ResCLR.m_data, comparer); p.Toc(); durCLR += p.Duration; } if (!ResCLR.Equals(ResILN)) { throw new Exception("invalid values!"); } Info(String.Format("ILNumerics.Net sort {1} desc, needed: {0}ms", durILN / 10, A.Dimensions.ToString())); Info(String.Format("CLR Array.Sort {1} desc, needed: {0}ms", durCLR / 10, A.Dimensions.ToString())); } catch (Exception e) { Error(0, e.Message); } }
private void TestQuickSortDesc() { try { ILArray <double> A = ILMath.counter(5, 4, 3); System.Diagnostics.Debug.Assert(!A.IsReference); ILArray <double> result = ILMath.sort(A, 1, true); ILArray <double> expect = A[":;3,2,1,0;:"]; if (!result.Equals(expect)) { throw new Exception("invalid values"); } // test scalar A = 3.0; result = ILMath.sort(A, 0, true); if (result != 3.0) { throw new Exception("invalid values: scalar"); } // test empty A = ILArray <double> .empty(); if (!ILMath.sort(A, 0, true).IsEmpty) { throw new Exception("invalid values: empty"); } Success(); } catch (Exception e) { Error(0, e.Message); } }
public void Test_Basic() { int errorCode = 0; try { ILMemoryPool.Pool.Reset(100, 200); ILArray <double> A = ILMath.rand(30, 30); double[] aArr = A.m_data; ILArray <double> B = A.C; A.Dispose(); // A's array should be in pool now - query double[] retArr = ILMemoryPool.Pool.New <double>(B.Dimensions.NumberOfElements); ILArray <double> Res = new ILArray <double>(retArr, B.Dimensions); if (!Res.Equals(B) || aArr != retArr) { throw new Exception("the expected array was not returned from pool!"); } // query again - should return new array aArr = ILMemoryPool.Pool.New <double>(B.Dimensions.NumberOfElements); if (aArr == retArr) { throw new Exception("after reclaiming array should be removed from pool!"); } // test if zero cleares everything; Res.Dispose(); Res = ILMath.zeros(B.Dimensions); // Res should have the same array, but cleared ? if (Res.m_data != retArr) { throw new Exception("unexpected array returned from pool"); } B = new ILArray <double>(new double[30 * 30], 30, 30); if (!Res.Equals(B)) { throw new Exception("invalid values - ILMath.zeros should return zeros!"); } Success(); } catch (Exception e) { Error(errorCode, e.Message); } }
public static void Run() { ILArray <complex> A = 1 / new complex(1, 1); if (!A.Equals(ccomplex(0.5, -0.5))) { throw new Exception("complex: 1 / 1+i -> wrong result!"); } ILArray <complex> y = ccomplex(real(A), -imag(A)).T; if (!y.Equals(conj(A))) // it's a scalar! { throw new Exception("conj(complex) -> wrong result!"); } }
public void Test_ReferenceFromArray() { int errorCode = 0; try { // B = A[idx,0] string[] data = new string[20]; for (int i = 0; i < data.Length; i++) data[i] = "Value: " + i; ILArray<string> A = new ILArray<string>(data, 4,5); double[] ddata = new double[20] { 4,2,5,1,8,9,0,0,1,1,2,3,19,18,17,16,15,14,13,12 }; ILArray<double> B = new ILArray<double>(ddata, 5, 2, 2); // create unshifted subarray, addressed by matrix -> physical storage ILArray<string> C = A[B]; string[] results = new string[20]; for (int i = 0; i < results.Length; i++) { results[i] = data[(int)ddata[i]]; } ILBaseArray<string> res = new ILArray<string>(results, 5, 2 ,2); if (!res.Equals(C)) throw new Exception("Invalid value of result!"); errorCode = 1; // elements out of bound error try { B[4, 1, 1] = 333.0; C = A[10,B]; // ( <= Quatsch!) Error("Expected \"Out of bound\" Exception: 333.0"); } catch (Exception) { // nothing } errorCode = 2; // elements out of bound error try { B[4, 1, 1] = -1.0; C = A[1000,B]; Error("Expected \"Out of bound\" Exception: -1.0"); } catch (Exception) { Info("Out of bounds signaled correctly."); } // B = A[idx,2] // A[idx] = B Success("Test_ReferenceFromArray successfull."); } catch (Exception e) { Error("Test_ReferenceFromArray failed on ErrorCode: " + errorCode + "due: " + e.Message); } }
private void TestQuickSortAsc() { try { ILArray <double> A = ILMath.counter(5, 4, 3); System.Diagnostics.Debug.Assert(!A.IsReference); ILArray <double> result = ILMath.sort(A, 1, false); ILArray <double> expect = A[":;:;:"]; if (!result.Equals(expect)) { throw new Exception("invalid values"); } // test real sortable values A = ILMath.counter(60.0, -1.0, 5, 4, 3); result = ILMath.sort(A, 1, false); expect = A[":;3,2,1,0;:"]; if (!result.Equals(expect)) { throw new Exception("invalid values"); } // test ascending values to sort ascending A = ILMath.counter(1.0, 1.0, 11, 2); result = ILMath.sort(A, 0, false); if (!result.Equals(A)) { throw new Exception("invalid values"); } // test descending values to sort ascending A = ILMath.counter(22.0, -1, 11, 2); result = ILMath.sort(A, 0, false); expect = ILMath.counter(1.0, 1.0, 11, 2)[":;1,0"]; if (!result.Equals(expect)) { throw new Exception("invalid values"); } // test scalar A = 3.0; result = ILMath.sort(A, 0, false); if (result != 3.0) { throw new Exception("invalid values: scalar"); } // test empty A = ILArray <double> .empty(); if (!ILMath.sort(A, 0, false).IsEmpty) { throw new Exception("invalid values: empty"); } // test nan A = ILMath.counter(1.0, 1.0, 20, 2); A[2] = double.NaN; A[4] = double.PositiveInfinity; A[8] = double.NegativeInfinity; A[18] = double.NegativeInfinity; A[14] = double.NaN; result = ILMath.sort(A, 0, false); expect = ILMath.counter(1.0, 1.0, 20, 2); expect[":;0"] = new double[] { double.NegativeInfinity, double.NegativeInfinity, 1, 2, 4, 6, 7, 8, 10, 11, 12, 13, 14, 16, 17, 18, 20, double.PositiveInfinity, double.NaN, double.NaN }; if (!result.Equals(expect)) { throw new Exception("invalid values"); } Success(); } catch (Exception e) { Error(0, e.Message); } }
public void Test_Apply() { int errorCode = 0; try { double[] data = new double [24]; for (int i = 0; i < data.Length; i++) data[i] = i; ILArray<double> A = new ILArray<double>(data, 2, 3, 4); ILArray<double> B = (ILArray<double>)A[1,"1,1;2,1,0;2:end"]; ILArray<double> Res = new ILArray<double>(new double[24] { 0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 }, 2, 3, 4); // apply on physical storage ILArray<double> C = ILMath.ceil(ILMath.tanh(A) - 0.5); if (!Res.Equals(C)) throw new Exception("Wrong values of Apply result! "); // apply on reference arbitrary storage errorCode = 1; Res = new ILArray<double>(new double[12] { -1,0,0,-1,0,0,-1,0,0,-1,0,0 }, 3, 2, 2); C = ILMath.floor(ILMath.sin(B)); if (!Res.Equals(C)) throw new Exception("Wrong values of Apply result! "); // apply on row reference vector errorCode = 2; B.MinimumRefDimensions = 2; C = (ILArray<double>) B["1,1;0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"+ "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;0"]; C = ILMath.pow(C, 2.3); Res = ILMath.ones(2, 36) * 507.00226068959716; if (!Res.Equals(C)) throw new Exception("Wrong values of Apply result! "); // apply on column reference vector errorCode = 3; C = (ILArray<double>)B[1,"1,1;0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"+ "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;0"]; C = ILMath.sqrt(C) + 2.4432112354; Res = ILMath.ones(36, 2) * 6.3161945816074176; if (!Res.Equals(C)) throw new Exception("Wrong values of Apply result! "); // apply large array -> performance test errorCode = 4; ILPerformer p = new ILPerformer(); A = ILMath.ones(1000, 1000, 10); long durationsMath = 0; ILPerformer p2 = new ILPerformer(); long durationsGC = 0; int cycles = 20; p2.Tic(); for (int i = 0; i < cycles; i++) { durationsGC += p.Duration; p.Tic(); A = A + 3.4; p.Toc(); durationsMath += p.Duration; p.Tic(); System.GC.Collect(); p.Toc(); } p2.Toc(); durationsMath /= cycles; durationsGC /= cycles; Info("Apply(1000x1000x10)[Phy] needed: " + durationsMath.ToString() +"/GC: "+ durationsGC.ToString() + "/All: " +p2.Duration.ToString()); errorCode = 5; durationsMath = 0; durationsGC = 0; cycles = 20; A = (ILArray<double>)A.T; p2.Tic(); for (int i = 0; i < cycles; i++) { p.Tic(); B = A + 3.4; p.Toc(); durationsMath += p.Duration; p.Tic(); System.GC.Collect(); p.Toc(); durationsGC += p.Duration; } p2.Toc(); durationsMath /= cycles; durationsGC /= cycles; Info("Apply(1000x1000x10)[Ref] needed: " + durationsMath.ToString() + "/GC: " + durationsGC.ToString() + "/All: " +p2.Duration.ToString()); Success(); } catch (Exception e) { Error(errorCode, e.Message); } }
private void TestQuickSortDescIDX(int dim, int len) { try { // asc along dimension (already ordered values) int[] dims = new int[4] { 5, 4, 3, 2 }; dims[dim] = len; ILArray <double> A = ILMath.counter(dims); ILArray <double> ind; ILArray <double> result = ILMath.sort(A, out ind, dim, true); ILArray <double> expect = null; ILArray <double> expectInd; expectInd = ILMath.counter(A.Dimensions[dim] - 1, -1.0, 1, A.Dimensions[dim]); ILBaseArray[] revDims = new ILBaseArray[dims.Length]; revDims[dim] = expectInd; expect = A[revDims]; if (!result.Equals(expect)) { throw new Exception("invalid values"); } int [] dimsEx = new int[dims.Length]; expectInd = ILMath.repmat(expectInd, A.Dimensions.SequentialIndexDistance(dim), 1); sortIDXTestHelper001(dim, dims, ref expectInd, dimsEx); expectInd = ILMath.repmat(expectInd, dimsEx); if (!ind.Equals(expectInd)) { throw new Exception("invalid indices"); } // reverse values ... A = ILMath.counter(A.Dimensions.NumberOfElements, -1.0, dims); result = ILMath.sort(A, out ind, dim, true); if (!result.Equals(A.C)) { throw new Exception("invalid values"); } expectInd = ILMath.counter(0.0, 1.0, 1, A.Dimensions[dim]); expectInd = ILMath.repmat(expectInd, A.Dimensions.SequentialIndexDistance(dim), 1); sortIDXTestHelper001(dim, dims, ref expectInd, dimsEx); expectInd = ILMath.repmat(expectInd, dimsEx); if (!ind.Equals(expectInd)) { throw new Exception("invalid indices"); } // test scalar A = 3.0; result = ILMath.sort(A, out ind, 0, true); if (result != 3.0) { throw new Exception("invalid values: scalar"); } if (ind != 0.0) { throw new Exception("invalid indices"); } // test empty A = ILArray <double> .empty(); if (!ILMath.sort(A, out ind, 0, true).IsEmpty) { throw new Exception("invalid values: empty"); } if (!ind.IsEmpty) { throw new Exception("invalid indices"); } Success(); } catch (Exception e) { Error(0, e.Message); } }
public void Test_Sum() { int errorCode = 0; try { double[] data = new double[120]; for (int i = 0; i < data.Length; i++) data[i] = i+1; ILArray<double> A = new ILArray<double>(data, 6, 5, 4); ILArray<double> Res = new ILArray<double>(new double[20] { 21,57,93,129,165,201,237,273,309,345,381,417,453,489,525,561,597,633,669,705}, 1, 5, 4); ILArray<double> S = ILMath.sum(A); ILPerformer p = new ILPerformer(); if (S.Dimensions.NonSingletonDimensions != 2) throw new Exception("Wrong size of Sum result! "); if (S.Dimensions.NumberOfDimensions != 3) throw new Exception("Wrong size of Sum result! "); if (S.Dimensions[0] != 1) throw new Exception("Wrong size of leading Dimension of Sum result! "); if (S.Dimensions[1] != 5) throw new Exception("Wrong size of 2. Dimension of Sum result! "); if (S.Dimensions[2] != 4) throw new Exception("Wrong size of 3. Dimension of Sum result! "); if (S.Dimensions.NumberOfElements != 20) throw new Exception("Wrong number of elements of Sum result! "); if (! Res.Equals(S)) throw new Exception("Wrong values of Sum result! "); ILArray<double> B = (ILArray<double>)A[1,"1:end;1:end;2"]; double [] data2 = new double[4] { 350,380,410,440 }; Res = new ILArray<double>(data2, 4, 1); if (!Res.Equals(ILMath.sum(B,1))) throw new Exception("Wrong values of Sum result! "); errorCode = 1; // test non-matrix, reference case A = (ILArray<double>)ILMath.counter(4,3,2).CreateReference(); if (!A.IsReference) throw new Exception("sum: test data creation failed: A should be reference array!"); Res = new double[,]{{15,18,21,24},{51,54,57,60}}; Res = ILMath.reshape(Res ,4,1,2); B = ILMath.sum(A,1); if (!Res.Equals(B)) throw new Exception("sum: invalid result (n-d, reference)"); ILArray<double> C = new ILArray<double>(data, 1, 1, 4, 5, 6); Res = new ILArray<double>(new double[30] { 10,26,42,58,74,90,106,122,138,154,170,186,202,218,234,250,266 ,282,298,314,330,346,362,378,394,410,426,442,458,474},1,1,1,5,6); p.Tic(); S = ILMath.sum(C,2); p.Toc(); if (!Res.Equals(S)) throw new Exception("Wrong values of Sum result! "); Info("Sum(1x1x4x5x6)[phy] needed: " + p.ToString()); p.Tic(); ILMath.sum(C); p.Toc(); Info("Sum(1x1x4x5x6)[phy ohne Zuweisung] needed: " + p.ToString()); errorCode = 2; C = (ILArray<double>)A.T; p.Tic(); B = ILMath.sum(C); p.Toc(); Info("Sum(5x4x6)[Ref] needed: " + p.ToString()); p.Tic(); ILMath.sum(C); p.Toc(); Info("Sum(5x4x6)[Ref ohne Zuweisung] needed: " + p.ToString()); data = new double[8]{15,51,18,54,21,57,24,60}; Res = new ILArray<double>(data,1,2,4); if (!Res.Equals(B)) throw new Exception("Wrong values of Sum result! "); errorCode = 3; // big array: data = new double[1000 * 1000 * 10]; A = new ILArray<double>(data,1000,1000,10); p.Tic(); C = ILMath.sum(A); p.Toc(); Info("Sum(1000x1000x10) [phy] needed: " + p.ToString()); p.Tic(); ILMath.sum(A); p.Toc(); Info("Sum(1000x1000x10) [phy ohne Zuweisung] needed: " + p.ToString()); errorCode = 4; data = new double[1000 * 1000 * 10]; A = new ILArray<double>(data, 1000000, 10); p.Tic(); C = ILMath.sum(A); p.Toc(); Info("Sum(1000000x10) [phy] needed: " + p.ToString()); p.Tic(); ILMath.sum(A); p.Toc(); Info("Sum(1000000x10) [phy ohne Zuweisung] needed: " + p.ToString()); errorCode = 5; data = new double[10 * 1000000]; A = new ILArray<double>(data, 10, 1000000); p.Tic(); C = ILMath.sum(A); p.Toc(); Info("Sum(10 x 1000000) [Phy] needed: " + p.ToString()); p.Tic(); ILMath.sum(A); p.Toc(); Info("Sum(10 x 1000000) [Phy ohne Zuweisung] needed: " + p.ToString()); errorCode = 6; data = new double[1000 * 1000 * 10]; data[1] = 1.0; A = (ILArray<double>)A.T; p.Tic(); C = ILMath.sum(A); p.Toc(); Info("Sum(1000000 x 10) [Ref] needed: " + p.ToString()); p.Tic(); ILMath.sum(A); p.Toc(); Info("Sum(1000000 x 10) [Ref ohne Zuweisung] needed: " + p.ToString()); errorCode = 7; data = new double[120]; for (int i = 0; i < data.Length; i++) data[i] = i + 1; A = new ILArray<double>(data, 6, 5, 4); data = new double[24]{65,70,75,80,85,90,215,220,225,230,235,240,365 ,370,375,380,385,390,515,520,525,530,535,540}; Res = new ILArray<double>(data, 6, 1, 4); p.Tic(); C = ILMath.sum(A,1); p.Toc(); Info("Sum(6, 5, 4) [phy, dim 2] needed: " + p.ToString()); p.Tic(); ILMath.sum(A,1); p.Toc(); Info("Sum(6, 5, 4) [phy ohne Zuweisung] needed: " + p.ToString()); if (!Res.Equals(C)) throw new Exception("Wrong values of Sum result! "); errorCode = 8; //data = new double[20000000]; //for (int i = 0; i < data.Length; i++) // data[i] = 2; A = ILMath.zeros(1,20000000) + 2.0; Res = 4.0e+07; p.Tic(); C = ILMath.sum(A); p.Toc(); Info("Sum(20000000,1) [phy] needed: " + p.ToString()); if (!Res.Equals(C)) throw new Exception("Wrong values of Sum result! "); p.Tic(); ILMath.sum(A); p.Toc(); Info("Sum(20000000,1) [phy ohne Zuw.] needed: " + p.ToString()); errorCode = 9; A.MinimumRefDimensions = 2; A.Dispose(); A = new ILArray<double>(ILMemoryPool.Pool.New<double>(20000000),1,20000000).R; p.Tic(); C = ILMath.sum(A); p.Toc(); Info("Sum(2,10000000) [ref 'vector'] needed: " + p.ToString()); p.Tic(); ILMath.sum(A); p.Toc(); Info("Sum(1,20000000) [ref vector ohne Zuw.] needed: " + p.ToString()); A = null; B = null; C = null; Res = null; errorCode = 10; Success("Test_Sum successfull"); } catch (SerializationException e) { Error("Test_Sum failed at errorCode: "+errorCode +" Reason: " + e.Message); } catch (Exception e) { Error("Test_Sum failed at errorCode: " + errorCode + " Reason: " + e.Message); } }
/* public void Test_Eye() { int errorCode = 0; try { double[] data = new double[24]; for (int i = 0; i < data.Length; i++) data[i] = i; ILArray<double> A = new ILArray<double>(data, 2, 3, 4); Success("Test_Apply successfull"); } catch (Exception e) { Error("Test_Apply failed at errorCode: " + errorCode + " Reason: " + e.Message); } } */ public void Test_Cat() { int errorCode = 0; try { double[] data = new double[24]; for (int i = 0; i < data.Length; i++) data[i] = i; ILArray<double> A = new ILArray<double>(data, 2, 3, 4); ILArray<double> B = A.GetShifted(3); ILArray<double> C = ILMath.horzcat(A, A); ILArray<double> CRef = ILMath.horzcat(B, B); double[] result = new double[48] { 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 18, 19, 20, 21, 22, 23 }; ILArray<double> Result = new ILArray<double>(result, 2, 6, 4); if (!Result.Equals(C)) { throw new Exception("Test Cat: Test result failed: invalid values detected."); } errorCode = 2; if (!Result.Equals(CRef)) { throw new Exception("Test Cat: Test result failed: invalid values detected."); } errorCode = 3; // test vertcat result = new double[72] { 0, 1, 0, 1, 0, 1, 2, 3, 2, 3, 2, 3, 4, 5, 4, 5, 4, 5, 6, 7, 6, 7, 6, 7, 8, 9, 8, 9, 8, 9, 10, 11, 10, 11, 10, 11, 12, 13, 12, 13, 12, 13, 14, 15, 14, 15, 14, 15, 16, 17, 16, 17, 16, 17, 18, 19, 18, 19, 18, 19, 20, 21, 20, 21, 20, 21, 22, 23, 22, 23, 22, 23 }; Result = new ILArray<double>(result, 6, 3, 4); C = ILMath.vertcat(A,A,A); if (!Result.Equals(C)) { throw new Exception("Test Cat: Test result failed: invalid values detected."); } errorCode = 4; CRef = ILMath.vertcat(B,B,B); if (!Result.Equals(C)) { throw new Exception("Test Cat: Test result failed: invalid values detected."); } // test large errorCode = 5; A = (ILArray<double>)ILMath.ones(1000, 1000, 10); Result = A.Concat(A, 2); if (Result.m_dimensions.NumberOfDimensions != 3 || Result.m_dimensions[0] != 1000 || Result.m_dimensions[1] != 1000 || Result.m_dimensions[2] != 20) { throw new Exception("Test Cat: Result dimension mismatch!"); } ILArray<double> dummy = ILMath.sum(Result); if (ILMath.sumall(dummy)!= 20000000) throw new Exception("sum partially wrong!"); dummy = ILMath.sum(dummy); if (ILMath.sumall(dummy)!= 20000000) throw new Exception("sum partially wrong!"); dummy = ILMath.sum(dummy); if (ILMath.sumall(dummy)!= 20000000) throw new Exception("sum partially wrong!"); if (ILMath.sum(ILMath.sum(ILMath.sum(Result))) != 20000000) { throw new Exception ("Test Cat: Invalid values detected."); } // test Reference Concatenation errorCode = 6; B = A.GetShifted(3); Result = B.Concat(B,2); if (Result.m_dimensions.NumberOfDimensions != 3 || Result.m_dimensions[0] != 1000 || Result.m_dimensions[1] != 1000 || Result.m_dimensions[2] != 20) { throw new Exception("Test Cat: Result dimension mismatch!"); } dummy = ILMath.sum(Result); if (ILMath.sumall(dummy)!= 20000000) throw new Exception("sum partially wrong!"); dummy = ILMath.sum(dummy); if (ILMath.sumall(dummy)!= 20000000) throw new Exception("sum partially wrong!"); dummy = ILMath.sum(dummy); if (ILMath.sumall(dummy)!= 20000000) throw new Exception("sum partially wrong!"); if (ILMath.sum(ILMath.sum(ILMath.sum(Result))) != 20000000) { throw new Exception ("Test Cat: Invalid values detected."); } Success("Test_Cat successfull"); } catch (Exception e) { Error("Test_Cat failed at errorCode: " + errorCode + " Reason: " + e.Message); } }
public void Test_Eye() { int errorCode = 0; try { ILArray<double> A = ILMath.eye(7, 4); double[] data = new double[28]; for (int i = 0; i < data.Length; i += 8) data[i] = 1; ILArray<double> Res = new ILArray<double>(data, 7, 4); if (!Res.Equals(A)) throw new Exception("Invalid value result: Eye should give diagonal matrix."); Success("Test_Eye successfull"); } catch (Exception e) { Error("Test_Eye failed at errorCode: " + errorCode + " Reason: " + e.Message); } }
public void Test_CopyFromArray() { int errorCode = 0; int oldRefMin = ILNumerics.Settings.ILSettings.MinimumRefDimensions; try { ILNumerics.Settings.ILSettings.MinimumRefDimensions = 4; // B = A[idx,0] ILArray<double> A = ILMath.reshape(ILMath.vector(1.0,1.0,24.0),2,3,4); ILArray<double> B = A["0,1;0,1,2;0,1,2,3"]; if (!B.Equals(A)) throw new Exception("Invalid value of result!"); errorCode = 1; B = A[1,"0,1;0,1,2;0,1,2,3"]; ILArray<double> Res = new ILArray<double>(new double[24]{1,3,5,7,9,11,13,15,17,19,21,23,2,4,6,8,10,12,14,16,18,20,22,24},3,4,2); if (!Res.Equals(B)) throw new Exception("Copy physical shifted: Invald result detected!"); ILNumerics.Settings.ILSettings.MinimumRefDimensions = oldRefMin; Success("Test_CopyFromArray successfull."); } catch (Exception e) { Error("Test_CopyFromArray failed on ErrorCode: " + errorCode + "due: " + e.Message); } finally { ILNumerics.Settings.ILSettings.MinimumRefDimensions = oldRefMin; } }
public void Test_linspace() { int errorCode = 0; try { ILArray<double> A = ILMath.linspace(20.2,3.2,-1); ILArray <double> Res = new ILArray<double>(3.2); if (!Res.Equals(A)) throw new Exception ("Test_linspace: invalid values detected!"); if (!Res.Equals(ILMath.linspace(20.2,3.2,0))) throw new Exception ("Test_linspace: invalid values detected!"); if (!Res.Equals(ILMath.linspace(20.2,3.2,1))) throw new Exception ("Test_linspace: invalid values detected!"); errorCode = 1; A = ILMath.linspace(20.2,3.2,6); Res = new ILArray<double>(new double[]{20.20000, 16.80000, 13.40000, 10.00000, 6.60000, 3.20000}); if (ILMath.norm(A - Res) > ILMath.MachineParameterDouble.eps * 10) throw new Exception ("Test_linspace: invalid values detected!"); errorCode = 2; A = ILMath.linspace(20.2,3.2); if (A.Length != 100) throw new ILArgumentException("linspace (start,end) should return 100 elements"); Success(); }catch(Exception e) { Error(errorCode,e.Message); } }
public void Test_Deserialize() { int errorCode = 0; try { double[] data = new double[1000 * 1000 * 10]; String filename = "ILDoubleArray_SerializeTest1.ils"; for (int i = 0; i < data.Length; i++) { data[i] = (double)i; } ILArray <double> A = new ILArray <double>(data, 1000, 1000, 10); BinaryFormatter bf = new BinaryFormatter(); FileStream fs = new FileStream(filename, FileMode.Open); Info("Deserializing from file: (please wait...)"); ILArray <double> ADS = (ILArray <double>)bf.Deserialize(fs); fs.Close(); FileInfo fi = new FileInfo(filename); Info("Deserialized from file: [1000 x 1000 x 10] => " + ((int)(fi.Length / 1024)) + "kB"); if (!ADS.Equals(A)) { throw new Exception("Values did not match! "); } // create reference storage from smaler range -> should Detach() errorCode = 1; filename = "ILDoubleArray_SerializeTest2.ils"; fs = new FileStream(filename, FileMode.Open); ILArray <double> AR1 = (ILArray <double>)A["0:600;30:400;0:end"]; ILArray <double> AR1DS = (ILArray <double>)bf.Deserialize(fs); fs.Close(); fi = new FileInfo(filename); Info("Deserialized from file: [600 x 360 x 10] => " + ((int)(fi.Length / 1024)) + "kB"); if (!AR1DS.Equals(AR1)) { throw new Exception("Values did not match! "); } // if reference storage saved effective memory - keep it as reference errorCode = 2; filename = "ILDoubleArray_SerializeTest3.ils"; fs = new FileStream(filename, FileMode.Open); ILArray <double> AR2 = (ILArray <double>)A["0:end,0:end;0:end,0:end;0:end"]; ILArray <double> AR2DS = (ILArray <double>)bf.Deserialize(fs); fs.Close(); fi = new FileInfo(filename); Info("Deserialized from file: [2000 x 2000 x 20] => " + ((int)(fi.Length / 1024)) + "kB"); if (!AR2DS.Equals(AR2)) { throw new Exception("Values did not match! "); } // test if small reference would NOT store full data array errorCode = 4; filename = "ILDoubleArray_SerializeTest4.ils"; fs = new FileStream(filename, FileMode.Open); ILArray <double> AR3 = (ILArray <double>)A["3;3;1"]; ILArray <double> AR3DS = (ILArray <double>)bf.Deserialize(fs); fs.Close(); fi = new FileInfo(filename); Info("Deserialized from file: [1 x 1] => " + ((int)(fi.Length / 1024)) + "kB"); if (!AR3DS.Equals(AR3)) { throw new Exception("Values did not match! "); } errorCode = 5; File.Delete("ILDoubleArray_SerializeTest1.ils"); File.Delete("ILDoubleArray_SerializeTest2.ils"); File.Delete("ILDoubleArray_SerializeTest3.ils"); File.Delete("ILDoubleArray_SerializeTest4.ils"); Success("Test_Deserialize successfull"); } catch (SerializationException e) { Error("Test_Deserialize failed on ErrorCode: " + errorCode + "due: " + e.Message); } catch (Exception e) { Error("Test_Deserialize failed on ErrorCode: " + errorCode + "due: " + e.Message); } }