You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2012/09/28 18:40:50 UTC

svn commit: r1391536 - /commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java

Author: luc
Date: Fri Sep 28 16:40:50 2012
New Revision: 1391536

URL: http://svn.apache.org/viewvc?rev=1391536&view=rev
Log:
Update differentiation of constant methods.

Constant methods do not depend on the input parameter.

Modified:
    commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java

Modified: commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java
URL: http://svn.apache.org/viewvc/commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java?rev=1391536&r1=1391535&r2=1391536&view=diff
==============================================================================
--- commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java (original)
+++ commons/sandbox/nabla/trunk/src/main/java/org/apache/commons/nabla/forward/analysis/MethodDifferentiator.java Fri Sep 28 16:40:50 2012
@@ -227,21 +227,7 @@ public class MethodDifferentiator {
                 for (final Iterator<AbstractInsnNode> i = method.instructions.iterator(); i.hasNext();) {
                     final AbstractInsnNode insn = i.next();
                     if (insn.getOpcode() == Opcodes.DRETURN) {
-                        final InsnList list = new InsnList();
-                        list.add(new VarInsnNode(Opcodes.ALOAD, 1));
-                        list.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
-                                                    Type.getInternalName(DerivativeStructure.class),
-                                                    "getFreeParameters",
-                                                    Type.getMethodDescriptor(Type.INT_TYPE)));
-                        list.add(new VarInsnNode(Opcodes.ALOAD, 1));
-                        list.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
-                                                    Type.getInternalName(DerivativeStructure.class),
-                                                    "getOrder",
-                                                    Type.getMethodDescriptor(Type.INT_TYPE)));
-                        list.add(new MethodInsnNode(Opcodes.INVOKESTATIC,
-                                                    Type.getInternalName(DerivativeStructure.class),
-                                                    "<init>",
-                                                    Type.getMethodDescriptor(dsType, Type.INT_TYPE, Type.INT_TYPE)));
+                        final InsnList list = convertDoubleToDerivativeStructure(1);
                         list.add(new InsnNode(Opcodes.ARETURN));
                         method.instructions.insert(insn, list);
                         method.instructions.remove(insn);
@@ -456,6 +442,44 @@ public class MethodDifferentiator {
 
     }
 
+    /** Create an instruction set converting a double into a constant {@link DerivativeStructure}.
+     * @param dsIndex index of the {@link DerivativeStructure} input variable
+     * @return instructions instructions converting the double into a {@link DerivativeStructure}
+     */
+    private InsnList convertDoubleToDerivativeStructure(final int dsIndex) {
+
+        final InsnList list = new InsnList();
+
+        // operand stack initial state: d
+        list.add(new TypeInsnNode(Opcodes.NEW,
+                                  Type.getInternalName(DerivativeStructure.class)));  // => d y_ds
+        list.add(new InsnNode(Opcodes.DUP_X2));                                       // => y_ds d y_ds
+        list.add(new InsnNode(Opcodes.DUP_X2));                                       // => y_ds y_ds d y_ds
+        list.add(new InsnNode(Opcodes.POP));                                          // => y_ds y_ds d
+        list.add(new VarInsnNode(Opcodes.ALOAD, dsIndex));                            // => y_ds y_ds d x_ds
+        list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
+                                    Type.getInternalName(DerivativeStructure.class),
+                                    "getFreeParameters",
+                                    Type.getMethodDescriptor(Type.INT_TYPE)));        // => y_ds y_ds d params
+        list.add(new VarInsnNode(Opcodes.ALOAD, 1));                                  // => y_ds y_ds d params x_ds
+        list.add(new MethodInsnNode(Opcodes.INVOKEVIRTUAL,
+                                    Type.getInternalName(DerivativeStructure.class),
+                                    "getOrder",
+                                    Type.getMethodDescriptor(Type.INT_TYPE)));        // => y_ds y_ds d params order
+        list.add(new InsnNode(Opcodes.DUP2_X2));                                      // => y_ds y_ds params order d params order
+        list.add(new InsnNode(Opcodes.POP2));                                         // => y_ds y_ds params order d
+        list.add(new MethodInsnNode(Opcodes.INVOKESPECIAL,
+                                    Type.getInternalName(DerivativeStructure.class),
+                                    "<init>",
+                                    Type.getMethodDescriptor(Type.VOID_TYPE,
+                                                             Type.INT_TYPE,
+                                                             Type.INT_TYPE,
+                                                             Type.DOUBLE_TYPE)));     // => y_ds
+
+        return list;
+
+    }
+
     /** Get the replacement list for an instruction.
      * @param insn instruction to replace
      * @return replacement instructions list