Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

*.pyc
167 changes: 167 additions & 0 deletions codes.Java/Fraction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package com.utils.wycode;

public class Fraction {
private Integer numerator; // ����;
private Integer denominator; // ��ĸ

/**
* ������
*/
public Fraction() {
}

public Fraction(Integer numerator, Integer denominator) {
this.numerator = numerator;
this.denominator = denominator;
}

public Integer getDenominator() {
return denominator;
}

public void setDenominator(Integer denominator) {
this.denominator = denominator;
}

public Integer getNumerator() {
return numerator;
}

public void setNumerator(Integer numerator) {
this.numerator = numerator;
}

/**
* �����Լ��
*
* @return
*/
private int getGCD(int a, int b) {
int max = Math.max(a, b);
int min = Math.min(a, b);
int mod = max % min;
if (mod == 0) {
return min;
} else {
return this.getGCD(mod, min);
}
}

/**
* �򻯺�Լ��
*
* @return
*/
public Fraction simplify() {
// ��ʽ��
if (this.numerator == 0) {
this.denominator = null;
return this;
} else if (this.denominator * this.numerator > 0
&& this.denominator < 0) {
this.denominator = Math.abs(this.denominator);
this.numerator = Math.abs(this.numerator);
return this;
} else if (this.denominator < 0) {
this.denominator = Math.abs(this.denominator);
this.numerator = -this.numerator;
return this;
}
// Լ��
int gcd = this.getGCD(Math.abs(denominator), Math.abs(numerator));
if (gcd == 1) {
return this;
} else {
this.denominator = this.denominator / gcd;
this.numerator = this.numerator / gcd;
return this;
}
}

/**
* �ӷ�
*
* @param fraction
* @return
*/
public Fraction add(Fraction fraction) {
Integer resultDenominator = Math.abs(this.denominator)
* Math.abs(fraction.getDenominator());
Integer resultNumerator = this.numerator * fraction.getDenominator()
+ this.denominator * fraction.getNumerator();
Fraction result = new Fraction(resultNumerator, resultDenominator);
return result.simplify();
}

/**
* ����
*
* @param fraction
* @return
*/
public Fraction minus(Fraction fraction) {
Integer resultDenominator = Math.abs(this.denominator)
* Math.abs(fraction.getDenominator());
Integer resultNumerator = this.numerator * fraction.getDenominator()
- this.denominator * fraction.getNumerator();
Fraction result = new Fraction(resultNumerator, resultDenominator);
return result.simplify();
}

/**
* �˷�
*
* @param fraction
* @return
*/
public Fraction multiply(Fraction fraction) {
Fraction result = null;
if (this.numerator == 0 || fraction.numerator == 0) {
result = new Fraction(0, null);
}
Integer resultDenominator = this.denominator
* fraction.getDenominator();
Integer resultNumerator = this.numerator * fraction.getNumerator();
result = new Fraction(resultNumerator, resultDenominator);
return result.simplify();
}

/**
*����
*
* @param fraction
* @return
*/
public Fraction divide(Fraction fraction) {
Fraction result = null;
if (this.numerator == 0) {
result = new Fraction(0, null);
}
Integer resultDenominator = this.denominator * fraction.getNumerator();
Integer resultNumerator = this.numerator * fraction.getDenominator();
result = new Fraction(resultNumerator, resultDenominator);
return result.simplify();
}

@Override
public String toString() {
return this.numerator + "/" + this.denominator;
}

/**
* ����
*
* @param args
*/
public static void main(String[] args) {
Fraction f1 = new Fraction(1, 2);
Fraction f2 = new Fraction(2, 4);
System.out.println(f1);
System.out.println(f2);
System.out.println(f2.simplify());
System.out.println(f1.add(f2));
System.out.println(f1.minus(f2));
System.out.println(f1.multiply(f2));
System.out.println(f1.divide(f2));
}
}
Binary file added codes.Java/Jama-1.0.2.jar
Binary file not shown.
150 changes: 150 additions & 0 deletions codes.Java/Matrix.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package com.utils.wycode;

public class Matrix {
private String[][] m;
private int row;
private int column;

public Matrix(String[][] m) {
this.m=m;
this.row=m.length;
this.column=m[0].length;
}

public static Matrix mulMatrix(Matrix a,Matrix b) {
int myrow=a.getRow();
int mycolumn=b.getColumn();
String[][] result=new String[myrow][mycolumn];
String atemp="";
String btemp="";
for(int i=0;i<myrow;i++)
for(int j=0;j<mycolumn;j++) {
result[i][j]="";
}
String[][] a1=a.getM();
String[][] b1=b.getM();
//***************************************************************************
for(int i=0;i<myrow;i++) {
for(int j=0;j<mycolumn;j++) {
for(int k=0;k<myrow;k++) {
if(a1[i][k].equals("0") || b1[k][j].equals("0")) {
result[i][j]+="";
}else if(a1[i][k].equals("1")) {
if(b1[k][j].contains("-")) {
result[i][j]+=b1[k][j];
}else {
result[i][j]+="+"+b1[k][j];
}

}else if(b1[k][j].equals("1")) {
if(a1[i][k].contains("-")) {
result[i][j]+=a1[i][k];
}else {
result[i][j]+="+"+a1[i][k];
}

}else if(b1[k][j].equals("-1")) {
if(a1[i][k].contains("-")) {
atemp=a1[i][k].substring(1,a1[i][k].length());
result[i][j]+="+"+atemp;
}else {
result[i][j]+="-"+a1[i][k];
}

}else if(a1[i][k].equals("-1")) {
if(b1[k][j].contains("-")) {
btemp=b1[k][j].substring(1,b1[k][j].length());
result[i][j]+="+"+btemp;
}else {
result[i][j]+="-"+b1[k][j];
}

}else {
if(b1[k][j].contains("-")) {
if(a1[i][k].contains("-")) {
atemp=a1[i][k].substring(1,a1[i][k].length());
btemp=b1[k][j].substring(1,b1[k][j].length());
result[i][j]+=atemp+"*"+btemp;
}else {
btemp=b1[k][j].substring(1,b1[k][j].length());
result[i][j]+="-"+a1[i][k]+"*"+btemp;
}

}else {
if(a1[i][k].contains("-")) {
atemp=a1[i][k].substring(1,a1[i][k].length());

result[i][j]+="-"+atemp+"*"+b1[k][j];
}else {

result[i][j]+="+"+a1[i][k]+"*"+b1[k][j];
}
}
}


}

}
}


//***************************************************************************
return new Matrix(result);
}

@Override
public String toString() {
String temp="";
String[][] array=this.getM();
for (int i=0;i<row;i++) {
for(int j=0;j<column;j++) {
temp+=array[i][j]+"\t";
}
temp+="\n";
}
return temp;

}
public String[][] getM() {
return m;
}

public void setM(String[][] m) {
this.m = m;
}

public int getRow() {
return row;
}

public void setRow(int row) {
this.row = row;
}

public int getColumn() {
return column;
}

public void setColumn(int column) {
this.column = column;
}

public static void main(String[] args) {
String[][] R=new String[3][3];
String[][] b=new String[3][3];

R[0][0]="cos(t)";
R[0][1]="sin(t)";
R[0][2]="0";
R[1][0]="-sin(t)";
R[1][1]="cos(t)";
R[1][2]="0";
R[2][0]="0";
R[2][1]="0";
R[2][2]="-1";
Matrix a=new Matrix(R);

System.out.print(a);
}
}
17 changes: 17 additions & 0 deletions codes.Java/p1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mat2java.wycode;
public class p1 {
public static void main(String[] args) {
int n=10;
double[] p=new double[n];

for(int i=0;i<n;i++) {
p[i]=0.1;

}


for(double a:p) {
System.out.println(a);
}
}
}
53 changes: 53 additions & 0 deletions codes.Java/p10.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.mat2java.wycode;

import Jama.Matrix;

public class p10 {
public static void main(String[] args) {
String[][] world = {{"red","green","green","red","red"},
{"red","red","green","red","red"},
{"red","red","green","green","red"},
{"red","red","red","red","red"}};

int nRow = world.length;
int nCol = world[0].length;

double pStart = 0.7;
double[][] p = new double[nRow][nCol];
for(int i = 0; i < nRow; i++) {
for(int j = 0; j < nCol; j++) {
p[i][j] = (1 - pStart) / (nRow * nCol - 1);
}
}
p[2][1] = pStart;

int[] stop = {0, 0};
int[] right = {0, 1};
int[] left = {0, -1};
int[] down = {1, 0};
int[] up = {-1, 0};

double pMoveCorrect = 0.8;
double[][] q = move1(p, right, pMoveCorrect);
for(int i = 0; i < q.length; i++) {
for(int j = 0; j < q[0].length; j++) {
System.out.printf("%1.4f%s", p[i][j], " " );
}
System.out.println();
}
}

public static double[][] move1(double[][] p, int[] u, double pMoveCorrect){
int nRow = p.length;
int nCol = p[0].length;

double[][] q = new double[nRow][nCol];

for(int i = 0; i < nRow; i++) {
for(int j = 0; j < nCol; j++ ) {
q[i][j] = pMoveCorrect * p[(i - 1 - u[0]) % nRow + 1][(j - 1 - u[1]) / nCol + 1] + (1 - pMoveCorrect) * p[i][j];
}
}
return q;
}
}
Loading