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); } }
private void TestQuickSortGen(ILArray <double> input, int dim, bool desc) { try { ILArray <int> A = ILMath.toint32(input); try { ILMatFile f1 = new ILMatFile("tempADebugQuickSort.mat"); A = (ILArray <int>)f1["DebugQuickSort"]; } catch (Exception) {} ILArray <int> result; System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); result = ILMath.sort(A, dim, desc); sw.Stop(); if (!isSorted(ILMath.todouble(result), dim, desc)) { //ILMatFile f = new ILMatFile(); //A.Name = "DebugQuickSort"; //f.Add(A); //System.IO.Stream s = new System.IO.FileStream("tempADebugQuickSort.mat",System.IO.FileMode.CreateNew); //f.Write(s); throw new Exception("invalid values"); } Success(input.Dimensions.ToString() + "(Int32) needed: " + sw.ElapsedMilliseconds + " ms"); } catch (Exception e) { Error(0, e.Message); } }
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); } }
/// <summary> /// Multidimensional scaling/PCoA: transform distances to points in a coordinate system. /// </summary> /// <param name="input">A matrix of pairwise distances. Zero indicates identical objects.</param> /// <returns>A matrix, the columns of which are coordinates in the nth dimension. /// The rows are in the same order as the input.</returns> public static ILArray <double> Scale(ILArray <double> input) { int n = input.Length; ILArray <double> p = ILMath.eye <double>(n, n) - ILMath.repmat(1.0 / n, n, n); ILArray <double> a = -.5 * ILMath.multiplyElem(input, input); ILArray <double> b = ILMath.multiply(p, a, p); ILArray <complex> V = ILMath.empty <complex>(); ILArray <complex> E = ILMath.eig((b + b.T) / 2, V); ILArray <int> i = ILMath.empty <int>(); ILArray <double> e = ILMath.sort(ILMath.diag(ILMath.real(E)), i); e = ILMath.flipud(e); i = ILMath.toint32(ILMath.flipud(ILMath.todouble(i))); ILArray <int> keep = ILMath.empty <int>(); for (int j = 0; j < e.Length; j++) { if (e[j] > 0.000000001) { keep.SetValue(j, keep.Length); } } ILArray <double> Y; if (ILMath.isempty(keep)) { Y = ILMath.zeros(n, 1); } else { Y = ILMath.zeros <double>(V.S[0], keep.Length); for (int j = 0; j < keep.Length; j++) { Y[ILMath.full, j] = ILMath.todouble(-V[ILMath.full, i[keep[j]]]); } Y = ILMath.multiply(Y, ILMath.diag(ILMath.sqrt(e[keep]))); } ILArray <int> maxind = ILMath.empty <int>(); ILMath.max(ILMath.abs(Y), maxind, 0); int d = Y.S[1]; ILArray <int> indices = maxind + ILMath.toint32(ILMath.array <int>(SteppedRange(0, n, (d - 1) * n))); ILArray <double> colsign = ILMath.sign(Y[indices]); for (int j = 0; j < Y.S[1]; j++) { Y[ILMath.full, j] = Y[ILMath.full, j] * colsign[j]; } return(Y); }
/// <summary>Sort a curve ascending in preparation for interpolation</summary> /// <remarks><para></para> /// </remarks> public void Sort() { if (this is ILCurve <double> ) { ILArray <double> indices; ILArray <double> xSorted = ILMath.sort(x as ILArray <double>, out indices, 0, false); ILArray <double> ySorted = (y as ILArray <double>)[indices]; x = xSorted as ILArray <BaseT>; x = ySorted as ILArray <BaseT>; } }
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); } }
protected static ILArray <double> sortrows(ILArray <double> ilArray, int sortedByColumnIndex) { ILArray <double> columnValues = ilArray[ILMath.full, sortedByColumnIndex - 1]; ILArray <int> rowsIndices = ILMath.empty <int>(); ILArray <double> sortedColumnValues = ILMath.sort(columnValues, rowsIndices); //ILArray<double> sortedArray = ilArray[ILMath.full, 0][rowsIndices]; ILArray <double> sortedArray = ILMath.empty(ilArray.Size[0], 0); for (var columnIndex = 0; columnIndex < ilArray.Size[1]; columnIndex++) { sortedArray = sortedArray.Concat(ilArray[ILMath.full, columnIndex][rowsIndices], 1); } return(sortedArray); }
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); } }
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); } }