Debian Codegraph @

net/sf/saxon/functions/MathFunctionSet.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.functions;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.Literal;
12 import net.sf.saxon.expr.XPathContext;
13 import net.sf.saxon.functions.registry.BuiltInFunctionSet;
14 import net.sf.saxon.lib.NamespaceConstant;
15 import net.sf.saxon.om.One;
16 import net.sf.saxon.om.Sequence;
17 import net.sf.saxon.om.ZeroOrOne;
18 import net.sf.saxon.trans.XPathException;
19 import net.sf.saxon.type.BuiltInAtomicType;
20 import net.sf.saxon.value.DoubleValue;
21 import net.sf.saxon.value.NumericValue;
22
23
24 /**
25 * Abstract class providing functionality common to functions math:sin(), math:cos(), math:sqrt() etc;
26 * contains the concrete implementations of these functions as inner subclasses
27 */
28 public class MathFunctionSet extends BuiltInFunctionSet {
29
30 private static MathFunctionSet THE_INSTANCE = new MathFunctionSet();
31
32 public static MathFunctionSet getInstance() {
33 return THE_INSTANCE;
34 }
35
36 private MathFunctionSet() {
37 init();
38 }
39
40 private void reg1(String name, Class<? extends SystemFunction> implementation) {
41 register(name, 1, implementation, BuiltInAtomicType.DOUBLE, OPT, 0, CARD0)
42 .arg(0, BuiltInAtomicType.DOUBLE, OPT, EMPTY);
43 }
44
45
46 private void init() {
47
48 // Arity 0 functions
49
50 register("pi", 0, PiFn.class, BuiltInAtomicType.DOUBLE, ONE, 0, 0);
51
52 // Arity 1 functions
53
54 reg1("sin", SinFn.class);
55 reg1("cos", CosFn.class);
56 reg1("tan", TanFn.class);
57 reg1("asin", AsinFn.class);
58 reg1("acos", AcosFn.class);
59 reg1("atan", AtanFn.class);
60 reg1("sqrt", SqrtFn.class);
61 reg1("log", LogFn.class);
62 reg1("log10", Log10Fn.class);
63 reg1("exp", ExpFn.class);
64 reg1("exp10", Exp10Fn.class);
65
66 // Arity 2 functions
67
68 register("pow", 2, PowFn.class, BuiltInAtomicType.DOUBLE, OPT, 0, CARD0)
69 .arg(0, BuiltInAtomicType.DOUBLE, OPT, EMPTY)
70 .arg(1, BuiltInAtomicType.DOUBLE, ONE, null);
71
72 register("atan2", 2, Atan2Fn.class, BuiltInAtomicType.DOUBLE, ONE, 0, 0)
73 .arg(0, BuiltInAtomicType.DOUBLE, ONE, null)
74 .arg(1, BuiltInAtomicType.DOUBLE, ONE, null);
75
76 }
77
78 @Override
79 public String getNamespace() {
80 return NamespaceConstant.MATH;
81 }
82
83 @Override
84 public String getConventionalPrefix() {
85 return "math";
86 }
87
88 /**
89 * Implement math:pi
90 */
91
92 public static class PiFn extends SystemFunction {
93 @Override
94 public Expression makeFunctionCall(Expression... arguments) {
95 return Literal.makeLiteral(new DoubleValue(Math.PI));
96 }
97
98 public DoubleValue call(XPathContext context, Sequence[] arguments) throws XPathException {
99 return new DoubleValue(Math.PI);
100 }
101 }
102
103 /**
104 * Generic superclass for all the arity-1 trig functions
105 */
106
107 private static abstract class TrigFn1 extends SystemFunction {
108
109 protected abstract double compute(double input);
110
111 public ZeroOrOne<DoubleValue> call(XPathContext context, Sequence[] args) throws XPathException {
112 DoubleValue in = (DoubleValue) args[0].head();
113 if (in == null) {
114 return ZeroOrOne.empty();
115 } else {
116 return One.dbl(compute(in.getDoubleValue()));
117 }
118 }
119 }
120
121 /**
122 * Implement math:sin
123 */
124
125 public static class SinFn extends TrigFn1 {
126
127 protected double compute(double input) {
128 return Math.sin(input);
129 }
130 }
131
132 /**
133 * Implement math:cos
134 */
135
136 public static class CosFn extends TrigFn1 {
137
138 protected double compute(double input) {
139 return Math.cos(input);
140 }
141 }
142
143 /**
144 * Implement math:tan
145 */
146
147 public static class TanFn extends TrigFn1 {
148
149 protected double compute(double input) {
150 return Math.tan(input);
151 }
152 }
153
154 /**
155 * Implement math:asin
156 */
157
158 public static class AsinFn extends TrigFn1 {
159
160 protected double compute(double input) {
161 return Math.asin(input);
162 }
163 }
164
165 /**
166 * Implement math:acos
167 */
168
169 public static class AcosFn extends TrigFn1 {
170
171 protected double compute(double input) {
172 return Math.acos(input);
173 }
174 }
175
176 /**
177 * Implement math:atan
178 */
179
180 public static class AtanFn extends TrigFn1 {
181
182 protected double compute(double input) {
183 return Math.atan(input);
184 }
185 }
186
187 /**
188 * Implement math:sqrt
189 */
190
191 public static class SqrtFn extends TrigFn1 {
192
193 protected double compute(double input) {
194 return Math.sqrt(input);
195 }
196 }
197
198 /**
199 * Implement math:log
200 */
201
202 public static class LogFn extends TrigFn1 {
203
204 protected double compute(double input) {
205 return Math.log(input);
206 }
207 }
208
209 /**
210 * Implement math:log10
211 */
212
213 public static class Log10Fn extends TrigFn1 {
214
215 protected double compute(double input) {
216 return Math.log10(input);
217 }
218 }
219
220 /**
221 * Implement math:exp
222 */
223
224 public static class ExpFn extends TrigFn1 {
225
226 protected double compute(double input) {
227 return Math.exp(input);
228 }
229 }
230
231 /**
232 * Implement math:exp10
233 */
234
235 public static class Exp10Fn extends TrigFn1 {
236
237 protected double compute(double input) {
238 return Math.pow(10, input);
239 }
240 }
241
242 /**
243 * Implement math:pow
244 */
245
246 public static class PowFn extends SystemFunction {
247 /**
248 * Invoke the function
249 *
250 * @param context the XPath dynamic evaluation context
251 * @param args the actual arguments to be supplied
252 * @return the result of invoking the function
253 * @throws XPathException if a dynamic error occurs within the function
254 */
255 @Override
256 public ZeroOrOne<DoubleValue> call(XPathContext context, Sequence[] args) throws XPathException {
257 DoubleValue x = (DoubleValue) args[0].head();
258 DoubleValue result;
259 if (x == null) {
260 result = null;
261 } else {
262 double dx = x.getDoubleValue();
263 if (dx == 1) {
264 result = x;
265 } else {
266 NumericValue y = (NumericValue) args[1].head();
267 assert y != null;
268 double dy = y.getDoubleValue();
269 if (dx == -1 && Double.isInfinite(dy)) {
270 result = new DoubleValue(1.0e0);
271 } else {
272 result = new DoubleValue(Math.pow(dx, dy));
273 }
274 }
275 }
276 return new ZeroOrOne<>(result);
277 }
278 }
279
280 /**
281 * Implement math:atan2
282 */
283
284 public static class Atan2Fn extends SystemFunction {
285 public DoubleValue call(XPathContext context, Sequence[] arguments) throws XPathException {
286 DoubleValue y = (DoubleValue) arguments[0].head();
287 assert y != null;
288 DoubleValue x = (DoubleValue) arguments[1].head();
289 assert x != null;
290 double result = Math.atan2(y.getDoubleValue(), x.getDoubleValue());
291 return new DoubleValue(result);
292 }
293 }
294
295
296 }
297
298 // Copyright (c) 2018 Saxonica Limited.