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 BASETENSORH
#define BASETENSORH
#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;
virtual for valueGen is optional;
valueGen can use random library to generate values
to initialize the tensor values;
double valueGen
std::randomdevice rd;
std::mt mtrd;
Create a uniform distribution between and
std::uniformrealdistribution dist;
Generate and print a random double value
double randomValue distmt;
return randomValue;
;
#endif BASETENSORH
Task 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 constructorsdestructor definition are required.
Private:
std::vector data;
public:
RankOneTensor;
RankOneTensorint size; dimension size;
RankOneTensorconst RankOneTensor& other;
~RankOneTensor;
class RankTwoTensor includes the private data member to hold the values for the tensor. The constructorsdestructor definition are required.
Private:
std::vector data;
public:
RankTwoTensorint rows, int cols;
RankTwoTensorconst RankTwoTensor& other;
~ RankTwoTensor;
Task Define and program the following operators for both class RankTwoTensor and class RankTwoTensor.
Task 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
RankOneTensor t ; t; t;
void testIncrementOperatorRankOne;
void testIncrementOperatorRankTwo;
Add two tensors of the same dimension element wise; invalidargument should be thrown if two tensor objects dimension does not match
RankOneTensor t t ;
t t ;
void testAddOperatorRankOne;
void testAddOperatorRankTwo;
Add one tensor with a scalar value element wise
RankOneTensor t;
void testAddOperatorRankOne;
void testAddOperatorRankTwo;
Assign one tensor to another of the same dimension; invalidargument should be thrown if two tensor objects dimension does not match
RankOneTensor t t t ;
tloadData ;
tloadData ;
t t t ;
void testAssignmentOperatorRankOne;
void testAssignmentOperatorRankTwo;
Output the tensors element values
RankOneTensor t ;
tloadData ;
cout t ;
void testCoutStreamOperatorRankOne;
void testCoutStreamOperatorRankTwo;
Input the tensors element values by console input
RankOneTensor t;
cin t;
void testCinStreamOperatorRankOne;
void testCinStreamOperatorRankTwo;
Retrieve value by index; invalidargument should be thrown if the index is out of the range
RankTwoTensor t;
cin t;
cout
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
