+ Follow This Topic
Results 1 to 2 of 2

Thread: inverse of matrices... in cpp!

  1. #1
    anachronistic's Avatar
    anachronistic Guest

    inverse of matrices... in cpp!

    here is a sample script of a program i'm running that gives me the determinant of a matrix as well as it's inverse;

    Code:
    //main matrix v0.1
    #
    #
    // ^^ those are the lines that are in the cpp compiler. i usually don't type em.
    // just so you're not confused.
    
    main()
    {
    cout << "[d11  d12]\n[d21  d22]\n\n";
    
    //you'll love my var names
    
    //the following doubles are vars for the outputted equation above
    
    double d11;
    // ^_^
    cout << "d11=";
    cin >> d11;
    cout << "\n\n";
    
    double d12;
    cout << "d12=";
    cin>> d12;
    cout << "\n\n";
    
    double d21;
    cout << "d21=";
    cin>> d21;
    cout << "\n\n";
    
    double d22;
    cout << "d22=";
    cin>> d22;
    cout << "\n\n";
    
    //determinant var
    
    double dDet;
    dDet=(d11*d22)-(d12*d21);
    
    //in the next set of variables, the original values of the matrix will be
    //made into a transverse or whatever the hell it's called.
    
    double dInv11;
    dInv11=d22;
    
    double dInv12;
    dInv12=d11*-1;
    
    double dInv21;
    dInv21=d21*-1;
    
    double dInv22;
    dInv22=d11;
    
    // the next set converts the transverse values into the inverse by multiplying
    // them by the determinant.
    
    double dDetInv11;
    dDetInv11=(1/dDet)*dInv11;
    
    double dDetInv12;
    dDetInv12=(1/dDet)*dInv12;
    
    double dDetInv21;
    dDetInv21=(1/dDet)*dInv21;
    
    double dDetInv22;
    dDetInv22=(1/dDet)*dInv22;
    
    //outputs the answers
    
    cout << "Determinant=";
    cout << dDet;
    cout << "\n\n";
    
    cout << "Answer=[";
    cout << dDetInv11;
    cout << "  ";
    cout << dDetInv12;
    cout << "]\n[";
    cout << dDetInv21;
    cout << "  ";
    cout << dDetInv22;
    cout << "]";
    
    system("PAUSE");
    
    return 0;
    
    }
    it's a little sloppy, i apologize for the format. every time i tab, the cursor goes from the field to somewhere else on the page. this is freshly typed. (i haven't tried to compile it yet)

    there are some extra variables that i added in because i was in a hurry when i wrote it, but i'm going to take those out when i get some time.

    if any programmers have any suggestions for my coding, please post. i'll post the program for people to see how it works later when it's atleast v0.15 and compiled.

  2. #2
    anachronistic's Avatar
    anachronistic Guest
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    //Matrix Determinant and Inverse program
    cout << "Matrix Determinant and Inverse program.\n\n";
    cout << "Input values for the 2x2 matrix in the following order:\n";
    cout << "[d11  d12]\n[d21  d22]\n\n";
    
    cout << "Inputting values of the matrix...\n\n";
    
    //the following doubles are vars for the outputted equation above
    
    double d11;
    cout << "d11=";
    cin >> d11;
    cout << "\n";
    
    double d12;
    cout << "d12=";
    cin>> d12;
    cout << "\n";
    
    double d21;
    cout << "d21=";
    cin>> d21;
    cout << "\n";
    
    double d22;
    cout << "d22=";
    cin>> d22;
    cout << "\n";
    
    //determinant var
    
    double dDet;
    dDet=(d11*d22)-(d12*d21);
    
    //in the next set of variables, the original values of the matrix will be
    //made into a transverse or whatever the hell it's called.
    
    double dInv11;
    dInv11=d22;
    
    double dInv12;
    dInv12=d11*-1;
    
    double dInv21;
    dInv21=d21*-1;
    
    double dInv22;
    dInv22=d11;
    
    // the next set converts the transverse values into the inverse by multiplying
    // them by the determinant.
    
    double dDetInv11;
    dDetInv11=(1/dDet)*dInv11;
    
    double dDetInv12;
    dDetInv12=(1/dDet)*dInv12;
    
    double dDetInv21;
    dDetInv21=(1/dDet)*dInv21;
    
    double dDetInv22;
    dDetInv22=(1/dDet)*dInv22;
    
    //outputs the answers
    
    cout << "Calculating the determinant...\n";
    
    cout << "Determinant=";
    cout << dDet;
    cout << "\n\n";
    
    cout << "Calculating the inverse of the matrix...\n";
    
    cout << "Answer=\n[";
    cout << dDetInv11;
    cout << "  ";
    cout << dDetInv12;
    cout << "]\n[";
    cout << dDetInv21;
    cout << "  ";
    cout << dDetInv22;
    cout << "]\n\n\n";
    
    system("PAUSE");
    
    return 0;
    }
    It's running pretty smoothly now. Here's a link if anyone wants to see what it does/looks like:
    (it's an .exe, so it probably won't work on a mac/linux unless you have something that allows you to use .exe's or you will convert it. I am on my xp partition right now... and i don't have a cpp compiler on my apple work computers. sorry!)

    [URL="www.angelfire.com/planet/lilwing89/matrixsolver101.zip"]matrixsolver101.zip[/URL]

    i'm debugging right now and thinking of a way to destroy the extra vars. right now if i input 1's for all the vars, i get a #inf and i'm not exactly sure what that means yet... it probably means that the determinant is 0 and there is no inverse. i'm too lazy to figure it out by hand plus to fix that i'll need an else case. not something i'm going to do tonight

    the other problem, is sometimes the inverse contains decimals. just out of curiousity, is there a way to convert them into fractions with c++?
    Last edited by anachronistic; 18-04-07 at 01:59 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •