@Test public void testNormalize() { MyDouble a = new MyDouble(1); MyDouble b = new MyDouble(2); MyDouble c = new MyDouble(3); QuadraticEquation NormQuad = new QuadraticEquation(a, b, c); MyDouble norm = new MyDouble(a.square().add(b.square().add(c.square()))); MyDouble normTest = new MyDouble(norm.sqrt()); assertTrue(normTest.equals(NormQuad.normalize())); }
@Test public void testDerivative() { MyDouble a = new MyDouble(1); MyDouble b = new MyDouble(2); MyDouble c = new MyDouble(3); QuadraticEquation DiffQuad = new QuadraticEquation(a, b, c); MyDouble expOne = new MyDouble(2); MyDouble expTwo = new MyDouble(1); QuadraticEquation ResultQuad = new QuadraticEquation(a.multiply(expOne), b.multiply(expTwo)); assertTrue(ResultQuad.equals(DiffQuad.derivative())); }
// /////////////////////////////////////////////////////////////////////////// // event handler interface // /////////////////////////////////////////////////////////////////////////// @Override public void reset(final int iteration) { log.debug("reset called"); if (iteration > currentIter) { log.debug("update iteration number from " + currentIter + " to " + iteration); currentIter = iteration; if (writeInterval > 0 && iteration % writeInterval == 0) { log.debug("call statistics print"); statsWriter.writeStats(iteration); } log.debug("update weights for drivers"); for (WeightsPerTimeBin ws : weightsForDrivers.values()) { for (MyDouble v : ws.weights.values()) v.value *= learningRate; } log.debug("update weights for passengers"); for (WeightsPerTimeBin ws : weightsForPassengers.values()) { for (MyDouble v : ws.weights.values()) v.value *= learningRate; } } }
// replace every x in tree by (x - vx) // i.e. replace fVar with (fvar - vx) private void translateX(ExpressionNode en, double vx, int varNo) { ExpressionValue left = en.getLeft(); ExpressionValue right = en.getRight(); // left tree if (left == fVars[varNo]) { try { // is there a constant number to the right? MyDouble num = (MyDouble) right; double temp; switch (en.getOperation()) { case ExpressionNode.PLUS: temp = num.getDouble() - vx; if (Kernel.isZero(temp)) { expression = expression.replaceAndWrap(en, fVars[varNo]); } else if (temp < 0) { en.setOperation(ExpressionNode.MINUS); num.set(-temp); } else { num.set(temp); } return; case ExpressionNode.MINUS: temp = num.getDouble() + vx; if (Kernel.isZero(temp)) { expression = expression.replaceAndWrap(en, fVars[varNo]); } else if (temp < 0) { en.setOperation(ExpressionNode.PLUS); num.set(-temp); } else { num.set(temp); } return; default: en.setLeft(shiftXnode(vx, varNo)); } } catch (Exception e) { en.setLeft(shiftXnode(vx, varNo)); } } else if (left instanceof ExpressionNode) { translateX((ExpressionNode) left, vx, varNo); } // right tree if (right == fVars[varNo]) { en.setRight(shiftXnode(vx, varNo)); } else if (right instanceof ExpressionNode) { translateX((ExpressionNode) right, vx, varNo); } }
@Test public void testSubtract() { MyDouble a = new MyDouble(1); MyDouble b = new MyDouble(2); MyDouble c = new MyDouble(3); QuadraticEquation EquatOne = new QuadraticEquation(a, b, c); QuadraticEquation EquatTwo = new QuadraticEquation(a, b, c); QuadraticEquation TestQuad = new QuadraticEquation(a.subtract(a), b.subtract(b), c.subtract(c)); assertTrue(TestQuad.equals(EquatOne.subtract(EquatTwo))); }
@Test public void testAdd() { MyDouble a = new MyDouble(1); MyDouble b = new MyDouble(2); MyDouble c = new MyDouble(3); QuadraticEquation EquatOne = new QuadraticEquation(a, b, c); QuadraticEquation EquatTwo = new QuadraticEquation(a, b, c); QuadraticEquation TestQuad = new QuadraticEquation(a.add(a), b.add(b), c.add(c)); assertTrue(TestQuad.equals(EquatOne.add(EquatTwo))); }
// YOU NEED TO IMPLEMENT AT LEAST THESE JUNIT TESTS BELOW @Test public void testGetters() { MyDouble a = new MyDouble(3); MyDouble b = new MyDouble(4); MyDouble c = new MyDouble(5); QuadraticEquation testQuad = new QuadraticEquation(a, b, c); assertTrue(a.equals(testQuad.getA())); assertTrue(b.equals(testQuad.getB())); assertTrue(c.equals(testQuad.getC())); }
@Test public void testMultiply() { MyDouble a = new MyDouble(1); MyDouble b = new MyDouble(2); MyDouble c = new MyDouble(3); QuadraticEquation TestOneQuad = new QuadraticEquation(a, b, c); QuadraticEquation TestTwoQuad = new QuadraticEquation(a, b, c); QuadraticEquation MultiQuadNull = TestOneQuad.limitedMultiply(TestTwoQuad); assertEquals(null, MultiQuadNull); QuadraticEquation TestThreeQuad = new QuadraticEquation(c); QuadraticEquation MultiQuad = TestOneQuad.limitedMultiply(TestThreeQuad); QuadraticEquation MultiTest = new QuadraticEquation(a.multiply(c), b.multiply(c), c.multiply(c)); assertTrue(MultiQuad.equals(MultiTest)); }