tile.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. include "../Matrix.php";
  3. /**
  4. * Tiling of matrix X in [rowWise by colWise] dimension. Tiling
  5. * creates a larger matrix than the original data X. Example, if
  6. * X is to be tiled in a [3 x 4] manner, then:
  7. *
  8. * / \
  9. * | X X X X |
  10. * C = | X X X X |
  11. * | X X X X |
  12. * \ /
  13. *
  14. * @param X Matrix
  15. * @param rowWise int
  16. * @param colWise int
  17. * @return Matrix
  18. */
  19. function tile(&$X, $rowWise, $colWise){
  20. $xArray = $X->getArray();
  21. print_r($xArray);
  22. $countRow = 0;
  23. $countColumn = 0;
  24. $m = $X->getRowDimension();
  25. $n = $X->getColumnDimension();
  26. if( $rowWise<1 || $colWise<1 ){
  27. die("tile : Array index is out-of-bound.");
  28. }
  29. $newRowDim = $m*$rowWise;
  30. $newColDim = $n*$colWise;
  31. $result = array();
  32. for($i=0 ; $i<$newRowDim; ++$i) {
  33. $holder = array();
  34. for($j=0 ; $j<$newColDim ; ++$j) {
  35. $holder[$j] = $xArray[$countRow][$countColumn++];
  36. // reset the column-index to zero to avoid reference to out-of-bound index in xArray[][]
  37. if($countColumn == $n) { $countColumn = 0; }
  38. } // end for
  39. ++$countRow;
  40. // reset the row-index to zero to avoid reference to out-of-bound index in xArray[][]
  41. if($countRow == $m) { $countRow = 0; }
  42. $result[$i] = $holder;
  43. } // end for
  44. return new Matrix($result);
  45. }
  46. $X =array(1,2,3,4,5,6,7,8,9);
  47. $nRow = 3;
  48. $nCol = 3;
  49. $tiled_matrix = tile(new Matrix($X), $nRow, $nCol);
  50. echo "<pre>";
  51. print_r($tiled_matrix);
  52. echo "</pre>";
  53. ?>