]> gitweb.michael.orlitzky.com - dunshire.git/blob - matrices.py
Add a norm function to the matrices module.
[dunshire.git] / matrices.py
1 from cvxopt import matrix, spmatrix
2 from math import sqrt
3
4 def append_col(A,b):
5 """
6 Append the column ``b`` to the right side of the matrix ``A``.
7 """
8 return matrix([A.trans(),b.trans()]).trans()
9
10 def append_row(A,b):
11 """
12 Append the row ``b`` to the bottom of the matrix ``A``.
13 """
14 return matrix([A,b])
15
16 def identity(n):
17 """
18 Return the ``n``-by-``n`` identity matrix.
19 """
20 return spmatrix(1,range(n),range(n))
21
22 def norm(x):
23 """
24 Return the Euclidean norm of the given vector.
25 """
26 return sqrt(sum([z**2 for z in x]))