Repository: incubator-systemml Updated Branches: refs/heads/master 10de3f4d3 -> 69acc217d
[SYSTEMML-681] Fix single-element sequence w/ negative increment We support sequences with both positive and negative increments. However, the special case of a single-element sequence only works for positive increments (e.g., seq(1,1,1)) but fails with negative increment (e.g., seq(1,1,-1)) although both should return equivalent results. This patch fixes both compiler and runtime accordingly. Project: http://git-wip-us.apache.org/repos/asf/incubator-systemml/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-systemml/commit/69acc217 Tree: http://git-wip-us.apache.org/repos/asf/incubator-systemml/tree/69acc217 Diff: http://git-wip-us.apache.org/repos/asf/incubator-systemml/diff/69acc217 Branch: refs/heads/master Commit: 69acc217d2dc68d0c90b801a595a1579707b46d6 Parents: 10de3f4 Author: Matthias Boehm <[email protected]> Authored: Tue May 10 20:32:32 2016 -0700 Committer: Matthias Boehm <[email protected]> Committed: Tue May 10 20:32:32 2016 -0700 ---------------------------------------------------------------------- .../sysml/parser/BuiltinFunctionExpression.java | 7 ++--- .../runtime/matrix/data/LibMatrixDatagen.java | 29 +++++++++----------- 2 files changed, 15 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/69acc217/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java b/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java index cba1daf..b1efb06 100644 --- a/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java +++ b/src/main/java/org/apache/sysml/parser/BuiltinFunctionExpression.java @@ -893,17 +893,15 @@ public class BuiltinFunctionExpression extends DataIdentifier long dim1=-1, dim2=1; if ( isConstant(getFirstExpr()) && isConstant(getSecondExpr()) && (getThirdExpr() != null ? isConstant(getThirdExpr()) : true) ) { double from, to, incr; - boolean neg; try { from = getDoubleValue(getFirstExpr()); to = getDoubleValue(getSecondExpr()); // Setup the value of increment // default value: 1 if from <= to; -1 if from > to - neg = (from > to); if(getThirdExpr() == null) { expandArguments(); - _args[2] = new DoubleIdentifier((neg? -1.0 : 1.0), + _args[2] = new DoubleIdentifier(((from > to) ? -1.0 : 1.0), this.getFilename(), this.getBeginLine(), this.getBeginColumn(), this.getEndLine(), this.getEndColumn()); } @@ -914,14 +912,13 @@ public class BuiltinFunctionExpression extends DataIdentifier throw new LanguageException("Arguments for seq() must be numeric."); } - if (neg != (incr < 0)) + if( (from > to) && (incr >= 0) ) throw new LanguageException("Wrong sign for the increment in a call to seq()"); // Both end points of the range must included i.e., [from,to] both inclusive. // Note that, "to" is included only if (to-from) is perfectly divisible by incr // For example, seq(0,1,0.5) produces (0.0 0.5 1.0) whereas seq(0,1,0.6) produces only (0.0 0.6) but not (0.0 0.6 1.0) dim1 = 1 + (long)Math.floor((to-from)/incr); - //System.out.println("seq("+from+","+to+","+incr+") -> dims("+dim1+","+dim2+")"); } output.setDataType(DataType.MATRIX); output.setValueType(ValueType.DOUBLE); http://git-wip-us.apache.org/repos/asf/incubator-systemml/blob/69acc217/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java index c190e50..2955393 100644 --- a/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java +++ b/src/main/java/org/apache/sysml/runtime/matrix/data/LibMatrixDatagen.java @@ -490,28 +490,25 @@ public class LibMatrixDatagen public static void generateSequence(MatrixBlock out, double from, double to, double incr) throws DMLRuntimeException { - boolean neg = (from > to); - if (neg != (incr < 0)) - throw new DMLRuntimeException("Wrong sign for the increment in a call to seq(): from="+from+", to="+to+ ", incr="+incr); + //check valid increment value + if( (from > to && incr > 0) || incr == 0 ) + throw new DMLRuntimeException("Wrong sequence increment: from="+from+", to="+to+ ", incr="+incr); + //prepare output matrix int rows = 1 + (int)Math.floor((to-from)/incr); - int cols = 1; - out.sparse = false; // sequence matrix is always dense - out.reset(rows, cols, out.sparse); - + int cols = 1; // sequence vector always dense + out.reset(rows, cols, false); out.allocateDenseBlock(); - - //System.out.println(System.nanoTime() + ": MatrixBlockDSM.seq(): seq("+from+","+to+","+incr+") rows = " + rows); - double[] c = out.denseBlock; - - c[0] = from; - for(int i=1; i < rows; i++) { - from += incr; - c[i] = from; + + //compute sequence data + double[] c = out.denseBlock; + double cur = from; + for(int i=0; i < rows; i++) { + c[i] = cur; + cur += incr; } out.recomputeNonZeros(); - //System.out.println(System.nanoTime() + ": end of seq()"); }
