1 """ Provides matrix utility functions.
4 # Author: Pearu Peterson
8 __docformat__
= "restructuredtext"
9 __all__
= ['eye', 'concatenate', 'jacobian']
11 from ..utils
import MATRIX
, MATRIX_DICT
12 from .algebra
import MatrixDict
, Matrix
, MatrixBase
14 def jacobian(expr_list
, var_list
):
15 """ Return a jacobian matrix of functions in expr_list with
16 respect to variables in var_list.
18 m
, n
= len(expr_list
), len(var_list
)
20 for i
, e
in enumerate(expr_list
):
21 for j
, v
in enumerate(var_list
):
25 def eye(m
, n
=None, k
=0):
26 """ Return n x m matrix where the k-th diagonal is all ones,
27 everything else is zeros.
33 for i
in range(min(m
,n
)+k
):
36 for i
in range(min(m
,n
)-k
):
38 return MatrixDict(MATRIX(m
, n
, MATRIX_DICT
), d
)
40 def concatenate(*args
, **kws
):
41 """ Join matrices together.
43 concatenate(m1, m2, ..) - join matrices m1, m2, .. together along rows
44 concatenate(m1, m2, .., axis=1) - join matrices m1, m2, .. together along columns
45 concatenate(m1, m2, .., diagonal=True) - join matrices m1, m2, .. together along diagonal
48 assert set(kws
).issubset(['axis','diagonal']) and len(kws
)<=1,`kws`
49 axis
= kws
.get('axis',0)
50 diagonal
= kws
.get('diagonal',False)
55 head
, data
= Matrix(a
)[:].pair
56 assert not (head
.is_transpose
or head
.is_diagonal
)
58 for (i
,j
),x
in data
.items():
64 head
, data
= Matrix(a
)[:].pair
65 assert not (head
.is_transpose
or head
.is_diagonal
)
67 for (i
,j
),x
in data
.items():
68 d
[rows
+ i
, cols
+ j
] = x
73 head
, data
= Matrix(a
)[:].pair
74 assert not (head
.is_transpose
or head
.is_diagonal
)
76 for (i
,j
),x
in data
.items():
80 return MatrixDict(MATRIX(rows
, cols
, MATRIX_DICT
), d
)