1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.nabla.algorithmic.forward; |
18 | |
|
19 | |
import java.io.IOException; |
20 | |
import java.io.InputStream; |
21 | |
import java.io.OutputStream; |
22 | |
import java.lang.reflect.Constructor; |
23 | |
import java.lang.reflect.InvocationTargetException; |
24 | |
import java.util.HashMap; |
25 | |
import java.util.HashSet; |
26 | |
import java.util.Set; |
27 | |
|
28 | |
import org.apache.commons.nabla.algorithmic.forward.analysis.ClassDifferentiator; |
29 | |
import org.apache.commons.nabla.core.DifferentiationException; |
30 | |
import org.apache.commons.nabla.core.UnivariateDerivative; |
31 | |
import org.apache.commons.nabla.core.UnivariateDifferentiable; |
32 | |
import org.apache.commons.nabla.core.UnivariateDifferentiator; |
33 | |
import org.objectweb.asm.ClassReader; |
34 | |
import org.objectweb.asm.ClassWriter; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class ForwardAlgorithmicDifferentiator implements UnivariateDifferentiator { |
54 | |
|
55 | |
|
56 | |
private final HashMap<Class<? extends UnivariateDifferentiable>, |
57 | |
Class<? extends UnivariateDerivative>> map; |
58 | |
|
59 | |
|
60 | |
private final Set<String> mathClasses; |
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | 66 | public ForwardAlgorithmicDifferentiator() { |
66 | 66 | map = new HashMap<Class<? extends UnivariateDifferentiable>, |
67 | |
Class<? extends UnivariateDerivative>>(); |
68 | 66 | mathClasses = new HashSet<String>(); |
69 | 66 | addMathImplementation(Math.class); |
70 | 66 | addMathImplementation(StrictMath.class); |
71 | 66 | } |
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public void addMathImplementation(final Class<?> mathClass) { |
83 | 188 | mathClasses.add(mathClass.getName().replace('.', '/')); |
84 | 188 | } |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public void dumpCache(final OutputStream out) { |
90 | |
|
91 | 0 | throw new RuntimeException("not implemented yet"); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
public UnivariateDerivative differentiate(final UnivariateDifferentiable d) |
96 | |
throws DifferentiationException { |
97 | |
|
98 | |
|
99 | 66 | final Class<? extends UnivariateDerivative> derivativeClass = |
100 | |
getDerivativeClass(d.getClass()); |
101 | |
|
102 | |
try { |
103 | |
|
104 | |
|
105 | 66 | final Constructor<? extends UnivariateDerivative> constructor = |
106 | |
derivativeClass.getConstructor(d.getClass()); |
107 | 66 | return constructor.newInstance(d); |
108 | |
|
109 | 0 | } catch (InstantiationException ie) { |
110 | 0 | throw new DifferentiationException("abstract class {0} cannot be instantiated ({1})", |
111 | |
derivativeClass.getName(), ie.getMessage()); |
112 | 0 | } catch (IllegalAccessException iae) { |
113 | 0 | throw new DifferentiationException("illegal access to class {0} constructor ({1})", |
114 | |
derivativeClass.getName(), iae.getMessage()); |
115 | 0 | } catch (NoSuchMethodException nsme) { |
116 | 0 | throw new DifferentiationException("class {0} cannot be built from an instance of class {1} ({2})", |
117 | |
derivativeClass.getName(), d.getClass().getName(), nsme.getMessage()); |
118 | 0 | } catch (InvocationTargetException ite) { |
119 | 0 | throw new DifferentiationException("class {0} instantiation from an instance of class {1} failed ({2})", |
120 | |
derivativeClass.getName(), d.getClass().getName(), ite.getMessage()); |
121 | |
} |
122 | |
|
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
private Class<? extends UnivariateDerivative> |
133 | |
getDerivativeClass(final Class<? extends UnivariateDifferentiable> differentiableClass) |
134 | |
throws DifferentiationException { |
135 | |
|
136 | |
|
137 | 66 | Class<? extends UnivariateDerivative> derivativeClass = |
138 | |
map.get(differentiableClass); |
139 | |
|
140 | |
|
141 | 66 | if (derivativeClass == null) { |
142 | |
|
143 | 66 | derivativeClass = createDerivativeClass(differentiableClass); |
144 | |
|
145 | |
|
146 | 66 | map.put(differentiableClass, derivativeClass); |
147 | |
|
148 | |
} |
149 | |
|
150 | |
|
151 | 66 | return derivativeClass; |
152 | |
|
153 | |
} |
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
private Class<? extends UnivariateDerivative> |
161 | |
createDerivativeClass(final Class<? extends UnivariateDifferentiable> differentiableClass) |
162 | |
throws DifferentiationException { |
163 | |
try { |
164 | |
|
165 | |
|
166 | 66 | final String classResourceName = "/" + differentiableClass.getName().replace('.', '/') + ".class"; |
167 | 66 | final InputStream stream = differentiableClass.getResourceAsStream(classResourceName); |
168 | 66 | final ClassReader reader = new ClassReader(stream); |
169 | 66 | final ClassWriter writer = new ClassWriter(reader, ClassWriter.COMPUTE_FRAMES); |
170 | |
|
171 | |
|
172 | 66 | final ClassDifferentiator differentiator = new ClassDifferentiator(mathClasses, writer); |
173 | 66 | reader.accept(differentiator, ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); |
174 | 66 | differentiator.reportErrors(); |
175 | |
|
176 | |
|
177 | 66 | return new DerivativeLoader(differentiableClass).defineClass(differentiator, writer); |
178 | |
|
179 | 0 | } catch (IOException ioe) { |
180 | 0 | throw new DifferentiationException("class {0} cannot be read ({1})", |
181 | |
differentiableClass.getName(), ioe.getMessage()); |
182 | |
} |
183 | |
} |
184 | |
|
185 | |
|
186 | |
private static class DerivativeLoader extends ClassLoader { |
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
public DerivativeLoader(final Class<? extends UnivariateDifferentiable> differentiableClass) { |
192 | 66 | super(differentiableClass.getClassLoader()); |
193 | 66 | } |
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
@SuppressWarnings("unchecked") |
201 | |
public Class<? extends UnivariateDerivative> |
202 | |
defineClass(final ClassDifferentiator differentiator, final ClassWriter writer) { |
203 | 66 | final String name = differentiator.getDerivativeClassName().replace('/', '.'); |
204 | 66 | final byte[] bytecode = writer.toByteArray(); |
205 | 66 | return (Class<? extends UnivariateDerivative>) defineClass(name, bytecode, 0, bytecode.length); |
206 | |
} |
207 | |
} |
208 | |
|
209 | |
} |