add hw3 solution

This commit is contained in:
JamesFlare1212
2025-02-10 20:49:15 -05:00
parent 2c07e10ea4
commit 4054554203
5 changed files with 513 additions and 17 deletions

View File

@@ -33,10 +33,8 @@ int main(){
std::cout << "Completed all simple tests." << std::endl;
//Uncomment this to allocate a lot of 100x100 matrices so leaks will be bigger.
/*
BatchTest(100,0.1,100,100,50);
std::cout << "Completed all batch tests." << std::endl;
*/
StudentTest();
std::cout << "Completed all student tests." << std::endl;
@@ -199,10 +197,128 @@ void SimpleTest(){ //well behaved getrow/read after
}
//Write your own test cases here
void StudentTest(){
void StudentTest() {
//Test transpose
Matrix m(2, 3, 0);
m.set(0, 0, 1);
m.set(0, 1, 2);
m.set(0, 2, 3);
m.set(1, 0, 4);
m.set(1, 1, 5);
m.set(1, 2, 6);
m.transpose();
assert(m.num_rows() == 3);
assert(m.num_cols() == 2);
double val;
m.get(0, 0, val);
assert(double_compare(val, 1.0));
m.get(0, 1, val);
assert(double_compare(val, 4.0));
m.get(1, 0, val);
assert(double_compare(val, 2.0));
m.get(2, 1, val);
assert(double_compare(val, 6.0));
//Test quarter with odd dimensions
Matrix q(5, 5, 1);
Matrix* quarters = q.quarter();
assert(quarters != nullptr);
//each quadrant should be 3x3 (ceiling: (5+1)/2 == 3)
assert(quarters[0].num_rows() == 3);
assert(quarters[0].num_cols() == 3);
assert(quarters[1].num_rows() == 3);
assert(quarters[1].num_cols() == 3);
assert(quarters[2].num_rows() == 3);
assert(quarters[2].num_cols() == 3);
assert(quarters[3].num_rows() == 3);
assert(quarters[3].num_cols() == 3);
//verify that the quadrants hold the expected values.
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
quarters[0].get(i, j, val);
assert(double_compare(val, 1.0));
quarters[1].get(i, j, val);
assert(double_compare(val, 1.0));
quarters[2].get(i, j, val);
assert(double_compare(val, 1.0));
quarters[3].get(i, j, val);
assert(double_compare(val, 1.0));
}
}
delete[] quarters;
//Test add and subtract
Matrix a(2, 2, 2);
Matrix b(2, 2, 3);
assert(a.add(b)); // Now a is all 5's.
double v;
a.get(0, 0, v);
assert(double_compare(v, 5.0));
assert(a.subtract(b)); // Now a is back to all 2's.
a.get(0, 0, v);
assert(double_compare(v, 2.0));
//Test multiply by coefficient
a.multiply_by_coefficient(2.5); //Now a is all 5's (2*2.5).
a.get(0, 0, v);
assert(double_compare(v, 5.0));
//Test get_row
double* row = a.get_row(0);
assert(row != nullptr);
assert(double_compare(row[0], 5.0));
assert(double_compare(row[1], 5.0));
delete[] row;
// Test get_col
double* col = a.get_col(1);
assert(col != nullptr);
assert(double_compare(col[0], 5.0));
assert(double_compare(col[1], 5.0));
delete[] col;
//Test clear
a.clear();
assert(a.num_rows() == 0 && a.num_cols() == 0);
//Test swap_row
Matrix s(3, 2, 0);
s.set(0, 0, 1); s.set(0, 1, 2);
s.set(1, 0, 3); s.set(1, 1, 4);
s.set(2, 0, 5); s.set(2, 1, 6);
//swap row 0 and row 2
assert(s.swap_row(0, 2));
s.get(0, 0, val);
assert(double_compare(val, 5.0));
s.get(0, 1, val);
assert(double_compare(val, 6.0));
s.get(2, 0, val);
assert(double_compare(val, 1.0));
s.get(2, 1, val);
assert(double_compare(val, 2.0));
//invalid swap should return false.
assert(!s.swap_row(0, 3));
//Test copy constructor and assignment operator.
Matrix orig(2, 3, 7);
Matrix copy(orig); // Using copy constructor.
Matrix assign;
assign = orig; // Using assignment operator.
//change orig to ensure copy and assign remain unchanged.
orig.set(0, 0, 10);
orig.get(0, 0, val);
assert(double_compare(val, 10.0));
copy.get(0, 0, val);
assert(double_compare(val, 7.0));
assign.get(0, 0, val);
assert(double_compare(val, 7.0));
//Test out-of-bound get and set
assert(!orig.get(5, 5, val));
assert(!orig.set(5, 5, 3.0));
}
////////////////Utility functions//////////////////////
/* Function that quickly populates a rows x cols matrix with values from