Layer(Layer that) { weights = new Matrix(that.weights); bias = Vec.copy(that.bias); net = Vec.copy(that.net); activation = Vec.copy(that.activation); error = Vec.copy(that.error); }
public StringBuilder toString(StringBuilder sb, String[] fs, long idx) { Vec vecs[] = vecs(); for (int c = 0; c < fs.length; c++) { Vec vec = vecs[c]; if (vec.isEnum()) { String s = "----------"; if (!vec.isNA(idx)) { int x = (int) vec.at8(idx); if (x >= 0 && x < vec._domain.length) s = vec._domain[x]; } sb.append(String.format(fs[c], s)); } else if (vec.isInt()) { if (vec.isNA(idx)) { Chunk C = vec.elem2BV(0); // 1st Chunk int len = C.pformat_len0(); // Printable width for (int i = 0; i < len; i++) sb.append('-'); } else { try { sb.append(String.format(fs[c], vec.at8(idx))); } catch (IllegalFormatException ife) { System.out.println("Format: " + fs[c] + " col=" + c + " not for ints"); ife.printStackTrace(); } } } else { sb.append(String.format(fs[c], vec.at(idx))); if (vec.isNA(idx)) sb.append(' '); } sb.append(' '); // Column seperator } sb.append('\n'); return sb; }
/** * Creates a new sparse vector by copying the values from another * * @param toCopy the vector to copy the values of */ public SparseVector(Vec toCopy) { this(toCopy.length(), toCopy.nnz()); for (IndexValue iv : toCopy) { indexes[used] = iv.getIndex(); values[used++] = iv.getValue(); } }
@Override void computeTangent(KeyFrame prev, KeyFrame next) { tgPVec = Vec.multiply(Vec.subtract(next.position(), prev.position()), 0.5f); tgQuat = Quat.squadTangent( (Quat) prev.orientation(), (Quat) orientation(), (Quat) next.orientation()); }
public void dropInteractions() { // only called to cleanup the InteractionWrappedVecs! if (_interactions != null) { Vec[] vecs = _adaptedFrame.remove(_interactionVecs); for (Vec v : vecs) v.remove(); _interactions = null; } }
public void nextStep() { synchronized (this) { if (needRecalc) { recalcImpact(); needRecalc = false; } for (iter.x = 0; iter.x < mSize; ++iter.x) { for (iter.y = 0; iter.y < nSize; ++iter.y) { final double imp = impMat[iter.x][iter.y]; if (rules.dieCond(imp)) { fldNew[iter.x][iter.y] = DEAD; } else if (rules.birthCond(imp)) { fldNew[iter.x][iter.y] = ALIVE; } } } swap(); needRecalc = true; recalcImpact(); } setChanged(); notifyObservers(new FieldUpdate(this)); }
public Matrix solve(Matrix B) { int m = LU.m, n = LU.n; if (B.m != m) throw new IllegalArgumentException("Matrix row dimensions must agree."); if (isSingular()) throw new RuntimeException("Matrix is singular"); // Copy right hand side with pivoting Matrix X = B.copy(); X.permuteRows(piv); // Solve L*Y = B(piv,:) for (int k = 0; k < n; k++) { Vec Xrowk = X.getRow(k); for (int i = k + 1; i < n; i++) { Vec Xrowi = X.getRow(i); Xrowk.addTo(Xrowi, -LU.get(i, k)); } } // Solve U*X = Y; for (int k = n - 1; k >= 0; k--) { Vec Xrowk = X.getRow(k); Xrowk.timesEquals(1.0 / LU.get(k, k)); for (int i = 0; i < k; i++) { Vec Xrowi = X.getRow(i); Xrowk.addTo(Xrowi, -LU.get(i, k)); } } return X; }
Vec vec_by_mouse(int x, int y) { Vec ans = new Vec(); ans.x = (x - dim.width / 2.) / size; ans.y = -(y - dim.height / 2.) / size; return ans; }
@Override public void map(Chunk[] ix, NewChunk[] ncs) { final Vec[] vecs = new Vec[_cols.length]; final Vec anyv = _base.anyVec(); final long nrow = anyv.length(); long r = ix[0].at80(0); int last_ci = anyv.elem2ChunkIdx(r < nrow ? r : 0); // memoize the last chunk index long last_c0 = anyv._espc[last_ci]; // ... last chunk start long last_c1 = anyv._espc[last_ci + 1]; // ... last chunk end Chunk[] last_cs = new Chunk[vecs.length]; // ... last chunks for (int c = 0; c < _cols.length; c++) { vecs[c] = _base.vecs()[_cols[c]]; last_cs[c] = vecs[c].elem2BV(last_ci); } for (int i = 0; i < ix[0]._len; i++) { // select one row r = ix[0].at80(i) - 1; // next row to select if (r < 0) continue; if (r >= nrow) { for (int c = 0; c < vecs.length; c++) ncs[c].addNum(Double.NaN); } else { if (r < last_c0 || r >= last_c1) { last_ci = anyv.elem2ChunkIdx(r); last_c0 = anyv._espc[last_ci]; last_c1 = anyv._espc[last_ci + 1]; for (int c = 0; c < vecs.length; c++) last_cs[c] = vecs[c].elem2BV(last_ci); } for (int c = 0; c < vecs.length; c++) ncs[c].addNum(last_cs[c].at(r)); } } }
@Override public void mutablePairwiseDivide(Vec b) { if (this.length() != b.length()) throw new ArithmeticException("Vectors must have the same length"); clearCaches(); for (int i = 0; i < used; i++) values[i] /= b.get(indexes[i]); // zeros stay zero }
Vec wall_power(Ball p) { Vec ans = new Vec(); is_colide = false; ans.x = wall_power2(p.pos.x); ans.y = wall_power2(p.pos.y); if (is_colide) ans.sub_to(p.speed.mult(10)); return ans; }
public static void assertValues(Vec v, String[] expValues) { Assert.assertEquals("Number of rows", expValues.length, v.length()); BufferedString tmpStr = new BufferedString(); for (int i = 0; i < v.length(); i++) { if (v.isNA(i)) Assert.assertEquals("NAs should match", null, expValues[i]); else Assert.assertEquals("Values should match", expValues[i], v.atStr(tmpStr, i).toString()); } }
public Tensor3 setRow(final int i, final int j, final Vec val) { if (val.dim() != dim3) throw new IllegalArgumentException( "val.dim() != dim3, val.dim() = " + val.dim() + ", dim3 = " + dim3); final int index = index(i, j, 0); for (int l = 0; l < dim3; l++) vec.set(index + l, val.get(l)); return this; }
public Vec replace(int col, Vec nv) { assert col < _names.length; Vec rv = vecs()[col]; assert rv.group().equals(nv.group()); _vecs[col] = nv; _keys[col] = nv._key; if (DKV.get(nv._key) == null) // If not already in KV, put it there DKV.put(nv._key, nv); return rv; }
/** * Check that the vectors are all compatible. All Vecs have their content sharded using same * number of rows per chunk. */ public void checkCompatible() { try { Vec v0 = anyVec(); int nchunks = v0.nChunks(); for (Vec vec : vecs()) { if (vec instanceof AppendableVec) continue; // New Vectors are endlessly compatible if (vec.nChunks() != nchunks) throw new IllegalArgumentException( "Vectors different numbers of chunks, " + nchunks + " and " + vec.nChunks()); } // Also check each chunk has same rows for (int i = 0; i < nchunks; i++) { long es = v0.chunk2StartElem(i); for (Vec vec : vecs()) if (!(vec instanceof AppendableVec) && vec.chunk2StartElem(i) != es) throw new IllegalArgumentException( "Vector chunks different numbers of rows, " + es + " and " + vec.chunk2StartElem(i)); } } catch (Throwable ex) { Throwables.propagate(ex); } }
@Test public void test_setNA() { // Create a vec with one chunk with 15 elements, and set its numbers Vec vec = new Vec(Vec.newKey(), new long[] {0, 15}).makeZeros(1, null, null, null, null)[0]; int[] vals = new int[] {0, 3, 0, 6, 0, 0, 0, -32769, 0, 12, 234, 32765, 0, 0, 19}; Vec.Writer w = vec.open(); for (int i = 0; i < vals.length; ++i) w.set(i, vals[i]); w.close(); Chunk cc = vec.chunkForChunkIdx(0); assert cc instanceof C2SChunk; Futures fs = new Futures(); fs.blockForPending(); for (int i = 0; i < vals.length; ++i) Assert.assertEquals(vals[i], cc.at80(i)); for (int i = 0; i < vals.length; ++i) Assert.assertEquals(vals[i], cc.at8(i)); int[] NAs = new int[] {1, 5, 2}; int[] notNAs = new int[] {0, 3, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14}; for (int na : NAs) cc.setNA(na); for (int na : NAs) Assert.assertTrue(cc.isNA0(na)); for (int na : NAs) Assert.assertTrue(cc.isNA(na)); for (int notna : notNAs) Assert.assertTrue(!cc.isNA0(notna)); for (int notna : notNAs) Assert.assertTrue(!cc.isNA(notna)); NewChunk nc = new NewChunk(null, 0); cc.inflate_impl(nc); nc.values(0, nc.len()); Assert.assertEquals(vals.length, nc.sparseLen()); Assert.assertEquals(vals.length, nc.len()); Iterator<NewChunk.Value> it = nc.values(0, vals.length); for (int i = 0; i < vals.length; ++i) Assert.assertTrue(it.next().rowId0() == i); Assert.assertTrue(!it.hasNext()); for (int na : NAs) Assert.assertTrue(cc.isNA0(na)); for (int na : NAs) Assert.assertTrue(cc.isNA(na)); for (int notna : notNAs) Assert.assertTrue(!cc.isNA0(notna)); for (int notna : notNAs) Assert.assertTrue(!cc.isNA(notna)); Chunk cc2 = nc.compress(); Assert.assertEquals(vals.length, cc.len()); Assert.assertTrue(cc2 instanceof C2SChunk); for (int na : NAs) Assert.assertTrue(cc.isNA0(na)); for (int na : NAs) Assert.assertTrue(cc.isNA(na)); for (int notna : notNAs) Assert.assertTrue(!cc.isNA0(notna)); for (int notna : notNAs) Assert.assertTrue(!cc.isNA(notna)); Assert.assertTrue(Arrays.equals(cc._mem, cc2._mem)); vec.remove(); }
Vec calc_collide_power(Ball p1, Ball p2, double dist) { if (dist > radius * 2) return new Vec(); // Friday, August 29, 2008 15:26:33: stickiness bug fix Vec speed_diff = p2.speed.sub(p1.speed); double force = 1000 * (dist - radius * 2); Vec npos1 = p1.pos.div(dist); // normalized Vec npos2 = p2.pos.div(dist); force += 10 * speed_diff.dot_mult(npos2.sub(npos1)); Vec ans = npos2.sub(npos1).mult(force); return ans; /* colide_power_x=force*(x2-x1)/dist; colide_power_y=force*(y2-y1)/dist;*/ }
@Override protected void setupLocal() { // Precompute the first input chunk index and start row inside that chunk for this partition Vec anyInVec = _srcVecs[0]; long[] partSizes = Utils.partitione(anyInVec.length(), _ratios); long pnrows = 0; for (int p = 0; p < _partIdx; p++) pnrows += partSizes[p]; long[] espc = anyInVec._espc; while (_pcidx < espc.length - 1 && (pnrows -= (espc[_pcidx + 1] - espc[_pcidx])) > 0) _pcidx++; assert pnrows <= 0; _psrow = (int) (pnrows + espc[_pcidx + 1] - espc[_pcidx]); }
// Make vector templates for all output frame vectors private Vec[][] makeTemplates(Frame dataset, float[] ratios) { Vec anyVec = dataset.anyVec(); final long[][] espcPerSplit = computeEspcPerSplit(anyVec._espc, anyVec.length(), ratios); final int num = dataset.numCols(); // number of columns in input frame final int nsplits = espcPerSplit.length; // number of splits final String[][] domains = dataset.domains(); // domains Vec[][] t = new Vec[nsplits][ /*num*/]; // resulting vectors for all for (int i = 0; i < nsplits; i++) { // vectors for j-th split t[i] = new Vec(Vec.newKey(), espcPerSplit[i /*-th split*/]).makeZeros(num, domains); } return t; }
@Override public void multiply(double c, Matrix A, Vec b) { if (this.length() != A.rows()) throw new ArithmeticException("Vector x Matrix dimensions do not agree"); else if (b.length() != A.cols()) throw new ArithmeticException("Destination vector is not the right size"); for (int i = 0; i < used; i++) { double val = c * this.values[i]; int index = this.indexes[i]; for (int j = 0; j < A.cols(); j++) b.increment(j, val * A.get(index, j)); } }
void animate() { dim = getSize(); size = (int) (Math.min(dim.height, dim.width) / 2.2); timer.tell_time(); if (timer.time_diff == 0) return; // not enought time has passed, dont animate-crach fix dragged_speed = dragged_vec.sub(last_dragged_vec).div(timer.time_diff); last_dragged_vec = dragged_vec; if (dragged_ball != -1) { balls.get2(dragged_ball).pos = dragged_vec.add(find_offset).trim(-1, 1); balls.get2(dragged_ball).speed = dragged_speed; } balls = new WorldAnimate().calc_new_frame(balls, springs, RADIUS, timer); }
public Vec get_displacement() { double min_signed_overlap = -get_overlap(axes[0]); double min_overlap = min_signed_overlap < 0 ? -min_signed_overlap : min_signed_overlap; Vec min_axis = axes[0]; for (int i = 1; i < axes.length; i++) { double overlap = -get_overlap(axes[i]); double unsigned_overlap = overlap < 0 ? -overlap : overlap; if (unsigned_overlap < min_overlap) { min_overlap = unsigned_overlap; min_signed_overlap = overlap; min_axis = axes[i]; } } return min_axis.mul(min_signed_overlap); }
@Override public void compute2() { _in.read_lock(_jobKey); // simply create a bogus new vector (don't even put it into KV) with appropriate number of lines // per chunk and then use it as a source to do multiple makeZero calls // to create empty vecs and than call RebalanceTask on each one of them. // RebalanceTask will fetch the appropriate src chunks and fetch the data from them. int rpc = (int) (_in.numRows() / _nchunks); int rem = (int) (_in.numRows() % _nchunks); long[] espc = new long[_nchunks + 1]; Arrays.fill(espc, rpc); for (int i = 0; i < rem; ++i) ++espc[i]; long sum = 0; for (int i = 0; i < espc.length; ++i) { long s = espc[i]; espc[i] = sum; sum += s; } assert espc[espc.length - 1] == _in.numRows() : "unexpected number of rows, expected " + _in.numRows() + ", got " + espc[espc.length - 1]; final Vec[] srcVecs = _in.vecs(); _out = new Frame( _okey, _in.names(), new Vec(Vec.newKey(), espc).makeZeros(srcVecs.length, _in.domains())); _out.delete_and_lock(_jobKey); new RebalanceTask(this, srcVecs).asyncExec(_out); }
@Test public void testPCTiles() { // Simplified version of tests in runit_quantile_1_golden.R. There we test // probs=seq(0,1,by=0.01) Vec vec = vec(5, 8, 9, 12, 13, 16, 18, 23, 27, 28, 30, 31, 33, 34, 43, 45, 48, 161); double[] pctiles = vec.pctiles(); // System.out.println(java.util.Arrays.toString(pctiles)); Assert.assertEquals(13.75, pctiles[4], 1e-5); vec.remove(); vec = vec(5, 8, 9, 9, 9, 16, 18, 23, 27, 28, 30, 31, 31, 34, 43, 43, 43, 161); pctiles = vec.pctiles(); // System.out.println(java.util.Arrays.toString(pctiles)); Assert.assertEquals(10.75, pctiles[4], 1e-5); vec.remove(); }
Vec calc_spring_power(Ball p1, Ball p2) { double dist = p1.pos.calc_dist(p2.pos); // if (abs(dist-STRING_LEN)<.1) // return; Vec speed_diff = p2.speed.sub(p1.speed); double force = 1000 * (dist - STRING_LEN); Vec npos1 = p1.pos.div(dist); // normalized Vec npos2 = p2.pos.div(dist); force += 100 * speed_diff.dot_mult(npos2.sub(npos1)); Vec ans = npos2.sub(npos1).mult(force); return ans; // force*=force; // colide_power_x=(x2-x1)/dist*force; // colide_power_y=(y2-y1)/dist*force; }
public static Vec Normalize (Vec v) { double one_over_len = 1.0 / v.Length (); return new Vec (v.x * one_over_len, v.y * one_over_len, v.z * one_over_len); }
public Frame(String[] names, Vec[] vecs) { // assert names==null || names.length == vecs.length : "Number of columns does not match to // number of cols' names."; _names = names; _vecs = vecs; _keys = new Key[vecs.length]; for (int i = 0; i < vecs.length; i++) { Key k = _keys[i] = vecs[i]._key; if (DKV.get(k) == null) // If not already in KV, put it there DKV.put(k, vecs[i]); } Vec v0 = anyVec(); if (v0 == null) return; VectorGroup grp = v0.group(); for (int i = 0; i < vecs.length; i++) assert grp.equals(vecs[i].group()); }
@Override Val apply(Env env, Env.StackHelp stk, AST asts[]) { // Compute the variable args. Find the common row count Val vals[] = new Val[asts.length]; Vec vec = null; for (int i = 1; i < asts.length; i++) { vals[i] = stk.track(asts[i].exec(env)); if (vals[i].isFrame()) { Vec anyvec = vals[i].getFrame().anyVec(); if (anyvec == null) continue; // Ignore the empty frame if (vec == null) vec = anyvec; else if (vec.length() != anyvec.length()) throw new IllegalArgumentException( "cbind frames must have all the same rows, found " + vec.length() + " and " + anyvec.length() + " rows."); } } boolean clean = false; if (vec == null) { vec = Vec.makeZero(1); clean = true; } // Default to length 1 // Populate the new Frame Frame fr = new Frame(); for (int i = 1; i < asts.length; i++) { switch (vals[i].type()) { case Val.FRM: fr.add(fr.makeCompatible(vals[i].getFrame())); break; case Val.FUN: throw H2O.unimpl(); case Val.STR: throw H2O.unimpl(); case Val.NUM: // Auto-expand scalars to fill every row double d = vals[i].getNum(); fr.add(Double.toString(d), vec.makeCon(d)); break; default: throw H2O.unimpl(); } } if (clean) vec.remove(); return new ValFrame(fr); }
@Override public void applyFunction(Function f) { if (f.f(0.0) != 0.0) super.applyFunction(f); else // Then we only need to apply it to the non zero values! { for (int i = 0; i < used; i++) values[i] = f.f(values[i]); } }
@Override public boolean equals(Object obj) { if (!(obj instanceof Vec)) return false; Vec otherVec = (Vec) obj; if (this.length() != otherVec.length()) return false; int z = 0; for (int i = 0; i < length(); i++) { // Move through until we hit the next null element, comparing the other vec to zero while (z < used && indexes[z] > i) if (otherVec.get(i++) != 0) return false; // We made it! (or are at the end). Is our non zero value the same? if (z < used && indexes[z] == i) if (values[z++] != otherVec.get(i)) return false; } return true; }