[GiNaC-list] Store matrix, then read it?

Cristobal Navarro axischire at gmail.com
Wed Jul 7 18:15:31 CEST 2010


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 at 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 at ginac.de
> https://www.cebix.net/mailman/listinfo/ginac-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cebix.net/pipermail/ginac-list/attachments/20100707/51035a28/attachment.htm>


More information about the GiNaC-list mailing list