public static void RowPermute <T>(Narray <T> data, Narray <int> permutation) { CHECK_ARG(data.Dim(0) == permutation.Length(), "data.Dim(0) == permutation.Length()"); Narray <bool> finished = new Narray <bool>(data.Dim(0)); finished.Fill(false); for (int start = 0; start < finished.Length(); start++) { if (finished[start]) { continue; } int index = start; Narray <T> value = new Narray <T>(); RowCopy(value, data, index); for ( ; ;) { int next = permutation[index]; if (next == start) { break; } RowCopy(data, index, next); index = next; CHECK_ARG(!finished[index], "!finished[index]"); finished[index] = true; index = next; } RowCopy(data, index, value); finished[index] = true; } }
public static void RowGet <T, S>(Narray <T> outv, Narray <S> data, int row) { outv.Resize(data.Dim(1)); for (int i = 0; i < outv.Length(); i++) { outv[i] = (T)Convert.ChangeType(data[row, i], typeof(T)); } }
/// <summary> /// Reverse an array /// </summary> public static void Reverse <T>(Narray <T> outa, Narray <T> ina) { outa.Clear(); for (int i = ina.Length() - 1; i >= 0; i--) { outa.Push(ina[i]); } }
public static void RowCopy <T>(Narray <T> a, int i, Narray <T> b) { CHECK_ARG(a.Dim(1) == b.Length(), "a.Dim(1) == b.Length()"); for (int k = 0; k < a.Dim(1); k++) { a[i, k] = b[k]; } }
public static void RowPush <T>(Narray <T> table, Narray <T> data) { if (table.Length1d() == 0) { table.Copy(data); table.Reshape(1, table.Length()); return; } CHECK_ARG(table.Dim(1) == data.Length(), "table.Dim(1) == data.Length()"); table.Reserve(table.Length1d() + data.Length()); table.SetDims(table.Dim(0) + 1, table.Dim(1), 0, 0); int irow = table.Dim(0) - 1; for (int k = 0; k < table.Dim(1); k++) { table[irow, k] = data.UnsafeAt1d(k); } }
/// <summary> /// Quicksort an array, generating a permutation of the indexes. /// </summary> public static void Quicksort <T>(Narray <int> outindex, Narray <T> values) { outindex.Resize(values.Length()); for (int i = 0; i < values.Length(); i++) { outindex[i] = i; } Quicksort(outindex, values, 0, outindex.Length()); }
/// <summary> /// Find unique elements. /// </summary> public static void Uniq <T>(Narray <T> values) { if (values.Length() == 0) { return; } Quicksort(values); int j = 1; for (int i = 1; i < values.Length(); i++) { if (values[i].Equals(values[j - 1])) { continue; } values[j++] = values[i]; } values.Truncate(j); }
public static int first_index_of(Narray <int> a, int target) { for (int i = 0; i < a.Length(); i++) { if (a[i] == target) { return(i); } } return(-1); }
public static int first_index_of <T>(Narray <T> a, T target) { for (int i = 0; i < a.Length(); i++) { if (a[i].Equals(target)) { return(i); } } return(-1); }
/// <summary> /// Original name: randomly_permute /// </summary> public static void RandomlyPermute <T>(Narray <T> v) { int n = v.Length(); for (int i = 0; i < n - 1; i++) { int target = DRandomizer.Default.nrand() % (n - i) + i; T temp = v[target]; v[target] = v[i]; v[i] = temp; } }
public static void Shuffle <T>(Narray <T> values) { Floatarray temp = new Floatarray(values.Length()); Intarray index = new Intarray(); for (int i = 0; i < temp.Length(); i++) { temp.UnsafePut1d(i, DRandomizer.Default.drand()); } Quicksort(index, temp); Permute(values, index); }
public static float Median(Narray <int> a) { Narray <int> s = new Narray <int>(); s.Copy(a); s.Reshape(s.Length1d()); Quicksort(s); int n = s.Length(); if (n == 0) { return(0); } if ((n % 2) > 0) { return(s[n / 2]); } else { return((s[n / 2 - 1] + s[n / 2]) / 2.0f); } }
/// <summary> /// Permute the elements of an array given a permutation. /// </summary> public static void Permute <T>(Narray <T> data, Narray <int> permutation) { if (!data.SameDims(permutation)) { throw new Exception("CHECK_ARG: data.SameDims(permutation)"); } Narray <bool> finished = new Narray <bool>(data.Length()); finished.Fill(false); for (int start = 0; start < finished.Length(); start++) { if (finished[start]) { continue; } int index = start; T value = data[index]; for ( ; ;) { int next = permutation[index]; if (next == start) { break; } data[index] = data[next]; index = next; //CHECK_ARG(!finished[index] && "not a permutation"); if (finished[index]) { throw new Exception("CHECK_ARG: !finished[index]"); } finished[index] = true; index = next; } data[index] = value; finished[index] = true; } }
public int Length() { return(data.Length()); }
public static void Quicksort <T>(Narray <T> values) { Quicksort(values, 0, values.Length()); }