Can't figure out how to print determinant
Dec 14, 2018 at 8:46pm UTC
I have been trying to print the determinant and I can't figure it out at all.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
Main.cpp
#include <iostream>
#include "Mat3.h"
using namespace std;
int main()
{
Mat3 a;
Mat3 b;
Mat3 c;
Mat3 d;
Vec3 v1(1, 2, 3);
Vec3 v2;
a.m[0] = 1;
a.m[1] = 4;
a.m[2] = 7;
a.m[3] = 2;
a.m[4] = 5;
a.m[5] = 8;
a.m[6] = 3;
a.m[7] = 6;
a.m[8] = 9;
b.m[0] = 1;
b.m[1] = 2;
b.m[2] = 3;
b.m[3] = 4;
b.m[4] = 5;
b.m[5] = 6;
b.m[6] = 7;
b.m[7] = 8;
b.m[8] = 9;
d.m[0] = 1;
d.m[1] = 5;
d.m[2] = 0;
d.m[3] = 2;
d.m[4] = 4;
d.m[5] = 3;
d.m[6] = 0;
d.m[7] = 6;
d.m[8] = 9;
c = a * b;
//Print Matrix
cout << "Matrix a * b = \t" << endl;
int i;
for (i = 0; i < 9; i++)
{
if (i == 3 || i == 6)
{
cout << endl;
}
cout << c.m[i] << " " ;
}
cout << endl;
cout << "\n" << "Matrix a * v1 = \t" ;
v2 = a * v1;
//Print Vector
cout << v2.x << "\t\t" << v2.y << "\t\t" << v2.z << "\n" << endl;
//Print Determinant of d
cout << "The determinant of d = \t" ;
//d.Inverse();
//Print Matrix
getchar();
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
Mat3.cpp
#include "Mat3.h"
/* Matrix Constructor
Set matrix entries to the identity matrix */
Mat3::Mat3()
{
SetIdentity();
}
/* SetIdentity Member Function
Set matrix entries to the identity matrix */
void Mat3::SetIdentity()
{
int i;
for (i = 0; i < 9; i++)
{
m[i] = 0;
}
m[0] = 1;
m[4] = 1;
m[8] = 1;
}
/* RotateZ Member Function
Set matrix entries to the rotation matrix */
void Mat3::RotateZ(float angle)
{
m[0] = cos(angle);
m[1] = -sin(angle);
m[2] = 0;
m[3] = sin(angle);
m[4] = cos(angle);
m[5] = 0;
m[6] = 0;
m[7] = 0;
m[8] = 1;
}
/* Scale Member Function
Set matrix entries to the scale matrix */
void Mat3::Scale(float x, float y, float z)
{
int i;
for (i = 0; i < 9; i++)
{
m[i] = 0;
}
m[0] = x;
m[4] = y;
m[8] = z;
}
/* Transpose Member Function
Set matrix entries to the transpose matrix */
void Mat3::Transpose()
{
float temp;
temp = m[1];
m[1] = m[3];
m[3] = temp;
temp = m[2];
m[2] = m[6];
m[6] = temp;
temp = m[7];
m[7] = m[5];
m[5] = temp;
}
/* Multiplication Member Function
Calculate the multiplication of two matrices and return the result */
Mat3 Mat3::operator *(Mat3 otherMat)
{
Mat3 tempM;
tempM.m[0] = (m[0] * otherMat.m[0]) + (m[1] * otherMat.m[3]) + (m[2] * otherMat.m[6]);
tempM.m[1] = (m[0] * otherMat.m[1]) + (m[1] * otherMat.m[4]) + (m[2] * otherMat.m[7]);
tempM.m[2] = (m[0] * otherMat.m[2]) + (m[1] * otherMat.m[5]) + (m[2] * otherMat.m[8]);
tempM.m[3] = (m[3] * otherMat.m[0]) + (m[4] * otherMat.m[3]) + (m[5] * otherMat.m[6]);
tempM.m[4] = (m[3] * otherMat.m[1]) + (m[4] * otherMat.m[4]) + (m[5] * otherMat.m[7]);
tempM.m[5] = (m[3] * otherMat.m[2]) + (m[4] * otherMat.m[5]) + (m[5] * otherMat.m[8]);
tempM.m[6] = (m[6] * otherMat.m[0]) + (m[7] * otherMat.m[3]) + (m[8] * otherMat.m[6]);
tempM.m[7] = (m[6] * otherMat.m[1]) + (m[7] * otherMat.m[4]) + (m[8] * otherMat.m[7]);
tempM.m[8] = (m[6] * otherMat.m[2]) + (m[7] * otherMat.m[5]) + (m[8] * otherMat.m[8]);
return tempM;
}
/* Matrix and Vector Multiplication Member Function
Calculate the multiplication of a matrix and vector, and return the result */
Vec3 Mat3::operator *(Vec3 vector)
{
float tempVec[3];
tempVec[0] = (m[0] * vector.x) + (m[1] * vector.y) + (m[2] * vector.z);
tempVec[1] = (m[3] * vector.x) + (m[4] * vector.y) + (m[5] * vector.z);
tempVec[2] = (m[6] * vector.x) + (m[7] * vector.y) + (m[8] * vector.z);
vector.x = tempVec[0];
vector.y = tempVec[1];
vector.z = tempVec[2];
return vector;
}
/* Determinant Member Function
Calculate the determinant of a matrix */
float Mat3::Det()
{
float determinate;
determinate = (m[0] * ((m[4] * m[8]) - (m[7] * m[5]))) - (m[1] * ((m[3] * m[8]) - (m[6] * m[5]))) + (m[2] * ((m[6] * m[2])));
return determinate;
}
/* Inverse Member Function
Calculate the inverse matrix
void Mat3::Inverse()
{
}
*/
Last edited on Dec 14, 2018 at 8:48pm UTC
Dec 14, 2018 at 8:48pm UTC
The formatting thing is a known bug. You can edit your post and re-add the formatting by clicking the <> button or by typing in [
code] and [
/code] around your code now that the post has been made.
Can you explain more what the actual issue is? Is it calculating the determinate
wrongly ? What do you expect the determinant to be? What is the actual result?
I assume your matrix's format is:
m[0] m[1] m[2]
m[3] m[4] m[5]
m[6] m[7] m[8]
?
determinate = (m[0] * ((m[4] * m[8]) - (m[7] * m[5]))) - (m[1] * ((m[3] * m[8]) - (m[6] * m[5]))) + (m[2] * ((m[6] * m[2])));
Let's format that a bit,
1 2 3 4
determinate =
m[0] * (m[4] * m[8] - m[7] * m[5])
- m[1] * (m[3] * m[8] - m[6] * m[5])
+ m[2] * m[6] * m[2];
What's going on with the third row there? Looks like a mistake to me.
Last edited on Dec 14, 2018 at 9:09pm UTC
Dec 14, 2018 at 8:48pm UTC
Yep fixed the formating issue
So the answer that I should get from the determinant is -72 but my problem is in main.cpp I can't figure out on how to actually print it in the console and yes my matrix format is
1 2 3
m[0] m[1] m[2]
m[3] m[4] m[5]
m[6] m[7] m[8]
Oh ye I missed one of them this is the fixed determinate:
determinate = (m[0] * ((m[4] * m[8]) - (m[7] * m[5]))) - (m[1] * ((m[3] * m[8]) - (m[6] * m[5]))) + (m[2] * ((m[3] * m[7]) - (m[6] * m[2])));
Last edited on Dec 14, 2018 at 8:59pm UTC
Dec 14, 2018 at 9:11pm UTC
So you mean you want to do this?
cout << "The determinant of d = \t" << d.Det() << endl;
Dec 14, 2018 at 9:15pm UTC
Yep that worked thank you. I have no clue why but sometimes my brain stops working and I can't find a simple answer
Dec 14, 2018 at 9:17pm UTC
cout << d.Det();
Topic archived. No new replies allowed.