+/EE312
software design
git clone https://git.julia.blue/+/EE312
f830e85
master
1#include <assert.h> 2#include <math.h> 3#include <stdint.h> 4#include <stdio.h> 5#include <stdlib.h> 6#include <string.h> 7 8#include "MatrixMultiply.h" 9 10 11void generateIdentityMatrix (double a [],const uint32_t rows , 12const uint32_t cols ); 13 14void generateIdentityMatrixPtr (double ** a ,const uint32_t rows , 15const uint32_t cols ); 16 17void perturbMatrix (double a [],const uint32_t rows ,const uint32_t cols , 18uint32_t src_row [],double perturb_values [], 19const uint32_t num_perturbations ); 20 21void perturbMatrixPtr (double ** a ,const uint32_t rows ,const uint32_t cols , 22uint32_t src_row [],double perturb_values [], 23const uint32_t num_perturbations ); 24 25void generateRandomMatrix (double a [],const uint32_t rows , 26const uint32_t cols ); 27 28void generateRandomMatrixPtr (double ** a ,const uint32_t rows , 29const uint32_t cols ); 30 31 32int assertEqualMatrices (double a [],double b [],const uint32_t rows , 33const uint32_t cols ); 34 35int assertEqualMatricesPtr (double ** a ,double ** b ,const uint32_t rows , 36const uint32_t cols ); 37 38void copyMatrix (double dest [],double src [],const uint32_t rows , 39const uint32_t cols ); 40 41void copyMatrixPtr (double ** dest ,double ** src ,const uint32_t rows , 42const uint32_t cols ); 43 44void printMatrix (double a [],const uint32_t rows ,const uint32_t cols ); 45 46void printMatrixPtr (double ** a ,const uint32_t rows ,const uint32_t cols ); 47 48void memoryRelease (double ** ptr ,int length ); 49 50void generateIdentityMatrix (double a [],const uint32_t rows , 51const uint32_t cols ) { 52uint32_t i ,j ;// two loop variables, uninteresting in all respects. 53i = 0 ; 54while (i < rows ) { 55j = 0 ; 56while (j < cols ) { 57if (i == j ) { 58a [(i * cols )+ j ]= 1.0 ; 59 }else { 60a [(i * cols )+ j ]= 0 ; 61 } 62j += 1 ; 63 } 64i += 1 ; 65 } 66} 67 68void generateIdentityMatrixPtr (double ** a ,const uint32_t rows , 69const uint32_t cols ) { 70uint32_t i ,j ;// two loop variables, uninteresting in all respects. 71i = 0 ; 72while (i < rows ) { 73j = 0 ; 74while (j < cols ) { 75if (i == j ) { 76a [i ][j ]= 1.0 ; 77 }else { 78a [i ][j ]= 0 ; 79 } 80j += 1 ; 81 } 82i += 1 ; 83 } 84} 85 86void perturbMatrix (double a [],const uint32_t rows ,const uint32_t cols , 87uint32_t src_row [],double perturb_values [], 88const uint32_t num_perturbations ) { 89uint32_t i ,k ;// two loop variables, uninteresting in all respects. 90uint32_t dst_row ;// index of the row being written into 91i = 0 ; 92while (i < num_perturbations ) { 93assert (src_row [i ]< rows ); 94/* apply perturbation i to every row (except to itself) */ 95dst_row = 0 ; 96while (dst_row < rows ) { 97if (dst_row != src_row [i ]) { 98/* take the src row, and scale it by the perturbation value 99* and add the resulting row to the destination row */ 100k = 0 ; 101while (k < cols ) { 102a [(dst_row * cols )+ k ]+= 103perturb_values [i ]* a [(src_row [i ]* cols )+ k ]; 104k += 1 ; 105 }// end for each element in a row 106 }// end if dst_row != src_row 107dst_row += 1 ; 108 }// end for each dst_row in the matrix 109i += 1 ; 110 }// end for each perturbation 111} 112 113void perturbMatrixPtr (double ** a ,const uint32_t rows ,const uint32_t cols , 114uint32_t src_row [],double perturb_values [], 115const uint32_t num_perturbations ) { 116uint32_t i ,k ;// two loop variables, uninteresting in all respects. 117uint32_t dst_row ;// index of the row being written into 118i = 0 ; 119while (i < num_perturbations ) { 120assert (src_row [i ]< rows ); 121/* apply perturbation i to every row (except to itself) */ 122dst_row = 0 ; 123while (dst_row < rows ) { 124if (dst_row != src_row [i ]) { 125/* take the src row, and scale it by the perturbation value 126* and add the resulting row to the destination row */ 127k = 0 ; 128while (k < cols ) { 129a [dst_row ][k ]+= perturb_values [i ]* a [src_row [i ]][k ]; 130k += 1 ; 131 }// end for each element in a row 132 }// end if dst_row != src_row 133dst_row += 1 ; 134 }// end for each dst_row in the matrix 135i += 1 ; 136 }// end for each perturbation 137} 138 139/* 140* generate a matrix with random elements. 141* the matrix should be stored in array a in row-major order 142* the parameters 'rows' and 'cols' specify the size of the matrix 143*/ 144void generateRandomMatrix (double a [],const uint32_t rows , 145const uint32_t cols ) { 146const int max_value = 100 ;// maximum scale factor to apply to a row 147const int perturbations = 7 ; 148uint32_t src_rows [7 ]; 149double perturb_values [7 ]; 150int k ;// an unininteresting loop variable 151 152/* randomly perturb the matrix seven times, that should scramble things pretty 153* well */ 154generateIdentityMatrix (a ,rows ,cols ); 155 156k = 0 ; 157while (k < perturbations ) { 158src_rows [k ]= rand () %rows ; 159perturb_values [k ]= max_value - (rand () % (2 * max_value )); 160k += 1 ; 161 } 162 163perturbMatrix (a ,rows ,cols ,src_rows ,perturb_values ,perturbations ); 164} 165 166void generateRandomMatrixPtr (double ** a ,const uint32_t rows , 167const uint32_t cols ) { 168const int max_value = 100 ;// maximum scale factor to apply to a row 169const int perturbations = 7 ; 170uint32_t src_rows [7 ]; 171double perturb_values [7 ]; 172int k ;// an unininteresting loop variable 173 174/* randomly perturb the matrix seven times, that should scramble things pretty 175* well */ 176generateIdentityMatrixPtr (a ,rows ,cols ); 177 178k = 0 ; 179while (k < perturbations ) { 180src_rows [k ]= rand () %rows ; 181perturb_values [k ]= max_value - (rand () % (2 * max_value )); 182k += 1 ; 183 } 184 185perturbMatrixPtr (a ,rows ,cols ,src_rows ,perturb_values ,perturbations ); 186} 187 188int assertEqualMatrices (double a [],double b [],const uint32_t rows , 189const uint32_t cols ) { 190uint32_t i ,j ;// two loop variables, uninteresting in all respects. 191 192i = 0 ; 193while (i < rows ) { 194j = 0 ; 195while (j < cols ) { 196double difference = fabs (a [(i * cols )+ j ]- b [(i * cols )+ j ]); 197if (difference > 1.0e-6 ) { 198fprintf (stderr ,"Sorry, matrices do not match, first difference at row " 199"%d and col %d\n" , 200i ,j ); 201return false; 202 } 203j += 1 ; 204 } 205i += 1 ; 206 } 207return true; 208} 209 210int assertEqualMatricesPtr (double ** a ,double ** b ,const uint32_t rows , 211const uint32_t cols ) { 212uint32_t i ,j ;// two loop variables, uninteresting in all respects. 213 214i = 0 ; 215while (i < rows ) { 216j = 0 ; 217while (j < cols ) { 218double difference = fabs (a [i ][j ]- b [i ][j ]); 219if (difference > 1.0e-6 ) { 220fprintf (stderr ,"Sorry, matrices do not match, first difference at row " 221"%d and col %d\n" , 222i ,j ); 223return false; 224 } 225j += 1 ; 226 } 227i += 1 ; 228 } 229return true; 230} 231 232void copyMatrix (double dest [],double src [],const uint32_t rows , 233const uint32_t cols ) { 234uint32_t i = 0 ; 235while (i < rows ) { 236uint32_t j = 0 ; 237while (j < cols ) { 238dest [(i * cols )+ j ]= src [(i * cols )+ j ]; 239j += 1 ; 240 } 241i += 1 ; 242 } 243} 244 245void copyMatrixPtr (double ** dest ,double ** src ,const uint32_t rows , 246const uint32_t cols ) { 247uint32_t i = 0 ; 248while (i < rows ) { 249uint32_t j = 0 ; 250while (j < cols ) { 251dest [i ][j ]= src [i ][j ]; 252j += 1 ; 253 } 254i += 1 ; 255 } 256} 257 258void printMatrix (double a [],const uint32_t rows ,const uint32_t cols ) { 259uint32_t i = 0 ; 260while (i < rows ) { 261uint32_t j = 0 ; 262while (j < cols ) { 263printf ("%g, " ,a [(i * cols )+ j ]);// %g is just like %f, only better 264j += 1 ; 265 } 266printf ("\n" ); 267i += 1 ; 268 } 269} 270 271void printMatrixPtr (double ** a ,const uint32_t rows ,const uint32_t cols ) { 272uint32_t i = 0 ; 273while (i < rows ) { 274uint32_t j = 0 ; 275while (j < cols ) { 276printf ("%g, " ,a [i ][j ]);// %g is just like %f, only better 277j += 1 ; 278 } 279printf ("\n" ); 280i += 1 ; 281 } 282} 283 284 285void memoryRelease (double ** ptr ,int length ) { 286for (int i = 0 ;i < length ;i ++ ) { 287free (* (ptr + i )); 288 } 289free (ptr ); 290} 291 292/* ---------------------------------------- */ 293 294void testMultiplyMatrixA (void ) { 295double a [100 ]; 296double b [100 ]; 297double c [100 ]; 298 299generateRandomMatrix (a ,10 ,10 ); 300generateIdentityMatrix (b ,10 ,10 ); 301multiplyMatrices (a ,10 ,10 ,b ,10 ,c ); 302printf ("Stage 1 test testMultiplyMatrixA:..." ); 303if (!assertEqualMatrices (a ,c ,10 ,10 )) { 304return ; 305 } 306printf ("passed! :-)\n" ); 307} 308 309void testMultiplyMatrixB (void ) { 310double a [100 ]; 311double b [100 ]; 312double c [100 ]; 313 314generateRandomMatrix (a ,10 ,10 ); 315generateIdentityMatrix (b ,10 ,10 ); 316multiplyMatrices (b ,10 ,10 ,a ,10 ,c ); 317printf ("Stage 1 test testMultiplyMatrixB:..." ); 318if (!assertEqualMatrices (a ,c ,10 ,10 )) { 319return ; 320 } 321printf ("passed! :-)\n" ); 322} 323 324void testMultiplyMatrixC (void ) { 325double a [100 ]; 326double b [100 ]; 327double c [100 ]; 328 329/* these arrays are used only for test C */ 330uint32_t perturb_rows [5 ]= {2 ,4 ,6 ,1 ,7 }; 331double perturb_vals [5 ]= {1.0 ,-1.0 ,2.0 ,-2.5 ,3.0 }; 332double expected []= { 3331 ,-10 ,0 ,0 ,18 ,0 ,-12 ,3 ,0 ,0 ,0 ,-6.5 ,0 ,0 ,10.5 ,0 ,-7 ,3 ,0 ,0 , 3340 ,-10 ,0 ,0 ,18 ,0 ,-12 ,3 ,0 ,0 ,0 ,-10 ,0 ,1 ,18 ,0 ,-12 ,3 ,0 ,0 , 3350 ,-10 ,1 ,0 ,20 ,0 ,-12 ,3 ,0 ,0 ,0 ,-10 ,0 ,0 ,18 ,1 ,-12 ,3 ,0 ,0 , 3360 ,-10 ,0 ,0 ,20 ,0 ,-13 ,3 ,0 ,0 ,0 ,-2.5 ,0 ,0 ,4.5 ,0 ,-3 ,1 ,0 ,0 , 3370 ,-10 ,0 ,0 ,18 ,0 ,-12 ,3 ,1 ,0 ,0 ,-10 ,0 ,0 ,18 ,0 ,-12 ,3 ,0 ,1 }; 338 339generateIdentityMatrix (a ,10 ,10 ); 340generateIdentityMatrix (b ,10 ,10 ); 341 342perturbMatrix (a ,10 ,10 ,perturb_rows ,perturb_vals ,5 ); 343copyMatrix (a ,b ,10 ,10 ); 344perturbMatrix (b ,10 ,10 ,perturb_rows ,perturb_vals ,5 ); 345 346multiplyMatrices (a ,10 ,10 ,b ,10 ,c ); 347 348printf ("Stage 1 test testMultiplyMatrixC:..." ); 349 350if (!assertEqualMatrices (c ,expected ,10 ,10 )) { 351return ; 352 } 353printf ("passed! :-)\n" ); 354} 355 356//added test case, try with non-square matrices and decimal points 357void testMultiplyMatrixD (void ) { 358double a [20 ]= { 3590.00 ,0.05 ,-0.04 ,0.67 , 360-0.26 ,0.75 ,0.38 ,0.43 , 3610.00 ,-0.48 ,0.07 ,0.16 , 3620.65 ,0.28 ,0.43 ,0.24 , 3630.20 ,0.00 ,0.00 ,1.00 }; 364double b [8 ]= { 3650.06 ,0.00 , 3660.00 ,0.34 , 367-0.49 ,0.71 , 368-0.44 ,0.00 }; 369double expected [10 ]= { 370-0.275200 ,-0.011400 , 371-0.391000 ,0.524800 , 372-0.104700 ,-0.113500 , 373-0.277300 ,0.400500 , 374-0.428000 ,0.000000 }; 375double c [10 ]; 376 377multiplyMatrices (a ,5 ,4 ,b ,2 ,c ); 378printf ("Stage 1 test testMultiplyMatrixD:..." ); 379if (!assertEqualMatrices (c ,expected ,5 ,2 )) { 380return ; 381 } 382printf ("passed! :-)\n" ); 383} 384 385void testStage1 (void ) { 386testMultiplyMatrixA (); 387testMultiplyMatrixB (); 388testMultiplyMatrixC (); 389testMultiplyMatrixD (); 390 391} 392 393/* ---------------------------------------- */ 394 395void testMultiplyMatrixPtrA (void ) { 396double ** a ; 397double ** b ; 398double ** c ; 399 400a = (double ** )malloc (sizeof (double * )* 10 ); 401for (int i = 0 ;i < 10 ;i ++ ) { 402* (a + i )= (double * )malloc (sizeof (double )* 10 ); 403 } 404b = (double ** )malloc (sizeof (double * )* 10 ); 405for (int i = 0 ;i < 10 ;i ++ ) { 406* (b + i )= (double * )malloc (sizeof (double )* 10 ); 407 } 408generateRandomMatrixPtr (a ,10 ,10 ); 409generateIdentityMatrixPtr (b ,10 ,10 ); 410c = multiplyMatricesPtr (a ,10 ,10 ,b ,10 ); 411printf ("Stage 2 testMultiplyMatrixPtrA:..." ); 412if (!assertEqualMatricesPtr (a ,c ,10 ,10 )) { 413return ; 414 } 415memoryRelease (a ,10 ); 416memoryRelease (b ,10 ); 417memoryRelease (c ,10 ); 418 419printf ("passed! :-)\n" ); 420} 421 422void testMultiplyMatrixPtrB (void ) { 423double ** a ; 424double ** b ; 425double ** c ; 426 427a = (double ** )malloc (sizeof (double * )* 10 ); 428for (int i = 0 ;i < 10 ;i ++ ) { 429* (a + i )= (double * )malloc (sizeof (double )* 10 ); 430 } 431b = (double ** )malloc (sizeof (double * )* 10 ); 432for (int i = 0 ;i < 10 ;i ++ ) { 433* (b + i )= (double * )malloc (sizeof (double )* 10 ); 434 } 435 436generateRandomMatrixPtr (a ,10 ,10 ); 437generateIdentityMatrixPtr (b ,10 ,10 ); 438c = multiplyMatricesPtr (b ,10 ,10 ,a ,10 ); 439 440printf ("Stage 2 testMultiplyMatrixPtrB:..." ); 441 442if (!assertEqualMatricesPtr (a ,c ,10 ,10 )) { 443return ; 444 } 445 446memoryRelease (a ,10 ); 447memoryRelease (b ,10 ); 448memoryRelease (c ,10 ); 449 450printf ("passed! :-)\n" ); 451} 452 453void testMultiplyMatrixPtrC (void ) { 454double ** a ; 455double ** b ; 456double ** c ; 457double ** expected_ptr ; 458 459/* these arrays are used only for test C */ 460uint32_t perturb_rows [5 ]= {2 ,4 ,6 ,1 ,7 }; 461double perturb_vals [5 ]= {1.0 ,-1.0 ,2.0 ,-2.5 ,3.0 }; 462double one [10 ]= {1 ,-10 ,0 ,0 ,18 ,0 ,-12 ,3 ,0 ,0 }; 463double two [10 ]= {0 ,-6.5 ,0 ,0 ,10.5 ,0 ,-7 ,3 ,0 ,0 }; 464double three [10 ]= {0 ,-10 ,0 ,0 ,18 ,0 ,-12 ,3 ,0 ,0 }; 465double four [10 ]= {0 ,-10 ,0 ,1 ,18 ,0 ,-12 ,3 ,0 ,0 }; 466double five [10 ]= {0 ,-10 ,1 ,0 ,20 ,0 ,-12 ,3 ,0 ,0 }; 467double six [10 ]= {0 ,-10 ,0 ,0 ,18 ,1 ,-12 ,3 ,0 ,0 }; 468double seven [10 ]= {0 ,-10 ,0 ,0 ,20 ,0 ,-13 ,3 ,0 ,0 }; 469double eight [10 ]= {0 ,-2.5 ,0 ,0 ,4.5 ,0 ,-3 ,1 ,0 ,0 }; 470double nine [10 ]= {0 ,-10 ,0 ,0 ,18 ,0 ,-12 ,3 ,1 ,0 }; 471double ten [10 ]= {0 ,-10 ,0 ,0 ,18 ,0 ,-12 ,3 ,0 ,1 }; 472 473double * expected []= {one ,two ,three ,four ,five , 474six ,seven ,eight ,nine ,ten }; 475 476expected_ptr = expected ; 477 478a = (double ** )malloc (sizeof (double * )* 10 ); 479for (int i = 0 ;i < 10 ;i ++ ) { 480* (a + i )= (double * )malloc (sizeof (double )* 10 ); 481 } 482b = (double ** )malloc (sizeof (double * )* 10 ); 483for (int i = 0 ;i < 10 ;i ++ ) { 484* (b + i )= (double * )malloc (sizeof (double )* 10 ); 485 } 486 487generateIdentityMatrixPtr (a ,10 ,10 ); 488perturbMatrixPtr (a ,10 ,10 ,perturb_rows ,perturb_vals ,5 ); 489 490generateIdentityMatrixPtr (b ,10 ,10 ); 491copyMatrixPtr (a ,b ,10 ,10 ); 492perturbMatrixPtr (b ,10 ,10 ,perturb_rows ,perturb_vals ,5 ); 493c = multiplyMatricesPtr (a ,10 ,10 ,b ,10 ); 494 495printf ("Stage 2 testMultiplyMatrixPtrC:..." ); 496 497if (!assertEqualMatricesPtr (c ,expected_ptr ,10 ,10 )) { 498return ; 499 } 500 501memoryRelease (a ,10 ); 502memoryRelease (b ,10 ); 503memoryRelease (c ,10 ); 504 505printf ("passed! :-)\n" ); 506} 507 508void testStage2 (void ) { 509testMultiplyMatrixPtrA (); 510testMultiplyMatrixPtrB (); 511testMultiplyMatrixPtrC (); 512} 513 514/* ---------------------------------------- */ 515 516void testTransposeA (void ) { 517double ** result ; 518double ** input_ptr ; 519double ** expected_ptr ; 520 521double one_a [2 ]= {1 ,2 }; 522double two_a [2 ]= {3 ,4 }; 523double * input_a []= {one_a ,two_a }; 524input_ptr = input_a ; 525 526double one_trans_a [2 ]= {1 ,3 }; 527double two_trans_a [2 ]= {2 ,4 }; 528 529double * expected_a []= {one_trans_a ,two_trans_a }; 530expected_ptr = expected_a ; 531 532 533printf ("Stage 3 testTransposeA:..." ); 534result = transposeMatrix (input_ptr ,2 ,2 ); 535 536if (!assertEqualMatricesPtr (result ,expected_ptr ,2 ,2 )) { 537return ; 538 } 539 540memoryRelease (result ,2 ); 541printf ("passed! :-)\n" ); 542} 543 544void testTransposeB (void ) { 545double ** result ; 546double ** input_ptr ; 547double ** expected_ptr ; 548 549double one_b [3 ]= {1 ,2 ,3 }; 550double two_b [3 ]= {4 ,5 ,6 }; 551double three_b [3 ]= {7 ,8 ,9 }; 552double * input_b []= {one_b ,two_b ,three_b }; 553input_ptr = input_b ; 554 555double one_trans_b [3 ]= {1 ,4 ,7 }; 556double two_trans_b [3 ]= {2 ,5 ,8 }; 557double three_trans_b [3 ]= {3 ,6 ,9 }; 558 559double * expected_b []= {one_trans_b ,two_trans_b ,three_trans_b }; 560expected_ptr = expected_b ; 561 562printf ("Stage 3 testTransposeB:..." ); 563result = transposeMatrix (input_ptr ,3 ,3 ); 564if (!assertEqualMatricesPtr (result ,expected_ptr ,3 ,3 )) { 565return ; 566 } 567 568memoryRelease (result ,3 ); 569printf ("passed! :-)\n" ); 570} 571 572void testTransposeC (void ) { 573double ** result ; 574double ** input_ptr ; 575double ** expected_ptr ; 576 577double one_c [3 ]= {1 ,2 ,3 }; 578double two_c [3 ]= {4 ,5 ,6 }; 579double * input_c []= {one_c ,two_c }; 580input_ptr = input_c ; 581 582double one_trans_c [2 ]= {1 ,4 }; 583double two_trans_c [2 ]= {2 ,5 }; 584double three_trans_c [2 ]= {3 ,6 }; 585 586double * expected_c []= {one_trans_c ,two_trans_c ,three_trans_c }; 587expected_ptr = expected_c ; 588 589printf ("Stage 3 testTransposeC:..." ); 590result = transposeMatrix (input_ptr ,2 ,3 ); 591if (!assertEqualMatricesPtr (result ,expected_ptr ,3 ,2 )) { 592return ; 593 } 594 595memoryRelease (result ,3 ); 596 597printf ("passed! :-)\n" ); 598} 599 600void testStage3 (void ) { 601testTransposeA (); 602testTransposeB (); 603testTransposeC (); 604} 605 606int main (int argc ,char ** argv ) { 607int i ; 608 609/** Run only a specific test */ 610for (i = 1 ;i < argc ;i ++ ) { 611if (strcmp ("testMultiplyMatrixA" ,argv [i ])== 0 ) { 612testMultiplyMatrixA (); 613 }else if (strcmp ("testMultiplyMatrixB" ,argv [i ])== 0 ) { 614testMultiplyMatrixB (); 615 }else if (strcmp ("testMultiplyMatrixC" ,argv [i ])== 0 ) { 616testMultiplyMatrixC (); 617 }else if (strcmp ("testMultiplyMatrixD" ,argv [i ])== 0 ) { 618testMultiplyMatrixD (); 619 }else if (strcmp ("testTransposeA" ,argv [i ])== 0 ) { 620testTransposeA (); 621 }else if (strcmp ("testTransposeB" ,argv [i ])== 0 ) { 622testTransposeB (); 623 }else if (strcmp ("testTransposeC" ,argv [i ])== 0 ) { 624testTransposeC (); 625 }else if (strcmp ("testMultiplyMatrixPtrA" ,argv [i ])== 0 ) { 626testMultiplyMatrixPtrA (); 627 }else if (strcmp ("testMultiplyMatrixPtrB" ,argv [i ])== 0 ) { 628testMultiplyMatrixPtrB (); 629 }else if (strcmp ("testMultiplyMatrixPtrC" ,argv [i ])== 0 ) { 630testMultiplyMatrixPtrC (); 631 }else { 632printf ("ERROR: Incorrect test name %s\n" ,argv [i ]); 633exit (1 ); 634 } 635 } 636 637if (argc == 1 ) { 638testStage1 (); 639testStage2 (); 640testStage3 (); 641 } 642 643return 0 ; 644}