1 | package agents.uk.ac.soton.ecs.gp4j.util;
|
---|
2 |
|
---|
3 | import agents.org.apache.commons.math.stat.StatUtils;
|
---|
4 |
|
---|
5 | public class ArrayUtils extends agents.org.apache.commons.lang.ArrayUtils {
|
---|
6 | public static double[][] combine(double[][] combinations, double[] addition) {
|
---|
7 | double[][] result;
|
---|
8 |
|
---|
9 | if (combinations == null || combinations.length == 0) {
|
---|
10 | result = new double[addition.length][1];
|
---|
11 |
|
---|
12 | for (int i = 0; i < addition.length; i++)
|
---|
13 | result[i][0] = addition[i];
|
---|
14 | } else {
|
---|
15 | result = new double[combinations.length * addition.length][combinations[0].length + 1];
|
---|
16 |
|
---|
17 | for (int i = 0; i < combinations.length; i++) {
|
---|
18 | for (int j = 0; j < addition.length; j++) {
|
---|
19 | System.arraycopy(combinations[i], 0, result[i
|
---|
20 | * addition.length + j], 0, combinations[0].length);
|
---|
21 | result[i * addition.length + j][result[0].length - 1] = addition[j];
|
---|
22 | }
|
---|
23 | }
|
---|
24 |
|
---|
25 | }
|
---|
26 | return result;
|
---|
27 | }
|
---|
28 |
|
---|
29 | public static int[][] combine(int[][] combinations, int[] addition) {
|
---|
30 | int[][] result;
|
---|
31 |
|
---|
32 | if (combinations == null || combinations.length == 0) {
|
---|
33 | result = new int[addition.length][1];
|
---|
34 |
|
---|
35 | for (int i = 0; i < addition.length; i++)
|
---|
36 | result[i][0] = addition[i];
|
---|
37 | } else {
|
---|
38 | result = new int[combinations.length * addition.length][combinations[0].length + 1];
|
---|
39 |
|
---|
40 | for (int i = 0; i < combinations.length; i++) {
|
---|
41 | for (int j = 0; j < addition.length; j++) {
|
---|
42 | System.arraycopy(combinations[i], 0, result[i
|
---|
43 | * addition.length + j], 0, combinations[0].length);
|
---|
44 | result[i * addition.length + j][result[0].length - 1] = addition[j];
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | }
|
---|
49 | return result;
|
---|
50 | }
|
---|
51 |
|
---|
52 | public static int[][] allCombinations(int[][] values) {
|
---|
53 | int result[][] = combine(null, values[0]);
|
---|
54 |
|
---|
55 | for (int i = 1; i < values.length; i++)
|
---|
56 | result = combine(result, values[i]);
|
---|
57 |
|
---|
58 | return result;
|
---|
59 | }
|
---|
60 |
|
---|
61 | public static int[][] allCombinations(int[] values, int repetitions) {
|
---|
62 | int[][] input = new int[repetitions][values.length];
|
---|
63 |
|
---|
64 | for (int i = 0; i < input.length; i++) {
|
---|
65 | input[i] = values;
|
---|
66 | }
|
---|
67 |
|
---|
68 | return allCombinations(input);
|
---|
69 | }
|
---|
70 |
|
---|
71 | public static double[][] allCombinations(double[][] values) {
|
---|
72 | double result[][] = combine(null, values[0]);
|
---|
73 |
|
---|
74 | for (int i = 1; i < values.length; i++)
|
---|
75 | result = combine(result, values[i]);
|
---|
76 |
|
---|
77 | return result;
|
---|
78 | }
|
---|
79 |
|
---|
80 | public static double[] linspace(double min, double max, int sampleCount) {
|
---|
81 | if (sampleCount == 1)
|
---|
82 | return new double[] { (max - min) / 2 };
|
---|
83 |
|
---|
84 | double[] samples = new double[sampleCount];
|
---|
85 | double increment = (max - min) / (sampleCount - 1);
|
---|
86 | samples[0] = min;
|
---|
87 | samples[samples.length - 1] = max;
|
---|
88 |
|
---|
89 | for (int i = 1; i < sampleCount - 1; i++)
|
---|
90 | samples[i] = samples[i - 1] + increment;
|
---|
91 |
|
---|
92 | return samples;
|
---|
93 | }
|
---|
94 |
|
---|
95 | public static double[] log(double[] array) {
|
---|
96 | double[] result = new double[array.length];
|
---|
97 |
|
---|
98 | for (int i = 0; i < array.length; i++)
|
---|
99 | result[i] = Math.log(array[i]);
|
---|
100 |
|
---|
101 | return result;
|
---|
102 | }
|
---|
103 |
|
---|
104 | public static double[] exp(double[] array) {
|
---|
105 | double[] result = new double[array.length];
|
---|
106 |
|
---|
107 | for (int i = 0; i < array.length; i++)
|
---|
108 | result[i] = Math.exp(array[i]);
|
---|
109 |
|
---|
110 | return result;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static double[] normalize(double[] array) {
|
---|
114 | double sum = StatUtils.sum(array);
|
---|
115 | return divide(array, sum);
|
---|
116 | }
|
---|
117 |
|
---|
118 | public static double[] divide(double[] array, double scalar) {
|
---|
119 | double[] result = new double[array.length];
|
---|
120 |
|
---|
121 | for (int i = 0; i < array.length; i++) {
|
---|
122 | result[i] = array[i] / scalar;
|
---|
123 | }
|
---|
124 |
|
---|
125 | return result;
|
---|
126 | }
|
---|
127 | }
|
---|