001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.nabla.algorithmic.forward.functions;
018    
019    import org.apache.commons.nabla.algorithmic.forward.analysis.MethodDifferentiator;
020    import org.objectweb.asm.Opcodes;
021    import org.objectweb.asm.tree.InsnList;
022    import org.objectweb.asm.tree.InsnNode;
023    import org.objectweb.asm.tree.MethodInsnNode;
024    
025    
026    /** Differentiation transformer for the acos function invocation instructions.
027     */
028    public class AcosTransformer implements MathInvocationTransformer {
029    
030        /** {@inheritDoc} */
031        public InsnList getReplacementList(final String owner, final MethodDifferentiator methodDifferentiator) {
032    
033            // generate differential code
034            // ... u0, u1  --> ...  acos(u0), -u1 / sqrt(1 - u0 * u0)
035            final InsnList list = new InsnList();
036            list.add(new InsnNode(Opcodes.DUP2_X2));
037            list.add(new InsnNode(Opcodes.POP2));
038            list.add(new InsnNode(Opcodes.DUP2_X2));
039            list.add(new InsnNode(Opcodes.DUP2));
040            list.add(new InsnNode(Opcodes.DMUL));
041            list.add(new InsnNode(Opcodes.DCONST_1));
042            list.add(new InsnNode(Opcodes.DSUB));
043            list.add(new InsnNode(Opcodes.DNEG));
044            list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, owner, "sqrt", D_RETURN_D_DESCRIPTOR));
045            list.add(new InsnNode(Opcodes.DDIV));
046            list.add(new InsnNode(Opcodes.DNEG));
047            list.add(new InsnNode(Opcodes.DUP2_X2));
048            list.add(new InsnNode(Opcodes.POP2));
049            list.add(new MethodInsnNode(Opcodes.INVOKESTATIC, owner, "acos", D_RETURN_D_DESCRIPTOR));
050            list.add(new InsnNode(Opcodes.DUP2_X2));
051            list.add(new InsnNode(Opcodes.POP2));
052            return list;
053    
054        }
055    
056    }