progJ.pdf progJ-s.pdf progJ-e.pdf 記事一覧 |
変数のウオッチ |
progJ-e.pdf にprogJ.pdf の[#30]-[#31]に対応する部分の準備となる演習を追加しました.
//Mat2.java
package myproj;
public class Mat2 {
Complex[ ][ ] e = new Complex[2][2];
Mat2( ){
int i, k;
for(i=0; i e[i][k] = new Complex( );
}
}
void load(Mat2 w){
int i, k;
for(i=0; i e[i][k].load(w.e[i][k]);
}
}
void add(Mat2 w){
int i, k;
for(i=0; i e[i][k].add(w.e[i][k]);
}
}
void chk(int n, Complex z){
System.out.println(n +": "+ z.r +", "+ z.i);
}
void multL(Mat2 w){
Mat2 v; Complex z; int i, k;
v = new Mat2( ); z = new Complex( );
v.load(this);
for(i=0; i e[i][k].load(w.e[i][0]);
e[i][k].mult(v.e[0][k]);
z.load(w.e[i][1]); z.mult(v.e[1][k]);
e[i][k].add(z);
}
}
/* その他の定義 */
}
//Main.java
package myproj;
public class Main {
public static void main(String[] args) {
Mat2 w1 = new Mat2(), w2 = new Mat2();
w1.e[0][0] = new Complex(1, 0);
w1.e[0][1] = new Complex(2, 1);
w1.e[1][0] = new Complex(0, 3);
w1.e[1][1] = new Complex(0, 0);
w2.load(w1); w2.add(w1);
w2.load(w1); w2.multL(w1);
}
}
/////////////////////////////////////////////
//Complex.java
package myproj;
public class Complex {
double r, i;
Complex(double x, double y) //-
{r = x; i = y;}
void add(Complex z) //-
{r += z.r; i += z.i;}
void mult(Complex z){
double tmp = r * z.r - i * z.i;
i = r * z.i + i * z.r; r = tmp;
}
double abs( ){
return Math.sqrt(r * r + i * i);
}
//----------------
double re( ){return r;}
double im( ){return i;}
void conj( ){i = -i;}
double abs2( ){return r * r + i * i;}
//----------------
void recip( ){
double a2 = this.abs2(); r = r/a2; i = -i/a2;
}
Complex( ){r = i = 0;}
Complex(double x){r = x; i = 0;}
void load(Complex z){r = z.r; i = z.i;}
}