Changeset 18234 for branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter
- Timestamp:
- 03/14/22 11:35:10 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeVectorInterpreter.cs
r18230 r18234 728 728 } 729 729 case OpCodes.SubVector: { 730 DoubleVector SubVector(DoubleVector v , double start, double end) { 731 int size = v.Count; 732 int ToIdx(double x) { 733 int idx = (int)Math.Round(x * (size - 1)); 734 return (idx % size + size) % size; // positive mod 735 } 736 int startIdx = ToIdx(start); 737 int endIdx = ToIdx(end); 738 if (startIdx <= endIdx) { 739 return v.SubVector(startIdx, count: endIdx - startIdx + 1); // incl end 740 } else { // wrap around 741 var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx) + 1); 742 v.CopySubVectorTo(resultVector, sourceIndex: startIdx, targetIndex: 0, count: size - startIdx); // copy [startIdx:size] to [0:size-startIdx] 743 v.CopySubVectorTo(resultVector, sourceIndex: 0, targetIndex: size - startIdx, count: endIdx); // copy [0:endIdx] to [size-startIdx:size] 744 return resultVector; 745 } 730 DoubleVector SubVector(DoubleVector v , double start, double end) { 731 int startIdx = ToVectorIdx(start, v.Count), endIdx = ToVectorIdx(end, v.Count); 732 return RoundTripSubVector(v, startIdx, endIdx); 746 733 } 747 734 var cur = Evaluate(dataset, ref row, state, traceDict); … … 756 743 case OpCodes.SubVectorSubtree: { 757 744 DoubleVector SubVector(DoubleVector v, double start, double end) { 758 int Mod(int x, int m) => (x % m + m) % m; 759 int startIdx = Mod((int)Math.Round(start * v.Count), v.Count); 760 int endIdx = Mod((int)Math.Round(end * v.Count), v.Count); 761 int size = v.Count; 762 if (startIdx < endIdx) { 763 return v.SubVector(startIdx, count: endIdx - startIdx); 764 } else { // wrap around 765 var resultVector = DoubleVector.Build.Dense(size: size - (startIdx - endIdx)); 766 v.CopySubVectorTo(resultVector, startIdx, 0, size - startIdx); // copy [startIdx:size] to [0:size-startIdx] 767 v.CopySubVectorTo(resultVector, 0, size - startIdx, endIdx); // copy [0:endIdx] to [size-startIdx:size] 768 return resultVector; 769 } 745 int startIdx = ToVectorIdx(start, v.Count), endIdx = ToVectorIdx(end, v.Count); 746 return RoundTripSubVector(v, startIdx, endIdx); 770 747 } 771 748 var cur = Evaluate(dataset, ref row, state, traceDict); … … 1187 1164 } 1188 1165 1166 public static int ToVectorIdx(double relPosition, int size) { 1167 int idx = (int)Math.Round(relPosition * (size - 1)); 1168 return (idx % size + size) % size; // positive mod 1169 } 1170 1171 public static IEnumerable<Tuple<int, int>>/*start, count*/ GetVectorSlices(int startIdx, int endIdx, int size) { 1172 if (startIdx <= endIdx) { 1173 yield return Tuple.Create(startIdx, endIdx - startIdx + 1); // incl end 1174 } else { 1175 yield return Tuple.Create(startIdx, size - startIdx); // startIdx to end of vector 1176 yield return Tuple.Create(0, endIdx); // start to endIdx of vector 1177 } 1178 } 1179 1180 public static DoubleVector RoundTripSubVector(DoubleVector v, int startIdx, int endIdx) { 1181 var slices = GetVectorSlices(startIdx, endIdx, v.Count).ToList(); 1182 var totalSize = slices.Sum(s => s.Item2); 1183 var resultVector = DoubleVector.Build.Dense(size: totalSize); 1184 1185 var curIdx = 0; 1186 foreach (var slice in slices) { 1187 int start = slice.Item1, count = slice.Item2; 1188 v.CopySubVectorTo(resultVector, sourceIndex: start, targetIndex: curIdx, count: count); 1189 curIdx += count; 1190 } 1191 return resultVector; 1192 } 1193 1189 1194 private static int LongestStrikeAbove(DoubleVector v, double threshold) { 1190 1195 int longestStrike = 0, currentStrike = 0;
Note: See TracChangeset
for help on using the changeset viewer.