1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <random>
#include <iomanip>
#include <cmath>
#include <ctime>
class mat
{
public:
mat(){A=NULL;}
~mat(){}
mat(int r, int c){row=r; col=c; ///Build an empty matrix of row r and column c
A=new double *[row];
for(int i=0; i<row; i++)
{
A[i]=new double[col];
}
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
A[i][j]=0.0;
}
}
}
int getrow(){return row;}
int getcol(){return col;} ///Retrieve the needed info
//int getValue(int r, int c)
//{
//cout << A[r][c] << endl;
//return A[r][c];
//}
void display()
{
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
cout<<setw(8)<<A[i][j];
}
cout<<endl;
}
cout<<endl;
}
void ident(){ ///convert the current matrix into an indentity matrix
if(col==1){A[0][0]=1.0;}
else{
for(int i=0; i<row; i++)
A[i][i]=1.0;
}
}
void random_mat(double m) ///Fill the matrix with random values
{ double sum=0.0;
double r;
default_random_engine gen(time(NULL));
uniform_real_distribution<double> rdist(-1*m, m);
for(int i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
r=rdist(gen);
A[i][j]=r;
sum = sum+abs(r);
}
A[i][i]=sum;
sum=0.0;
}
}
mat matAdd(mat &a, mat &b) ///add two matrices to each other
{
int bRow = b.getrow();
int bCol = b.getcol();
mat c(bRow,bCol);
for(int i=0; i<bRow; i++)
{
for(int j=0; j<bCol; j++)
{
c.A[i][j] = a.A[i][j]+b.A[i][j];
}
}
return c;
}
mat matSub(mat &a, mat &b) ///subtract two matrices to each other
{
int bRow = b.getrow();
int bCol = b.getcol();
mat c(bRow,bCol);
for(int i=0; i<bRow; i++)
{
for(int j=0; j<bCol; j++)
{
c.A[i][j] = a.A[i][j]-b.A[i][j];
}
}
return c;
}
mat matMul(mat a, int s) ///multiply a matrix and a scaler
{
int aRow = a.getrow();
int aCol = a.getcol();
mat c(aRow,aCol);
for(int i=0; i<aRow; i++)
{
for(int j=0; j<aCol; j++)
{
c.A[i][j] = a.A[i][j] * s;
}
}
return c;
}
mat matMul(mat a, mat b)
{
int aRow = a.getrow();
int aCol = a.getcol();
int bRow = b.getrow();
int bCol = b.getcol();
if (aRow == 1 && aCol == 1) /// If matrix a has only one value treat it like a scaler to b
{
mat c(bRow,bCol);
for(int i=0; i<bRow; i++)
{
for(int j=0; j<bCol; j++)
{
c.A[i][j] = b.A[i][j] * a.A[1][1];
}
}
return c;
}
else if (bRow == 1 && bCol == 1) ///if matrix b only has one value, treat it like a scaler to a
{
int tempA = a.getrow();
int tempB = a.getcol();
mat c(aRow,aCol);
for(int i=0; i<aRow; i++)
{
for(int j=0; j<aCol; j++)
{
c.A[i][j] = a.A[i][j] * b.A[1][1];
}
}
return c;
}
else if (aRow == 1 && bCol == 1 && aCol == bRow)///if a row is multiplied by a column, get a single number
{
mat c(1,1);
int sum = 0;
for(int i=0; i<aCol; i++)
{
sum = sum + a.A[1][i] * b.A[i][1];
}
c.A[1][1] = sum;
return c;
}
}
private:
int row;
int col;
double ** A;
};
|