KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > exslt > Math


1 package net.sf.saxon.exslt;
2 import net.sf.saxon.expr.XPathContext;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.SequenceIterator;
5 import net.sf.saxon.trans.DynamicError;
6 import net.sf.saxon.trans.XPathException;
7 import net.sf.saxon.value.EmptySequence;
8 import net.sf.saxon.value.SequenceExtent;
9 import net.sf.saxon.value.Value;
10
11 import java.util.ArrayList JavaDoc;
12
13 /**
14 * This class implements extension functions in the
15 * http://exslt.org/math namespace. <p>
16 */

17
18 public abstract class Math {
19
20     /**
21     * Get the maximum numeric value of the string-value of each of a set of nodes
22     */

23
24     public static double max (SequenceIterator nsv) throws XPathException {
25         double max = Double.NEGATIVE_INFINITY;
26         try {
27             while (true) {
28                 Item it = nsv.next();
29                 if (it == null) break;
30                 double x = Value.stringToNumber(it.getStringValueCS());
31                 if (Double.isNaN(x)) return x;
32                 if (x>max) max = x;
33             }
34             return max;
35         } catch (NumberFormatException JavaDoc err) {
36             return Double.NaN;
37         }
38     }
39
40
41     /**
42     * Get the minimum numeric value of the string-value of each of a set of nodes
43     */

44
45     public static double min (SequenceIterator nsv) throws XPathException {
46         try {
47             double min = Double.POSITIVE_INFINITY;
48             while (true) {
49                 Item it = nsv.next();
50                 if (it == null) break;
51                 double x = Value.stringToNumber(it.getStringValueCS());
52                 if (Double.isNaN(x)) return x;
53                 if (x<min) min = x;
54             }
55             return min;
56         } catch (NumberFormatException JavaDoc e) {
57             return Double.NaN;
58         }
59     }
60
61
62     /**
63     * Get the items with maximum numeric value of the string-value of each of a sequence of items.
64     * The items are returned in the order of the original sequence.
65     */

66
67     public static Value highest (SequenceIterator nsv) throws XPathException {
68         try {
69             double max = Double.NEGATIVE_INFINITY;
70             ArrayList JavaDoc highest = new ArrayList JavaDoc();
71             while (true) {
72                 Item it = nsv.next();
73                 if (it == null) break;
74                 double x = Value.stringToNumber(it.getStringValueCS());
75                 if (Double.isNaN(x)) return EmptySequence.getInstance();
76                 if (x==max) {
77                     highest.add(it);
78                 } else if (x>max) {
79                     max = x;
80                     highest.clear();
81                     highest.add(it);
82                 }
83             }
84             return new SequenceExtent(highest);
85         } catch (NumberFormatException JavaDoc e) {
86             return EmptySequence.getInstance();
87         }
88     }
89
90
91
92     /**
93     * Get the items with minimum numeric value of the string-value of each of a sequence of items
94     * The items are returned in the order of the original sequence.
95     */

96
97     public static Value lowest (SequenceIterator nsv) throws XPathException {
98         try {
99             double min = Double.POSITIVE_INFINITY;
100             ArrayList JavaDoc lowest = new ArrayList JavaDoc();
101             while (true) {
102                Item it = nsv.next();
103                if (it == null) break;
104                double x = Value.stringToNumber(it.getStringValueCS());
105                if (Double.isNaN(x)) return EmptySequence.getInstance();
106                if (x==min) {
107                    lowest.add(it);
108                } else if (x<min) {
109                    min = x;
110                    lowest.clear();
111                    lowest.add(it);
112                }
113            }
114             return new SequenceExtent(lowest);
115         } catch (NumberFormatException JavaDoc e) {
116             return EmptySequence.getInstance();
117         }
118     }
119
120     /**
121     * Get the absolute value of a numeric value (SStL)
122     */

123
124     public static double abs (double x) {
125         return java.lang.Math.abs(x);
126     }
127
128     /**
129     * Get the square root of a numeric value (SStL)
130     */

131
132     public static double sqrt (double x) {
133         return java.lang.Math.sqrt(x);
134     }
135
136     /**
137     * Get the power of two numeric values (SStL)
138     */

139
140     public static double power (double x, double y) {
141         return java.lang.Math.pow(x,y);
142     }
143
144     /**
145     * Get a named constant to a given precision (SStL)
146     */

147
148     public static double constant (XPathContext context, String JavaDoc name, double precision) throws XPathException {
149         //PI, E, SQRRT2, LN2, LN10, LOG2E, SQRT1_2
150

151         String JavaDoc con = "";
152
153         if (name.equals("PI")) {
154             con="3.1415926535897932384626433832795028841971693993751";
155         } else if (name.equals("E")) {
156             con="2.71828182845904523536028747135266249775724709369996";
157         } else if (name.equals("SQRRT2")) {
158             con="1.41421356237309504880168872420969807856967187537694";
159         } else if (name.equals("LN2")) {
160             con="0.69314718055994530941723212145817656807550013436025";
161         } else if (name.equals("LN10")) {
162             con="2.302585092994046";
163         } else if (name.equals("LOG2E")) {
164             con="1.4426950408889633";
165         } else if (name.equals("SQRT1_2")) {
166             con="0.7071067811865476";
167         } else {
168             DynamicError e = new DynamicError("Unknown math constant " + name);
169             e.setXPathContext(context);
170             throw e;
171         }
172
173         int x = (int) precision;
174         String JavaDoc returnVal=con.substring(0,x+2);
175         double rV=new Double JavaDoc(returnVal).doubleValue();
176         return rV;
177     }
178
179     /**
180     * Get the logarithm of a numeric value (SStL)
181     */

182
183     public static double log (double x) {
184         return java.lang.Math.log(x);
185     }
186
187     /**
188     * Get a random numeric value (SStL)
189     */

190
191     public static double random() {
192         return java.lang.Math.random();
193     }
194
195     /**
196     * Get the sine of a numeric value (SStL)
197     */

198
199     public static double sin (double x) {
200         return java.lang.Math.sin(x);
201     }
202
203     /**
204     * Get the cosine of a numeric value (SStL)
205     */

206
207     public static double cos (double x) {
208         return java.lang.Math.cos(x);
209     }
210
211     /**
212     * Get the tangent of a numeric value (SStL)
213     */

214
215     public static double tan (double x) {
216         return java.lang.Math.tan(x);
217     }
218
219     /**
220     * Get the arcsine of a numeric value (SStL)
221     */

222
223     public static double asin (double x) {
224         return java.lang.Math.asin(x);
225     }
226
227     /**
228     * Get the arccosine of a numeric value (SStL)
229     */

230
231     public static double acos (double x) {
232         return java.lang.Math.acos(x);
233     }
234
235     /**
236     * Get the arctangent of a numeric value (SStL)
237     */

238
239     public static double atan (double x) {
240         return java.lang.Math.atan(x);
241     }
242
243     /**
244     * Converts rectangular coordinates to polar (SStL)
245     */

246
247     public static double atan2 (double x, double y) {
248         return java.lang.Math.atan2(x,y);
249     }
250
251     /**
252     * Get the exponential of a numeric value (SStL)
253     */

254
255     public static double exp (double x) {
256         return java.lang.Math.exp(x);
257     }
258
259 }
260
261 //
262
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
263
// you may not use this file except in compliance with the License. You may obtain a copy of the
264
// License at http://www.mozilla.org/MPL/
265
//
266
// Software distributed under the License is distributed on an "AS IS" basis,
267
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
268
// See the License for the specific language governing rights and limitations under the License.
269
//
270
// The Original Code is: all this file.
271
//
272
// The Initial Developer of the Original Code is Michael H. Kay.
273
//
274
// Portions marked SStL were provided by Simon St.Laurent [simonstl@simonstl.com]. All Rights Reserved.
275
//
276
// Contributor(s): none.
277
//
278
Popular Tags