home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_11_09 / 1109080b < prev    next >
Text File  |  1993-04-10  |  2KB  |  93 lines

  1. #include <iostream.h>
  2. #include <stdio.h>
  3. #include "fixed.h"
  4.  
  5.  
  6. void main( void )
  7. {
  8.    // create a bunch of Fixed arrays
  9.    Fixed array1[3][3];
  10.    Fixed array2[3][3];
  11.    Fixed result[3][3];
  12.    Fixed temp,zero_value;
  13.  
  14.    // Initialize them
  15.    array1[0][0] = array1[1][1] = array1[2][2] =  1;
  16.    array2[0][0] = array2[1][1] = array2[2][2] =  1;
  17.  
  18.  
  19.    array1[0][0] =  1;
  20.    array1[0][1] =  3;
  21.    array1[0][2] = -4;
  22.  
  23.    array1[1][0] = 1;
  24.    array1[1][1] = 1;
  25.    array1[1][2] = -2;
  26.  
  27.    array1[2][0] = -1;
  28.    array1[2][1] = -2;
  29.    array1[2][2] = 5;
  30.  
  31.  
  32.  
  33.    array2[0][0] = 8;
  34.    array2[1][0] = 3;
  35.    array2[2][0] = 0;
  36.  
  37.    array2[0][1] = 3;
  38.    array2[1][1] = 10;
  39.    array2[2][1] = 2;
  40.  
  41.    array2[0][2] = 0;
  42.    array2[1][2] = 2;
  43.    array2[2][2] = 6;
  44.  
  45.    zero_value = 0;
  46.  
  47.  
  48.    // perform a simple matrix multiplication 
  49.  
  50.    for ( unsigned z = 0 ; z < 100000 ; z ++)
  51.    { // matrix multiply
  52.  
  53.    for (int i = 0 ; i < 3 ; i++)
  54.       {
  55.       for ( int j = 0 ; j < 3 ; j++)
  56.          {
  57.          temp = zero_value;
  58.          for ( int k = 0 ; k < 3 ; k++)
  59.             {
  60.             // the slow, temporary producing version
  61. //            temp = temp + array1[i][k] * array2[k][j];
  62.  
  63.             // the much faster, specialized function
  64.             temp.addProduct(array1[i][k],array2[k][j]);
  65.             }
  66.          result[i][j] = temp;
  67.          }
  68.       }
  69.    }
  70.  
  71.  
  72.    { // print matrix results
  73.  
  74.    cout << "\n";
  75.  
  76.    for ( int i = 0 ; i < 3 ; i++)
  77.       {
  78.       cout << "| " << array1[i][0] << " "
  79.          << array1[i][1] << " "
  80.          << array1[i][2] << " |";
  81.       cout << "| " << array2[i][0] << " "
  82.          << array2[i][1] << " "
  83.          << array2[i][2] << " |";
  84.       cout << "  | " << result[i][0] << " "
  85.          << result[i][1] << " "
  86.          << result[i][2] << " |";
  87.  
  88.       cout << "\n";
  89.       }
  90.  
  91.    }
  92. }
  93.