[GiNaC-list] Store matrix, then read it?
Alexei Sheplyakov
alexei.sheplyakov at gmail.com
Wed Jul 7 07:41:27 CEST 2010
Hello,
On Tue, Jul 06, 2010 at 09:05:06PM -0400, Cristobal Navarro wrote:
>
> i am experiencing a little trouble when reading my stored matrix.
> when i write and read the same matrix i stored, it works.
> but if i only run the program to read the matrix file, i get this error.
>
> terminate called after throwing an instance of 'std::runtime_error'
> what(): unknown matrix dimensions in archive
>
The example below works for me just fine.
#include <string>
#include <fstream>
#include <stdexcept>
#include <ginac/ginac.h>
using namespace std;
using namespace GiNaC;
static void read(const string& filename, matrix& qvCol, matrix& qvMat, const lst& symlst)
{
archive ar;
ifstream f(filename.c_str());
if (!f.is_open())
throw std::runtime_error(string("Failed to open input file: ") + filename);
f >> ar;
f.close();
ex qvColEx = ar.unarchive_ex(symlst, "qvCol");
ex qvMatEx = ar.unarchive_ex(symlst, "qvMat");
if (!(is_a<matrix>(qvColEx) && is_a<matrix>(qvMatEx)))
throw std::runtime_error("expected a matrix");
qvCol = ex_to<matrix>(qvColEx);
qvMat = ex_to<matrix>(qvMatEx);
}
static void write(const string& filename, const matrix& qvCol, const matrix& qvMat)
{
archive ar;
ar.archive_ex(qvCol, "qvCol");
ar.archive_ex(qvMat, "qvMat");
ofstream f(filename.c_str());
if (!f.is_open())
throw std::runtime_error(string("Failed to open output file: ") + filename);
f << ar << flush;
f.close();
}
int main(int argc, char** argv)
{
const symbol q("q"), v("v");
lst symlst;
symlst = q, v;
matrix qvMat(2, 2), qvCol(2, 1);
qvCol = q*v, 2*q*v;
qvMat = q*v, 2*q*v,
3*q*v, 4*q*v;
cout << endl << "m1: " << endl << qvCol << endl;
cout << "m2: " << endl << qvMat << endl << endl;
write("example.gar", qvCol, qvMat);
matrix rm1, rm2;
read("example.gar", rm1, rm2, symlst);
cout << endl << "rm1: " << endl << rm1 << endl;
cout << "rm2: " << endl << rm2 << endl << endl;
return 0;
}
> This is a one-file example source code i made for you guys so you can test,
> its based on the same problem i have on my bigger code.
> try running it as it is, then comment the "write" method and run it again so
> it only reads the matrices, youll get the error eventually. but you will
> never get it if you write and read, its weird.
[skipped the code]
> how can this be solved?
First of all, please (re)read the manual, in particular, the (sub)section
called `Archiving'. Secondly, stop abusing read_archive() and archive_node.
Hope this helps,
Alexei
More information about the GiNaC-list
mailing list