One more time.
| @@ -0,0 +1,25 @@ | ||
| 1 | +/** | |
| 2 | + * | |
| 3 | + */ | |
| 4 | +package math; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * @author ishihara | |
| 8 | + * | |
| 9 | + */ | |
| 10 | +public class Tips { | |
| 11 | + public static int gcd(int n1,int n2) | |
| 12 | + { | |
| 13 | + int m=Math.max(Math.abs(n1), Math.abs(n2)); | |
| 14 | + int n=Math.min(Math.abs(n1), Math.abs(n2)); | |
| 15 | + if (n==0) | |
| 16 | + return m; | |
| 17 | + int l=m%n; | |
| 18 | + if(l==0) | |
| 19 | + return n; | |
| 20 | + else | |
| 21 | + return Tips.gcd(l, n); | |
| 22 | + } | |
| 23 | + | |
| 24 | + | |
| 25 | +} |
| @@ -0,0 +1,36 @@ | ||
| 1 | +/** | |
| 2 | + * | |
| 3 | + */ | |
| 4 | +package math; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * @author ishihara | |
| 8 | + * | |
| 9 | + */ | |
| 10 | +public class Test { | |
| 11 | + | |
| 12 | + /** | |
| 13 | + * @param args | |
| 14 | + */ | |
| 15 | + public static void main(String[] args) { | |
| 16 | + // TODO 自動生成されたメソッド・スタブ | |
| 17 | + Frac f1 = new Frac(-3); | |
| 18 | + f1.printFrac(); | |
| 19 | + Frac f2 = new Frac(2,3); | |
| 20 | + f2.printFrac(); | |
| 21 | + Frac f3 = Frac.plus(f1, f2); | |
| 22 | + f3.printFrac(); | |
| 23 | + Frac f4 = Frac.minus(f1, f2); | |
| 24 | + f4.printFrac(); | |
| 25 | + Frac f5 = Frac.mult(f1, f2); | |
| 26 | + f5.printFrac(); | |
| 27 | + Frac f6 = Frac.div(f1, f2); | |
| 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 | + | |
| 34 | + } | |
| 35 | + | |
| 36 | +} |
| @@ -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 | +} |
| @@ -0,0 +1,64 @@ | ||
| 1 | +/** | |
| 2 | + * | |
| 3 | + */ | |
| 4 | +package math; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * @author ishihara | |
| 8 | + * | |
| 9 | + */ | |
| 10 | +public class Frac { | |
| 11 | + private int numerator; | |
| 12 | + private int denominator; | |
| 13 | + | |
| 14 | + public Frac(int numerator) | |
| 15 | + { | |
| 16 | + this(numerator,1); | |
| 17 | + } | |
| 18 | + | |
| 19 | + public Frac(int numerator, int denominator) | |
| 20 | + { | |
| 21 | + this.numerator=numerator; | |
| 22 | + this.denominator=denominator; | |
| 23 | + reduction(); | |
| 24 | + } | |
| 25 | + | |
| 26 | + public static Frac plus(Frac f1,Frac f2) | |
| 27 | + { | |
| 28 | + return new Frac(f1.getNumerator()*f2.getDenominator()+f1.getDenominator()*f2.getNumerator(),f1.getDenominator()*f2.getDenominator()); | |
| 29 | + } | |
| 30 | + | |
| 31 | + public static Frac minus(Frac f1,Frac f2) | |
| 32 | + { | |
| 33 | + return new Frac(f1.getNumerator()*f2.getDenominator()-f1.getDenominator()*f2.getNumerator(),f1.getDenominator()*f2.getDenominator()); | |
| 34 | + } | |
| 35 | + | |
| 36 | + public static Frac mult(Frac f1,Frac f2) | |
| 37 | + { | |
| 38 | + return new Frac(f1.getNumerator()*f2.getNumerator(),f1.getDenominator()*f2.getDenominator()); | |
| 39 | + } | |
| 40 | + | |
| 41 | + public static Frac div(Frac f1,Frac f2) | |
| 42 | + { | |
| 43 | + return new Frac(f1.getNumerator()*f2.getDenominator(),f1.getDenominator()*f2.getNumerator()); | |
| 44 | + } | |
| 45 | + | |
| 46 | + private int getNumerator(){return this.numerator;} | |
| 47 | + private int getDenominator(){return this.denominator;} | |
| 48 | + | |
| 49 | + public void printFrac(){System.out.println(this.numerator+"/"+this.denominator);} | |
| 50 | + | |
| 51 | + private void reduction() | |
| 52 | + { | |
| 53 | + int g = Tips.gcd(this.numerator, this.denominator); | |
| 54 | + try | |
| 55 | + { | |
| 56 | + this.numerator=this.numerator*(this.denominator/Math.abs(this.denominator))/g; | |
| 57 | + this.denominator=this.denominator*(this.denominator/Math.abs(this.denominator))/g; | |
| 58 | + }catch(ArithmeticException e) | |
| 59 | + { | |
| 60 | + System.out.println("DIV 0!!"); | |
| 61 | + } | |
| 62 | + } | |
| 63 | + | |
| 64 | +} |