Mancer1 commented on code in PR #2560:
URL: https://github.com/apache/systemds/pull/2560#discussion_r3753718618


##########
src/main/java/org/apache/sysds/runtime/compress/colgroup/functional/PiecewiseLinearUtils.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.sysds.runtime.compress.colgroup.functional;
+
+import org.apache.sysds.runtime.compress.CompressionSettings;
+import org.apache.sysds.runtime.matrix.data.MatrixBlock;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+public class PiecewiseLinearUtils {
+       /**
+        * Utility methods for piecewise linear compression of matric columns 
supports compression used the segmented least
+        * squares algorithm which is implemented with dynamic programming and 
a successive method, which puts all values in
+        * a segment till the target loss is exceeded
+        */
+
+       private PiecewiseLinearUtils() {
+       }
+
+       public static final class SegmentedRegression {
+               private final int[] breakpoints;
+               private final double[] slopes;
+               private final double[] intercepts;
+
+               public SegmentedRegression(int[] breakpoints, double[] slopes, 
double[] intercepts) {
+                       this.breakpoints = breakpoints;
+                       this.slopes = slopes;
+                       this.intercepts = intercepts;
+               }
+
+               public int[] getBreakpoints() {
+                       return breakpoints;
+               }
+
+               public double[] getSlopes() {
+                       return slopes;
+               }
+
+               public double[] getIntercepts() {
+                       return intercepts;
+               }
+       }
+
+       public static double[] getColumn(MatrixBlock in, int colIndex) {
+               final int numRows = in.getNumRows();
+               final double[] column = new double[numRows];
+               for(int row = 0; row < numRows; row++) {
+                       column[row] = in.get(row, colIndex);
+               }
+               return column;
+       }
+
+       public static SegmentedRegression 
compressSuccessivePiecewiseLinear(double[] column, CompressionSettings cs) {
+               // compute Breakpoints for a Column with a sukzessive 
breakpoints algorithm
+
+               final List<Integer> breakpointsList = 
computeBreakpointSuccessive(column, cs);
+               final int[] breakpoints = 
breakpointsList.stream().mapToInt(Integer::intValue).toArray();
+
+               // get values for Regression
+               final int numSeg = breakpoints.length - 1;
+               final double[] slopes = new double[numSeg];
+               final double[] intercepts = new double[numSeg];
+
+               // Regress per Segment
+               for(int seg = 0; seg < numSeg; seg++) {
+                       final int segstart = breakpoints[seg];
+                       final int segEnd = breakpoints[seg + 1];
+                       final double[] line = regressSegment(column, segstart, 
segEnd);
+                       slopes[seg] = line[0];
+                       intercepts[seg] = line[1];
+               }
+               return new SegmentedRegression(breakpoints, slopes, intercepts);
+       }

Review Comment:
   Done. Clear documentation comment above the function



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to