forked from FredericoNesti/HMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHMM0.java
More file actions
64 lines (64 loc) · 2.08 KB
/
HMM0.java
File metadata and controls
64 lines (64 loc) · 2.08 KB
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
import java.util.Scanner;
public class hmm0 {
public static void print_table_to_line(double[][] A){
String out = String.format("%d %d ", A.length, A[0].length);
for(int i=0;i<A.length;i++){
for(int j=0;j<A[0].length;j++){
out += String.format("%f ", A[i][j]);
}
}
int tmp = out.length();
System.out.println(out.substring(0, tmp-1));
}
public static double[][] multiplyTable(double[][] A, double[][] B){
double[][] C = new double[A.length][B[0].length];
for(int i=0;i<A.length;i++){
for(int j=0;j<B[0].length;j++){
double sum = 0;
for (int k=0;k<A[i].length;k++){
sum+= A[i][k]*B[k][j];
}
C[i][j] = sum;
}
}
return C;
}
public static void print_table(double[][] arr){
for(int i=0;i<arr.length;i++){
if (arr[i].getClass().isArray()){
for(int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]);
System.out.print(' ');
}
}else{
System.out.print(arr[i]);
}
System.out.println();
}
}
public static double[][] readinputtable(String inp){
Scanner s = new Scanner(inp);
int rows = s.nextInt();
int cols = s.nextInt();
double[][] A = new double[rows][cols];
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
A[i][j] = s.nextDouble();
}
}
s.close();
return A;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double[][] A = readinputtable(scan.nextLine());
double[][] B = readinputtable(scan.nextLine());
double[][] p = readinputtable(scan.nextLine());
//print_table(A);
//print_table(B);
//print_table(p);
double[][] tmp = multiplyTable(p,A);
print_table_to_line(multiplyTable(tmp,B));
scan.close();
}
}