Example usage for java.io DataOutput writeDouble

List of usage examples for java.io DataOutput writeDouble

Introduction

In this page you can find the example usage for java.io DataOutput writeDouble.

Prototype

void writeDouble(double v) throws IOException;

Source Link

Document

Writes a double value, which is comprised of eight bytes, to the output stream.

Usage

From source file:ml.shifu.shifu.core.correlation.CorrelationWritable.java

@Override
public void write(DataOutput out) throws IOException {
    out.writeInt(this.columnIndex);
    out.writeDouble(this.sum);
    out.writeDouble(this.sumSquare);
    out.writeDouble(this.count);
    if (this.xySum == null) {
        out.writeInt(0);//from w  ww.  j a va 2 s . co  m
    } else {
        out.writeInt(this.xySum.length);
        for (double doub : this.xySum) {
            out.writeDouble(doub);
        }
    }

    if (this.xxSum == null) {
        out.writeInt(0);
    } else {
        out.writeInt(this.xxSum.length);
        for (double doub : this.xxSum) {
            out.writeDouble(doub);
        }
    }

    if (this.yySum == null) {
        out.writeInt(0);
    } else {
        out.writeInt(this.yySum.length);
        for (double doub : this.yySum) {
            out.writeDouble(doub);
        }
    }

    if (this.adjustCount == null) {
        out.writeInt(0);
    } else {
        out.writeInt(this.adjustCount.length);
        for (double doub : this.adjustCount) {
            out.writeDouble(doub);
        }
    }

    if (this.adjustSumX == null) {
        out.writeInt(0);
    } else {
        out.writeInt(this.adjustSumX.length);
        for (double doub : this.adjustSumX) {
            out.writeDouble(doub);
        }
    }

    if (this.adjustSumY == null) {
        out.writeInt(0);
    } else {
        out.writeInt(this.adjustSumY.length);
        for (double doub : this.adjustSumY) {
            out.writeDouble(doub);
        }
    }
}

From source file:ml.shifu.shifu.core.dtrain.dataset.PersistBasicFloatNetwork.java

private void writeDoubleArray(DataOutput out, double[] array) throws IOException {
    if (array == null) {
        out.writeInt(0);/*from   w  ww .ja  v  a 2s.c  o m*/
    } else {
        out.writeInt(array.length);
        for (double d : array) {
            out.writeDouble(d);
        }
    }
}

From source file:ml.shifu.shifu.core.dtrain.dataset.PersistBasicFloatNetwork.java

public void saveNetwork(DataOutput out, final BasicFloatNetwork network) throws IOException {
    final FlatNetwork flat = network.getStructure().getFlat();
    // write general properties
    Map<String, String> properties = network.getProperties();
    if (properties == null) {
        out.writeInt(0);/*from   w w  w .j  a  va 2 s.c  o m*/
    } else {
        out.writeInt(properties.size());
        for (Entry<String, String> entry : properties.entrySet()) {
            ml.shifu.shifu.core.dtrain.StringUtils.writeString(out, entry.getKey());
            ml.shifu.shifu.core.dtrain.StringUtils.writeString(out, entry.getValue());
        }
    }

    // write fields values in BasicFloatNetwork
    out.writeInt(flat.getBeginTraining());
    out.writeDouble(flat.getConnectionLimit());

    writeIntArray(out, flat.getContextTargetOffset());
    writeIntArray(out, flat.getContextTargetSize());

    out.writeInt(flat.getEndTraining());
    out.writeBoolean(flat.getHasContext());
    out.writeInt(flat.getInputCount());

    writeIntArray(out, flat.getLayerCounts());
    writeIntArray(out, flat.getLayerFeedCounts());
    writeIntArray(out, flat.getLayerContextCount());
    writeIntArray(out, flat.getLayerIndex());
    writeDoubleArray(out, flat.getLayerOutput());
    out.writeInt(flat.getOutputCount());
    writeIntArray(out, flat.getWeightIndex());
    writeDoubleArray(out, flat.getWeights());
    writeDoubleArray(out, flat.getBiasActivation());

    // write activation list
    out.writeInt(flat.getActivationFunctions().length);
    for (final ActivationFunction af : flat.getActivationFunctions()) {
        ml.shifu.shifu.core.dtrain.StringUtils.writeString(out, af.getClass().getSimpleName());
        writeDoubleArray(out, af.getParams());
    }
    // write sub sets
    Set<Integer> featureList = network.getFeatureSet();
    if (featureList == null || featureList.size() == 0) {
        out.writeInt(0);
    } else {
        out.writeInt(featureList.size());
        for (Integer integer : featureList) {
            out.writeInt(integer);
        }
    }
}

From source file:com.marklogic.tree.ExpandedTree.java

@Override
public void write(DataOutput out) throws IOException {
    out.writeLong(uriKey);/* w  w w .j av a2s .  c om*/
    out.writeLong(uniqKey);
    out.writeLong(linkKey);
    out.writeInt(numKeys);
    if (numKeys > 0) {
        for (long key : keys) {
            out.writeLong(key);
        }
    }
    if (atomData != null && atomData.length > 0) {
        out.writeInt(atomData.length);
        for (int i = 0; i < atomData.length; i++) {
            out.writeByte(atomData[i]);
        }
    } else {
        out.writeInt(0);
    }
    out.writeInt(atomLimit);
    if (atomIndex != null && atomIndex.length > 0) {
        for (int i = 0; i < atomIndex.length; i++) {
            out.writeInt(atomIndex[i]);
        }
    }
    if (nodeNameNameAtom != null && nodeNameNameAtom.length > 0) {
        out.writeInt(nodeNameNameAtom.length);
        for (int i = 0; i < nodeNameNameAtom.length; i++) {
            out.writeInt(nodeNameNameAtom[i]);
            out.writeInt(nodeNameNamespaceAtom[i]);
        }
    } else {
        out.writeInt(0);
    }
    out.writeInt(numNodeReps);
    if (numNodeReps > 0) {
        for (int i = 0; i < numNodeReps; i++) {
            out.writeLong(nodeOrdinal[i]);
            out.writeByte(nodeKind[i]);
            out.writeInt(nodeRepID[i]);
            out.writeInt(nodeParentNodeRepID[i]);
        }
    }
    if (elemNodeNodeNameRepID != null && elemNodeNodeNameRepID.length > 0) {
        out.writeInt(elemNodeNodeNameRepID.length);
        for (int i = 0; i < elemNodeNodeNameRepID.length; i++) {
            out.writeInt(elemNodeNodeNameRepID[i]);
            out.writeInt(elemNodeAttrNodeRepID[i]);
            out.writeInt(elemNodeChildNodeRepID[i]);
            out.writeInt(elemNodeElemDeclRepID[i]);
            out.writeInt(elemNodeNumAttributes[i]);
            out.writeInt(elemNodeNumDefaultAttrs[i]);
            out.writeInt(elemNodeNumChildren[i]);
            out.writeInt(elemNodeFlags[i]);
        }
    } else {
        out.writeInt(0);
    }
    if (attrNodeNodeNameRepID != null && attrNodeNodeNameRepID.length > 0) {
        out.writeInt(attrNodeNodeNameRepID.length);
        for (int i = 0; i < attrNodeNodeNameRepID.length; i++) {
            out.writeInt(attrNodeNodeNameRepID[i]);
            out.writeInt(attrNodeTextRepID[i]);
            out.writeInt(attrNodeAttrDeclRepID[i]);
        }
    } else {
        out.writeInt(0);
    }
    out.writeInt(numLinkNodeReps);
    if (numLinkNodeReps > 0) {
        for (int i = 0; i < numLinkNodeReps; i++) {
            out.writeLong(linkNodeKey[i]);
            out.writeLong(linkNodeNodeCount[i]);
            out.writeInt(linkNodeNodeNameRepID[i]);
            out.writeInt(linkNodeNodeRepID[i]);
        }
    }
    if (docNodeTextRepID != null && docNodeTextRepID.length > 0) {
        out.writeInt(docNodeTextRepID.length);
        for (int i = 0; i < docNodeTextRepID.length; i++) {
            out.writeInt(docNodeTextRepID[i]);
            out.writeInt(docNodeChildNodeRepID[i]);
            out.writeInt(docNodeNumChildren[i]);
        }
    } else {
        out.writeInt(0);
    }
    if (piNodeTargetAtom != null && piNodeTargetAtom.length > 0) {
        out.writeInt(piNodeTargetAtom.length);
        for (int i = 0; i < piNodeTargetAtom.length; i++) {
            out.writeInt(piNodeTargetAtom[i]);
            out.writeInt(piNodeTextRepID[i]);
        }
    } else {
        out.writeInt(0);
    }
    out.writeInt(numNSNodeReps);
    if (numNSNodeReps > 0) {
        for (int i = 0; i < numNSNodeReps; i++) {
            out.writeLong(nsNodeOrdinal[i]);
            out.writeInt(nsNodePrevNSNodeRepID[i]);
            out.writeInt(nsNodePrefixAtom[i]);
            out.writeInt(nsNodeUriAtom[i]);
        }
    }
    // skip permission node since it's not exposed to the API
    out.writeInt(uriTextRepID);
    out.writeInt(colsTextRepID);
    out.writeInt(numTextReps);
    if (numTextReps > 0) {
        for (int i = 0; i < numTextReps; i++) {
            out.writeInt(textReps[i]);
        }
    }
    if (arrayNodeTextRepID != null && arrayNodeTextRepID.length > 0) {
        out.writeInt(arrayNodeTextRepID.length);
        for (int i = 0; i < arrayNodeTextRepID.length; i++) {
            out.writeInt(arrayNodeTextRepID[i]);
            out.writeInt(arrayNodeChildNodeRepID[i]);
            out.writeInt(arrayNodeNumChildren[i]);
        }
    } else {
        out.writeInt(0);
    }
    if (doubles != null && doubles.length > 0) {
        out.writeInt(doubles.length);
        for (int i = 0; i < doubles.length; i++) {
            out.writeDouble(doubles[i]);
        }
    } else {
        out.writeInt(0);
    }

}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * //from   www .j  a  v  a2s.c  o  m
 * @param out
 * @throws IOException
 */
private void writeSparseToDense(DataOutput out) throws IOException {
    //write block type 'dense'
    out.writeByte(BlockType.DENSE_BLOCK.ordinal());

    //write data (from sparse to dense)
    if (sparseRows == null) //empty block
        for (int i = 0; i < rlen * clen; i++)
            out.writeDouble(0);
    else //existing sparse block
    {
        for (int i = 0; i < rlen; i++) {
            if (i < sparseRows.length && sparseRows[i] != null && !sparseRows[i].isEmpty()) {
                SparseRow arow = sparseRows[i];
                int alen = arow.size();
                int[] aix = arow.getIndexContainer();
                double[] avals = arow.getValueContainer();
                //foreach non-zero value, fill with 0s if required
                for (int j = 0, j2 = 0; j2 < alen; j++, j2++) {
                    for (; j < aix[j2]; j++)
                        out.writeDouble(0);
                    out.writeDouble(avals[j2]);
                }
                //remaining 0 values in row
                for (int j = aix[alen - 1] + 1; j < clen; j++)
                    out.writeDouble(0);
            } else //empty row
                for (int j = 0; j < clen; j++)
                    out.writeDouble(0);
        }
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*from w  w  w .  j ava2s. c  om*/
 * @param out
 * @throws IOException
 */
private void writeDenseBlock(DataOutput out) throws IOException {
    out.writeByte(BlockType.DENSE_BLOCK.ordinal());

    int limit = rlen * clen;
    if (out instanceof MatrixBlockDataOutput) //fast serialize
        ((MatrixBlockDataOutput) out).writeDoubleArray(limit, denseBlock);
    else //general case (if fast serialize not supported)
        for (int i = 0; i < limit; i++)
            out.writeDouble(denseBlock[i]);
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*from   www.j a  v a  2  s. c om*/
 * @param out
 * @throws IOException
 */
private void writeDenseToUltraSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, true);

    long wnnz = 0;

    if (clen > 1) //ULTRA-SPARSE BLOCK
    {
        //block: write ijv-triples
        for (int r = 0, ix = 0; r < rlen; r++)
            for (int c = 0; c < clen; c++, ix++)
                if (denseBlock[ix] != 0) {
                    out.writeInt(r);
                    out.writeInt(c);
                    out.writeDouble(denseBlock[ix]);
                    wnnz++;
                }
    } else //ULTRA-SPARSE COL
    {
        //col: write iv-pairs
        for (int r = 0; r < rlen; r++)
            if (denseBlock[r] != 0) {
                out.writeInt(r);
                out.writeDouble(denseBlock[r]);
                wnnz++;
            }
    }

    //validity check (nnz must exactly match written nnz)
    if (nonZeros != wnnz) {
        throw new IOException(
                "Invalid number of serialized non-zeros: " + wnnz + " (expected: " + nonZeros + ")");
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*from w  w  w  . j  a v a 2  s .  co  m*/
 * @param out
 * @throws IOException
 */
private void writeDenseToSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.SPARSE_BLOCK.ordinal()); //block type
    writeNnzInfo(out, false);

    int start = 0;
    for (int r = 0; r < rlen; r++) {
        //count nonzeros
        int nr = 0;
        for (int i = start; i < start + clen; i++)
            if (denseBlock[i] != 0.0)
                nr++;
        out.writeInt(nr);
        for (int c = 0; c < clen; c++) {
            if (denseBlock[start] != 0.0) {
                out.writeInt(c);
                out.writeDouble(denseBlock[start]);
            }
            start++;
        }
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * /*from  w w  w  . j  a  v a 2s  .  c om*/
 * @param out
 * @throws IOException
 */
private void writeSparseToUltraSparse(DataOutput out) throws IOException {
    out.writeByte(BlockType.ULTRA_SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, true);

    long wnnz = 0;
    if (clen > 1) //ULTRA-SPARSE BLOCK
    {
        //block: write ijv-triples
        for (int r = 0; r < Math.min(rlen, sparseRows.length); r++)
            if (sparseRows[r] != null && !sparseRows[r].isEmpty()) {
                int alen = sparseRows[r].size();
                int[] aix = sparseRows[r].getIndexContainer();
                double[] avals = sparseRows[r].getValueContainer();
                for (int j = 0; j < alen; j++) {
                    //ultra-sparse block: write ijv-triples
                    out.writeInt(r);
                    out.writeInt(aix[j]);
                    out.writeDouble(avals[j]);
                    wnnz++;
                }
            }
    } else //ULTRA-SPARSE COL
    {
        //block: write iv-pairs (should never happen since always dense)
        for (int r = 0; r < Math.min(rlen, sparseRows.length); r++)
            if (sparseRows[r] != null && !sparseRows[r].isEmpty()) {
                out.writeInt(r);
                out.writeDouble(sparseRows[r].getValueContainer()[0]);
                wnnz++;
            }
    }

    //validity check (nnz must exactly match written nnz)
    if (nonZeros != wnnz) {
        throw new IOException(
                "Invalid number of serialized non-zeros: " + wnnz + " (expected: " + nonZeros + ")");
    }
}

From source file:com.ibm.bi.dml.runtime.matrix.data.MatrixBlock.java

/**
 * //from w w  w  .  j av  a  2  s.c  om
 * @param out
 * @throws IOException
 */
private void writeSparseBlock(DataOutput out) throws IOException {
    out.writeByte(BlockType.SPARSE_BLOCK.ordinal());
    writeNnzInfo(out, false);

    if (out instanceof MatrixBlockDataOutput) //fast serialize
        ((MatrixBlockDataOutput) out).writeSparseRows(rlen, sparseRows);
    else //general case (if fast serialize not supported)
    {
        int r = 0;
        for (; r < Math.min(rlen, sparseRows.length); r++) {
            if (sparseRows[r] == null)
                out.writeInt(0);
            else {
                int nr = sparseRows[r].size();
                out.writeInt(nr);
                int[] cols = sparseRows[r].getIndexContainer();
                double[] values = sparseRows[r].getValueContainer();
                for (int j = 0; j < nr; j++) {
                    out.writeInt(cols[j]);
                    out.writeDouble(values[j]);
                }

            }
        }
        for (; r < rlen; r++)
            out.writeInt(0);
    }
}