lab 3 start

This commit is contained in:
2022-03-22 11:49:18 -05:00
parent ae7af06af6
commit 431726e6ab
7 changed files with 422 additions and 1 deletions

20
Lab3/main.cpp Normal file
View File

@@ -0,0 +1,20 @@
/**
* @file main.cpp
* @author Trevor Barnes (barnestr@msoe.edu)
* @brief
* @version 1.0
* @date 2022-03-29
*
* @copyright Copyright (c) 2022
*
*/
#include<iostream>
#include "matrix.h"
using namespace std;
int main()
{
Matrix m1(2,2);
// add more tests
return 0;
}

25
Lab3/makefile Normal file
View File

@@ -0,0 +1,25 @@
# lab3 Makefile
CC = g++
CFLAGS = -c -MMD
LFLAGS =
# Change w/ every new project
SOURCES = main.cpp
OBJECTS = $(SOURCES:.cpp=.o)
# Change w/ every new project
EXECUTABLE = lab3
all: $(EXECUTABLE) $(SOURCES)
$(EXECUTABLE): $(OBJECTS)
$(CC) $(LFLAGS) -o $@ $(OBJECTS)
-include *.d
%.o:%.cpp
$(CC) $(CFLAGS) $<
clean:
rm -f $(EXECUTABLE)
rm -f $(OBJECTS)
rm -f *.d

136
Lab3/matrix.cpp Normal file
View File

@@ -0,0 +1,136 @@
/**
* @file matrix.cpp
* @author Trevor Barnes (barnestr@msoe.edu)
* @brief
* @version 1.0
* @date 2022-03-29
*
* @copyright Copyright (c) 2022
*
*/
#include "matrix.h"
#include <iomanip>
using namespace std;
// constructor
Matrix::Matrix(unsigned int rows, unsigned int cols){
// Ensure size is valid
if(rows < 1 || cols < 1){
throw(out_of_range("rows and cols must be greater than 0"));
}
// Assign given row and col size to matrix
this->rows = rows;
this->cols = cols;
// Create the row pointer
this->the_matrix = new Row*[rows];
for(int i = 0; i < rows; i++){
// Assign the rows of the matrix to new memory
this->the_matrix[i] = new Row(cols);
}
// finish
}
// Copy constructor
Matrix::Matrix(const Matrix& from){
this->rows = from.rows;
this->cols = from.cols;
for(int i = 0; i < this->rows; i++){
}
}
// Destructor
Matrix::~Matrix(){
// finish
}
// Assignment operator
Matrix& Matrix::operator=(const Matrix& rhs){
// finish
return *this;
}
// Named Constructor
Matrix Matrix::identity(unsigned int size){
// Ensure size is valid
if(size < 1){
throw(out_of_range("rows and cols must be greater than 0"));
}
// Create square matrix
Matrix result(size, size);
for(int i = 0; i < size; i++){
// Fill each diagonal value with 1
result[i][i] = 1;
}
return result;
// finish
}
// Matrix addition
Matrix Matrix::operator+(const Matrix& rhs) const{
Matrix result(this->rows, this->cols);
// finish
return result;
}
// Matrix multiplication
Matrix Matrix::operator*(const Matrix& rhs) const{
Matrix result(this->rows, rhs.cols);
// finish
return result;
}
// Scalar multiplication
Matrix Matrix::operator*(const double scale) const{
Matrix result(this->rows, this->cols);
// finish
return result;
}
// Transpose of a Matrix
Matrix Matrix::operator~() const{
Matrix result(this->cols, this->rows);
// finish
return result;
}
// Clear Matrix
void Matrix::clear(){
// finish
}
// Access Operators - non-const
Row& Matrix::operator[](unsigned int row){
// Ensure the row is in range
if(row >= rows){
throw(out_of_range("Row is out of range"));
}
return *(the_matrix[row]);
}
// Access Operators - const
const Row& Matrix::operator[](unsigned int row) const{
// Ensure the row is in range
if(row >= rows){
throw(out_of_range("Row is out of range"));
}
return *(the_matrix[row]);
}
// print to output stream
void Matrix::out(std::ostream& os) const{
os << setprecision(4);
os << setw(10);
// finish
}
// global insertion operator
std::ostream& operator<<(std::ostream& os, const Matrix& rhs){
rhs.out(os);
return os;
}
// global scalar multiplication
Matrix operator*(const double scale, const Matrix& rhs){
return rhs*scale;
}

103
Lab3/matrix.h Normal file
View File

@@ -0,0 +1,103 @@
#ifndef matrix_h
#define matrix_h
#include <iostream>
#include "row.h"
class Matrix
{
public:
// No default (no argument) constructor. It doesn't really make
// sense to have one as we cannot rely on a size. This may trip
// us up later, but it will lead to a better implementation.
// matrix();
// Constructor - create Matrix and clear cells. If rows or
// cols is < 1, throw an exception
Matrix(unsigned int rows, unsigned int cols);
// Copy constructor - make a new Matrix just like rhs
Matrix(const Matrix& from);
// Destructor. Free allocated memory
~Matrix();
// Assignment operator - make this just like rhs. Must function
// correctly even if rhs is a different size than this.
Matrix& operator=(const Matrix& rhs);
// Named Constructor - produce a square identity matrix of the
// requested size. Since we do not know how the object produced will
// be used, we pretty much have to return by value. A size of 0
// would not make sense and should throw an exception.
static Matrix identity(unsigned int size);
// Matrix addition - lhs and rhs must be same size otherwise
// an exception shall be thrown
Matrix operator+(const Matrix& rhs) const;
// Matrix multiplication - lhs and rhs must be compatible
// otherwise an exception shall be thrown
Matrix operator*(const Matrix& rhs) const;
// Scalar multiplication. Note, this function will support
// someMatrixObject * 5.0, but not 5.0 * someMatrixObject.
Matrix operator*(const double scale) const;
// Transpose of a Matrix - should always work, hence no exception
Matrix operator~() const;
// Clear Matrix to all members 0.0
void clear();
// Access Operators - throw an exception if index out of range
Row& operator[](unsigned int row);
// const version of above - throws an exception if indices are out of
// range
const Row& operator[](unsigned int row) const;
// I/O - for convenience - this is intended to be called by the global
// << operator declared below.
void out(std::ostream& os) const;
private:
// An array of Row pointers size "rows" that each point to a double array
// of size "cols"
Row** the_matrix;
unsigned int rows;
unsigned int cols;
/** routines **/
// add any "helper" routine here, such as routines to support
// matrix inversion
};
/** Some Related Global Functions **/
// Overloaded global << with std::ostream as lhs, Matrix as rhs. This method
// should generate output compatible with an ostream which is commonly used
// with console (cout) and files. Something like:
// [[ r0c0, r0c1, r0c2 ]
// [ r1c0, r1c1, r1c2 ]
// [ r0c0, r0c1, r0c2 ]]
// would be appropriate.
//
// Since this is a global function, it does not have access to the private
// data of a Matrix object. So, it will need to use the public interface of
// Matrix to do its job. The method Matrix::out was added to Matrix
// specifically for this purpose. The other option would have been to make
// it a "friend"
std::ostream& operator<<(std::ostream& os, const Matrix& rhs);
// We would normally have a corresponding >> operator, but
// will defer that exercise that until a later assignment.
// Scalar multiplication with a global function. Note, this function will
// support 5.0 * someMatrixObject, but not someMatrixObject * 5.0
Matrix operator*(const double scale, const Matrix& rhs);
#endif
// Based on lab by Dr. Darrin Rothe ((c) 2015 Dr. Darrin Rothe)

87
Lab3/row.cpp Normal file
View File

@@ -0,0 +1,87 @@
/**
* @file row.cpp
* @author Trevor Barnes (barnestr@msoe.edu)
* @brief Contains the main functionality for row matrices utilizing arrays of
* double values
* @version 1.0
* @date 2022-03-22
*
* @copyright Copyright (c) 2022
*
*/
#include <stdexcept>
#include "row.h"
using namespace std;
// parameterized constructor
Row::Row(unsigned int length){
// Set matrix length private variable to passed in length
this->length = length;
// Create new array in heap for row_data
this->row_data = new double[length];
// Clear all values in new array to 0
clear();
}
// copy constructor
Row::Row(const Row& from){
// New row matrix gets length from previous matrix
this->length = from.length;
// Create new array in heap with new length
this->row_data = new double[this->length];
// Copy all row_data values over to new array
for(int i = 0; i < this->length; i++) {
this->row_data[i] = from[i];
}
}
// destructor
Row::~Row(){
// Check for valid length then free the heap memory
if(length > 0 ) {
delete[] row_data;
}
}
// access operator (const)
double Row::operator[](unsigned int column) const{
if (column >= length) {
throw(out_of_range("Column is out of range"));
}
return row_data[column];
}
// access operator (non-const)
double& Row::operator[](unsigned int column){
if (column >= length) {
throw(out_of_range("Column is out of range"));
}
return row_data[column];
}
// assignment operator
Row& Row::operator= (const Row& rhs){
if(&rhs != this){
// Delete the current row matrix
if(length > 0){
delete[] this->row_data;
}
// New row matrix gets length from previous matrix
this->length = rhs.length;
// Create new array in heap with new length
this->row_data = new double[this->length];
// Copy all row_data values over to new array
for(int i = 0; i < this->length; i ++) {
this->row_data[i] = rhs.row_data[i];
}
}
// Return address of the Row
return *this;
}
// clear row data
void Row::clear(){
for(int i = 0; i < length; i++) {
this->row_data[i] = 0;
}
}

51
Lab3/row.h Normal file
View File

@@ -0,0 +1,51 @@
#ifndef row_h
#define row_h
class Row{
public:
/* Parameterized constructor
* Takes in length and creates a row matrix with values cleared
* to zero
*/
Row(unsigned int length);
/* Copy constructor
* Create a new row matrix with the same size and values as the
* from matrix
*/
Row(const Row& from);
/* Destructor
* Correctly delete any heap memory
*/
~Row();
/* Access operator (const version)
* Allow access to row matrix data
* Should return an exception if column is too large
*/
double operator[](unsigned int column) const;
/* Access operator (non const version)
* Allow access to row matrix data
* Should return an exception if column is too large
*/
double& operator[] (unsigned int column);
/* Assignment operator
* 1. Check if two sides are the same object
* 2. Delete the current row matrix
* 3. Create a new row matrix with the same size and values as
* the rhs matrix
*/
Row& operator= (const Row& rhs);
/* Clear all data values to zero
*/
void clear();
private:
// Row matrix data
double * row_data;
// Size of row matrix
unsigned int length;
};
#endif