Add Matrix.java
| @@ -26,6 +26,11 @@ | ||
| 26 | 26 | f5.printFrac(); |
| 27 | 27 | Frac f6 = Frac.div(f1, f2); |
| 28 | 28 | f6.printFrac(); |
| 29 | + int a[]={1,2,3}; | |
| 30 | + int b[][]={{1,2,3},{2,3,4},{3,4,5}}; | |
| 31 | + Matrix.printVector(Matrix.multMatrix(a, b)); | |
| 32 | + Matrix.printMatrix(Matrix.multMatrix(b, b)); | |
| 33 | + | |
| 29 | 34 | } |
| 30 | 35 | |
| 31 | 36 | } |
| @@ -20,4 +20,6 @@ | ||
| 20 | 20 | else |
| 21 | 21 | return Tips.gcd(l, n); |
| 22 | 22 | } |
| 23 | + | |
| 24 | + | |
| 23 | 25 | } |
| @@ -0,0 +1,54 @@ | ||
| 1 | +/** | |
| 2 | + * | |
| 3 | + */ | |
| 4 | +package math; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * @author ishihara | |
| 8 | + * | |
| 9 | + */ | |
| 10 | +public class Matrix { | |
| 11 | + public static void printVector(int[] v) | |
| 12 | + { | |
| 13 | + for(int i=0;i<v.length;i++) | |
| 14 | + System.out.print(v[i]+" "); | |
| 15 | + System.out.println(); | |
| 16 | + } | |
| 17 | + public static void printMatrix(int[][] m) | |
| 18 | + { | |
| 19 | + for(int i=0;i<m.length;i++) | |
| 20 | + { | |
| 21 | + for(int j=0;j<m[i].length;j++) | |
| 22 | + System.out.print(m[i][j]+" "); | |
| 23 | + System.out.println(); | |
| 24 | + } | |
| 25 | + System.out.println(); | |
| 26 | + } | |
| 27 | + | |
| 28 | + public static int[] multMatrix(int a[],int b[][]) | |
| 29 | + { | |
| 30 | + int c[]=new int[a.length]; | |
| 31 | + for(int i=0;i<a.length;i++) | |
| 32 | + { | |
| 33 | + c[i]=0; | |
| 34 | + for(int j=0;j<b.length;j++) | |
| 35 | + { | |
| 36 | + c[i]+=a[j]*b[j][i]; | |
| 37 | + } | |
| 38 | + } | |
| 39 | + return c; | |
| 40 | + } | |
| 41 | + | |
| 42 | + public static int[][] multMatrix(int a[][],int b[][]) | |
| 43 | + { | |
| 44 | + int c[][]=new int[a.length][b[0].length]; | |
| 45 | + for(int i=0;i<a.length;i++) | |
| 46 | + for(int j=0;j<b.length;j++) | |
| 47 | + { | |
| 48 | + c[i][j]=0; | |
| 49 | + for(int k=0;k<b[i].length;k++) | |
| 50 | + c[i][j]+=a[i][k]*b[k][j]; | |
| 51 | + } | |
| 52 | + return c; | |
| 53 | + } | |
| 54 | +} |