matlabfile.cpp

来自「Dolfin provide a high-performance linear」· C++ 代码 · 共 66 行

CPP
66
字号
// Copyright (C) 2003-2006 Johan Hoffman and Anders Logg.// Licensed under the GNU LGPL Version 2.1.//// Modified by Erik Svensson, 2003.// Modified by Andy R. Terrel, 2005.// Modified by Garth N. Wells, 2006.//// First added:  2003-02-17// Last changed: 2006-05-30#include <stdio.h>#include <dolfin/dolfin_log.h>#include <dolfin/Matrix.h>#include <dolfin/MatlabFile.h>#include <dolfin/Array.h>using namespace dolfin;//-----------------------------------------------------------------------------MatlabFile::MatlabFile(const std::string filename) : MFile(filename){  type = "MATLAB";}//-----------------------------------------------------------------------------MatlabFile::~MatlabFile(){  // Do nothing}//-----------------------------------------------------------------------------void MatlabFile::operator<<(Matrix& A){  // Open file  FILE *fp = fopen(filename.c_str(), "a");  // Write matrix in sparse format  fprintf(fp, "%s = [", A.name().c_str());  int ncols = 0;  Array<int> columns;  Array<real> values;    for (uint i = 0; i < A.size(0); i++)  {    A.getRow(i, ncols, columns, values);    for (int pos = 0; pos < ncols; pos++)    {      fprintf(fp, " %u %i %.15g", i + 1, columns[pos] + 1, values[pos]);      if ( i == (A.size(0) - 1) && (pos + 1 == ncols) )        fprintf(fp, "];\n");      else {        fprintf(fp, "\n");      }    }  }  fprintf(fp, "%s = spconvert(%s);\n", A.name().c_str(), A.name().c_str());    // Close file  fclose(fp);  message(1, "Saved matrix %s (%s) to file %s in sparse MATLAB format",          A.name().c_str(), A.label().c_str(), filename.c_str());}//-----------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?