Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

76
LINES

< > BotCompany Repo | #1035700 // InterpolatedDoubleArray

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (9488L/52K).

persistable sclass InterpolatedDoubleArray is IntSize {
  int[] indices;
  double[] values;
  
  *(int[] *indices, double[] *values) {}
  
  public int size aka length() {
    ret empty(indices) ? 0 : last(indices)+1;
  }
  
  int nPillars() {
    ret l(indices);
  }
  
  double[] toDoubleArray aka get() {
    int n = length();
    double[] array = new[n];
    for (int i = 0; i < indices.length; i++) {
      int iEnd = indices[i];
      double value = values[i];
      array[iEnd] = value;
      if (i > 0) {
        int iStart = indices[i-1];
        if (iStart+1 < iEnd) {
          double startValue = values[i-1];
          double step = (value-startValue)/(iEnd-iStart), val = startValue;
          for (int j = iStart+1; j < iEnd; j++) {
            val += step;
            array[j] = val;
          }
        }
      }
    }
    ret array;
  }
  
  int[] rounded() {
    ret iroundDoubleArray(get());
  }
  
  double[] indicesAndValues() {
    int n = nPillars();
    double[] array = new[n*2];
    for i to n: {
      array[i*2] = indices[i];
      array[i*2+1] = values[i];
    }
    ret array;
  }
  
  double[] indicesAndValues_withoutFirstAndLastIndex() {
    int n = nPillars();
    double[] array = new[max(1, n*2-2)];
    int j = 0;
    for i to n: {
      if (i != 0 && i != n-1)
        array[j++] = indices[i];
      array[j++] = values[i];
    }
    ret array;
  }
  
  int nInts_withoutFirstAndLastIndex() {
    ret max(1, nPillars()*2-2);
  }
  
  int nInts() {
    ret nPillars()*2;
  }
  
  InterpolatedDoubleArray topPart(int nPillars) {
    ret new InterpolatedDoubleArray(
      takeFirst(nPillars, indices),
      takeFirst(nPillars, values));
  }
}

Author comment

Began life as a copy of #1035699

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035700
Snippet name: InterpolatedDoubleArray
Eternal ID of this version: #1035700/15
Text MD5: 2e335225d6c7b3da4423cf3f06d5c65c
Transpilation MD5: bbefca06f04cd980f9035895afee5360
Author: stefan
Category: javax / geometric compression
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-07-25 14:37:20
Source code size: 1758 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 95 / 201
Version history: 14 change(s)
Referenced in: [show references]