KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > exslt > Math


1 package com.icl.saxon.exslt;
2 import com.icl.saxon.expr.*;
3 import com.icl.saxon.Context;
4 import com.icl.saxon.Controller;
5 import com.icl.saxon.om.NodeInfo;
6 import com.icl.saxon.om.NodeEnumeration;
7 import java.util.Vector JavaDoc;
8
9 /**
10 * This class implements extension functions in the
11 * http://exslt.org/math namespace. <p>
12 */

13
14 public abstract class Math {
15
16     /**
17     * Get the maximum numeric value of the string-value of each of a set of nodes
18     */

19
20     public static double max (NodeEnumeration nsv) throws XPathException {
21         double max = Double.NEGATIVE_INFINITY;
22         while (nsv.hasMoreElements()) {
23             double x = Value.stringToNumber(nsv.nextElement().getStringValue());
24             if (Double.isNaN(x)) return x;
25             if (x>max) max = x;
26         }
27         return max;
28     }
29
30
31     /**
32     * Get the minimum numeric value of the string-value of each of a set of nodes
33     */

34
35     public static double min (NodeEnumeration nsv) throws XPathException {
36         double min = Double.POSITIVE_INFINITY;
37         while (nsv.hasMoreElements()) {
38             double x = Value.stringToNumber(nsv.nextElement().getStringValue());
39             if (Double.isNaN(x)) return x;
40             if (x<min) min = x;
41         }
42         return min;
43     }
44
45
46     /**
47     * Get the nodes with maximum numeric value of the string-value of each of a set of nodes
48     */

49
50     public static NodeSetValue highest (Context c, NodeEnumeration nsv) throws XPathException {
51         double max = Double.NEGATIVE_INFINITY;
52         Vector JavaDoc highest = new Vector JavaDoc();
53         while (nsv.hasMoreElements()) {
54             NodeInfo node = nsv.nextElement();
55             double x = Value.stringToNumber(node.getStringValue());
56             if (Double.isNaN(x)) return new EmptyNodeSet();
57             if (x==max) {
58                 highest.addElement(node);
59             } else if (x>max) {
60                 max = x;
61                 highest.removeAllElements();
62                 highest.addElement(node);
63             }
64         }
65         return new NodeSetExtent(highest, c.getController());
66     }
67
68
69
70     /**
71     * Get the node with minimum numeric value of the string-value of each of a set of nodes
72     */

73
74     public static NodeSetValue lowest (Context c, NodeEnumeration nsv) throws XPathException {
75         double min = Double.POSITIVE_INFINITY;
76         Vector JavaDoc lowest = new Vector JavaDoc();
77         while (nsv.hasMoreElements()) {
78             NodeInfo node = nsv.nextElement();
79             double x = Value.stringToNumber(node.getStringValue());
80             if (Double.isNaN(x)) return new EmptyNodeSet();
81             if (x==min) {
82                 lowest.addElement(node);
83             } else if (x<min) {
84                 min = x;
85                 lowest.removeAllElements();
86                 lowest.addElement(node);
87             }
88         }
89         return new NodeSetExtent(lowest, c.getController());
90     }
91
92     /**
93     * Get the absolute value of a numeric value (SStL)
94     */

95
96     public static double abs (double x) throws XPathException {
97             
98         return java.lang.Math.abs(x);
99     }
100     
101     /**
102     * Get the square root of a numeric value (SStL)
103     */

104
105     public static double sqrt (double x) throws XPathException {
106         
107             
108         return java.lang.Math.sqrt(x);
109     }
110
111     /**
112     * Get the power of two numeric values (SStL)
113     */

114
115     public static double power (double x, double y) throws XPathException {
116
117         return java.lang.Math.pow(x,y);
118     }
119     
120     /**
121     * Get a named constant to a given precision (SStL)
122     */

123
124     public static double constant (String JavaDoc name, double precision) throws XPathException {
125         //PI, E, SQRRT2, LN2, LN10, LOG2E, SQRT1_2
126

127     String JavaDoc con=new String JavaDoc();
128     
129     if (name.equals("PI")) {
130         con="3.1415926535897932384626433832795028841971693993751";
131     } else if (name.equals("E")) {
132         con="2.71828182845904523536028747135266249775724709369996";
133     } else if (name.equals("SQRRT2")) {
134         con="1.41421356237309504880168872420969807856967187537694";
135     } else if (name.equals("LN2")) {
136         con="0.69314718055994530941723212145817656807550013436025";
137     } else if (name.equals("LN10")) {
138         con="2.302585092994046";
139     } else if (name.equals("LOG2E")) {
140         con="1.4426950408889633";
141     } else if (name.equals("SQRT1_2")) {
142         con="0.7071067811865476";
143     }
144         int x = (int) precision;
145         String JavaDoc returnVal=con.substring(0,x+2);
146         double rV=new Double JavaDoc(returnVal).doubleValue();
147         return rV;
148     }
149
150     /**
151     * Get the logarithm of a numeric value (SStL)
152     */

153
154     public static double log (double x) throws XPathException {
155        
156         return java.lang.Math.log(x);
157     }
158
159     /**
160     * Get a random numeric value (SStL)
161     */

162
163     public static double random () throws XPathException {
164         
165
166         return java.lang.Math.random();
167     }
168
169     /**
170     * Get the sine of a numeric value (SStL)
171     */

172
173     public static double sin (double x) throws XPathException {
174        
175         return java.lang.Math.sin(x);
176     }
177
178     /**
179     * Get the cosine of a numeric value (SStL)
180     */

181
182     public static double cos (double x) throws XPathException {
183             
184         return java.lang.Math.cos(x);
185     }
186     
187     /**
188     * Get the tangent of a numeric value (SStL)
189     */

190
191     public static double tan (double x) throws XPathException {
192
193         return java.lang.Math.tan(x);
194     }
195     
196     /**
197     * Get the arcsine of a numeric value (SStL)
198     */

199
200     public static double asin (double x) throws XPathException {
201             
202         return java.lang.Math.asin(x);
203     }
204     
205     /**
206     * Get the arccosine of a numeric value (SStL)
207     */

208
209     public static double acos (double x) throws XPathException {
210             
211         return java.lang.Math.acos(x);
212     }
213     
214     /**
215     * Get the arctangent of a numeric value (SStL)
216     */

217
218     public static double atan (double x) throws XPathException {
219             
220         return java.lang.Math.atan(x);
221     }
222     
223     /**
224     * Converts rectangular coordinates to polar (SStL)
225     */

226
227     public static double atan2 (double x, double y) throws XPathException {
228
229         return java.lang.Math.atan2(x,y);
230     }
231     
232     /**
233     * Get the exponential of a numeric value (SStL)
234     */

235
236     public static double exp (double x) throws XPathException {
237             
238         return java.lang.Math.exp(x);
239     }
240
241 }
242
243
244
245
246
247 //
248
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
249
// you may not use this file except in compliance with the License. You may obtain a copy of the
250
// License at http://www.mozilla.org/MPL/
251
//
252
// Software distributed under the License is distributed on an "AS IS" basis,
253
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
254
// See the License for the specific language governing rights and limitations under the License.
255
//
256
// The Original Code is: all this file.
257
//
258
// The Initial Developer of the Original Code is
259
// Michael Kay of International Computers Limited (michael.h.kay@ntlworld.com).
260
//
261
// Portions marked SStL were provided by Simon St.Laurent [simonstl@simonstl.com]. All Rights Reserved.
262
//
263
// Contributor(s): none.
264
//
265
Popular Tags