/** Test of method getProjectedEnd, of class Ray. */ @Test public void testGetProjectedEnd() throws Exception { Ray instance = new Ray(new Vec3(1, 2, 3), new Vec3(4, 5, 6)); float fExpResult = 15.f / (float) Math.sqrt(3); float fResult = instance.getProjectedEnd(); assertEquals(fExpResult, fResult, 0.00001); }
/** Test of method getLength, of class Ray. */ @Test public void testGetLength() throws Exception { Ray instance = new Ray(new Vec3(1, 2, 3), new Vec3(4, 5, 6)); float fExpResult = (float) Math.sqrt(3 * 9); float fResult = instance.getLength(); assertEquals(fExpResult, fResult, 0.00001); }
public void checkInner(ImageFloat32 image, int c_x, int c_y, int w, int h) { ImplDescribePointPixelRegionNCC_F32 alg = new ImplDescribePointPixelRegionNCC_F32(w, h); NccFeature desc = new NccFeature(alg.getDescriptorLength()); alg.setImage(image); assertTrue(alg.isInBounds(c_x, c_y)); alg.process(c_x, c_y, desc); int y0 = c_y - h / 2; int x0 = c_x - w / 2; double mean = 0; for (int y = y0; y < y0 + h; y++) { for (int x = x0; x < x0 + w; x++) { mean += image.get(x, y); } } mean /= w * h; double variance = 0; for (int y = y0; y < y0 + h; y++) { for (int x = x0; x < x0 + w; x++) { double a = image.get(x, y) - mean; variance += a * a; } } variance /= w * h; assertEquals(desc.mean, mean, 1e-8); assertEquals(desc.sigma, Math.sqrt(variance), 1e-8); int index = 0; for (int y = y0; y < y0 + h; y++) { for (int x = x0; x < x0 + w; x++, index++) { assertEquals(image.get(x, y) - mean, desc.value[index], 1e-4); } } }
/** Test of method getDir, of class Ray. */ @Test public void testGetDir() throws Exception { Ray instance = new Ray(new Vec3(1, 2, 3), new Vec3(4, 5, 6)); Vec3 expResult = new Vec3(1.f / (float) Math.sqrt(3)); Vec3 result = instance.getDir(); assertEquals(expResult, result); }
/** Test of runScriptlet method, of class ScriptingContainer. */ @Test public void testRunScriptlet_InputStream_String() throws FileNotFoundException { System.out.println("runScriptlet(istream, filename)"); InputStream istream = null; String filename = ""; ScriptingContainer instance = new ScriptingContainer(); Object expResult = null; Object result = instance.runScriptlet(istream, filename); assertEquals(expResult, result); filename = "org/jruby/embed/ruby/law_of_cosines.rb"; istream = getClass().getClassLoader().getResourceAsStream(filename); instance.put("@a", 2.0); instance.put("@b", 2 * Math.sqrt(3.0)); instance.put("@c", 2.0); List<Double> angles = (List<Double>) instance.runScriptlet(istream, filename); // this result goes to 30.00000000000004,30.00000000000004,120.0. // these should be 30.0, 30.0, 120.0. conversion precision error? System.out.println(angles.get(0) + ", " + angles.get(1) + ", " + angles.get(2)); assertEquals(30.0, angles.get(0), 0.00001); assertEquals(30.0, angles.get(1), 0.00001); assertEquals(120.0, angles.get(2), 0.00001); instance.getVarMap().clear(); instance = null; }
@Test public void testDistance() { Vector4 x = new Vector4(2, 3, 4, 5); Vector4 y = new Vector4(1, 2, 3, 4); doAssertDouble(2, x.distance(y)); x = new Vector4(10, 3, 6, -5); y = new Vector4(7, 3, -2, 1); doAssertDouble(Math.sqrt(109), x.distance(y)); }
private double stdDev( Map<List<String>, Integer> histogram, double expVal, List<List<String>> input) { if (histogram.size() == 0) return 0.0; double stdDev = 0; for (int value : histogram.values()) { stdDev += (value * value) / productSize(input); } stdDev = Math.sqrt(stdDev - expVal); return stdDev; }
@Test public void testDistanceEuclidienne3() throws DataFormatException { List<Double> variables = new LinkedList<Double>(); for (double d : p.getVariables()) variables.add(d + 1); InfinitePoint Point = new InfinitePoint(variables); assertTrue( p.distanceEuclidienne(Point) - Math.sqrt(Point.getDimension()) < InfinitePoint.getEpsilon()); }
@Test public void hessianNormalForm() { PlaneGeneral3D_F64 a = new PlaneGeneral3D_F64(2, -3, 4, 5); double n = Math.sqrt(2 * 2 + 3 * 3 + 4 * 4); UtilPlane3D_F64.hessianNormalForm(a); assertEquals(2 / n, a.A, GrlConstants.DOUBLE_TEST_TOL); assertEquals(-3 / n, a.B, GrlConstants.DOUBLE_TEST_TOL); assertEquals(4 / n, a.C, GrlConstants.DOUBLE_TEST_TOL); assertEquals(5 / n, a.D, GrlConstants.DOUBLE_TEST_TOL); }
@Test public void testBruteForce() { /* This test creates a dataset where feature values are multiples of consecutive natural numbers The obtained values are compared to the theoretical mean and std dev */ double tolerancePerc = 0.01; // 0.01% of correct value int nSamples = 5120; int x = 1, y = 2, z = 3; INDArray featureX = Nd4j.linspace(1, nSamples, nSamples).reshape(nSamples, 1).mul(x); INDArray featureY = featureX.mul(y); INDArray featureZ = featureX.mul(z); INDArray featureSet = Nd4j.concat(1, featureX, featureY, featureZ); INDArray labelSet = Nd4j.zeros(nSamples, 1); DataSet sampleDataSet = new DataSet(featureSet, labelSet); double meanNaturalNums = (nSamples + 1) / 2.0; INDArray theoreticalMean = Nd4j.create(new double[] {meanNaturalNums * x, meanNaturalNums * y, meanNaturalNums * z}); double stdNaturalNums = Math.sqrt((nSamples * nSamples - 1) / 12.0); INDArray theoreticalStd = Nd4j.create(new double[] {stdNaturalNums * x, stdNaturalNums * y, stdNaturalNums * z}); NormalizerStandardize myNormalizer = new NormalizerStandardize(); myNormalizer.fit(sampleDataSet); INDArray meanDelta = Transforms.abs(theoreticalMean.sub(myNormalizer.getMean())); INDArray meanDeltaPerc = meanDelta.div(theoreticalMean).mul(100); double maxMeanDeltaPerc = meanDeltaPerc.max(1).getDouble(0, 0); assertTrue(maxMeanDeltaPerc < tolerancePerc); INDArray stdDelta = Transforms.abs(theoreticalMean.sub(myNormalizer.getMean())); INDArray stdDeltaPerc = stdDelta.div(theoreticalStd).mul(100); double maxStdDeltaPerc = stdDeltaPerc.max(1).getDouble(0, 0); assertTrue(maxStdDeltaPerc < tolerancePerc); // SAME TEST WITH THE ITERATOR int bSize = 10; tolerancePerc = 1; // 1% of correct value DataSetIterator sampleIter = new TestDataSetIterator(sampleDataSet, bSize); myNormalizer.fit(sampleIter); meanDelta = Transforms.abs(theoreticalMean.sub(myNormalizer.getMean())); meanDeltaPerc = meanDelta.div(theoreticalMean).mul(100); maxMeanDeltaPerc = meanDeltaPerc.max(1).getDouble(0, 0); assertTrue(maxMeanDeltaPerc < tolerancePerc); stdDelta = Transforms.abs(theoreticalMean.sub(myNormalizer.getMean())); stdDeltaPerc = stdDelta.div(theoreticalStd).mul(100); maxStdDeltaPerc = stdDeltaPerc.max(1).getDouble(0, 0); assertTrue(maxStdDeltaPerc < tolerancePerc); }
@Test public void testCor_double_array() { logger.info("\ntesting cor(double[] x, double[] y)"); assertEquals(Double.NaN, MathUtil.cor(null, null), 0.0); assertEquals(Double.NaN, MathUtil.cor(new double[0], null), 0.0); assertEquals(Double.NaN, MathUtil.cor(new double[] {1.0, 2.0}, null), 0.0); assertEquals(Double.NaN, MathUtil.cor(null, new double[] {1.0, 2.0}), 0.0); assertEquals(Double.NaN, MathUtil.cor(null, new double[0]), 0.0); assertEquals(Double.NaN, MathUtil.cor(new double[] {1.0}, new double[] {1.0}), 0.0); assertEquals(Double.NaN, MathUtil.cor(new double[] {1, 2}, new double[] {1, 2, 3}), 0.0); assertEquals(1.0, MathUtil.cor(new double[] {1, 2}, new double[] {1, 2}), 0.00000001); System.out.println(Math.sqrt(-1)); }
@Test public void test() { Frame frame = null; try { Futures fs = new Futures(); Random random = new Random(); Vec[] vecs = new Vec[1]; AppendableVec vec = new AppendableVec(Vec.newKey(), Vec.T_NUM); for (int i = 0; i < 2; i++) { NewChunk chunk = new NewChunk(vec, i); for (int r = 0; r < 1000; r++) chunk.addNum(random.nextInt(1000)); chunk.close(i, fs); } vecs[0] = vec.layout_and_close(fs); fs.blockForPending(); frame = new Frame(Key.<Frame>make(), null, vecs); // Make sure we test the multi-chunk case vecs = frame.vecs(); assert vecs[0].nChunks() > 1; long rows = frame.numRows(); Vec v = vecs[0]; double min = Double.POSITIVE_INFINITY, max = Double.NEGATIVE_INFINITY, mean = 0, sigma = 0; for (int r = 0; r < rows; r++) { double d = v.at(r); if (d < min) min = d; if (d > max) max = d; mean += d; } mean /= rows; for (int r = 0; r < rows; r++) { double d = v.at(r); sigma += (d - mean) * (d - mean); } sigma = Math.sqrt(sigma / (rows - 1)); double epsilon = 1e-9; assertEquals(max, v.max(), epsilon); assertEquals(min, v.min(), epsilon); assertEquals(mean, v.mean(), epsilon); assertEquals(sigma, v.sigma(), epsilon); } finally { if (frame != null) frame.delete(); } }
public genRandomDataSet(int nSamples, int nFeatures, int a, int b, long randSeed) { int i = 0; // Randomly generate scaling constants and add offsets // to get aA and bB INDArray aA = Nd4j.rand(1, nFeatures, randSeed).add(a); INDArray bB = Nd4j.rand(1, nFeatures, randSeed).add(b); // transform ndarray as X = aA + bB * X INDArray randomFeatures = Nd4j.zeros(nSamples, nFeatures); while (i < nFeatures) { INDArray randomSlice = Nd4j.randn(nSamples, 1, randSeed); randomSlice.muli(aA.getScalar(0, i)); randomSlice.addi(bB.getScalar(0, i)); randomFeatures.putColumn(i, randomSlice); i++; } INDArray randomLabels = Nd4j.zeros(nSamples, 1); this.sampleDataSet = new DataSet(randomFeatures, randomLabels); this.theoreticalMean = bB; this.theoreticalStd = aA; this.theoreticalSEM = this.theoreticalStd.div(Math.sqrt(nSamples)); }
/** Test of callMethod method, of class ScriptingContainer. */ @Test public void testCallMethod_3args() { System.out.println("callMethod(receiver, methodName, returnType)"); Object receiver = null; String methodName = ""; Class<Object> returnType = null; String basedir = System.getProperty("user.dir"); String[] paths = {basedir + "/lib/ruby/1.8"}; ScriptingContainer instance = new ScriptingContainer(); instance.getProvider().setLoadPaths(Arrays.asList(paths)); Object expResult = null; Object result = instance.callMethod(receiver, methodName, returnType); assertEquals(expResult, result); String filename = "org/jruby/embed/ruby/next_year_1.rb"; receiver = instance.runScriptlet(PathType.CLASSPATH, filename); int next_year = instance.callMethod(receiver, "get_year", Integer.class); assertEquals(getNextYear(), next_year); String script = "def volume\n" + " (Math::PI * (@r ** 2.0) * @h)/3.0\n" + "end\n" + "def surface_area\n" + " Math::PI * @r * Math.sqrt((@r ** 2.0) + (@h ** 2.0)) + Math::PI * (@r ** 2.0)\n" + "end"; receiver = instance.runScriptlet(script); instance.put("@r", 1.0); instance.put("@h", Math.sqrt(3.0)); double volume = instance.callMethod(receiver, "volume", Double.class); assertEquals(1.813799, volume, 0.000001); double surface_area = instance.callMethod(receiver, "surface_area", Double.class); assertEquals(9.424778, surface_area, 0.000001); instance.getVarMap().clear(); instance = null; }
/** Test of newRuntimeAdapter method, of class ScriptingContainer. */ @Test public void testNewRuntimeAdapter() { System.out.println("newRuntimeAdapter"); ScriptingContainer instance = new ScriptingContainer(); EmbedRubyRuntimeAdapter result = instance.newRuntimeAdapter(); String script = "def volume\n" + " (Math::PI * (@r ** 2.0) * @h)/3.0\n" + "end\n" + "def surface_area\n" + " Math::PI * @r * Math.sqrt((@r ** 2.0) + (@h ** 2.0)) + Math::PI * (@r ** 2.0)\n" + "end\n" + "return volume, surface_area"; instance.put("@r", 1.0); instance.put("@h", Math.sqrt(3.0)); EmbedEvalUnit unit = result.parse(script); IRubyObject ret = unit.run(); List<Double> rightCircularCone = (List<Double>) ret.toJava(List.class); assertEquals(1.813799, rightCircularCone.get(0), 0.000001); assertEquals(9.424778, rightCircularCone.get(1), 0.000001); instance.getVarMap().clear(); instance = null; }
@Test public void testLength() { Vector4 x = new Vector4(2, 3, 4, 5); doAssertDouble(Math.sqrt(54), x.length()); }
@Test public void TestR() { Point p = new Point(1, 1); assertEquals(p.r(), Math.sqrt(2), DELTA); }
public class VectorDefinitionTest { private static final double NoDifferenceAllowed = 0.0; private static final double TestInitX1 = 0.0; private static final double TestInitY1 = 0.0; private static final double TestInitX2 = 1.0; private static final double TestInitY2 = 1.0; private static final double TestInitLength = Math.sqrt(2.0); private static final double TestNewLength = 2.0 * TestInitLength; private static final double TestScale = 2.0; private static final double TestShiftedX2 = -1.0; private static final double TestShiftedY2 = -1.0; private static final double TestShiftValue = 7.8; private static final double TestRotation = Math.toRadians(45.0); private static final double TestRotatedX = 0.0; private static final double TestRotatedY = Math.sqrt(2.0); private static final double TestRotationAllowedDifference = Math.pow(10, -10); private static final double TestNewX1 = 3.0; private static final double TestNewY1 = 4.0; private static final double TestNewX2 = 5.0; private static final double TestNewY2 = 6.0; private VectorDefinition vectorDefinition; @Before public void setUp() // do not catch these, successful reflection must be successful to be able to test throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Constructor<VectorDefinition> constructor = VectorDefinition.class.getDeclaredConstructor( double.class, double.class, double.class, double.class); constructor.setAccessible(true); vectorDefinition = (VectorDefinition) constructor.newInstance(TestInitX1, TestInitY1, TestInitX2, TestInitY2); } @Test public final void testGetLength() { assertEquals(TestInitLength, vectorDefinition.getLength(), NoDifferenceAllowed); } @Test public final void testAppendLength() { vectorDefinition.AppendLength(TestInitLength); assertEquals(TestNewLength, vectorDefinition.getLength(), NoDifferenceAllowed); } @Test public final void testScale() { vectorDefinition.Scale(TestScale); assertEquals(TestInitX2 * TestScale, vectorDefinition.getX2(), NoDifferenceAllowed); assertEquals(TestInitY2 * TestScale, vectorDefinition.getY2(), NoDifferenceAllowed); } @Test public final void testSetLength() { vectorDefinition.setLength(TestNewLength); assertEquals(TestNewLength, vectorDefinition.getLength(), NoDifferenceAllowed); } @Test public final void testShiftOrigin() { vectorDefinition.shiftOrigin(); assertEquals(TestShiftedX2, vectorDefinition.getX2(), NoDifferenceAllowed); assertEquals(TestShiftedY2, vectorDefinition.getY2(), NoDifferenceAllowed); } @Test public final void testShiftOnArrow() { vectorDefinition.shiftOnArrow(TestShiftValue); assertEquals( TestInitX2 - TestInitX1, vectorDefinition.getX2() - vectorDefinition.getX1(), NoDifferenceAllowed); assertEquals( TestInitY2 - TestInitY1, vectorDefinition.getY2() - vectorDefinition.getY1(), NoDifferenceAllowed); } @Test public final void testRotate() { vectorDefinition.Rotate(TestRotation); assertEquals(TestRotatedX, vectorDefinition.getX2(), TestRotationAllowedDifference); assertEquals(TestRotatedY, vectorDefinition.getY2(), TestRotationAllowedDifference); } @Test public final void testGetX1() { assertEquals(TestInitX1, vectorDefinition.getX1(), NoDifferenceAllowed); } @Test public final void testSetX1() { vectorDefinition.setX1(TestNewX1); assertEquals(TestNewX1, vectorDefinition.getX1(), NoDifferenceAllowed); } @Test public final void testGetY1() { assertEquals(TestInitY1, vectorDefinition.getY1(), NoDifferenceAllowed); } @Test public final void testSetY1() { vectorDefinition.setY1(TestNewY1); assertEquals(TestNewY1, vectorDefinition.getY1(), NoDifferenceAllowed); } @Test public final void testGetX2() { assertEquals(TestInitX2, vectorDefinition.getX2(), NoDifferenceAllowed); } @Test public final void testSetX2() { vectorDefinition.setX2(TestNewX2); assertEquals(TestNewX2, vectorDefinition.getX2(), NoDifferenceAllowed); } @Test public final void testGetY2() { assertEquals(TestInitY2, vectorDefinition.getY2(), NoDifferenceAllowed); } @Test public final void testSetY2() { vectorDefinition.setY2(TestNewY2); assertEquals(TestNewY2, vectorDefinition.getY2(), NoDifferenceAllowed); } @Test public final void testGetPointStart() { I2DPoint resultPoint = vectorDefinition.getPointStart(); assertEquals(TestInitX1, resultPoint.getX(), NoDifferenceAllowed); assertEquals(TestInitY1, resultPoint.getY(), NoDifferenceAllowed); } @Test public final void testGetPointEnd() { I2DPoint resultPoint = vectorDefinition.getPointEnd(); assertEquals(TestInitX2, resultPoint.getX(), NoDifferenceAllowed); assertEquals(TestInitY2, resultPoint.getY(), NoDifferenceAllowed); } }
/** Test of moverPunto method, of class OperacionesGeométricas. */ @Test public void testMoverPunto() { System.out.println("MoverPunto"); // Mover distancia y ángulo 0 Point2D.Double punto = new Point2D.Double(3, 3); double ángulo = 0.0F; double distancia = 0.0F; Point2D.Double expResult = punto; Point2D.Double result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); assertEquals(expResult, result); // Puntos Cardinales ángulo = Math.toRadians(0); distancia = 1; result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(4, 3); assertEquals(expResult, result); ángulo = Math.toRadians(90); distancia = 1; result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(3, 4); assertEquals(expResult, result); ángulo = Math.toRadians(180); distancia = 1; result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(2, 3); assertEquals(expResult, result); ángulo = Math.toRadians(-90); distancia = 1; result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(3, 2); assertEquals(expResult, result); // Mover distancia 0 ángulo = Math.toRadians(0); distancia = 0; result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(3, 3); assertEquals(expResult, result); ángulo = Math.toRadians(90); distancia = 0; result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(3, 3); assertEquals(expResult, result); ángulo = Math.toRadians(180); distancia = 0; result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(3, 3); assertEquals(expResult, result); ángulo = Math.toRadians(270); distancia = 0; result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(3, 3); assertEquals(expResult, result); ángulo = Math.toRadians(33); distancia = 0; result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(3, 3); assertEquals(expResult, result); // Mover en diagonal double delta = 0.000001; distancia = Math.sqrt(2); ángulo = Math.toRadians(45); result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(4, 4); // assertEquals(expResult, result); assertTrue(estáEnRango(result.getX(), expResult.getX(), delta)); assertTrue(estáEnRango(result.getY(), expResult.getY(), delta)); ángulo = Math.toRadians(135); result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(2, 4); // assertEquals(expResult, result); assertTrue(estáEnRango(result.getX(), expResult.getX(), delta)); assertTrue(estáEnRango(result.getY(), expResult.getY(), delta)); ángulo = Math.toRadians(225); result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(2, 2); assertTrue(estáEnRango(result.getX(), expResult.getX(), delta)); assertTrue(estáEnRango(result.getY(), expResult.getY(), delta)); ángulo = Math.toRadians(315); result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia); expResult = new Point2D.Double(4, 2); // assertEquals(expResult, result); assertTrue(estáEnRango(result.getX(), expResult.getX(), delta)); assertTrue(estáEnRango(result.getY(), expResult.getY(), delta)); // assertEquals(expResult, result); }