2016-05-22 11 views
-4

Wie kann ich den inversen Wert von 2x2 Matrix zurückgeben? Verwenden der Klassenmethode CalcInverse;C++, Umkehrung der 2x2-Matrix; Klassenmethode

#include <iostream> 

    class Matrix2x2 { 
     private: 
      double val00; // first row, first column 
      double val01; // first row, second column 
      double val10; // second row, first colum 
      double val11; // second row, second column 

     public: 
      Matrix2x2(); 
      Matrix2x2(const Matrix2x2& other); //why do you want to overide? 
      Matrix2x2(double a, double b, double c, double d); 
      double CalcDeterminant() const; 
      Matrix2x2 CalcInverse() const; 

      double Getval00() const {return val00;} 
      double Getval01() const {return val01;} 
      double Getval10() const {return val10;} 
      double Getval11() const {return val11;} 

      //non mandatory 
      void Print() const; //should i use this to print out the inverse value? 
    }; 

    #endif /* SUBMISSION_MATRIX2X2_HPP_ */ 

//my useclass method below 


    Matrix2x2::Matrix2x2(){ 
     val00 = 0.0; // first row, first column 
     val01 = 0.0; // first row, second column 
     val10 = 0.0; // second row, first colum 
     val11 = 0.0; // second row, second column 
    } 
    Matrix2x2::Matrix2x2(const Matrix2x2& other){ //copy constructer 
     val00 = other.val01; 
     val01 = other.val01; 
     val10 = other.val10; 
     val11 = other.val11; 
    } 

    Matrix2x2::Matrix2x2(double a, double b, double c, double d){ 

     val00 = a; 
     val01 = b; 
     val10 = c; 
     val11 = d; 
    } 

    double Matrix2x2::CalcDeterminant() const{ 
     double Determinant = val00*val11-val01*val10; 
     return Determinant; 
     } 


    Matrix2x2 Matrix2x2::CalcInverse() const{ // is this the correct way to make inverse method? 
     Matrix2x2 other; 
     double Determinant = val00*val11-val01*val10; 
     other.val00 = Determinant*val11; 
     other.val01 = Determinant*val01; 
     other.val10 = Determinant*val10; 
     other.val11 = Determinant*val00; 
     return other; 
    } 

    void Print() const{ 
     ? 
    } 

kann ich main.cpp-Datei verwenden, um den inversen Wert auszudrucken, aber ich bin nicht sicher, wie dies zu tun. Abgesehen davon, dass nur die privaten Memberwerte abgerufen werden, val00..etc. und die Determinante. Meine Datei useclass.cpp; Die erste Methode ist private Mitglieder; Hoffe ich bin auf dem richtigen Weg.

+0

@ Avis - ein Fehler in Kopie Konstruktor: 'val00 = other.val01' ist falsch; sollte 'val00 = other.val00' sein – max66

+0

Stellen Sie sicher, dass Sie nicht durch Null dividieren. – Matsmath

Antwort

0

Ich habe habe nur die beiden Funktionen CalcInverse und Print und es gibt zwei Arten von Fehlern:

  • Sichtbarkeit Fehler (man kann nicht eine private Variable durch Aufruf other.val00 ändern, da val00 privat);
  • Fehler in der Formel zur Berechnung der inversen Matrix (siehe this link als Referenz).

CalcInverse sollten wie folgt sein:

Matrix2x2 Matrix2x2::CalcInverse() const{   
    double invVal00, invVal01, invVal10, invVal11; 
    double determinant = val00*val11-val01*val10; 
    invVal00 = val11/determinant; 
    invVal01 = -val01/determinant; 
    invVal10 = -val10/determinant; 
    invVal11 = val00/determinant; 
    Matrix2x2 invMatrix(invVal00, invVal01, invVal10, invVal11); 
    return invMatrix; 
} 

Dann benötigen Sie eine Methode, um den Matrixwert zu drucken. Zum Beispiel sollten Sie etwas wie folgt verwenden:

void Matrix2x2::Print() const{ 
    std::cout << val00 << " " << val01 << std::endl; 
    std::cout << val10 << " " << val11 << std::endl; 
} 
+0

Ja hat die Inverse nicht korrekt berechnet, das war mein Ausrutscher. Und eine sehr wichtige Lektion, die mir nicht bewusst war, dass ich Member-Klassen-Methoden in anderen Memberclass-Methoden verwenden könnte. Sinn macht jetzt danke. Und ich denke, so würde ich die Umkehrung der Matrix mit meiner Kopierklasse ausdrucken. int main() { \t Matrix2x2 andere (4,23,22,2); Sonstiges.CalcInverse(); \t std :: cout << other.Print(); – Aviv

+0

Nein, sollte es sein: int main() {Matrix2x2 andere (4,23,22,2); Matrix2x2 inverseMatrix = other.CalcInverse(); std :: cout << inverseMatrix.Print(); zurückgeben 0; }. Wenn es Ihr Problem behebt, legen Sie die Antwort als Antwort fest, um die Frage zu schließen. –

Verwandte Themen