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
|
#include <iostream>
#include <vector>
using namespace std;
using matrix = vector< vector<double> >;
//----------------------------------------------------------------------
matrix operator *( const matrix &A, const matrix &B ) // Matrix multiply
{
int rowsA = A.size(), colsA = A[0].size(); // Get numbers of rows and columns
int rowsB = B.size(), colsB = B[0].size(); // (Number of elements stored with vector)
matrix result( rowsA, vector<double>( colsB, 0.0 ) ); // Appropriate size, initialised to 0
if ( colsA != rowsB )
{
cout << "Matrices incompatible for multiplication\n";
}
else
{
for ( int i = 0; i < rowsA; i++ )
{
for ( int j = 0; j < colsB; j++ )
{
for ( int k = 0; k < colsA; k++ ) result[i][j] += A[i][k] * B[k][j];
}
}
}
return result;
}
//----------------------------------------------------------------------
matrix operator +( const matrix &A, const matrix &B ) // Matrix add
{
int rowsA = A.size(), colsA = A[0].size();
int rowsB = B.size(), colsB = B[0].size();
matrix result( rowsA, vector<double>( colsA, 0.0 ) );
if ( rowsA != rowsB || colsA != colsB )
{
cout << "Matrices incompatible for addition\n";
}
else
{
for ( int i = 0; i < rowsA; i++ )
{
for ( int j = 0; j < colsA; j++ ) result[i][j] = A[i][j] + B[i][j];
}
}
return result;
}
//----------------------------------------------------------------------
matrix operator -( const matrix &A, const matrix &B ) // Matrix subtraction
{
int rowsA = A.size(), colsA = A[0].size();
int rowsB = B.size(), colsB = B[0].size();
matrix result( rowsA, vector<double>( colsA, 0.0 ) );
if ( rowsA != rowsB || colsA != colsB )
{
cout << "Matrices incompatible for subtraction\n";
}
else
{
for ( int i = 0; i < rowsA; i++ )
{
for ( int j = 0; j < colsA; j++ ) result[i][j] = A[i][j] - B[i][j];
}
}
return result;
}
//----------------------------------------------------------------------
matrix operator *( double c, const matrix &M ) // Scalar multiply
{
int rows = M.size(), cols = M[0].size();
matrix result( rows, vector<double>( cols, 0.0 ) );
for ( int i = 0; i < rows; i++ )
{
for ( int j = 0; j < cols; j++ ) result[i][j] = c * M[i][j];
}
return result;
}
//----------------------------------------------------------------------
ostream &operator << ( ostream &strm, const matrix &M ) // Output a matrix
{
for ( auto row : M )
{
for ( auto e : row ) strm << e << '\t';
cout << '\n';
}
return strm;
}
//----------------------------------------------------------------------
int main()
{
matrix A = { { 1, 2, 3 },
{ 4, 5, 6 } };
matrix B = { { 10, 11 },
{ 12, 13 },
{ 14, 15 } };
matrix C = { { 20, 25 },
{ 30, 35 },
{ 40, 45 } };
cout << "A \n" << A << '\n';
cout << "B \n" << B << '\n';
cout << "C \n" << C << '\n';
cout << "AB \n" << A*B << '\n';
cout << "B+C\n" << B+C << '\n';
cout << "B-C\n" << B-C << '\n';
cout << "5A \n" << 5*A << '\n';
}
|