You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ni...@apache.org on 2008/04/16 02:17:29 UTC

svn commit: r648463 - in /commons/sandbox/me/trunk: build-lib/ src/org/apache/commons/me/lang/ src/org/apache/commons/me/math/ src/org/apache/commons/me/util/ test-src/org/apache/commons/me/lang/ test-src/org/apache/commons/me/math/

Author: nick
Date: Tue Apr 15 17:17:27 2008
New Revision: 648463

URL: http://svn.apache.org/viewvc?rev=648463&view=rev
Log:
Add MoreMath class

Added:
    commons/sandbox/me/trunk/src/org/apache/commons/me/lang/
    commons/sandbox/me/trunk/src/org/apache/commons/me/math/MoreMath.java   (with props)
    commons/sandbox/me/trunk/test-src/org/apache/commons/me/lang/
    commons/sandbox/me/trunk/test-src/org/apache/commons/me/math/TestMoreMath.java   (with props)
Modified:
    commons/sandbox/me/trunk/build-lib/   (props changed)
    commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayHelper.java

Propchange: commons/sandbox/me/trunk/build-lib/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Tue Apr 15 17:17:27 2008
@@ -0,0 +1,3 @@
+junit-3.8.1.jar
+*.class
+*.swp

Added: commons/sandbox/me/trunk/src/org/apache/commons/me/math/MoreMath.java
URL: http://svn.apache.org/viewvc/commons/sandbox/me/trunk/src/org/apache/commons/me/math/MoreMath.java?rev=648463&view=auto
==============================================================================
--- commons/sandbox/me/trunk/src/org/apache/commons/me/math/MoreMath.java (added)
+++ commons/sandbox/me/trunk/src/org/apache/commons/me/math/MoreMath.java Tue Apr 15 17:17:27 2008
@@ -0,0 +1,160 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.commons.me.math;
+
+/**
+ * Fills in holes in the JavaME math libraries
+ */
+public class MoreMath {
+	/* Define some of our constants */
+	static final double PI = java.lang.Math.PI;
+    static final double HALF_PI = 1.5707963267948966135;
+    static final double NaN = (0.0/0.0);
+
+	// Various numbers based on the square root of two
+    static final double SQRT2    = 1.414213562373095048802e0;
+    static final double SQRT2_P1 = 2.414213562373095048802e0;
+    static final double SQRT2_M1 = 0.414213562373095048802e0;
+
+    // These are all used for the atan function
+    static final double p4  = 16.1536412982230228262;
+    static final double p3  = 268.42548195503973794141;
+    static final double p2  = 1153.0293515404850115428136;
+    static final double p1  = 1780.40631643319697105464587;
+    static final double p0  = 896.78597403663861959987488;
+    static final double q4  = 58.95697050844462222791;
+    static final double q3  = 536.265374031215315104235;
+    static final double q2  = 1666.7838148816337184521798;
+    static final double q1  = 2079.33497444540981287275926;
+    static final double q0  = 896.78597403663861962481162;
+
+	/**
+ 	 * Implement the asin (arc-sine) function
+ 	 */
+	public static double asin(double val) {
+		double work;
+		int sign;
+
+		// Work on positive numbers
+		sign = 0;
+		if(val < 0) {
+			val = -val;
+			sign++;
+		}
+
+		// Can't take the asin of a number >1 or <-1
+		if(val > 1) return NaN;
+
+		// Calculate
+		work = java.lang.Math.sqrt(1 - val*val);
+		if(val > 0.7) {
+			work = HALF_PI - atan(work/val);
+		} else {
+			work = atan(val/work);
+		}
+
+		// Fix the sign as needed
+		if(sign > 0) {
+			work = -work;
+		}
+		return work;
+	}
+
+	/**
+ 	 * Implement the acos (arc-cosine) function
+ 	 */
+	public static double acos(double val) {
+		// Can't take the acos of a number >1 or <-1
+		if(val > 1 || val < -1) return NaN;
+
+		// Can implement acos off asin
+		return HALF_PI - asin(val);
+	}
+
+	/**
+	 * Implement the atan (arc-tangent) function
+	 */
+	public static double atan(double val) {
+		if(val > 0) {
+			return scaling_atan(val);
+		}
+		return -scaling_atan( -val );
+    }
+
+	/**
+	 * Implement the atan2 function, which
+	 *  converts rectangular coordinates (x, y) to polar (r, theta)
+	 */
+	public static double atan2(double val1, double val2) {
+		if(val1+val2 == val1) {
+			if(val1 >= 0) {
+				return HALF_PI;
+			}
+			return -HALF_PI;
+		}
+
+		val1 = atan(val1/val2);
+		if(val2 < 0) {
+			if(val1 <= 0) {
+				return val1 + PI;
+			}
+			return val1 - PI;
+		}
+		return val1;
+    }
+
+	/**
+	 * Takes a positive value between 0 and
+	 *  PI, scales it into the range 0 to 0.414,
+	 *  and calls reducted_atan on it.
+	 */
+	private static double scaling_atan(double val) {
+		if(val < SQRT2_M1) {
+			// Already in right range, use
+			return reducted_atan(val);
+		}
+		if(val > SQRT2_P1) {
+			// Long way out of range, invert
+			return HALF_PI - reducted_atan(1/val);
+		}
+		// Between sqrt(2)-1 and sqrt(2)+1
+		// Scale slightly
+		return HALF_PI/2 + reducted_atan((val-1)/(val+1));
+    }
+
+	/**
+	 * Performs the atan function, but only on a limited
+	 *  range of inputs.
+	 * Works for the range -0.414 to +0.414
+	 */
+    private static double reducted_atan(double val) {
+		double val_sq, value;
+
+		val_sq = val*val;
+		value = ((((p4*val_sq + p3)*val_sq + p2)*val_sq + p1)*val_sq + p0);
+		value = value / (((((val_sq + q4)*val_sq + q3)*val_sq + q2)*val_sq + q1)*val_sq + q0);
+		return value*val;
+	}
+
+
+	/**
+	 * Empty constructor - largely un-used as
+	 *  methods are static
+	 */
+	public MoreMath() {
+	}
+}

Propchange: commons/sandbox/me/trunk/src/org/apache/commons/me/math/MoreMath.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayHelper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayHelper.java?rev=648463&r1=648462&r2=648463&view=diff
==============================================================================
--- commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayHelper.java (original)
+++ commons/sandbox/me/trunk/src/org/apache/commons/me/util/ArrayHelper.java Tue Apr 15 17:17:27 2008
@@ -1,3 +1,19 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
 package org.apache.commons.me.util;
 
 /**

Added: commons/sandbox/me/trunk/test-src/org/apache/commons/me/math/TestMoreMath.java
URL: http://svn.apache.org/viewvc/commons/sandbox/me/trunk/test-src/org/apache/commons/me/math/TestMoreMath.java?rev=648463&view=auto
==============================================================================
--- commons/sandbox/me/trunk/test-src/org/apache/commons/me/math/TestMoreMath.java (added)
+++ commons/sandbox/me/trunk/test-src/org/apache/commons/me/math/TestMoreMath.java Tue Apr 15 17:17:27 2008
@@ -0,0 +1,65 @@
+/* ====================================================================
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+==================================================================== */
+package org.apache.commons.me.math;
+
+import junit.framework.TestCase;
+
+/**
+ * Tests for the More Maths functions
+ */
+public class TestMoreMath extends TestCase {
+	private static double[] testArcVals = new double[] {
+		0, 0.1, 0.11, 0.5, 0.9, 1.0, -0.1, -0.5, -1.0
+	};
+	private static double[] testMoreArcVals = new double[] {
+		1.1, 10, 20, 100, 1000, Math.PI, -1.4, -20
+	};
+
+	public void testASin() {
+		for(int i=0; i<testArcVals.length; i++) {
+			assertEquals( 
+				MoreMath.asin(testArcVals[i]), Math.asin(testArcVals[i]), 0.0001 
+			); 
+		}
+		assertTrue(Double.isNaN( MoreMath.asin( 1.1 ) ));
+		assertTrue(Double.isNaN( MoreMath.asin( -1.1 ) ));
+	}
+
+	public void testACos() {
+		for(int i=0; i<testArcVals.length; i++) {
+			assertEquals( 
+				MoreMath.acos(testArcVals[i]), Math.acos(testArcVals[i]), 0.0001 
+			); 
+		}
+		assertTrue(Double.isNaN( MoreMath.acos( 1.1 ) ));
+		assertTrue(Double.isNaN( MoreMath.acos( -1.1 ) ));
+	}
+
+	public void testATan() {
+		for(int i=0; i<testArcVals.length; i++) {
+			assertEquals( 
+				MoreMath.atan(testArcVals[i]), Math.atan(testArcVals[i]), 0.0001 
+			); 
+		}
+		// atan can go over 1, as tan is crazy like that
+		for(int i=0; i<testMoreArcVals.length; i++) {
+			assertEquals( 
+				MoreMath.atan(testMoreArcVals[i]), Math.atan(testMoreArcVals[i]), 0.0001 
+			); 
+		}
+	}
+}

Propchange: commons/sandbox/me/trunk/test-src/org/apache/commons/me/math/TestMoreMath.java
------------------------------------------------------------------------------
    svn:eol-style = native