adding hw3 and move old hw3 to old hw folder
215
old_hws/03_matrix_class/README.md
Normal file
@@ -0,0 +1,215 @@
|
||||
# Homework 3 — Dynamic Matrices
|
||||
|
||||
In this assignment you will build a custom class named Matrix, which will mimic traditional matrices (the plural of matrix). You will not be expected to have intimate knowledge of matrices, but if you are curious you can read more about them online: https://en.wikipedia.org/wiki/Matrix_(mathematics).
|
||||
|
||||
Matrices are used in many different applications, and over the years many optimizations, tricks, and numerical methods have been developed to quickly handle matrix operations and solve more complicated problems.
|
||||
|
||||
Building this data structure will give you practice with pointers, dynamic array allocation and deallocation, 2D pointers, and class design. The implementation of this data structure will involve writing one new class. You are not allowed to use any of the STL container classes in your implementation or use any additional classes or structs besides Matrix. You will need to make use of the new and delete keywords. You can use array indexing ([]). Please read the entire handout before beginning your implementation.
|
||||
|
||||
## The Data Structure
|
||||
|
||||
A matrix is a two-dimensional arrangement of numbers. In this assignment we will assume every matrix
|
||||
contains doubles. We refer to the size of a matrix with m rows and n columns as an m×n matrix. For
|
||||
example, shown below is a 4×3 matrix:
|
||||
|
||||

|
||||
|
||||
We will represent the data inside our Matrix class by using a two-dimensional array. Because a matrix may
|
||||
be any size, you will need to use dynamic memory for this task. The same matrix shown above can be
|
||||
represented like so:
|
||||
|
||||

|
||||
|
||||
We will denote a<sub>i</sub>,<sub>j</sub> as the value in matrix A that is in row i and column j. So a general matrix can be
|
||||
described as:
|
||||
|
||||

|
||||
|
||||
## Basic Functionality
|
||||
|
||||
The private section of your class will be fairly small, and the main challenge will be working with the
|
||||
dynamic memory as you implement features to make the class functional. You can implement these methods
|
||||
in any order; we start by mentioning a few that will make debugging easier.
|
||||
|
||||
The first thing we suggest is writing a constructor that takes two unsigned ints: a rows count and a columns
|
||||
count, and a double fill value. The constructor should create a data representation of a rows×columns matrix
|
||||
with every value initialized to fill. If either dimension is 0, the resulting matrix should be of size 0×0.
|
||||
|
||||
Your class must support the equality operator == and the inequality operator !=. Two matrices are considered
|
||||
to be equal if they have the same value in every position. In other words, matrices A and B are equal if and
|
||||
only if
|
||||
|
||||

|
||||
|
||||
∀ is a common shorthand for “for all,” so ∀i,j means “for every value of i and j.” ∈ is a common shorthand for “in”.
|
||||
|
||||
Since a matrix has two dimensions, you will need to implement num rows() and num cols() which return the
|
||||
number of rows and the number of columns in the matrix respectively.
|
||||
We may want to change a previously filled matrix to an empty one, so you must write a clear() method as
|
||||
well. This method should reset the number of rows and columns to 0, and deallocate any memory currently
|
||||
held by the Matrix.
|
||||
|
||||
Naturally we want to be able to access data stored in the Matrix class. To do so we will implement a “safe”
|
||||
accessor called get(), which takes in a row, a column, and a double. If the row and column are within the
|
||||
bounds of the matrix, then the value at arow,col should be stored in the double, and the function should
|
||||
return true. Otherwise the function should return false.
|
||||
|
||||
A complementary, but similar task to accessing data is to be able to set a value at a particular position in
|
||||
the matrix. This is done through a safe modifier called set(). This function also takes in a row, column, and
|
||||
a double value. set() returns false if the position is out of bounds, and true if the position is valid. If the
|
||||
position is valid, the function should also set arow,col to the value of the double that was passed in.
|
||||
|
||||
## Overloaded Output Operator
|
||||
|
||||
At some point, it is probably a good idea to write a method to do output for us. Unlike previous classes where
|
||||
we wrote a method to do the printing, we will instead rely on a non-member overload of the operator<<. We
|
||||
have practiced overloading other operators for calls to std::sort() before, and this will be similar. Outside
|
||||
of the Matrix class definition, but still in your .cpp and .h files, you should write the following operator:
|
||||
|
||||
```cpp
|
||||
std::ostream& operator<< (std::ostream& out, const Matrix& m)
|
||||
```
|
||||
|
||||
This will allow us to print one or more outputs sequentially. All of the following code should work if your
|
||||
operator<< is implemented correctly:
|
||||
|
||||
```cpp
|
||||
Matrix m1;
|
||||
Matrix m2;
|
||||
std::ofstream outfile(output_filename); //Assuming we already had the filename
|
||||
std::cout << m1 << m2 << std::endl;
|
||||
outfile << m1;
|
||||
outfile << m2 << std::endl;
|
||||
std::cout << "Done printing." << std::endl;
|
||||
```
|
||||
|
||||
Let us assume in the above example that:
|
||||
|
||||

|
||||
|
||||
Then the output should look something like this:
|
||||
```console
|
||||
0 x 0 matrix:
|
||||
[ ]
|
||||
3 x 2 matrix:
|
||||
[ 3 5.21
|
||||
-2 4
|
||||
-18 1 ]
|
||||
Done printing.
|
||||
```
|
||||
|
||||
We will ignore whitespace, but we do expect that your operator outputs the elements of the matrix in the
|
||||
right order, that the size output comes before the matrix and follows the format shown below - one row per
|
||||
line, and spacing between elements. Note that even in these examples, the alignment is not ideal. We would
|
||||
rather you focus on the task of implementing the Matrix class correctly and handling memory properly
|
||||
instead of focusing on making the output pretty.
|
||||
|
||||
### Simple Matrix Operations
|
||||
|
||||
To start with, we introduce some basic matrix operations. The first is the method multiply by coefficient(),
|
||||
which takes a double called a coefficient. The method should multiply every element in the matrix by the
|
||||
coefficient. For example:
|
||||
|
||||

|
||||
|
||||
Another common operation is to swap two rows of a matrix. This will be accomplished by the method
|
||||
swap row(), which takes two arguments of type unsigned int: a source row number and a target row
|
||||
number. If both rows are inside the bounds of the matrix, then the function should switch the values in the
|
||||
two rows and return true. Otherwise the function should return false.
|
||||
For example:
|
||||
|
||||

|
||||
|
||||
NOTE: With the basic functions and swap row() done, the tests related to the provided rref() function in
|
||||
matrix main.cpp can be called. We do not explain the function in detail here, and you don’t need to know
|
||||
how it works, but computing the Reduced Row Echelon Form (RREF) can be used to find an inverse matrix,
|
||||
which is important in many fields. We use a simple to implement method called Gauss-Jordan Elimination,
|
||||
which you can read about here: https://en.wikipedia.org/wiki/Gaussian_elimination . There are
|
||||
other techniques for finding the RREF that are better, but we chose this one for its simplicity.
|
||||
|
||||
It is common to need to “flip” a matrix, a process called transposition. You will need to write the transpose()
|
||||
method, which has a return type of void. Formally, transposition of m×n matrix A into n×m matrix AT
|
||||
is
|
||||
defined as:
|
||||
|
||||

|
||||
|
||||

|
||||
|
||||
## Binary Matrix Operations
|
||||
|
||||
Binary matrix operations are ones that involve two matrices. To keep things simple, we will write them as
|
||||
methods (not operators) that are inside the class definition, so the current Matrix object will always be the
|
||||
“left-hand” matrix, A. You will be required to implement both add() and subtract(). Both functions take in
|
||||
just one argument, a second Matrix which we will refer to as B, and modify A if the dimensions of A and B
|
||||
match. If the dimensions match, the functions should return true, otherwise they should return false.
|
||||
Addition of two matrices, C = A + B, and subtraction of two matrices, D = A − B are formally defined as:
|
||||
|
||||

|
||||
|
||||
Consider these two matrices:
|
||||
|
||||

|
||||
|
||||
## Harder Matrix Operations
|
||||
|
||||
If we want to get the contents of an entire row or column, it’s annoying to have to extract the values one by
|
||||
one using get(), especially since our implementation is a “safe” accessor so we can’t use some of the coding
|
||||
shortcuts we normally use. To fix this, you will implement two more accessors, get row() and get col().
|
||||
Both functions take one unsigned int and return a double*. For get row() the argument is the number of
|
||||
row to retrieve, while for get col() the argument is the number of the column to retrieve. If the requested
|
||||
row/column is outside of the matrix bounds, the method should return a pointer set to NULL.
|
||||
The final method we expect you to implement, quarter(), is not a traditional matrix operation. The method
|
||||
takes no arguments and returns a Matrix* containing four new Matrix elements in order: an Upper Left
|
||||
(UL) quadrant, an Upper Right (UR) quadrant, a Lower Left (LL) quadrant, and finally a Lower Right
|
||||
(LR) quadrant. All four quadrants should be the same size. Remember that when a function ends all
|
||||
local variables go out of scope and are destroyed, so you will need to be particularly careful about how you
|
||||
construct and return the quadrants. On the next page are two examples of the quarter operation:
|
||||
|
||||

|
||||
|
||||
## Testing and Debugging
|
||||
|
||||
We provide a [matrix_main.cpp](matrix_main.cpp) file with a wide variety of tests of the Matrix class. Some of these tests
|
||||
are initially commented out. We recommend that you get your class working on the basic tests, and then
|
||||
uncomment the additional tests as you implement and debug the remaining functionality. Study the provided
|
||||
test cases to understand the arguments and return values.
|
||||
|
||||
Note: Do not edit the provided [matrix_main.cpp](matrix_main.cpp) file, except to uncomment the provided test cases as you work through your implementation and to add your own test cases where specified.
|
||||
|
||||
The assert() function is used throughout the test code. This is a function available in both C and C++
|
||||
that will do nothing if the condition is true, and will cause an immediate crash if the condition is false. If
|
||||
the condition is false, your command line should show the assertion that failed immediate prior to the crash.
|
||||
|
||||
We recommend using a memory debugging tool to find memory errors and memory leaks. Information on
|
||||
installation and use of the memory debuggers “Dr. Memory” (available for Linux/MacOSX/Windows) and
|
||||
“Valgrind” (available for Linux/OSX) is presented on the course webpage:
|
||||
http://www.cs.rpi.edu/academics/courses/fall23/csci1200/memory_debugging.php
|
||||
|
||||
The homework submission server will also run your code with Dr. Memory to search for memory problems.
|
||||
Your program must be memory error free and memory leak free to receive full credit.
|
||||
|
||||
## Your Task & Provided Code
|
||||
|
||||
You must implement the Matrix class as described in this handout. Your class should be split between a .cpp
|
||||
and a .h file. You should also include some extra tests in the StudentTest() function in [matrix_main.cpp](matrix_main.cpp).
|
||||
When implementing the class, pay particular attention to correctly implementing the copy constructor,
|
||||
assignment operator, and destructor.
|
||||
|
||||
<!--The Vec<T> class we looked at in lecture is templated, while the
|
||||
Matrix class is not. You may still find the lecture notes to be a useful starting point, but remember that in
|
||||
the Matrix class we should not be writing template class<T> anywhere. -->
|
||||
|
||||
As you implement your classes, be careful with return types, the const keyword, and passing by reference.
|
||||
If you have correctly implemented the Matrix class, then running the provided [matrix_main.cpp](matrix_main.cpp) file with
|
||||
your class, should produce the output provided in [sample_output.txt](sample_output.txt). We are not going to be particularly
|
||||
picky about differences in whitespace, but you should be making an effort to try and match both spacing
|
||||
and newlines between our output and your output.
|
||||
|
||||
## Submission
|
||||
|
||||
You will need to submit your [matrix_main.cpp](matrix_main.cpp), Matrix.cpp, Matrix.h, and README.txt file. Be aware that Submitty will be using an instructor copy of [matrix_main.cpp](matrix_main.cpp) for most of the tests, so you must make sure your Matrix implementation can compile given the provided file. Also make sure to name your class implementation files with a capital letter since Linux is case sensitive.
|
||||
|
||||
Be sure to write your own new test cases and don’t forget to comment your code! Use the provided template [README.txt](README.txt) file for notes you want the grader to read. Fill out the order notation section as well in the [README.txt](README.txt) file. You must do this assignment on your own, as described in the “Collaboration Policy & Academic Integrity” handout. If you did discuss this assignment, problem solving techniques, or error messages, etc. with anyone, please list their names in your README.txt file.
|
||||
|
||||
**Due Date**: 09/28/2023, Thursday, 23:59pm.
|
||||
60
old_hws/03_matrix_class/README.txt
Normal file
@@ -0,0 +1,60 @@
|
||||
HOMEWORK 3: MATRIX CLASS
|
||||
|
||||
|
||||
NAME: < insert name >
|
||||
|
||||
|
||||
COLLABORATORS AND OTHER RESOURCES:
|
||||
List the names of everyone you talked to about this assignment
|
||||
(classmates, TAs, ALAC tutors, upperclassmen, students/instructor via
|
||||
LMS, etc.), and all of the resources (books, online reference
|
||||
material, etc.) you consulted in completing this assignment.
|
||||
|
||||
< insert collaborators / resources >
|
||||
|
||||
Remember: Your implementation for this assignment must be done on your
|
||||
own, as described in "Academic Integrity for Homework" handout.
|
||||
|
||||
|
||||
ESTIMATE OF # OF HOURS SPENT ON THIS ASSIGNMENT: < insert # hours >
|
||||
|
||||
|
||||
|
||||
ORDER NOTATION:
|
||||
For each of the functions below, write the order notation O().
|
||||
Write each answer in terms of m = the number of rows and n = the
|
||||
number of columns. You should assume that calling new [] or delete []
|
||||
on an array will take time proportional to the number of elements in
|
||||
the array.
|
||||
|
||||
get
|
||||
|
||||
set
|
||||
|
||||
num_rows
|
||||
|
||||
get_column
|
||||
|
||||
operator<<
|
||||
|
||||
quarter
|
||||
|
||||
operator==
|
||||
|
||||
operator!=
|
||||
|
||||
swap_rows
|
||||
|
||||
rref (provided in matrix_main.cpp)
|
||||
|
||||
|
||||
|
||||
TESTING & DEBUGGING STRATEGY:
|
||||
Discuss your strategy for testing & debugging your program.
|
||||
What tools did you use (gdb/lldb/Visual Studio debugger,
|
||||
Valgrind/Dr. Memory, std::cout & print, etc.)? How did you test the
|
||||
"corner cases" of your Matrix class design & implementation?
|
||||
|
||||
MISC. COMMENTS TO GRADER:
|
||||
(optional, please be concise!)
|
||||
|
||||
BIN
old_hws/03_matrix_class/images/matrix1.png
Normal file
|
After Width: | Height: | Size: 5.0 KiB |
BIN
old_hws/03_matrix_class/images/matrix1_array.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
old_hws/03_matrix_class/images/matrix_addition.png
Normal file
|
After Width: | Height: | Size: 28 KiB |
BIN
old_hws/03_matrix_class/images/matrix_addition_formula.png
Normal file
|
After Width: | Height: | Size: 17 KiB |
BIN
old_hws/03_matrix_class/images/matrix_equal_definition.png
Normal file
|
After Width: | Height: | Size: 8.0 KiB |
BIN
old_hws/03_matrix_class/images/matrix_general.png
Normal file
|
After Width: | Height: | Size: 13 KiB |
BIN
old_hws/03_matrix_class/images/matrix_m1_m2.png
Normal file
|
After Width: | Height: | Size: 5.2 KiB |
BIN
old_hws/03_matrix_class/images/matrix_multiply.png
Normal file
|
After Width: | Height: | Size: 10 KiB |
BIN
old_hws/03_matrix_class/images/matrix_quarter.png
Normal file
|
After Width: | Height: | Size: 27 KiB |
BIN
old_hws/03_matrix_class/images/matrix_swaprow.png
Normal file
|
After Width: | Height: | Size: 12 KiB |
BIN
old_hws/03_matrix_class/images/matrix_transpose.png
Normal file
|
After Width: | Height: | Size: 8.8 KiB |
BIN
old_hws/03_matrix_class/images/matrix_transpose_formula.png
Normal file
|
After Width: | Height: | Size: 8.2 KiB |
310
old_hws/03_matrix_class/matrix_main.cpp
Normal file
@@ -0,0 +1,310 @@
|
||||
// =======================================================
|
||||
//
|
||||
// IMPORTANT NOTE: Do not modify this file
|
||||
// (except to uncomment the provided test cases
|
||||
// and add your test cases where specified)
|
||||
//
|
||||
// =======================================================
|
||||
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cmath>
|
||||
#include <cassert>
|
||||
#include "Matrix.h"
|
||||
|
||||
#define __EPSILON 0.0000000001 //Need this to compare doubles because representation.
|
||||
|
||||
void SimpleTest(); //Some basic tests
|
||||
void StudentTest(); //Write your own test cases here
|
||||
void ExtraCreditTest(); //Write this if you write resize()
|
||||
|
||||
|
||||
//Function to test a ton of matrices at once.
|
||||
void BatchTest(double start, double step, unsigned int rows, unsigned int cols,
|
||||
unsigned int num);
|
||||
|
||||
//Quick function that returns if two doubles are very similar to each other.
|
||||
bool double_compare(double a, double b);
|
||||
|
||||
//Uses Gauss-Jordan elimination to create a Reduced Row Echelon Form matrix.
|
||||
Matrix rref(const Matrix& m);
|
||||
|
||||
int main(){
|
||||
SimpleTest();
|
||||
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;
|
||||
|
||||
//Uncomment this if you write the resize() function.
|
||||
/*
|
||||
ExtraCreditTest();
|
||||
std::cout << "Completed all student extra credit tests." << std::endl;
|
||||
*/
|
||||
return 0;
|
||||
}
|
||||
|
||||
////////////////Test functions//////////////////////
|
||||
//Some basic tests
|
||||
void SimpleTest(){ //well behaved getrow/read after
|
||||
//Default constructor
|
||||
Matrix m1;
|
||||
assert(m1.num_rows() == 0 && m1.num_cols() == 0);
|
||||
|
||||
//Copy constructor
|
||||
Matrix m2(3,4,0);
|
||||
assert(m2.num_rows() == 3 && m2.num_cols() == 4);
|
||||
|
||||
Matrix m2_copy(m2);
|
||||
assert(m2_copy.num_rows() == 3 && m2_copy.num_cols() == 4);
|
||||
m2_copy.set(1,1,27);
|
||||
double d0;
|
||||
assert(m2.get(1,1,d0));
|
||||
assert(double_compare(d0,0.0));
|
||||
assert(m2_copy.get(1,1,d0));
|
||||
assert(double_compare(d0,27));
|
||||
|
||||
//Equality and Inequality
|
||||
Matrix m3;
|
||||
assert(m1 == m3);
|
||||
assert(m1 != m2);
|
||||
|
||||
//Printing
|
||||
std::cout << "Empty matrix:";
|
||||
std::cout << m1 << std::endl;
|
||||
|
||||
std::cout << "Zeroed 3x4 matrix:";
|
||||
std::cout << m2 << std::endl;
|
||||
|
||||
std::cout << "One after the other:";
|
||||
std::cout << m1 << m2 << std::endl;
|
||||
|
||||
//Set & get
|
||||
Matrix m5(4,4,2);
|
||||
Matrix m6(4,4,12);
|
||||
assert(m6.set(2,1,7));
|
||||
assert(m6.set(3,3,11));
|
||||
double d1;
|
||||
assert(m6.get(2,1,d1));
|
||||
assert(d1==7);
|
||||
|
||||
//Addition
|
||||
std::cout << "Adding m5 and m6" << std::endl;
|
||||
std::cout << m5 << m6 << std::endl;
|
||||
|
||||
Matrix m7;
|
||||
m7 = m5;
|
||||
Matrix m8(m5);
|
||||
assert(m7 == m8);
|
||||
|
||||
assert(m7.add(m6));
|
||||
std::cout << "The result" << std::endl;
|
||||
std::cout << m7 << std::endl;
|
||||
|
||||
double* r1 = NULL;
|
||||
r1 = m7.get_row(2);
|
||||
assert(r1[0] == 14 && r1[1] == 9);
|
||||
|
||||
delete [] r1; //Remember we need to clean up dynamic allocations.
|
||||
|
||||
Matrix m9(3,6,0);
|
||||
m9.set(0,0,1);
|
||||
m9.set(0,1,2);
|
||||
m9.set(0,2,1);
|
||||
m9.set(0,3,1);
|
||||
m9.set(1,0,2);
|
||||
m9.set(1,1,3);
|
||||
m9.set(1,2,-1);
|
||||
m9.set(1,4,1);
|
||||
m9.set(2,0,3);
|
||||
m9.set(2,1,-2);
|
||||
m9.set(2,2,-1);
|
||||
m9.set(2,5,1);
|
||||
|
||||
std::cout << "Attempting Gauss-Jordan reduced row echelon form."
|
||||
<< m9 << std::endl;
|
||||
Matrix m12 = rref(m9);
|
||||
std::cout << m12 << std::endl;
|
||||
double comparison_value;
|
||||
assert(m12.get(0,3,comparison_value));
|
||||
assert(double_compare(comparison_value,0.25));
|
||||
assert(m12.get(0,1,comparison_value));
|
||||
assert(double_compare(comparison_value,0.0));
|
||||
assert(m12.get(1,5,comparison_value));
|
||||
assert(double_compare(comparison_value,-3.00/20));
|
||||
assert(m9.get(0,3,comparison_value));
|
||||
assert(double_compare(comparison_value,1.0));
|
||||
assert(m9.get(0,1,comparison_value));
|
||||
assert(double_compare(comparison_value,2.0));
|
||||
assert(m9.get(1,5,comparison_value));
|
||||
assert(double_compare(comparison_value,0.0));
|
||||
|
||||
Matrix m11(3,4,0);
|
||||
m11.set(0,0,1);
|
||||
m11.set(0,1,2);
|
||||
m11.set(0,2,3);
|
||||
m11.set(0,3,4);
|
||||
|
||||
m11.set(1,0,5);
|
||||
m11.set(1,1,6);
|
||||
m11.set(1,2,7);
|
||||
m11.set(1,3,8);
|
||||
|
||||
m11.set(2,0,9);
|
||||
m11.set(2,1,10);
|
||||
m11.set(2,2,11);
|
||||
m11.set(2,3,12);
|
||||
|
||||
std::cout << "M11 to be quartered: " << std::endl;
|
||||
std::cout << m11 << std::endl;
|
||||
|
||||
Matrix* ma1 = NULL;
|
||||
ma1 = m11.quarter();
|
||||
assert(ma1 != NULL);
|
||||
|
||||
std::cout << "UL: " << std::endl << ma1[0] << std::endl;
|
||||
std::cout << "UR: " << std::endl << ma1[1] << std::endl;
|
||||
std::cout << "LL: " << std::endl << ma1[2] << std::endl;
|
||||
std::cout << "LR: " << std::endl << ma1[3] << std::endl;
|
||||
|
||||
for(unsigned int i=0; i<4; i++){
|
||||
assert((ma1[i].num_rows() == 2) && (ma1[i].num_cols() == 2));
|
||||
}
|
||||
|
||||
//Upper Left
|
||||
assert(ma1[0].get(0,0,comparison_value));
|
||||
assert(double_compare(comparison_value,1));
|
||||
assert(ma1[0].get(1,1,comparison_value));
|
||||
assert(double_compare(comparison_value,6));
|
||||
|
||||
//Upper Right
|
||||
assert(ma1[1].get(0,0,comparison_value));
|
||||
assert(double_compare(comparison_value,3));
|
||||
assert(ma1[1].get(1,1,comparison_value));
|
||||
assert(double_compare(comparison_value,8));
|
||||
|
||||
//Lower Left
|
||||
assert(ma1[2].get(0,0,comparison_value));
|
||||
assert(double_compare(comparison_value,5));
|
||||
assert(ma1[2].get(1,1,comparison_value));
|
||||
assert(double_compare(comparison_value,10));
|
||||
|
||||
//Lower Right
|
||||
assert(ma1[3].get(0,0,comparison_value));
|
||||
assert(double_compare(comparison_value,7));
|
||||
assert(ma1[3].get(1,1,comparison_value));
|
||||
assert(double_compare(comparison_value,12));
|
||||
|
||||
delete [] ma1;
|
||||
}
|
||||
|
||||
//Write your own test cases here
|
||||
void StudentTest(){
|
||||
|
||||
}
|
||||
|
||||
//Write this if you write resize()
|
||||
void ExtraCreditTest(){
|
||||
|
||||
}
|
||||
|
||||
|
||||
////////////////Utility functions//////////////////////
|
||||
|
||||
/* Function that quickly populates a rows x cols matrix with values from
|
||||
* start in increments of step. Does this num_times times.
|
||||
*/
|
||||
void BatchTest(double start, double step, unsigned int rows, unsigned int cols,
|
||||
unsigned int num){
|
||||
Matrix* m_arr = new Matrix[num];
|
||||
for(unsigned int i=0; i<num; i++){
|
||||
m_arr[i] = Matrix(rows,cols,0.0);
|
||||
unsigned int curr_elem = 0;
|
||||
for(unsigned int j=0; j<rows; j++){
|
||||
for(unsigned int k=0; k<rows; k++){
|
||||
m_arr[i].set(j,k,start+(step*curr_elem));
|
||||
curr_elem++;
|
||||
}
|
||||
}
|
||||
}
|
||||
delete [] m_arr;
|
||||
}
|
||||
|
||||
//Quick function that returns if two doubles are very similar to each other.
|
||||
bool double_compare(double a, double b){
|
||||
return (fabs(a-b) < __EPSILON);
|
||||
}
|
||||
|
||||
/* Uses Gauss-Jordan elimination to create a Reduced Row Echelon Form matrix.
|
||||
* These are some good and some bad variable names.
|
||||
* See how much harder it makes it to follow the code?
|
||||
* The lack of comments doesn't help either.
|
||||
*/
|
||||
|
||||
Matrix rref(const Matrix& m){
|
||||
Matrix ret(m);
|
||||
unsigned int curr_col = 0;
|
||||
double dummy;
|
||||
for(unsigned int i=0; i<ret.num_rows(); i++){
|
||||
bool col_all_zeros = true;
|
||||
//while(col_all_zeros && col_all_zeros < ret.num_cols()){
|
||||
while(col_all_zeros && curr_col < ret.num_cols()){
|
||||
for(unsigned int scan_i = 0; scan_i < ret.num_rows(); scan_i++){
|
||||
ret.get(scan_i,curr_col,dummy);
|
||||
if (!double_compare(dummy,0.0)){
|
||||
col_all_zeros = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(col_all_zeros){
|
||||
curr_col += 1;
|
||||
}
|
||||
}
|
||||
if(curr_col>=ret.num_cols()){
|
||||
return ret;
|
||||
}
|
||||
|
||||
ret.get(i,curr_col,dummy);
|
||||
if(double_compare(dummy,0.0)){
|
||||
//Swap with a nonzero row
|
||||
for(unsigned int scan_i = i+1; scan_i < ret.num_rows(); scan_i++){
|
||||
ret.get(scan_i,curr_col,dummy);
|
||||
if(!double_compare(dummy,0.0)){
|
||||
ret.swap_row(scan_i,i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Now we know ret i,curr_col is non-zero so we can use it as a pivot.
|
||||
double pivot;
|
||||
ret.get(i,curr_col,pivot);
|
||||
for(unsigned int j=0; j<ret.num_cols(); j++){
|
||||
ret.get(i,j,dummy);
|
||||
ret.set(i,j,dummy/pivot);
|
||||
}
|
||||
|
||||
for(unsigned int row_i = 0; row_i < ret.num_rows(); row_i++){
|
||||
if (row_i == i){
|
||||
continue;
|
||||
}
|
||||
double row_leading_coeff;
|
||||
ret.get(row_i,curr_col,row_leading_coeff);
|
||||
for(unsigned int col_j = 0; col_j < ret.num_cols(); col_j++){
|
||||
double lhs_dummy,rhs_dummy;
|
||||
ret.get(row_i,col_j,lhs_dummy);
|
||||
ret.get(i,col_j,rhs_dummy);
|
||||
ret.set(row_i,col_j,lhs_dummy - (row_leading_coeff*rhs_dummy));
|
||||
}
|
||||
}
|
||||
curr_col +=1 ;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
35
old_hws/03_matrix_class/rubric.txt
Normal file
@@ -0,0 +1,35 @@
|
||||
This is just from one of the previous semesters, made by the instructor who was teaching this course as well as the graders who did the grading work. We do not guarantee the rubric we use this semester will be 100% equivalent to this one, and you should just use this as a reference.
|
||||
|
||||
20 pts
|
||||
- README.txt Completed (2 pts)
|
||||
- One of name, collaborators, or hours not filled in (-1)
|
||||
- Two or more of name, collaborators, or hours not filled in (-2)
|
||||
- OVERALL CLASS DECLARATION & IMPLEMENTATION AND CODING STYLE (Good class design, split into a .h and .cpp file. Functions > 1 line are in .cpp file. Organized class implementation and reasonable comments throughout. Correct use of const/const& and of class method const. ) (5 pts)
|
||||
- No credit (significantly incomplete implementation) (-5)
|
||||
- No documentation for Matrix itself is provided (function documentation and section headings don't count). (-1)
|
||||
- Function bodies containing more than one statement are placed in the .h file. (-2)
|
||||
- Missing include guards in the .h file. (Or does not declare them correctly) (-1)
|
||||
- Functions are not well documented or are poorly commented, in either the .h or the .cpp file. (-1)
|
||||
- Improper uses or omissions of const and reference. (-1)
|
||||
- Overly cramped, excessive whitespace, or poor indentation. (-1)
|
||||
- Poor variable names. (-1)
|
||||
- Contains useless comments like commented-out code, terminal commands, or silly notes. (-1)
|
||||
- DATA REPRESENTATION (Does not use STL vector/list/etc. for the implementation.) (4 pts)
|
||||
- No credit (significantly incomplete implementation). (-4)
|
||||
- Uses STL data structures (lists, vectors, etc). (-4)
|
||||
- Member variables are public. (-2)
|
||||
- ORDER NOTATION (Readme contains correct analysis of order notation, including proper notation and use of provided variables.) (5 pts)
|
||||
- Does not use O() syntax. (-1)
|
||||
- get_column incorrect. (-0.5)
|
||||
- operator<< incorrect. (-0.5)
|
||||
- quarter incorrect. (-0.5)
|
||||
- operator== or operator!= incorrect:. (-0.5)
|
||||
- swap_rows incorrect. (-0.5)
|
||||
- rref incorrect. (-0.5)
|
||||
- did not write order notation part. (-5)
|
||||
- ADDITIONAL TEST CASES (A wide variety of additional student-written test cases.) (4 pts)
|
||||
- Does not test transpose. (-1)
|
||||
- Does not test multiply_by_coefficient. (-1)
|
||||
- Does not test get_col. (-1)
|
||||
- Does not test corner cases of some kind (rows or cols = 0, quartering with odd dimensions, etc). (-1)
|
||||
- No test cases, or else trivial/minimal ones that only test things covered in SimpleTests. (-4)
|
||||
86
old_hws/03_matrix_class/sample_output.txt
Normal file
@@ -0,0 +1,86 @@
|
||||
Empty matrix:
|
||||
0 x 0 matrix:
|
||||
[ ]
|
||||
|
||||
Zeroed 3x4 matrix:
|
||||
3 x 4 matrix:
|
||||
[ 0 0 0 0
|
||||
0 0 0 0
|
||||
0 0 0 0 ]
|
||||
|
||||
One after the other:
|
||||
0 x 0 matrix:
|
||||
[ ]
|
||||
|
||||
3 x 4 matrix:
|
||||
[ 0 0 0 0
|
||||
0 0 0 0
|
||||
0 0 0 0 ]
|
||||
|
||||
Adding m5 and m6
|
||||
|
||||
4 x 4 matrix:
|
||||
[ 2 2 2 2
|
||||
2 2 2 2
|
||||
2 2 2 2
|
||||
2 2 2 2 ]
|
||||
|
||||
4 x 4 matrix:
|
||||
[ 12 12 12 12
|
||||
12 12 12 12
|
||||
12 7 12 12
|
||||
12 12 12 11 ]
|
||||
|
||||
The result
|
||||
|
||||
4 x 4 matrix:
|
||||
[ 14 14 14 14
|
||||
14 14 14 14
|
||||
14 9 14 14
|
||||
14 14 14 13 ]
|
||||
|
||||
Attempting Gauss-Jordan reduced row echelon form.
|
||||
3 x 6 matrix:
|
||||
[ 1 2 1 1 0 0
|
||||
2 3 -1 0 1 0
|
||||
3 -2 -1 0 0 1 ]
|
||||
|
||||
|
||||
3 x 6 matrix:
|
||||
[ 1 0 0 0.25 0 0.25
|
||||
-0 1 0 0.05 0.2 -0.15
|
||||
0 0 1 0.65 -0.4 0.05 ]
|
||||
|
||||
M11 to be quartered:
|
||||
|
||||
3 x 4 matrix:
|
||||
[ 1 2 3 4
|
||||
5 6 7 8
|
||||
9 10 11 12 ]
|
||||
|
||||
UL:
|
||||
|
||||
2 x 2 matrix:
|
||||
[ 1 2
|
||||
5 6 ]
|
||||
|
||||
UR:
|
||||
|
||||
2 x 2 matrix:
|
||||
[ 3 4
|
||||
7 8 ]
|
||||
|
||||
LL:
|
||||
|
||||
2 x 2 matrix:
|
||||
[ 5 6
|
||||
9 10 ]
|
||||
|
||||
LR:
|
||||
|
||||
2 x 2 matrix:
|
||||
[ 7 8
|
||||
11 12 ]
|
||||
|
||||
Completed all simple tests.
|
||||
Completed all student tests.
|
||||