Matrix inversion

Discussion in 'C++' started by Sprechen sie von C++, Jun 19, 2010.

  1. I was looking to massage some code, wanted to add matrix inversion to my
    matrix template and related functions.
    I have had some issues trying to figure out math books and get it working so
    I thought I would inquire here.
    I have a vector template and a matrix template now
    They support all manor of math operations
    Ideas?
    Sprechen sie von C++, Jun 19, 2010
    #1
    1. Advertising

  2. "Victor Bazarov" <> wrote in message
    news:hvh13u$t57$...
    > On 6/18/2010 7:21 PM, Sprechen sie von C++ wrote:
    >> I was looking to massage some code, wanted to add matrix inversion to my
    >> matrix template and related functions. [..]
    >> Ideas?

    >
    > Uh, how does "go google it" sound? If you don't like Google, use any
    > other internet search engine. And next time start by reading the FAQ.


    I tried that, nothing but old crappy code that is hard to figure out.

    I am trying to make a fresh start with all new metaprogramming techniques
    Sprechen sie von C++, Jun 19, 2010
    #2
    1. Advertising

  3. "Sprechen sie von C++" <> wrote:
    > "Victor Bazarov" <> wrote in message
    >
    > news:hvh13u$t57$...
    >
    > > On 6/18/2010 7:21 PM, Sprechen sie von C++ wrote:
    > >> I was looking to massage some code, wanted to add matrix inversion to my
    > >> matrix template and related functions.  [..]
    > >> Ideas?

    >
    > > Uh, how does "go google it" sound?  If you don't like Google, use any
    > > other internet search engine.  And next time start by reading the FAQ..

    >
    > I tried that, nothing but old crappy code that is hard to figure out.
    >
    > I am trying to make a fresh start with all new metaprogramming techniques


    Ah, I love to see fresh new approaches! Can I see some of the snippets
    you're currently feeding to your compiler? I'd like to give a shot at
    implementing the matrix inversion, if you don't mind sharing...

    --
    FSC
    http://userscripts.org/scripts/show/59948
    Francesco S. Carta, Jun 19, 2010
    #3
  4. Try this little tidbit.

    // Useful for Gauss-Siedel and Jacobi, no pivoting with Guass Elimination
    template <typename base> bool diagonal_dominant(matrix<base> a) {
    base sum;
    for(int i=0; i<a.size(); i++) {
    sum = -fabs(a);
    for(int j = 0; j<a.size(); j++)
    sum += (base)fabs(a[j]);
    if (abs(a)<sum) return false;
    }
    return true;
    }
    Sprechen sie von C++, Jun 19, 2010
    #4
  5. "Sprechen sie von C++" <> wrote:
    > Try this little tidbit.
    >
    > // Useful for Gauss-Siedel and Jacobi, no pivoting with Guass Elimination
    > template <typename base> bool diagonal_dominant(matrix<base> a) {
    >         base sum;
    >         for(int i=0; i<a.size(); i++) {
    >                 sum = -fabs(a);
    >                 for(int j = 0; j<a.size(); j++)
    >                         sum += (base)fabs(a[j]);
    >                 if (abs(a)<sum) return false;
    >     }
    >         return true;
    >
    > }
    >
    >


    At a glance, I would prefer:

    sum += base(fabs(a[j]));

    furthermore, I would have completely avoided any conversion, and
    diagonal_dominant would have been a member of matrix, but I suppose
    that's matter of stylistic preferences.

    Mind sharing something more, so that we can discuss about the overall
    structural approach?

    --
    FSC
    http://userscripts.org/scripts/show/59948
    Francesco S. Carta, Jun 19, 2010
    #5
  6. "Sprechen sie von C++" <> wrote:
    > Try this little tidbit.
    >
    > // Useful for Gauss-Siedel and Jacobi, no pivoting with Guass Elimination
    > template <typename base> bool diagonal_dominant(matrix<base> a) {
    >         base sum;
    >         for(int i=0; i<a.size(); i++) {
    >                 sum = -fabs(a);
    >                 for(int j = 0; j<a.size(); j++)
    >                         sum += (base)fabs(a[j]);
    >                 if (abs(a)<sum) return false;
    >     }
    >         return true;
    >
    > }
    >


    Wait, why are you casting that float to "base" in one case, when
    you're implying the implicit conversion in other two cases? Is that a
    joke?

    --
    FSC
    http://userscripts.org/scripts/show/59948
    Francesco S. Carta, Jun 19, 2010
    #6
  7. > Wait, why are you casting that float to "base" in one case, when
    > you're implying the implicit conversion in other two cases? Is that a
    > joke?


    I design my code to be equally flexible with float, double, complex<base>
    and even arbitrary precision.

    I have an arbitrary precision integer class that I hold out as an example.

    I still have some concerns about the fabs but for now its all I have to work
    with. The integer package has a overload so its no a big deal.
    Sprechen sie von C++, Jun 19, 2010
    #7
  8. "Sprechen sie von C++" <> wrote:
    > > Wait, why are you casting that float to "base" in one case, when
    > > you're implying the implicit conversion in other two cases? Is that a
    > > joke?

    >
    > I design my code to be equally flexible with float, double, complex<base>
    > and even arbitrary precision.
    >
    > I have an arbitrary precision integer class that I hold out as an example.
    >
    > I still have some concerns about the fabs but for now its all I have to work
    > with. The integer package has a overload so its no a big deal.


    That does not justify the fact that you're relying on implicit
    conversion in two lines and using a cast on a third one, all in the
    same function - not to speak that you shouldn't be casting at all.

    If it were up to me, I would define an abs() template function and get
    rid of any issue.

    I'm not sure whether you missed my previous message, I'd like to see
    your matrix implementation and see if a different approach could be
    applicable - as I said there, I would implement all those functions as
    members.

    --
    FSC
    http://userscripts.org/scripts/show/59948
    Francesco S. Carta, Jun 19, 2010
    #8
  9. > If it were up to me, I would define an abs() template function and get
    > rid of any issue.


    Feel free to post one.
    Sprechen sie von C++, Jun 19, 2010
    #9
  10. > This version of the wheel has been reinvented already. Google for "boost
    > ublas matrix inversion".


    I have no use for the boost library. It so stuff with fluff its hard to use.

    The boost idea is all fine and dandy but its gone over the top

    It would be better if they had a set of headers that were better thought
    out.
    Sprechen sie von C++, Jun 19, 2010
    #10
  11. "Sprechen sie von C++" <> wrote:
    > > If it were up to me, I would define an abs() template function and get
    > > rid of any issue.

    >
    > Feel free to post one.


    I'm sure there is no need to post such a simple template. Actually,
    those were all just suggestions about the approach to take.

    If you're not going to discuss the approach and/or post your
    implementations here I am not able to see any useful outcome for all
    of this.

    --
    FSC
    http://userscripts.org/scripts/show/59948
    Francesco S. Carta, Jun 19, 2010
    #11
  12. Sprechen sie von C++

    Öö Tiib Guest

    On 19 juuni, 21:27, Christian Hackl <> wrote:
    > Sprechen sie von C++ ha scritto:
    >
    > >> This version of the wheel has been reinvented already. Google for "boost
    > >> ublas matrix inversion".

    >
    > > I have no use for the boost library. It so stuff with fluff its hard to use.

    >
    > > The boost idea is all fine and dandy but its gone over the top

    >
    > Well, then just install uBLAS and do not install the rest. The Boost
    > libraries are designed according to the "don't pay for what you don't
    > need" principle.
    >
    > If I'm not very much mistaken, uBLAS is even one of the header-only
    > libraries, so "installing" comes down to copying some files and adding
    > one include path to your development environment.
    >
    > As far as I am concerned, uBLAS is not hard to use at all. I once needed
    > some matrix calculations in a program and could immediately figure out
    > how to use the library and how to integrate it in my own code. Surely
    > beats designing, implementing and testing my own matrix library.


    The trouble with ublas is that it is well ... 5000+ LOC in some
    particular headers. matrix.hpp was around 4000 LOC when i last
    checked. Such size is sort of on border between "over-ripe" and
    "smelly" so i won't argue with anyone who refuses to use it because of
    "too stuff with fluff".

    If all goes well and ublas/matrix behaves within your use-case, it is
    OK. If something fires back then happy digging there. Every piece of
    software, including boost templates contains bugs.
    Öö Tiib, Jun 19, 2010
    #12
  13. > I'm sure there is no need to post such a simple template. Actually,
    > those were all just suggestions about the approach to take.
    >
    > If you're not going to discuss the approach and/or post your
    > implementations here I am not able to see any useful outcome for all
    > of this.


    template <typename T> T& fabs(T& ls, T& rs) {
    }

    Is this what you had in mind?
    Sprechen sie von C++, Jun 20, 2010
    #13
  14. > The trouble with ublas is that it is well ... 5000+ LOC in some
    > particular headers. matrix.hpp was around 4000 LOC when i last
    > checked. Such size is sort of on border between "over-ripe" and
    > "smelly" so i won't argue with anyone who refuses to use it because of
    > "too stuff with fluff".
    >
    > If all goes well and ublas/matrix behaves within your use-case, it is
    > OK. If something fires back then happy digging there. Every piece of
    > software, including boost templates contains bugs.


    I have enough trouble with my own software defects so the last thing I need
    is a dodgy 3rd party library.

    Today I enhanced my matrix template, I added getcol/setcol so that I could
    implement different algorithms in parallel.

    The advantage with my code, I have the source and I can fix it if its
    broken.

    My tensor.h file is 369 lines as of today and its already mighty powerful.
    That includes vector, matrix and some functionality to boot.
    Sprechen sie von C++, Jun 20, 2010
    #14
  15. "Sprechen sie von C++" <> wrote:
    > > I'm sure there is no need to post such a simple template. Actually,
    > > those were all just suggestions about the approach to take.

    >
    > > If you're not going to discuss the approach and/or post your
    > > implementations here I am not able to see any useful outcome for all
    > > of this.

    >
    > template <typename T> T& fabs(T& ls, T& rs) {
    >
    > }
    >
    > Is this what you had in mind?


    Of the two, one:
    - You haven't realized that such a function should take just an
    argument - and that would mean that you have no clue and you're trying
    to implement something above your head (unlikely, but possible).
    - You missed to get the arguments right on purpose, trying to get some
    fun out of this thread, and that would mean that you really have no
    clue.

    In either case, I'm out.

    --
    FSC
    http://userscripts.org/scripts/show/59948
    Francesco S. Carta, Jun 20, 2010
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert M. Gary

    Matrix inversion algorithm examples

    Robert M. Gary, Feb 10, 2006, in forum: Java
    Replies:
    11
    Views:
    34,582
    ersid
    Oct 29, 2008
  2. Jan-Hendrik Huehne

    matrix inversion

    Jan-Hendrik Huehne, Feb 7, 2004, in forum: C++
    Replies:
    2
    Views:
    634
    Victor Bazarov
    Feb 7, 2004
  3. Rafal 'Raf256' Maj

    [ot?] matrix inversion

    Rafal 'Raf256' Maj, Jan 14, 2004, in forum: C Programming
    Replies:
    87
    Views:
    1,856
    Dan Pop
    Jan 19, 2004
  4. lancered
    Replies:
    3
    Views:
    360
    Lou Pecora
    Apr 4, 2007
  5. lancered
    Replies:
    6
    Views:
    753
    Travis Oliphant
    Apr 4, 2007
Loading...

Share This Page