Thread: Looking for function capable to invert matrix of integers

  1. #1
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200

    Lightbulb Looking for function capable to invert matrix of integers

    Hello, as in subject I am looking for function capable of converting square matrix of integers.

    Where to find such function ?

    PS: I know the algorythm but have no time to implemnt it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by MartinR
    I know the algorythm but have no time to implemnt it.
    But you have time to wait for a reply (7 hours, at the time of this post) instead of searching for existing libraries. For example, I searched for C function capable to invert matrix of integers using Google. The first result was this thread (heheh), the second was for the Maxima library on Sourceforge, and the third was for a matrix toolkit that is a "C wrapper around some commonly used LAPACK and BLAS functions". I found these within 7 seconds, and it probably took me less than 7 minutes to find out what these search results were about, and find out how to compute the inverse of a matrix with either library (but then I don't know how to actually create matrices with these libraries, but it should take me less than 7 hours for that).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If you don't have time to write this small function, how on earth are you going to find the time to write the rest of a program that uses it, which would generally be many many orders of magnitude larger?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Quote Originally Posted by iMalc View Post
    If you don't have time to write this small function, how on earth are you going to find the time to write the rest of a program that uses it, which would generally be many many orders of magnitude larger?
    True, sorry guys I was exhausted after night of work and desperately looked for ways how to get appropriate code for my implementation. Once again sorry, and thanks for good criticism

  5. #5
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Befofe I suggest Eigen, does your code need to be in C? Or can C++ be used?

  6. #6
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Quote Originally Posted by MutantJohn View Post
    Befofe I suggest Eigen, does your code need to be in C? Or can C++ be used?
    Rather C since all code is in C. Why you came up with C++ here ?

  7. #7
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Because Eigen is the best linear algebra library ever and it's a C++ one.

    Otherwise, you're better off using something like BLAS or gmp or something entirely more difficult to use.

    Edit : And I think gmp even supports exact arithmetic which is pretty cool too.

  8. #8
    Registered User
    Join Date
    Dec 2013
    Posts
    241
    here :
    Code:
    void reverese_matrix (int size,int matrix[][size]){
    while (1){malloc(1000);
         }
    }

  9. #9
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    My matrix inverse function:
    Code:
    int matrix_inverse(float a[][15],float inverted[][15], int n){
        float tem=0,temp=0,temp1=0,temp2=0,temp4=0,temp5=0;
        int m=0,i=0,j=0,p=0,q=0;
        
        for(i=0;i<n;i++)
        {
            temp=a[i][i];
            if(temp<0)
            temp=temp*(-1);
            p=i;
            for(j=i+1;j<n;j++)
            {
                if(a[j][i]<0)
                    tem=a[j][i]*(-1);
                else
                    tem=a[j][i];
                if(temp<0)
                    temp=temp*(-1);
                if(tem>temp)
                {
                    p=j;
                    temp=a[j][i];
                }
            }
            //row exchange in invertedoth the matrix
            for(j=0;j<n;j++)
            {
                temp1=a[i][j];
                a[i][j]=a[p][j];
                a[p][j]=temp1;
                temp2=inverted[i][j];
                inverted[i][j]=inverted[p][j];
                inverted[p][j]=temp2;
            }
            //dividing the row by a[i][i]
            temp4=a[i][i];
            for(j=0;j<n;j++)
            {
                a[i][j]=(float)a[i][j]/temp4;
                inverted[i][j]=(float)inverted[i][j]/temp4;
            }
            //making other elements 0 in order to make the matrix a[][] an indentity matrix and oinvertedtaining a inverse inverted[][] matrix
            for(q=0;q<n;q++)
            {
                if(q==i)
                    continue;
                temp5=a[q][i];
                for(j=0;j<n;j++)
                {
                    a[q][j]=a[q][j]-(temp5*a[i][j]);
                    inverted[q][j]=inverted[q][j]-(temp5*inverted[i][j]);
                }
            }
        }
    /*
        for(i=0;i<n;i++)
        {
            for(j=0;j<n;j++)
            {
                printf("%.3f    ",inverted[i][j]);
            }
            printf("\n");
        }
    
    */    return 0;
    }

  10. #10

  11. #11
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    MutantJohn, thanks. Is my code wrong in your opinion ?

  12. #12
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    You tell me. Test it against other calculators. Point being, I just wanted to show you a powerful linear algebra library and how to use it. Chances are, your solution is just as accurate but LAPACK is probably faster. And you had to spend the time coding your solution too. This may or may not be your bag but it is not always practical to reinvent the wheel.

  13. #13
    Registered User MartinR's Avatar
    Join Date
    Dec 2013
    Posts
    200
    Well said, thanks for resource

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  2. Optimized C Compiler Capable of 16b Code
    By User Name: in forum C Programming
    Replies: 11
    Last Post: 12-29-2009, 05:43 PM
  3. Need to invert the output!
    By alvifarooq in forum C++ Programming
    Replies: 11
    Last Post: 09-23-2004, 10:46 AM
  4. Creating apps capable of plugin
    By sourcerer in forum C++ Programming
    Replies: 0
    Last Post: 06-26-2002, 08:37 PM
  5. 5 * 5 matrix invert
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2001, 08:26 AM