Permalink
Browse files

added simple example where a sparse jacobian is evaluated and its spa…

…rsity pattern is computed
  • Loading branch information...
1 parent 354b8fb commit 2279b3d5d1902a7f93118918f28b120506009279 @b45ch1 committed Feb 25, 2011
Showing with 41 additions and 0 deletions.
  1. +41 −0 examples/sparse_jacobi_matrix.py
@@ -0,0 +1,41 @@
+import adolc
+import numpy
+
+M,N = 4,2
+
+sparsity_pattern_list = [numpy.random.randint(0,4*N,M)//(3*N) for n in range(N)]
+
+
+def F(x):
+ y = numpy.ones(M, dtype=x.dtype)
+ for n,sp in enumerate(sparsity_pattern_list):
+ for ns, s in enumerate(sp):
+ if s == 1:
+ y[ns] *= x[n]
+
+ return y
+
+x = numpy.random.rand(N)
+
+adolc.trace_on(0)
+x = adolc.adouble(x)
+adolc.independent(x)
+y = F(x)
+adolc.dependent(y)
+adolc.trace_off()
+
+x = numpy.random.rand(N)
+y = F(x)
+y2 = adolc.function(0,x)
+assert numpy.allclose(y,y2)
+
+options = numpy.array([0,0,0,0],dtype=int)
+pat = adolc.sparse.jac_pat(0,x,options)
+result = adolc.colpack.sparse_jac_no_repeat(0,x,options)
+
+print adolc.jacobian(0,x)
+print pat
+
+print result
+
+

0 comments on commit 2279b3d

Please sign in to comment.