1
// Copyright (C) 2003-2006 Johan Hoffman and Anders Logg.
2
// Licensed under the GNU LGPL Version 2.1.
4
// Modified by Erik Svensson, 2003.
5
// Modified by Garth N. Wells, 2006.
7
// First added: 2003-02-26
8
// Last changed: 2006-05-31
10
// FIXME: Use streams rather than stdio
13
#include <dolfin/dolfin_log.h>
14
#include <dolfin/Array.h>
15
#include <dolfin/Vector.h>
16
#include <dolfin/Matrix.h>
17
#include <dolfin/MatlabFile.h>
18
#include <dolfin/OctaveFile.h>
20
using namespace dolfin;
22
//-----------------------------------------------------------------------------
23
OctaveFile::OctaveFile(const std::string filename) : MFile(filename)
27
//-----------------------------------------------------------------------------
28
OctaveFile::~OctaveFile()
32
//-----------------------------------------------------------------------------
33
void OctaveFile::operator<<(Matrix& A)
35
// Octave file format for Matrix is not the same as the Matlab format,
36
// since octave cannot handle sparse matrices.
40
real* row = new real[N];
42
FILE *fp = fopen(filename.c_str(), "a");
43
fprintf(fp, "%s = zeros(%u, %u);\n", A.name().c_str(), M, N);
45
for (uint i = 0; i < M; i++)
47
// Get nonzero entries
51
A.getRow(i, ncols, columns, values);
53
// Write nonzero entries
54
for (int pos = 0; pos < ncols; pos++)
55
fprintf(fp, "%s(%d, %d) = %.16e;\n",
56
A.name().c_str(), (int)i + 1, columns[pos] + 1, values[pos]);
62
message(1, "Saved matrix %s (%s) to file %s in Octave format.",
63
A.name().c_str(), A.label().c_str(), filename.c_str());
65
//-----------------------------------------------------------------------------