matrix/coloring.py

This example looks at coloring methods for sparse matrices

View output (PDF) | source (python)

See also
Sparsity uni_coloring(const Sparsity &AT=Sparsity(), casadi_int cutoff=std::numeric_limits< casadi_int >::max()) const
Perform a unidirectional coloring: A greedy distance-2 coloring algorithm.
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 from casadi import *
25 
26 
27 
28 def color(A):
29  print("="*80)
30  print("Original:")
31  print(repr(DM(A,1)))
32  print("Colored: ")
33  print(repr(DM(A.uni_coloring(),1)))
34 
35 A = Sparsity.diag(5)
36 color(A)
37 
38 
39 
40 color(Sparsity.dense(5,10))
41 
42 
43 
44 
45 
46 color(A+Sparsity.triplet(5,5,[0],[4]))
47 
48 
49 
50 color(A+Sparsity.triplet(5,5,[4],[0]))
51 
52 
53 
54 color(A+Sparsity.triplet(5,5,[0]*5,list(range(5))))
55 
56 
57 
58 
59 color(A+Sparsity.triplet(5,5,list(range(5)),[0]*5))
60 
61 
62 
63 
64 
65 def color(A):
66  print("="*80)
67  print("Original:")
68  print(repr(DM(A,1)))
69  print("Star colored: ")
70  print(repr(DM(A.star_coloring(1),1)))
71 
72 color(A)
73 
74 
75 
76 color(Sparsity.dense(5,5))
77 
78 
79 
80 color(A+Sparsity.triplet(5,5,[0]*5,list(range(5)))+Sparsity.triplet(5,5,list(range(5)),[0]*5))
81 
82 
83 
84 
85 
86 
87 A = DM([[1,1,0,0,0,0],[1,1,1,0,1,1],[0,1,1,1,0,0],[0,0,1,1,0,1],[0,1,0,0,1,0],[0,1,0,1,0,1]])
88 A = sparsify(A)
89 color(A.sparsity())