|
Last change
on this file since 80e83b6c was 80e83b6c, checked in by Peter A. Buhr <pabuhr@…>, 8 days ago |
|
last proofread array chapter
|
-
Property mode
set to
100644
|
|
File size:
1.6 KB
|
| Line | |
|---|
| 1 | #include <iostream>
|
|---|
| 2 | #include <vector>
|
|---|
| 3 | using namespace std;
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 | void mul(
|
|---|
| 22 |
|
|---|
| 23 | vector<vector<float>> & res,
|
|---|
| 24 | vector<vector<float>> & lhs,
|
|---|
| 25 | vector<vector<float>> & rhs ) {
|
|---|
| 26 | for ( size_t i = 0;
|
|---|
| 27 | i < res.size(); i++ )
|
|---|
| 28 | for ( size_t j = 0;
|
|---|
| 29 | j < res.at(i).size(); j++ ) {
|
|---|
| 30 | res.at(i).at(j) = 0.0;
|
|---|
| 31 | for ( size_t k = 0;
|
|---|
| 32 | k < rhs.size(); k++ )
|
|---|
| 33 | @res.at(i).at(j) +=@
|
|---|
| 34 | @lhs.at(i).at(k) *@
|
|---|
| 35 | @rhs.at(k).at(j);@
|
|---|
| 36 | }
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
|
|---|
| 47 | #ifdef RUNIT
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | static void zero( vector<vector<float>> & mat ) {
|
|---|
| 51 | for ( size_t i = 0; i < mat.size(); i++ )
|
|---|
| 52 | for ( size_t j = 0; j < mat.at(i).size(); j++ )
|
|---|
| 53 | mat.at(i).at(j) = 0.0;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 | static void fill( vector<vector<float>> & mat ) {
|
|---|
| 58 | for ( size_t i = 0; i < mat.size(); i++ )
|
|---|
| 59 | for ( size_t j = 0; j < mat.at(i).size(); j++ )
|
|---|
| 60 | mat.at(i).at(j) = 1.0 * (i + 1) + 0.1 * (j+1);
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 |
|
|---|
| 64 | static void print( vector<vector<float>> & mat ) {
|
|---|
| 65 | for ( size_t i = 0; i < mat.size(); i++ ) {
|
|---|
| 66 | for ( size_t j = 0; j < mat.at(i).size(); j++ )
|
|---|
| 67 | cout << mat.at(i).at(j) << " ";
|
|---|
| 68 | cout << endl;
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | #define EXPSZ_M 2
|
|---|
| 73 | #define EXPSZ_N 3
|
|---|
| 74 | #define EXPSZ_P 4
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 | int main() {
|
|---|
| 78 | vector<vector<float>> res( EXPSZ_M, vector<float>( EXPSZ_N ) ); zero( res );
|
|---|
| 79 | vector<vector<float>> lhs( EXPSZ_M, vector<float>( EXPSZ_P ) ); fill( lhs );
|
|---|
| 80 | vector<vector<float>> rhs( EXPSZ_P, vector<float>( EXPSZ_N ) ); fill( rhs );
|
|---|
| 81 | mul( res, lhs, rhs );
|
|---|
| 82 | print(lhs);
|
|---|
| 83 | printf("*\n");
|
|---|
| 84 | print(rhs);
|
|---|
| 85 | printf("=\n");
|
|---|
| 86 | print(res);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 |
|
|---|
| 91 | #endif
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.