Added unit test for x-x and x/x (where d/x = 0).
@@ -8,6 +8,7 @@ import org.junit.Test;
import net.finmath.montecarlo.AbstractRandomVariableFactory;
import net.finmath.montecarlo.RandomVariableFactory;
+import net.finmath.montecarlo.automaticdifferentiation.AbstractRandomVariableDifferentiableFactory;
import net.finmath.montecarlo.automaticdifferentiation.RandomVariableDifferentiableInterface;
import net.finmath.stochastic.RandomVariableInterface;
@@ -18,6 +19,64 @@ import net.finmath.stochastic.RandomVariableInterface;
*/
public class RandomVariableDifferentiableAADTest {
+ @Test
+ public void testArithmeticSubSelf() {
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put("isGradientRetainsLeafNodesOnly", true);
+
+ AbstractRandomVariableFactory randomVariableFactoryValue = new RandomVariableFactory();
+
+ AbstractRandomVariableDifferentiableFactory randomVariableFactoryDifferentiable = new RandomVariableDifferentiableAADFactory(
+ randomVariableFactoryValue, properties);
+
+ RandomVariableDifferentiableInterface x = randomVariableFactoryDifferentiable.createRandomVariable(2.0);
+
+ System.out.println("Checking x.sub(x):");
+
+ /*
+ * x.sub(x)
+ */
+
+ RandomVariableInterface z = x.sub(x);
+ System.out.println("Value:" + z.getAverage());
+
+ Map<Long, RandomVariableInterface> gradient = ((RandomVariableDifferentiableInterface)z).getGradient();
+ RandomVariableInterface dx = gradient.get(x.getID());
+
+ System.out.println("Derivative:" + z.getAverage());
+
+ Assert.assertEquals("Derivative d/dx (x-x)", dx.getAverage(), 0.0, 0.0);
+ }
+
+ @Test
+ public void testArithmeticDivSelf() {
+ Map<String, Object> properties = new HashMap<String, Object>();
+ properties.put("isGradientRetainsLeafNodesOnly", true);
+
+ AbstractRandomVariableFactory randomVariableFactoryValue = new RandomVariableFactory();
+
+ AbstractRandomVariableDifferentiableFactory randomVariableFactoryDifferentiable = new RandomVariableDifferentiableAADFactory(
+ randomVariableFactoryValue, properties);
+
+ RandomVariableDifferentiableInterface x = randomVariableFactoryDifferentiable.createRandomVariable(2.0);
+
+ System.out.println("Checking x.sub(x):");
+
+ /*
+ * x.div(x)
+ */
+
+ RandomVariableInterface z = x.div(x);
+ System.out.println("Value:" + z.getAverage());
+
+ Map<Long, RandomVariableInterface> gradient = ((RandomVariableDifferentiableInterface)z).getGradient();
+ RandomVariableInterface dx = gradient.get(x.getID());
+
+ System.out.println("Derivative:" + z.getAverage());
+
+ Assert.assertEquals("Derivative d/dx (x/x)", dx.getAverage(), 0.0, 0.0);
+ }
+
@Test
public void testTypePriorityAdd() {
Map<String, Object> properties = new HashMap<String, Object>();
@@ -94,7 +153,7 @@ public class RandomVariableDifferentiableAADTest {
System.out.println("Checking the return type of operators upon commutation:");
/*
- * add
+ * cap
*/
RandomVariableInterface z1 = x.cap(y);
@@ -124,7 +183,7 @@ public class RandomVariableDifferentiableAADTest {
System.out.println("Checking the return type of operators upon commutation:");
/*
- * add
+ * floor
*/
RandomVariableInterface z1 = x.floor(y);
