Question: Problem Two Statement Polymorphism, Abstract Class and Operator Overloading The context of problem two is about tensors. Tensors are data structures that generalize the concepts

Problem Two Statement Polymorphism, Abstract Class and Operator Overloading
The context of problem two is about tensors. Tensors are data structures that generalize the concepts of vectors and matrices for an arbitrary number of dimensions. The number of different dimensions of a tensor is also called tensor rank.
#ifndef BASETENSOR_H_
#define BASETENSOR_H_
#include
#include
// Base class for all tensors
class BaseTensor {
public:
/**
* Virtual method for load data to a tensor;
* Given the dimensions are different for each type
* of tensor, the loadData() function to initialize
* the values of a tensor should overriden by
* derived tensor classes;
*/
virtual void loadData()=0;
/**
* virtual for valueGen() is optional;
* valueGen() can use random library to generate values
* to initialize the tensor values;
*/
double valueGen(){
std::random_device rd;
std::mt19937 mt(rd());
// Create a uniform distribution between 0 and 1
std::uniform_real_distribution dist(0.0,1.0);
// Generate and print a random double value
double randomValue = dist(mt);
return randomValue;
}
};
#endif /* BASETENSOR_H_*/
Task 2.1 Please program two derived class RankOneTensor and class RankTwoTensor from the base class BaseTensor. The virtual methods should be overridden by each derived class according to the tensor type.
Class RankOneTensor includes the private data member to hold the values for the tensor. The constructors/destructor definition are required.
Private:
std::vector data;
public:
RankOneTensor();
RankOneTensor(int size); // dimension size;
RankOneTensor(const RankOneTensor& other);
~RankOneTensor();
class RankTwoTensor includes the private data member to hold the values for the tensor. The constructors/destructor definition are required.
Private:
std::vector> data;
public:
RankTwoTensor(int rows, int cols);
RankTwoTensor(const RankTwoTensor& other);
~ RankTwoTensor();
Task 2.2 Define and program the following operators for both class RankTwoTensor and class RankTwoTensor.
Task 2.3 Program unit test functions listed in the table below in testDriver.cpp. Call these test functions in the main() function of testDrive.cpp. Operator
Semantic Meaning
Example and Unit Test Function
++
Increment each elements value by 1
RankOneTensor t (3); t++; ++t;
void testIncrementOperatorRankOne();
void testIncrementOperatorRankTwo();
+
Add two tensors of the same dimension element wise; invalid_argument should be thrown if two tensor objects dimension does not match
RankOneTensor t1(3), t2(3) ;
t1+ t2 ;
void testAddOperatorRankOne();
void testAddOperatorRankTwo();
+
Add one tensor with a scalar value element wise
RankOneTensor t(3)+0.5;
void testAddOperatorRankOne();
void testAddOperatorRankTwo();
=
Assign one tensor to another of the same dimension; invalid_argument should be thrown if two tensor objects dimension does not match
RankOneTensor t1(2), t2(2), t3(2) ;
t1.loadData() ;
t2.loadData() ;
t3= t2+ t3 ;
void testAssignmentOperatorRankOne();
void testAssignmentOperatorRankTwo();
>>
Output the tensors element values
RankOneTensor t(3) ;
t.loadData() ;
cout << t ;
void testCoutStreamOperatorRankOne();
void testCoutStreamOperatorRankTwo();
<<
Input the tensors element values by console input
RankOneTensor t(3);
cin >> t;
void testCinStreamOperatorRankOne();
void testCinStreamOperatorRankTwo();
[]
Retrieve value by index; invalid_argument should be thrown if the index is out of the range
RankTwoTensor t(2,2);
cin >> t;
cout <

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!