summaryrefslogtreecommitdiff
path: root/gi/posterior-regularisation/prjava/src/util/Printing.java
blob: 14fcbe9166c95f4b4d201d92e1c7f5d64194d9b0 (plain)
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package util;

public class Printing {
	static java.text.DecimalFormat fmt = new java.text.DecimalFormat();

	public static String padWithSpace(String s, int len){
		StringBuffer sb = new StringBuffer();
		while(sb.length() +s.length() < len){
			sb.append(" ");
		}
		sb.append(s);
		return sb.toString();
	}
	
	public static String prettyPrint(double d, String patt, int len) {
		fmt.applyPattern(patt);
		String s = fmt.format(d);
		while (s.length() < len) {
			s = " " + s;
		}
		return s;
	}
	
	public static  String formatTime(long duration) {
		StringBuilder sb = new StringBuilder();
		double d = duration / 1000;
		fmt.applyPattern("00");
		sb.append(fmt.format((int) (d / (60 * 60))) + ":");
		d -= ((int) d / (60 * 60)) * 60 * 60;
		sb.append(fmt.format((int) (d / 60)) + ":");
		d -= ((int) d / 60) * 60;
		fmt.applyPattern("00.0");
		sb.append(fmt.format(d));
		return sb.toString();
	}
	
	
	public static String doubleArrayToString(double[] array, String[] labels, String arrayName) {
		StringBuffer res = new StringBuffer();
		res.append(arrayName);
		res.append("\n");
		for (int i = 0; i < array.length; i++) {
			if (labels == null){
				res.append(i+"       \t");
			}else{
				res.append(labels[i]+     "\t");
			}
		}
		res.append("sum\n");
		double sum = 0;
		for (int i = 0; i < array.length; i++) {
			res.append(prettyPrint(array[i],
					"0.00000E00", 8) + "\t");
			sum+=array[i];
		}
		res.append(prettyPrint(sum,
				"0.00000E00", 8)+"\n");
		return res.toString();
	}
	
	
	
	public static void printDoubleArray(double[] array, String labels[], String arrayName) {
		System.out.println(doubleArrayToString(array, labels,arrayName));
	}
	
	
	public static String doubleArrayToString(double[][] array, String[] labels1, String[] labels2,
			String arrayName){
		StringBuffer res = new StringBuffer();
		res.append(arrayName);
		res.append("\n\t");
		//Calculates the column sum to keeps the sums
		double[] sums = new double[array[0].length+1];
		//Prints rows headings
		for (int i = 0; i < array[0].length; i++) {
			if (labels1 == null){
				res.append(i+"        \t");
			}else{
				res.append(labels1[i]+"        \t");
			}
		}
		res.append("sum\n");
		double sum = 0;
		//For each row print heading
		for (int i = 0; i < array.length; i++) {
			if (labels2 == null){
				res.append(i+"\t");
			}else{
				res.append(labels2[i]+"\t");
			}
			//Print values for that row
			for (int j = 0; j < array[0].length; j++) {
				res.append(" " + prettyPrint(array[i][j],
						"0.00000E00", 8) + "\t");
				sums[j] += array[i][j]; 
				sum+=array[i][j]; //Sum all values of that row
			}
			//Print row sum
			res.append(prettyPrint(sum,"0.00000E00", 8)+"\n");
			sums[array[0].length]+=sum;
			sum=0;
		}
		res.append("sum\t");
		//Print values for colums sum
		for (int i = 0; i < array[0].length+1; i++) {
			res.append(prettyPrint(sums[i],"0.00000E00", 8)+"\t");
		}
		res.append("\n");
		return res.toString();
	}
	
	public static void printDoubleArray(double[][] array, String[] labels1, String[] labels2
			, String arrayName) {
		System.out.println(doubleArrayToString(array, labels1,labels2,arrayName));
	}
	
	
	public static void printIntArray(int[][] array, String[] labels1, String[] labels2, String arrayName,
			int size1, int size2) {
		System.out.println(arrayName);
		for (int i = 0; i < size1; i++) {
			for (int j = 0; j < size2; j++) {
				System.out.print(" " + array[i][j] +  " ");

			}
			System.out.println();
		}
		System.out.println();
	}
	
	public static String intArrayToString(int[] array, String[] labels, String arrayName) {
		StringBuffer res = new StringBuffer();
		res.append(arrayName);
		for (int i = 0; i < array.length; i++) {
			res.append(" " + array[i] + " ");
			
		}
		res.append("\n");
		return res.toString();
	}
	
	public static void printIntArray(int[] array, String[] labels, String arrayName) {
		System.out.println(intArrayToString(array, labels,arrayName));
	}
	
	public static String toString(double[][] d){
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < d.length; i++) {
			for (int j = 0; j < d[0].length; j++) {
				sb.append(prettyPrint(d[i][j], "0.00E0", 10));
			}
			sb.append("\n");
		}
		return sb.toString();
	}
	
}