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 package ffx.numerics.switching;
39
40 import static java.lang.Boolean.parseBoolean;
41 import static java.lang.Double.parseDouble;
42 import static java.lang.String.format;
43 import static java.util.Arrays.copyOfRange;
44
45 import java.util.Arrays;
46
47
48
49
50
51
52
53 public class UnivariateFunctionFactory {
54
55
56 private UnivariateFunctionFactory() {
57 }
58
59
60
61
62
63
64
65
66 public static UnivariateSwitchingFunction parseUSF(String[] toks, int offset) {
67 return parseUSF(copyOfRange(toks, offset, toks.length));
68 }
69
70
71
72
73
74
75
76 public static UnivariateSwitchingFunction parseUSF(String[] toks) {
77 String selectionString = toks[0].toUpperCase()
78 .replaceAll("-", "")
79 .replaceAll("_", "")
80 .replaceAll(" ", "");
81 return switch (selectionString) {
82 case "BELL", "BELLCURVE", "BELLCURVESWITCH" -> parseBell(toks);
83 case "CONSTANT", "NONE", "FLAT" -> new ConstantSwitch();
84 case "LINEARDERIVATIVE" -> new LinearDerivativeSwitch();
85 case "MULTIPLICATIVE", "PENTICHERMITE" -> parseMultiplicative(toks);
86 case "POWER" -> parsePower(toks);
87 case "LINEAR" -> parseSpecificPow(1.0, toks);
88 case "QUADRATIC" -> parseSpecificPow(2.0, toks);
89 case "CUBIC" -> parseSpecificPow(3.0, toks);
90 case "TRIGONOMETRIC", "TRIG", "SINSQUARED" -> parseTrig(toks);
91 default -> throw new IllegalArgumentException(
92 format(" Could not parse %s as a valid univariate switching function!",
93 Arrays.toString(toks)));
94 };
95 }
96
97 private static BellCurveSwitch parseBell(String[] toks) {
98 double midpoint = 0.5;
99 if (toks.length > 2) {
100 midpoint = parseDouble(toks[1]);
101 }
102 double width = 1.0;
103 if (toks.length > 3) {
104 width = parseDouble(toks[2]);
105 }
106 return new BellCurveSwitch(midpoint, width);
107 }
108
109 private static MultiplicativeSwitch parseMultiplicative(String[] toks) {
110 if (toks.length > 3) {
111 return new MultiplicativeSwitch(parseDouble(toks[2]), parseDouble(toks[1]));
112 } else {
113 return new MultiplicativeSwitch();
114 }
115 }
116
117 private static PowerSwitch parsePower(String[] toks) {
118 double pow = 1.0;
119 if (toks.length > 1) {
120 pow = parseDouble(toks[1]);
121 }
122 double alpha = 1.0;
123 if (toks.length > 2) {
124 alpha = parseDouble(toks[2]);
125 }
126 return new PowerSwitch(alpha, pow);
127 }
128
129 private static PowerSwitch parseSpecificPow(double pow, String[] toks) {
130 double alpha = 1.0;
131 if (toks.length > 1) {
132 alpha = parseDouble(toks[1]);
133 }
134 return new PowerSwitch(alpha, pow);
135 }
136
137 private static SquaredTrigSwitch parseTrig(String[] toks) {
138 boolean trig = false;
139 if (toks.length > 1) {
140 trig = parseBoolean(toks[1]);
141 }
142 if (toks.length > 2) {
143 return new SquaredTrigSwitch(parseDouble(toks[2]), trig);
144 } else {
145 return new SquaredTrigSwitch(trig);
146 }
147 }
148 }