Created
September 22, 2017 07:27
-
-
Save programaths/c817f11f775722ba79b61bf4611c4dfc to your computer and use it in GitHub Desktop.
rotate Array Linear Algebra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| function matMul($a,$b){ | |
| $countI = count($a); | |
| $countJ = count($b[0]); | |
| $countK = count($b); | |
| $c=[]; | |
| for($i=0;$i<$countI;$i++){ | |
| for($j=0;$j<$countJ;$j++){ | |
| $sum = 0; | |
| for($k=0;$k<$countK;$k++){ | |
| $sum += $a[$i][$k] * $b[$k][$j]; | |
| } | |
| $c[$i][$j] = $sum; | |
| } | |
| } | |
| return $c; | |
| } | |
| function rotateArrayLeft($a){ | |
| $aR = count($a); | |
| $aC = count($a[0]); | |
| $rotMatrix = [[0,-1] | |
| ,[1,0]]; | |
| $rotated = []; | |
| for($i=0;$i<$aR;$i++){ | |
| for($j=0;$j<$aC;$j++){ | |
| $newCoords = matMul($rotMatrix,[[$i],[$j]]); | |
| $rotated[$newCoords[0][0]+$aR][$newCoords[1][0]] = $a[$i][$j]; | |
| } | |
| } | |
| //$rotated = | |
| //$rotated = array_values($rotated); | |
| ksort($rotated); | |
| print_r($rotated); | |
| } | |
| rotateArrayLeft([[1,2,3],[4,5,6]]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment