matrix/btf.py

This example looks at btf

View output (PDF) | source (python)

See also
casadi::diagcat();
casadi_int btf(std::vector< casadi_int > &rowperm, std::vector< casadi_int > &colperm, std::vector< casadi_int > &rowblock, std::vector< casadi_int > &colblock, std::vector< casadi_int > &coarse_rowblock, std::vector< casadi_int > &coarse_colblock) const
Calculate the block triangular form (BTF)
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 import numpy
26 
27 
28 
29 b1 = DM([[2,3],[4,5]])
30 b2 = DM([[6,7,8],[9,10,11],[12,13,14]])
31 A = diagcat(1,b1,b2,15)
32 
33 print("original: ")
34 print(A)
35 
36 
37 
38 numpy.random.seed(0)
39 p1 = numpy.random.permutation(A.size1())
40 p2 = numpy.random.permutation(A.size2())
41 
42 S = A[p1,:]
43 
44 
45 print("randomly permuted: ")
46 print(S)
47 nb, rowperm, colperm, rowblock, colblock, coarse_rowblock, coarse_colblock = S.sparsity().btf()
48 
49 print("number of blocks: ", nb)
50 print("rowperm: ", rowperm)
51 print("colperm: ", colperm)
52 print("restored:")
53 print(S[rowperm,colperm])
54 print("rowblock: ", rowblock)
55 print("colblock: ", colblock)
56 print("coarse_rowblock: ", coarse_rowblock)
57 print("coarse_colblock: ", coarse_colblock)
58