-
Notifications
You must be signed in to change notification settings - Fork 0
/
Calc.java
109 lines (101 loc) · 2.63 KB
/
Calc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
class Calc {
// ベクトル同士の減算を計算する
public static double[] addVec(double x[], double y[]) {
int N = x.length;
double[] z = new double[N];
if (x.length != y.length) {
System.out.println("x.length!=y.length");
return z;
}
for (int i = 0; i < x.length; i++) {
z[i] = x[i] + y[i];
}
return z;
}
// ベクトル同士の減算を計算する
public static double[] subVec(double x[], double y[]) {
int N = x.length;
double[] z = new double[N];
if (x.length != y.length) {
System.out.println("x.length!=y.length");
return z;
}
for (int i = 0; i < x.length; i++) {
z[i] = x[i] - y[i];
}
return z;
}
// 行列Aとベクトルxの積を計算する
public static double[] matVec(double A[][], double x[]) {
double[] z = new double[A.length];
for (int i = 0; i < A.length; i++) {
for (int j = 0; j < A.length; j++) {
z[i] += A[i][j] * x[j];
}
}
return z;
}
// ベクトルの∞ノルムを計算する
// 絶対値の中で一番値が大きいもの
public static double vecNormInf(double x[]) {
double z = 0;
for (int i = 0; i < x.length; i++) {
if (z < Math.abs(x[i])) {
z = Math.abs(x[i]);
}
}
return z;
}
public static double[] copyVec(double[] vec) {
int n = vec.length;
double[] copy = new double[n];
for (int i = 0; i < n; i++) {
copy[i] = vec[i];
}
return copy;
}
public static double[][] copyMat(double[][] mat) {
double[][] copy = new double[mat.length][mat[0].length];
for (int i = 0; i < mat.length; i++) {
for (int j = 0; j < mat[0].length; j++) {
copy[i][j] = mat[i][j];
}
}
return copy;
}
public static void printMat(double[][] mat) {
int n = mat.length;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%10.2e ", mat[i][j]);
}
System.out.println();
}
}
public static void printVec(double[] vec) {
int n = vec.length;
for (int i = 0; i < n; i++) {
System.out.printf("%10.2e ", vec[i]);
}
System.out.println();
}
public static void printMatWolfram(double[][] mat) {
int n = mat.length;
System.out.print("[");
for (int i = 0; i < n; i++) {
System.out.print("[");
for (int j = 0; j < n; j++) {
System.out.printf("%.2f", mat[i][j]);
if (j != n - 1) {
System.out.print(",");
}
}
System.out.print("]");
if (i != n - 1) {
System.out.print(",");
}
}
System.out.print("]");
System.out.println();
}
}