matrix inverse

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

matrix inverse

AAA BBB
Hi,
Can someone show me how to get the inverse of a square matrix using quantilb
? example piece of code would be really nice ; )

Thanks much.

JJ

_________________________________________________________________
Turn searches into helpful donations. Make your search count.
http://click4thecause.live.com/search/charity/default.aspx?source=hmemtagline_donation&FORM=WLMTAG


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: matrix inverse

Klaus Spanderen
Hi JJ

the following code calculates the inverse of a square matrix using boost/
quantlib.

cheers
  Klaus


#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/vector_proxy.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/triangular.hpp>
#include <boost/numeric/ublas/lu.hpp>

#include <ql/Math/matrix.hpp>

namespace QuantLib {

    Disposable<Matrix> inverse(const Matrix& m) {
        QL_REQUIRE(m.rows() == m.columns(), "matrix is not square");

        boost::numeric::ublas::matrix<Real> a(m.rows(), m.columns());
        std::copy(m.begin(), m.end(), a.data().begin());

        boost::numeric::ublas::permutation_matrix<Size> pert(m.rows());
       
        // lu decomposition
        const Size singular = lu_factorize(a, pert);
        QL_REQUIRE(singular == 0, "singular matrix given");
       
        boost::numeric::ublas::matrix<Real>
            inverse = boost::numeric::ublas::identity_matrix<Real>(m.rows());
       
        // backsubstitution
        boost::numeric::ublas::lu_substitute(a, pert, inverse);

        Matrix retVal(m.rows(), m.columns());
        std::copy(inverse.data().begin(), inverse.data().end(),
                  retVal.begin());

        return retVal;
    }
}

On Monday 12 February 2007 1:22 am, AAA BBB wrote:

> Hi,
> Can someone show me how to get the inverse of a square matrix using
> quantilb ? example piece of code would be really nice ; )
>
> Thanks much.
>
> JJ
>
> _________________________________________________________________
> Turn searches into helpful donations. Make your search count.
> http://click4thecause.live.com/search/charity/default.aspx?source=hmemtagli
>ne_donation&FORM=WLMTAG
>
>
> -------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services, security?
> Get stuff done quickly with pre-integrated technology to make your job
> easier. Download IBM WebSphere Application Server v.1.0.1 based on Apache
> Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> _______________________________________________
> QuantLib-users mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/quantlib-users


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users
Reply | Threaded
Open this post in threaded view
|

Re: matrix inverse

AAA BBB

Thanks much.  Big Help ; )

JJ

>From: Klaus Spanderen <[hidden email]>
>Reply-To: [hidden email]
>To: "AAA BBB" <[hidden email]>
>CC: [hidden email]
>Subject: Re: [Quantlib-users] matrix inverse
>Date: Mon, 12 Feb 2007 12:03:01 +0100
>
>Hi JJ
>
>the following code calculates the inverse of a square matrix using boost/
>quantlib.
>
>cheers
>   Klaus
>
>
>#include <boost/numeric/ublas/vector.hpp>
>#include <boost/numeric/ublas/vector_proxy.hpp>
>#include <boost/numeric/ublas/matrix.hpp>
>#include <boost/numeric/ublas/triangular.hpp>
>#include <boost/numeric/ublas/lu.hpp>
>
>#include <ql/Math/matrix.hpp>
>
>namespace QuantLib {
>
>     Disposable<Matrix> inverse(const Matrix& m) {
>         QL_REQUIRE(m.rows() == m.columns(), "matrix is not square");
>
>         boost::numeric::ublas::matrix<Real> a(m.rows(), m.columns());
>         std::copy(m.begin(), m.end(), a.data().begin());
>
>         boost::numeric::ublas::permutation_matrix<Size> pert(m.rows());
>
>         // lu decomposition
>         const Size singular = lu_factorize(a, pert);
>         QL_REQUIRE(singular == 0, "singular matrix given");
>
>         boost::numeric::ublas::matrix<Real>
>             inverse =
>boost::numeric::ublas::identity_matrix<Real>(m.rows());
>
>         // backsubstitution
>         boost::numeric::ublas::lu_substitute(a, pert, inverse);
>
>         Matrix retVal(m.rows(), m.columns());
>         std::copy(inverse.data().begin(), inverse.data().end(),
>                   retVal.begin());
>
>         return retVal;
>     }
>}
>
>On Monday 12 February 2007 1:22 am, AAA BBB wrote:
> > Hi,
> > Can someone show me how to get the inverse of a square matrix using
> > quantilb ? example piece of code would be really nice ; )
> >
> > Thanks much.
> >
> > JJ
> >
> > _________________________________________________________________
> > Turn searches into helpful donations. Make your search count.
> >
>http://click4thecause.live.com/search/charity/default.aspx?source=hmemtagli
> >ne_donation&FORM=WLMTAG
> >
> >
> >
>-------------------------------------------------------------------------
> > Using Tomcat but need to do more? Need to support web services,
>security?
> > Get stuff done quickly with pre-integrated technology to make your job
> > easier. Download IBM WebSphere Application Server v.1.0.1 based on
>Apache
> > Geronimo
> > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> > _______________________________________________
> > QuantLib-users mailing list
> > [hidden email]
> > https://lists.sourceforge.net/lists/listinfo/quantlib-users
>
_________________________________________________________________
Don’t miss your chance to WIN 10 hours of private jet travel from Microsoft
Office Live http://clk.atdmt.com/MRT/go/mcrssaub0540002499mrt/direct/01/



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier.
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users