Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //217CR 1st Assignment
- //Brendan Smith
- //TMatrix Contructors and Functions
- #include "TMatrix.h"
- #include <iostream>
- using namespace std;
- //Constructor
- TMatrix::TMatrix(void)
- {
- Matrix[0][0] = 0.0; Matrix[0][1] = 0.0; Matrix[0][2] = 0.0;
- Matrix[1][0] = 0.0; Matrix[1][1] = 0.0; Matrix[1][2] = 0.0;
- Matrix[2][0] = 0.0; Matrix[2][1] = 0.0; Matrix[2][2] = 0.0;
- }
- TMatrix::TMatrix(double MX1A,double MX1B,double MX1C,double MX2A,double MX2B,double MX2C,double MX3A,double MX3B,double MX3C)
- {
- Matrix[0][0] = MX1A; Matrix[0][1] = MX1B; Matrix[0][2] = MX1C;
- Matrix[1][0] = MX2A; Matrix[1][1] = MX2B; Matrix[1][2] = MX2C;
- Matrix[2][0] = MX3A; Matrix[2][1] = MX3B; Matrix[2][2] = MX3C;
- }
- //Function that adds the TMatrix
- TMatrix TMatrix::AddMatrix(TMatrix Ma1, TMatrix Ma2, TMatrix Result)
- {
- int x, y;
- for (x = 0; x < 3; x++) {
- for (y = 0; y < 3; y++)
- Result.Matrix[x][y]=Ma1.Matrix[x][y] + Ma2.Matrix[x][y]; }
- for (x = 0; x < 3; x++) {
- for (y = 0; y < 3; y++)
- cout<<" Addition: "<<Result.Matrix[x][y];}
- cout<<" \n";
- cout<<" \n";
- return Result;
- }
- //Function that Subtracts the TMatrix
- TMatrix TMatrix::Subtract(TMatrix Ma1, TMatrix Ma2, TMatrix Result)
- {
- int x, y;
- for (x = 0; x < 3; x++) {
- for (y = 0; y < 3; y++)
- Result.Matrix[x][y]=Ma1.Matrix[x][y] - Ma2.Matrix[x][y]; }
- for (x = 0; x < 3; x++) {
- for (y = 0; y < 3; y++)
- cout<<" Subtract: "<<Result.Matrix[x][y];}
- cout<<" \n";
- cout<<" \n";
- return Result;
- }
- //Function that Multipys the TMatrix
- TMatrix TMatrix::MultiTMatrix(TMatrix Ma1, TMatrix Ma2, TMatrix Result)
- {
- int x, y;
- Result.Matrix[0][0] = (Ma1.Matrix[0][0] * Ma2.Matrix[0][0]) + (Ma1.Matrix[0][1] * Ma2.Matrix[1][0]) + (Ma1.Matrix[0][2] * Ma2.Matrix[2][0]);
- Result.Matrix[0][1] = (Ma1.Matrix[0][0] * Ma2.Matrix[0][1]) + (Ma1.Matrix[0][1] * Ma2.Matrix[1][1]) + (Ma1.Matrix[0][2] * Ma2.Matrix[2][1]);
- Result.Matrix[0][2] = (Ma1.Matrix[0][0] * Ma2.Matrix[0][2]) + (Ma1.Matrix[0][1] * Ma2.Matrix[1][2]) + (Ma1.Matrix[0][2] * Ma2.Matrix[2][2]);
- Result.Matrix[1][0] = (Ma1.Matrix[1][0] * Ma2.Matrix[0][0]) + (Ma1.Matrix[1][1] * Ma2.Matrix[1][0]) + (Ma1.Matrix[1][2] * Ma2.Matrix[2][0]);
- Result.Matrix[1][1] = (Ma1.Matrix[1][0] * Ma2.Matrix[0][1]) + (Ma1.Matrix[1][1] * Ma2.Matrix[1][1]) + (Ma1.Matrix[1][2] * Ma2.Matrix[2][1]);
- Result.Matrix[1][2] = (Ma1.Matrix[1][0] * Ma2.Matrix[0][2]) + (Ma1.Matrix[1][1] * Ma2.Matrix[1][2]) + (Ma1.Matrix[1][2] * Ma2.Matrix[2][2]);
- Result.Matrix[2][0] = (Ma1.Matrix[2][0] * Ma2.Matrix[0][0]) + (Ma1.Matrix[2][1] * Ma2.Matrix[1][0]) + (Ma1.Matrix[2][2] * Ma2.Matrix[2][0]);
- Result.Matrix[2][1] = (Ma1.Matrix[2][0] * Ma2.Matrix[0][1]) + (Ma1.Matrix[2][1] * Ma2.Matrix[1][1]) + (Ma1.Matrix[2][2] * Ma2.Matrix[2][1]);
- Result.Matrix[2][2] = (Ma1.Matrix[2][0] * Ma2.Matrix[0][2]) + (Ma1.Matrix[2][1] * Ma2.Matrix[1][2]) + (Ma1.Matrix[2][2] * Ma2.Matrix[2][2]);
- for (x = 0; x < 3; x++) {
- for (y = 0; y < 3; y++)
- cout<<" Multiply: "<<Result.Matrix[x][y];}
- cout<<" \n";
- cout<<" \n";
- return Result;
- }
- //Function that Tranpose the TMatrix
- TMatrix TMatrix::Transpose(TMatrix Ma1, TMatrix Result)
- {
- int x, y;
- for (x = 0; x < 3; x++) {
- for (y = 0; y < 3; y++)
- Result.Matrix[y][x]=Ma1.Matrix[x][y]; }
- for (x = 0; x < 3; x++) {
- for (y = 0; y < 3; y++)
- cout<<" Tranpose: "<<Result.Matrix[x][y];}
- cout<<" \n";
- cout<<" \n";
- return Result;
- }
- //Function that Inverses the TMatrix
- TMatrix TMatrix::Inverse(TMatrix Ma1, TMatrix Result)
- {
- int x, y;
- double Determinant, amount;
- Determinant=Ma1.Matrix[0][0] * ((Ma1.Matrix[1][1] * Ma1.Matrix[2][2]) -(Ma1.Matrix[1][2] * Ma1.Matrix[2][1])) - Ma1.Matrix[0][1] * ((Ma1.Matrix[1][0] * Ma1.Matrix[2][2]) - (Ma1.Matrix[1][2] * Ma1.Matrix[2][1])) + Ma1.Matrix[0][2] * ((Ma1.Matrix[1][0] * Ma1.Matrix[2][1]) - (Ma1.Matrix[1][1] * Ma1.Matrix[2][0]));
- amount = 1/Determinant;
- Result.Matrix[0][0] = amount * (Ma1.Matrix[1][1] * Ma1.Matrix[2][2]) - (Ma1.Matrix[1][2] * Ma1.Matrix[2][1]);
- Result.Matrix[0][1] = amount * (Ma1.Matrix[0][2] * Ma1.Matrix[2][1]) - (Ma1.Matrix[0][1] * Ma1.Matrix[2][2]);
- Result.Matrix[0][2] = amount * (Ma1.Matrix[0][1] * Ma1.Matrix[1][2]) - (Ma1.Matrix[0][2] * Ma1.Matrix[1][1]);
- Result.Matrix[1][0] = amount * (Ma1.Matrix[1][2] * Ma1.Matrix[2][0]) - (Ma1.Matrix[1][0] * Ma1.Matrix[2][2]);
- Result.Matrix[1][1] = amount * (Ma1.Matrix[0][0] * Ma1.Matrix[2][2]) - (Ma1.Matrix[0][2] * Ma1.Matrix[2][0]);
- Result.Matrix[1][2] = amount * (Ma1.Matrix[0][2] * Ma1.Matrix[1][0]) - (Ma1.Matrix[0][0] * Ma1.Matrix[1][2]);
- Result.Matrix[2][0] = amount * (Ma1.Matrix[1][0] * Ma1.Matrix[2][1]) - (Ma1.Matrix[1][1] * Ma1.Matrix[2][0]);
- Result.Matrix[2][1] = amount * (Ma1.Matrix[0][1] * Ma1.Matrix[2][0]) - (Ma1.Matrix[0][0] * Ma1.Matrix[2][1]);
- Result.Matrix[2][2] = amount * (Ma1.Matrix[0][0] * Ma1.Matrix[1][1]) - (Ma1.Matrix[0][1] * Ma1.Matrix[1][0]);
- for (x = 0; x < 3; x++) {
- for (y = 0; y < 3; y++)
- cout<<" Inverse: "<<Result.Matrix[x][y];}
- cout<<" \n";
- cout<<" \n";
- return Result;
- }
- //Determinant Function
- double TMatrix::Determinant(TMatrix Ma1, double Result)
- {
- Result=Ma1.Matrix[0][0] * ((Ma1.Matrix[1][1] * Ma1.Matrix[2][2]) -(Ma1.Matrix[1][2] * Ma1.Matrix[2][1])) - Ma1.Matrix[0][1] * ((Ma1.Matrix[1][0] * Ma1.Matrix[2][2]) - (Ma1.Matrix[1][2] * Ma1.Matrix[2][1])) + Ma1.Matrix[0][2] * ((Ma1.Matrix[1][0] * Ma1.Matrix[2][1]) - (Ma1.Matrix[1][1] * Ma1.Matrix[2][0]));
- cout<<" Determinant: " <<Result;
- cout<<" \n";
- cout<<" \n";
- return Result;
- }
Add Comment
Please, Sign In to add comment