# HG changeset patch
# User Adrien Brochard <aaa.brochard@gmail.com>
# Date 1339530694 14400
# Node ID 34dd132610a81ef3bcc5d968c4e97c4cdd00cd59
# Parent 9ab4ab6e12d0ce97566db069205815303514de4d
Trac 12733: can now compute the norm of a symbolic matrix and fix some doctest
diff --git a/sage/matrix/matrix2.pyx b/sage/matrix/matrix2.pyx
a
|
b
|
|
10284 | 10284 | 0.0 |
10285 | 10285 | sage: a.norm(Infinity) == a.norm(1) |
10286 | 10286 | True |
| 10287 | |
| 10288 | Norms of symbolic matrices can also be calculated, except for the norm p=2. :: |
| 10289 | |
| 10290 | sage: var('a, b, c, d') |
| 10291 | (a, b, c, d) |
| 10292 | sage: m = matrix(SR, [[a, b], [c, d]]) |
| 10293 | sage: m |
| 10294 | [a b] |
| 10295 | [c d] |
| 10296 | sage: m.norm(1) |
| 10297 | a + c |
| 10298 | sage: m.norm(infinity) |
| 10299 | a + b |
| 10300 | sage: m.norm('frob') |
| 10301 | sqrt(a^2 + b^2 + c^2 + d^2) |
10287 | 10302 | """ |
10288 | 10303 | |
| 10304 | from sage.symbolic.all import SR |
| 10305 | |
10289 | 10306 | if self._nrows == 0 or self._ncols == 0: |
10290 | 10307 | return RDF(0) |
10291 | 10308 | |
10292 | 10309 | # 2-norm: |
10293 | 10310 | if p == 2: |
| 10311 | if self.base_ring() == SR: |
| 10312 | raise NotImplementedError |
10294 | 10313 | A = self.change_ring(CDF) |
10295 | 10314 | A = A.conjugate().transpose() * A |
10296 | 10315 | U, S, V = A.SVD() |
10297 | 10316 | return max(S.list()).real().sqrt() |
10298 | 10317 | |
10299 | | A = self.apply_map(abs).change_ring(RDF) |
| 10318 | if self.base_ring() != SR: |
| 10319 | A = self.apply_map(abs).change_ring(RDF) |
| 10320 | else: |
| 10321 | A = self |
10300 | 10322 | |
10301 | 10323 | # 1-norm: largest column-sum |
10302 | | if p == 1: |
| 10324 | if p == 1: |
10303 | 10325 | A = A.transpose() |
10304 | 10326 | return max([sum(i) for i in list(A)]) |
10305 | 10327 | |
diff --git a/sage/misc/functional.py b/sage/misc/functional.py
a
|
b
|
|
1136 | 1136 | The norm of matrices:: |
1137 | 1137 | |
1138 | 1138 | sage: z = 1 + 2*I |
1139 | | sage: norm(matrix([[z]])) |
| 1139 | sage: matrix(CC, [[z]]).norm() |
1140 | 1140 | 2.2360679775 |
1141 | 1141 | sage: M = matrix(ZZ, [[1,2,4,3], [-1,0,3,-10]]) |
1142 | 1142 | sage: norm(M) |