This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Save JacobiSVD into a file

Tags: None
(comma "," separated)
User avatar
dzenanz
Registered Member
Posts
35
Karma
0
OS

Save JacobiSVD into a file

Thu Aug 02, 2012 2:08 pm
I am dealing with mesh processing, and solving some deformations using matrix equations. Since my matrix is constant, I only need to change right hand side. I used to save the LU decomposition to a file, because calculating it would take some seconds. The problem was in fact least squares fitting, but I boiled it down to normal matrix equation Ax=b.

Now I need to add some regularization (more equations, same number of unknowns), and I intend to switch to JacobiSVD. However, I don't know which class members need to be saved to a file (and later loaded), so that solve() method works correctly.

I would guess that m_matrixU, m_matrixV, m_singularValues and m_isInitialized have to be saved/loaded. What about the rest?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Save JacobiSVD into a file  Topic is solved

Thu Aug 02, 2012 8:33 pm
you also need to save m_nonzeroSingularValues, and restore the values of: m_rows, m_cols, m_diagSize.
User avatar
dzenanz
Registered Member
Posts
35
Karma
0
OS

Re: Save JacobiSVD into a file

Mon Aug 06, 2012 9:55 am
If someone else needs it, here are the functions (to be added into the definition of JacobiSVD):

Code: Select all
    /** Saves the decomposition into a file */
    void save(const char *filename) const
    {
        ofstream f(filename, ios::binary);
        f.write((char *)&m_rows, sizeof(m_rows));
        f.write((char *)&m_cols, sizeof(m_cols));
        f.write((char *)&m_nonzeroSingularValues, sizeof(m_nonzeroSingularValues));
        f.write((char *)&m_singularValues[0], sizeof(Scalar)*m_diagSize);
        f.write((char *)&m_matrixU(0,0), sizeof(Scalar)*m_rows*m_cols);
        f.write((char *)&m_matrixV(0,0), sizeof(Scalar)*m_cols*m_cols);
        f.close();
    }

    /** Loads the decomposition from a file */
    void load(const char *filename)
    {
        m_isAllocated=false;
        ifstream f(filename, ios::binary);
        f.read((char *)&m_rows, sizeof(m_rows));
        f.read((char *)&m_cols, sizeof(m_cols));
        allocate(m_rows, m_cols, Eigen::ComputeThinU | Eigen::ComputeThinV);
        f.read((char *)&m_nonzeroSingularValues, sizeof(m_nonzeroSingularValues));
        f.read((char *)&m_singularValues[0], sizeof(Scalar)*m_diagSize);
        f.read((char *)&m_matrixU(0,0), sizeof(Scalar)*m_rows*m_cols);
        f.read((char *)&m_matrixV(0,0), sizeof(Scalar)*m_cols*m_cols);
        if (f.bad())
            throw std::exception("Error reading decomposition");
        f.close();
        m_isInitialized=true;
    }
Neoneo
Registered Member
Posts
1
Karma
0

Re: Save JacobiSVD into a file

Tue Aug 28, 2012 8:00 am
dzenanz wrote:If someone else needs it, here are the functions (to be added into the definition of JacobiSVD):

Code: Select all
    /** Saves the decomposition into a file */
    void save(const char *filename) const
    {
        ofstream f(filename, ios::binary);
        f.write((char *)&m_rows, sizeof(m_rows));
        f.write((char *)&m_cols, sizeof(m_cols));
        f.write((char *)&m_nonzeroSingularValues, sizeof(m_nonzeroSingularValues));
        f.write((char *)&m_singularValues[0], sizeof(Scalar)*m_diagSize);
        f.write((char *)&m_matrixU(0,0), sizeof(Scalar)*m_rows*m_cols);
        f.write((char *)&m_matrixV(0,0), sizeof(Scalar)*m_cols*m_cols);
        f.close();
    }

    /** Loads the decomposition from a file */
    void load(const char *filename)
    {
        m_isAllocated=false;
        ifstream f(filename, ios::binary);
        f.read((char *)&m_rows, sizeof(m_rows));
        f.read((char *)&m_cols, sizeof(m_cols));
        allocate(m_rows, m_cols, Eigen::ComputeThinU | Eigen::ComputeThinV);
        f.read((char *)&m_nonzeroSingularValues, sizeof(m_nonzeroSingularValues));
        f.read((char *)&m_singularValues[0], sizeof(Scalar)*m_diagSize);
        f.read((char *)&m_matrixU(0,0), sizeof(Scalar)*m_rows*m_cols);
        f.read((char *)&m_matrixV(0,0), sizeof(Scalar)*m_cols*m_cols);
        if (f.bad())
            throw std::exception("Error reading decomposition");
        f.close();
        m_isInitialized=true;
    }


Hi dzenanz,
I've added this code to the JacobiSvd definition but it will not compile due to ofstream and ifstream. I've added #include <fstream>
#include <iostream> but I get a bunch of different errors. Could you please guide me to the correct way of adding this code? Thanks
User avatar
dzenanz
Registered Member
Posts
35
Karma
0
OS

Re: Save JacobiSVD into a file

Fri Aug 31, 2012 11:50 am
What kind of other errors are you getting? I was also afraid I will need to include fstream, but I guess I included it before including Eigen so it worked for me.


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]