public DefaultDenseObjectMatrix2D(Matrix m) throws MatrixException {
   this.rows = (int) m.getRowCount();
   this.cols = (int) m.getColumnCount();
   this.size = new long[] {rows, cols};
   if (m instanceof DefaultDenseObjectMatrix2D) {
     Object[] v = ((DefaultDenseObjectMatrix2D) m).values;
     this.values = new Object[v.length];
     System.arraycopy(v, 0, this.values, 0, v.length);
   } else {
     this.values = new Object[rows * cols];
     for (long[] c : m.allCoordinates()) {
       setObject(m.getAsObject(c), c);
     }
   }
 }
  public void asLatex() throws IOException {
    final String EOL = System.getProperty("line.separator");
    final Writer writer = getWriter();
    final Matrix matrix = getMatrix();

    final long rowCount = matrix.getRowCount();
    final long colCount = matrix.getColumnCount();

    writer.write("\\begin{table}[!ht]" + EOL);
    writer.write("\\centering" + EOL);

    if (matrix.getLabelObject() != null) {
      writer.write(
          "\\caption{"
              + UJMPFormat.getSingleLineInstance().format(matrix.getLabelObject())
              + "}"
              + EOL);
    }

    StringBuilder buf = new StringBuilder();
    for (long i = matrix.getColumnCount() - 1; i != -1; i--) {
      buf.append('c');
    }
    String alignment = buf.toString();

    writer.write("\\begin{tabular}{" + alignment + "}" + EOL);
    writer.write("\\toprule" + EOL);

    for (int row = 0; row < rowCount; row++) {
      for (int col = 0; col < colCount; col++) {
        writer.write(UJMPFormat.getSingleLineInstance().format(matrix.getAsObject(row, col)));

        if (col < colCount - 1) {
          writer.write(" & ");
        }
      }
      writer.write(" \\\\" + EOL);
    }

    writer.write("\\bottomrule" + EOL);
    writer.write("\\end{tabular}" + EOL);
    writer.write("\\end{table}" + EOL);
  }