Hello, 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 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. how can this be solved? //CODE #include <fstream> #include <ginac/ginac.h> using namespace std; using namespace GiNaC; symbol q("q"),v("v"); void read( string filename, GiNaC::matrix* qvCol, GiNaC::matrix* qvMat ); void write( string filename, GiNaC::matrix qvCol, GiNaC::matrix qvMat ); int main(int argc, char** argv){ GiNaC::matrix qvMat(2,2), qvCol(2,1), rm1, rm2; //mat1 qvCol(0,0) = q*v; qvCol(1,0) = 2*q*v; //mat2 qvMat(0,0) = q*v; qvMat(0,1) = 2*q*v; qvMat(1,0) = 3*q*v; qvMat(1,1) = 4*q*v; cout << endl << "m1: " << endl << qvCol << endl; cout << "m2: " << endl << qvMat << endl << endl; write("example.gin", qvCol, qvMat); read("example.gin", &rm1, &rm2); cout << endl << "rm1: " << endl << rm1 << endl; cout << "rm2: " << endl << rm2 << endl << endl; return 0; } void read( string filename, GiNaC::matrix* qvCol, GiNaC::matrix* qvMat ){ cout << "Main::Read File......."; fstream fr(filename.c_str(),ios::in|ios::binary); if( !fr.is_open() ){ printf("bad file\n"); return; } GiNaC::archive_node canCol, canMat; GiNaC::lst syms(q, v); fr >> canCol; fr >> canMat; qvCol->read_archive(canCol, syms); qvMat->read_archive(canMat, syms); fr.close(); cout << "OK: " << filename << endl; } void write( string filename, GiNaC::matrix qvCol, GiNaC::matrix qvMat ){ cout << "Main::Write File......"; fflush(stdout); //abrir archivo para escritura fstream fw(filename.c_str(),ios::out|ios::binary); GiNaC::archive_node anCol, anMat; qvCol.archive( anCol ); qvMat.archive( anMat ); fw << anCol; fw << anMat; fw.close(); cout << "OK: " << filename << endl; } //END
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
Thanks Alexei i complicated myself with the archive_node because i didnt know i could do this ex_to<matrix> best regards, Cristobal Cristobal <http://www.youtube.com/neoideo> On Wed, Jul 7, 2010 at 1:41 AM, Alexei Sheplyakov < alexei.sheplyakov@gmail.com> wrote:
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
_______________________________________________ GiNaC-list mailing list GiNaC-list@ginac.de https://www.cebix.net/mailman/listinfo/ginac-list
participants (2)
-
Alexei Sheplyakov
-
Cristobal Navarro