MAHOUT-1752: Implement CbindScalar operator on H2O This closes apache/mahout#144
Signed-off-by: Anand Avati <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/mahout/repo Commit: http://git-wip-us.apache.org/repos/asf/mahout/commit/60571afa Tree: http://git-wip-us.apache.org/repos/asf/mahout/tree/60571afa Diff: http://git-wip-us.apache.org/repos/asf/mahout/diff/60571afa Branch: refs/heads/mahout-0.10.x Commit: 60571afa143084397f344d1e43a9e5508f45c53c Parents: 7870462 Author: Anand Avati <[email protected]> Authored: Wed Jun 24 09:51:08 2015 -0700 Committer: Anand Avati <[email protected]> Committed: Wed Jun 24 11:29:14 2015 -0700 ---------------------------------------------------------------------- .../mahout/h2obindings/ops/CbindScalar.java | 55 ++++++++++++++++++++ .../apache/mahout/h2obindings/H2OEngine.scala | 1 + 2 files changed, 56 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/mahout/blob/60571afa/h2o/src/main/java/org/apache/mahout/h2obindings/ops/CbindScalar.java ---------------------------------------------------------------------- diff --git a/h2o/src/main/java/org/apache/mahout/h2obindings/ops/CbindScalar.java b/h2o/src/main/java/org/apache/mahout/h2obindings/ops/CbindScalar.java new file mode 100644 index 0000000..bed0d10 --- /dev/null +++ b/h2o/src/main/java/org/apache/mahout/h2obindings/ops/CbindScalar.java @@ -0,0 +1,55 @@ +/* + * 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.mahout.h2obindings.ops; + +import water.MRTask; +import water.fvec.Frame; +import water.fvec.Vec; +import water.fvec.Chunk; + +import org.apache.mahout.h2obindings.drm.H2ODrm; + +/** + * R-like cbind like operator, on a DRM and a new column containing + * the given scalar value. + */ +public class CbindScalar { + /** + * Combine the columns of DRM A with a new column storing + * the given scalar. + * + * @param drmA DRM representing matrix A. + * @param d value to be filled in new column. + * @return new DRM containing columns of A and d. + */ + public static H2ODrm exec(H2ODrm drmA, double scalar, boolean leftbind) { + Frame fra = drmA.frame; + Vec newcol = fra.anyVec().makeCon(scalar); + Vec vecs[] = new Vec[fra.vecs().length + 1]; + int d = 0; + + if (leftbind) + vecs[d++] = newcol; + for (Vec vfra : fra.vecs()) + vecs[d++] = vfra; + if (!leftbind) + vecs[d++] = newcol; + + return new H2ODrm(new Frame(vecs), drmA.keys); + } +} http://git-wip-us.apache.org/repos/asf/mahout/blob/60571afa/h2o/src/main/scala/org/apache/mahout/h2obindings/H2OEngine.scala ---------------------------------------------------------------------- diff --git a/h2o/src/main/scala/org/apache/mahout/h2obindings/H2OEngine.scala b/h2o/src/main/scala/org/apache/mahout/h2obindings/H2OEngine.scala index 420c22d..4236b95 100644 --- a/h2o/src/main/scala/org/apache/mahout/h2obindings/H2OEngine.scala +++ b/h2o/src/main/scala/org/apache/mahout/h2obindings/H2OEngine.scala @@ -107,6 +107,7 @@ object H2OEngine extends DistributedEngine { case op@OpTimesRightMatrix(a, m) => TimesRightMatrix.exec(tr2phys(a)(op.classTagA), m) // Non arithmetic case op@OpCbind(a, b) => Cbind.exec(tr2phys(a)(op.classTagA), tr2phys(b)(op.classTagB)) + case op@OpCbindScalar(a, d, left) => CbindScalar.exec(tr2phys(a)(op.classTagA), d, left) case op@OpRbind(a, b) => Rbind.exec(tr2phys(a)(op.classTagA), tr2phys(b)(op.classTagB)) case op@OpRowRange(a, r) => RowRange.exec(tr2phys(a)(op.classTagA), r) // Custom operators
