File: sparse_jacobian.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (656 lines) | stat: -rw-r--r-- 18,195 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------

/*
Old sparse Jacobian example
*/

# include <cppad/cppad.hpp>
namespace { // ---------------------------------------------------------

bool rc_tridiagonal(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   size_t i, j, k, ell;
   double eps10 = 10. * CppAD::epsilon<double>();

   // domain space vector
   size_t n = 13; // must be greater than or equal 3 (see n_sweep below)
   CPPAD_TESTVECTOR(AD<double>)  X(n);
   CPPAD_TESTVECTOR(double)        x(n);
   for(j = 0; j < n; j++)
      X[j] = x[j] = double(j+1);

   // declare independent variables and starting recording
   CppAD::Independent(X);

   size_t m = n;
   CPPAD_TESTVECTOR(AD<double>)  Y(m);
   CPPAD_TESTVECTOR(double) check(m * n );
   for(ell = 0; ell < m * n; ell++)
      check[ell] = 0.0;

   size_t K = 0;
   for(i = 0; i < n; i++)
   {  ell        = i * n + i;
      Y[i]       = double(ell+1) * 0.5 * X[i] * X[i];
      check[ell] = double(ell+1) * x[i];
      K++;
      if( i < n-1 )
      {  j          = i + 1;
         ell        = i * n + j;
         Y[i]      += double(ell+1) * 0.5 * X[i+1] * X[i+1];
         check[ell] = double(ell+1) * x[i+1];
         K++;
      }
      if(i > 0 )
      {  j          = i - 1;
         ell        = i * n + j;
         Y[i]      += double(ell+1) * 0.5 * X[i-1] * X[i-1];
         check[ell] = double(ell+1) * x[i-1];
      }
   }

   // create f: X -> Y and stop tape recording
   CppAD::ADFun<double> f(X, Y);

   // sparsity pattern
   CppAD::vector< std::set<size_t> > s(m), p(m);
   for(i = 0; i < m; i++)
      s[i].insert(i);
   p   = f.RevSparseJac(m, s);

   // Request the upper triangle of the array
   CPPAD_TESTVECTOR(size_t) r(K), c(K);
   CPPAD_TESTVECTOR(double) jac(K);
   k = 0;
   for(i = 0; i < n; i++)
   {  r[k] = i;
      c[k] = i;
      k++;
      if( i < n-1 )
      {  r[k] = i;
         c[k] = i+1;
         k++;
      }
   }
   ok &= K == k;

   CppAD::sparse_jacobian_work work;
   size_t n_sweep = f.SparseJacobianForward(x, p, r, c, jac, work);
   ok &= n_sweep == 3;
   for(k = 0; k < K; k++)
   {  ell = r[k] * n + c[k];
      ok &=  NearEqual(check[ell], jac[k], eps10, eps10);
   }
   work.clear();
   n_sweep = f.SparseJacobianReverse(x, p, r, c, jac, work);
   ok &= n_sweep == 3;
   for(k = 0; k < K; k++)
   {  ell = r[k] * n + c[k];
      ok &=  NearEqual(check[ell], jac[k], eps10, eps10);
   }

   return ok;
}

template <class BaseVector, class SetVector>
bool rc_set(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   size_t i, j, k, ell;
   double eps10 = 10. * CppAD::epsilon<double>();

   // domain space vector
   size_t n = 4;
   CPPAD_TESTVECTOR(AD<double>)  X(n);
   for(j = 0; j < n; j++)
      X[j] = AD<double> (0);

   // declare independent variables and starting recording
   CppAD::Independent(X);

   size_t m = 3;
   CPPAD_TESTVECTOR(AD<double>)  Y(m);
   Y[0] = 1.0*X[0] + 2.0*X[1];
   Y[1] = 3.0*X[2] + 4.0*X[3];
   Y[2] = 5.0*X[0] + 6.0*X[1] + 7.0*X[3]*X[3]/2.;

   // create f: X -> Y and stop tape recording
   CppAD::ADFun<double> f(X, Y);

   // new value for the independent variable vector
   BaseVector x(n);
   for(j = 0; j < n; j++)
      x[j] = double(j);

   // Jacobian of y
   /*
          [ 1 2 0 0    ]
   jac = [ 0 0 3 4    ]
          [ 5 6 0 7*x_3]
   */
   BaseVector check(m * n);
   check[0] = 1.; check[1] = 2.; check[2]  = 0.; check[3]  = 0.;
   check[4] = 0.; check[5] = 0.; check[6]  = 3.; check[7]  = 4.;
   check[8] = 5.; check[9] = 6.; check[10] = 0.; check[11] = 7.*x[3];

   // sparsity pattern
   SetVector s(m), p(m);
   for(i = 0; i < m; i++)
      s[i].insert(i);
   p   = f.RevSparseJac(m, s);

   // Use forward mode to compute columns 0 and 2
   // (make sure order of rows and columns does not matter)
   CPPAD_TESTVECTOR(size_t) r(3), c(3);
   BaseVector jac(3);
   r[0] = 2; c[0] = 0;
   r[1] = 1; c[1] = 2;
   r[2] = 0; c[2] = 0;
   CppAD::sparse_jacobian_work work;
   size_t n_sweep = f.SparseJacobianForward(x, p, r, c, jac, work);
   for(k = 0; k < 3; k++)
   {  ell = r[k] * n + c[k];
      ok &=  NearEqual(check[ell], jac[k], eps10, eps10);
   }
   ok &= (n_sweep == 1);

   // Use reverse mode to compute rows 0 and 1
   // (make sure order of rows and columns does not matter)
   r.resize(4), c.resize(4); jac.resize(4);
   r[0] = 0; c[0] = 0;
   r[1] = 1; c[1] = 2;
   r[2] = 0; c[2] = 1;
   r[3] = 1; c[3] = 3;
   work.clear();
   n_sweep = f.SparseJacobianReverse(x, p, r, c, jac, work);
   for(k = 0; k < 4; k++)
   {  ell = r[k] * n + c[k];
      ok &=  NearEqual(check[ell], jac[k], eps10, eps10);
   }
   ok &= (n_sweep == 1);

   return ok;
}
template <class BaseVector, class BoolVector>
bool rc_bool(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   size_t j, k, ell;
   double eps10 = 10. * CppAD::epsilon<double>();

   // domain space vector
   size_t n = 4;
   CPPAD_TESTVECTOR(AD<double>)  X(n);
   for(j = 0; j < n; j++)
      X[j] = AD<double> (0);

   // declare independent variables and starting recording
   CppAD::Independent(X);

   size_t m = 3;
   CPPAD_TESTVECTOR(AD<double>)  Y(m);
   Y[0] = 1.0*X[0] + 2.0*X[1];
   Y[1] = 3.0*X[2] + 4.0*X[3];
   Y[2] = 5.0*X[0] + 6.0*X[1] + 7.0*X[3]*X[3]/2.;

   // create f: X -> Y and stop tape recording
   CppAD::ADFun<double> f(X, Y);

   // new value for the independent variable vector
   BaseVector x(n);
   for(j = 0; j < n; j++)
      x[j] = double(j);

   // Jacobian of y
   /*
          [ 1 2 0 0    ]
   jac = [ 0 0 3 4    ]
          [ 5 6 0 7*x_3]
   */
   BaseVector check(m * n);
   check[0] = 1.; check[1] = 2.; check[2]  = 0.; check[3]  = 0.;
   check[4] = 0.; check[5] = 0.; check[6]  = 3.; check[7]  = 4.;
   check[8] = 5.; check[9] = 6.; check[10] = 0.; check[11] = 7.*x[3];
   BoolVector s(m * n);
   s[0] = true;   s[1] = true;   s[2] = false;   s[3] = false;
   s[4] = false;  s[5] = false;  s[6] = true;    s[7] = true;
   s[8] = true;   s[9] = true;  s[10] = false;  s[11] = true;

   // Use forward mode to compute columns 0 and 2
   // (make sure order of rows and columns does not matter)
   CPPAD_TESTVECTOR(size_t) r(3), c(3);
   BaseVector jac(3);
   r[0] = 2; c[0] = 0;
   r[1] = 1; c[1] = 2;
   r[2] = 0; c[2] = 0;
   CppAD::sparse_jacobian_work work;
   size_t n_sweep = f.SparseJacobianForward(x, s, r, c, jac, work);
   for(k = 0; k < 3; k++)
   {  ell = r[k] * n + c[k];
      ok &=  NearEqual(check[ell], jac[k], eps10, eps10);
   }
   ok &= (n_sweep == 1);

   // Use reverse mode to compute rows 0 and 1
   // (make sure order of rows and columns does not matter)
   r.resize(4), c.resize(4); jac.resize(4);
   r[0] = 0; c[0] = 0;
   r[1] = 1; c[1] = 2;
   r[2] = 0; c[2] = 1;
   r[3] = 1; c[3] = 3;
   work.clear();
   n_sweep = f.SparseJacobianReverse(x, s, r, c, jac, work);
   for(k = 0; k < 4; k++)
   {  ell = r[k] * n + c[k];
      ok &=  NearEqual(check[ell], jac[k], eps10, eps10);
   }
   ok &= (n_sweep == 1);

   return ok;
}


template <class BaseVector, class BoolVector>
bool reverse_bool(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   size_t i, j, k;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   // domain space vector
   size_t n = 4;
   CPPAD_TESTVECTOR(AD<double>)  X(n);
   for(j = 0; j < n; j++)
      X[j] = AD<double> (0);

   // declare independent variables and starting recording
   CppAD::Independent(X);

   size_t m = 3;
   CPPAD_TESTVECTOR(AD<double>)  Y(m);
   Y[0] = 1.0*X[0] + 2.0*X[1];
   Y[1] = 3.0*X[2] + 4.0*X[3];
   Y[2] = 5.0*X[0] + 6.0*X[1] + 7.0*X[2] + 8.0*X[3]*X[3]/2.;

   // create f: X -> Y and stop tape recording
   CppAD::ADFun<double> f(X, Y);

   // new value for the independent variable vector
   BaseVector x(n);
   for(j = 0; j < n; j++)
      x[j] = double(j);

   // Jacobian of y without sparsity pattern
   BaseVector jac(m * n);
   jac = f.SparseJacobian(x);
   /*
          [ 1 2 0 0    ]
   jac = [ 0 0 3 4    ]
          [ 5 6 7 8*x_3]
   */
   BaseVector check(m * n);
   check[0] = 1.; check[1] = 2.; check[2]  = 0.; check[3]  = 0.;
   check[4] = 0.; check[5] = 0.; check[6]  = 3.; check[7]  = 4.;
   check[8] = 5.; check[9] = 6.; check[10] = 7.; check[11] = 8.*x[3];
   for(k = 0; k < 12; k++)
      ok &=  NearEqual(check[k], jac[k], eps99, eps99 );

   // test passing sparsity pattern
   BoolVector s(m * m);
   BoolVector p(m * n);
   for(i = 0; i < m; i++)
   {  for(k = 0; k < m; k++)
         s[i * m + k] = false;
      s[i * m + i] = true;
   }
   p   = f.RevSparseJac(m, s);
   jac = f.SparseJacobian(x, p);
   for(k = 0; k < 12; k++)
      ok &=  NearEqual(check[k], jac[k], eps99, eps99 );

   return ok;
}

template <class BaseVector, class SetVector>
bool reverse_set(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   size_t i, j, k;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   // domain space vector
   size_t n = 4;
   CPPAD_TESTVECTOR(AD<double>)  X(n);
   for(j = 0; j < n; j++)
      X[j] = AD<double> (0);

   // declare independent variables and starting recording
   CppAD::Independent(X);

   size_t m = 3;
   CPPAD_TESTVECTOR(AD<double>)  Y(m);
   Y[0] = X[0] + X[1];
   Y[1] = X[2] + X[3];
   Y[2] = X[0] + X[1] + X[2] + X[3] * X[3] / 2.;

   // create f: X -> Y and stop tape recording
   CppAD::ADFun<double> f(X, Y);

   // new value for the independent variable vector
   BaseVector x(n);
   for(j = 0; j < n; j++)
      x[j] = double(j);

   // Jacobian of y without sparsity pattern
   BaseVector jac(m * n);
   jac = f.SparseJacobian(x);
   /*
          [ 1 1 0 0  ]
   jac = [ 0 0 1 1  ]
          [ 1 1 1 x_3]
   */
   BaseVector check(m * n);
   check[0] = 1.; check[1] = 1.; check[2]  = 0.; check[3]  = 0.;
   check[4] = 0.; check[5] = 0.; check[6]  = 1.; check[7]  = 1.;
   check[8] = 1.; check[9] = 1.; check[10] = 1.; check[11] = x[3];
   for(k = 0; k < 12; k++)
      ok &=  NearEqual(check[k], jac[k], eps99, eps99 );

   // test passing sparsity pattern
   SetVector s(m), p(m);
   for(i = 0; i < m; i++)
      s[i].insert(i);
   p   = f.RevSparseJac(m, s);
   jac = f.SparseJacobian(x, p);
   for(k = 0; k < 12; k++)
      ok &=  NearEqual(check[k], jac[k], eps99, eps99 );

   return ok;
}

template <class BaseVector, class BoolVector>
bool forward_bool(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   size_t j, k;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   // domain space vector
   size_t n = 3;
   CPPAD_TESTVECTOR(AD<double>)  X(n);
   for(j = 0; j < n; j++)
      X[j] = AD<double> (0);

   // declare independent variables and starting recording
   CppAD::Independent(X);

   size_t m = 4;
   CPPAD_TESTVECTOR(AD<double>)  Y(m);
   Y[0] = X[0] + X[2];
   Y[1] = X[0] + X[2];
   Y[2] = X[1] + X[2];
   Y[3] = X[1] + X[2] * X[2] / 2.;

   // create f: X -> Y and stop tape recording
   CppAD::ADFun<double> f(X, Y);

   // new value for the independent variable vector
   BaseVector x(n);
   for(j = 0; j < n; j++)
      x[j] = double(j);

   // Jacobian of y without sparsity pattern
   BaseVector jac(m * n);
   jac = f.SparseJacobian(x);
   /*
          [ 1 0 1   ]
   jac = [ 1 0 1   ]
          [ 0 1 1   ]
          [ 0 1 x_2 ]
   */
   BaseVector check(m * n);
   check[0] = 1.; check[1]  = 0.; check[2]  = 1.;
   check[3] = 1.; check[4]  = 0.; check[5]  = 1.;
   check[6] = 0.; check[7]  = 1.; check[8]  = 1.;
   check[9] = 0.; check[10] = 1.; check[11] = x[2];
   for(k = 0; k < 12; k++)
      ok &=  NearEqual(check[k], jac[k], eps99, eps99 );

   // test passing sparsity pattern
   BoolVector r(n * n);
   BoolVector p(m * n);
   for(j = 0; j < n; j++)
   {  for(k = 0; k < n; k++)
         r[j * n + k] = false;
      r[j * n + j] = true;
   }
   p   = f.ForSparseJac(n, r);
   jac = f.SparseJacobian(x, p);
   for(k = 0; k < 12; k++)
      ok &=  NearEqual(check[k], jac[k], eps99, eps99 );

   return ok;
}

template <class BaseVector, class SetVector>
bool forward_set(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   size_t j, k;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   // domain space vector
   size_t n = 3;
   CPPAD_TESTVECTOR(AD<double>)  X(n);
   for(j = 0; j < n; j++)
      X[j] = AD<double> (0);

   // declare independent variables and starting recording
   CppAD::Independent(X);

   size_t m = 4;
   CPPAD_TESTVECTOR(AD<double>)  Y(m);
   Y[0] = X[0] + X[2];
   Y[1] = X[0] + X[2];
   Y[2] = X[1] + X[2];
   Y[3] = X[1] + X[2] * X[2] / 2.;

   // create f: X -> Y and stop tape recording
   CppAD::ADFun<double> f(X, Y);

   // new value for the independent variable vector
   BaseVector x(n);
   for(j = 0; j < n; j++)
      x[j] = double(j);

   // Jacobian of y without sparsity pattern
   BaseVector jac(m * n);
   jac = f.SparseJacobian(x);
   /*
          [ 1 0 1   ]
   jac = [ 1 0 1   ]
          [ 0 1 1   ]
          [ 0 1 x_2 ]
   */
   BaseVector check(m * n);
   check[0] = 1.; check[1]  = 0.; check[2]  = 1.;
   check[3] = 1.; check[4]  = 0.; check[5]  = 1.;
   check[6] = 0.; check[7]  = 1.; check[8]  = 1.;
   check[9] = 0.; check[10] = 1.; check[11] = x[2];
   for(k = 0; k < 12; k++)
      ok &=  NearEqual(check[k], jac[k], eps99, eps99 );

   // test passing sparsity pattern
   SetVector r(n), p(m);
   for(j = 0; j < n; j++)
      r[j].insert(j);
   p   = f.ForSparseJac(n, r);
   jac = f.SparseJacobian(x, p);
   for(k = 0; k < 12; k++)
      ok &=  NearEqual(check[k], jac[k], eps99, eps99 );

   return ok;
}

bool multiple_of_n_bit(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::vector;
   size_t i, j;

   // should be the same as the corresponding typedef in
   // cppad/local/sparse/pack.hpp
   typedef size_t Pack;

   // number of bits per packed value
   size_t n_bit = std::numeric_limits<Pack>::digits;

   // check case where number of variables is equal to n_bit
   vector< AD<double> > x(n_bit);
   vector< AD<double> > y(n_bit);

   // create an AD function with domain and range dimension equal to n_bit
   CppAD::Independent(x);
   for(i = 0; i < n_bit; i++)
      y[i] = x[n_bit - i - 1];
   CppAD::ADFun<double> f(x, y);

   // Jacobian sparsity patterns
   vector<bool> r(n_bit * n_bit);
   vector<bool> s(n_bit * n_bit);
   for(i = 0; i < n_bit; i++)
   {  for(j = 0; j < n_bit; j++)
         r[ i * n_bit + j ] = (i == j);
   }
   s = f.ForSparseJac(n_bit, r);

   // check the result
   for(i = 0; i < n_bit; i++)
   {  for(j = 0; j < n_bit; j++)
      {  if( i == n_bit - j - 1 )
            ok = ok & s[ i * n_bit + j ];
         else
            ok = ok & (! s[i * n_bit + j] );
      }
   }

   return ok;
}

// check for a sparse_list bug that was fixed on 2017-04-06
void algo_sparse_list_bug(
   const CppAD::vector < CppAD::AD<double> >& ax ,
   CppAD::vector < CppAD::AD<double> >&       ay )
{
   ay[2] = 1.0;
   ay[0] = 1.0;
   ay[1] = ax[2];
   ay[3] = ax[2];
}
bool sparse_list_bug(void)
{  bool ok  = true;
   using CppAD::AD;
   using CppAD::vector;
   typedef CppAD::vector < std::set<size_t> > sparsity;
   //
   size_t n = 4;
   vector< AD<double> > ay(n), ax(n);
   for(size_t i = 0; i < n; ++i)
      ax[i] = 1.0;
   //
   // sparsity pattern corresponding to identity matrix
   sparsity eye(n);
   for (size_t i = 0; i < n; i++)
      eye[i].insert(i);
   //
   CppAD::checkpoint<double> atom_fun(
      "sparse_list_bug",
      algo_sparse_list_bug,
      ax,
      ay,
      CppAD::atomic_base<double>::set_sparsity_enum
   );
   //
   vector < AD<double> > au(n);
   for (size_t j = 0; j < n; j++)
      au[j] = 1.0;
   //
   // version of function that uses atom_fun
   CppAD::Independent(au);
   vector< AD<double> > av(n);
   atom_fun(au, ay);
   for (size_t j = 0; j < n; j++) {
      av[j] = ay[j] +  au[j];
   }
   CppAD::ADFun<double> yes_atom_fun(au, av);
   //
   // version of function that uses algoright
   CppAD::Independent(au);
   algo_sparse_list_bug(au, ay);
   for (size_t j = 0; j < n; j++) {
      av[j] = ay[j] +  au[j];
   }
   CppAD::ADFun<double> no_atom_fun(au, av);

   //
   sparsity pattern_yes = yes_atom_fun.RevSparseJac(n, eye);
   sparsity pattern_no  = no_atom_fun.RevSparseJac(n, eye);
   //
   for(size_t i = 0; i < n; i++)
      ok &= pattern_yes[i] == pattern_no[i];
   //
   return ok;
}



} // End empty namespace

# include <vector>
# include <valarray>
bool sparse_jacobian(void)
{  bool ok = true;
   ok &= rc_tridiagonal();
   ok &= multiple_of_n_bit();
   ok &= sparse_list_bug();
   // ---------------------------------------------------------------
   // vector of bool cases
   ok &=      rc_bool< CppAD::vector<double>, CppAD::vectorBool   >();
   ok &= forward_bool< CppAD::vector<double>, CppAD::vector<bool> >();
   //
   ok &= reverse_bool< std::vector<double>,   std::vector<bool>   >();
   ok &=      rc_bool< std::vector<double>,   std::valarray<bool> >();
   //
   ok &= forward_bool< std::valarray<double>, CppAD::vectorBool   >();
   ok &= reverse_bool< std::valarray<double>, CppAD::vector<bool> >();
   // ---------------------------------------------------------------
   // vector of set cases
   typedef std::vector< std::set<size_t> >   std_vector_set;
   typedef CppAD::vector< std::set<size_t> > cppad_vector_set;
   //
   ok &=      rc_set< CppAD::vector<double>, std_vector_set   >();
   ok &= forward_set< std::valarray<double>, std_vector_set   >();
   //
   ok &= reverse_set< std::vector<double>,   cppad_vector_set >();
   ok &=      rc_set< CppAD::vector<double>, cppad_vector_set >();
   //
   // According to section 26.3.2.3 of the 1998 C++ standard
   // a const valarray does not return references to its elements.
   // typedef std::valarray< std::set<size_t> > std_valarray_set;
   // ok &= forward_set< std::valarray<double>, std_valarray_set >();
   // ok &= reverse_set< std::valarray<double>, std_valarray_set >();
   // ---------------------------------------------------------------
   //
   return ok;
}