| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 26 |
|
| 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 |
|
| 49 |
|
| 50 |
register("pi", 0, PiFn.class, BuiltInAtomicType.DOUBLE, ONE, 0, 0); |
| 51 |
|
| 52 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 244 |
|
| 245 |
|
| 246 |
public static class PowFn extends SystemFunction { |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 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 |
|
| 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 |
|