| 12945 | def get_bandwidth(self): |
| 12946 | r""" |
| 12947 | Return the bandwidths of the matrix. |
| 12948 | |
| 12949 | INPUT: |
| 12950 | |
| 12951 | - ``self`` - a square matrix. |
| 12952 | |
| 12953 | OUTPUT: |
| 12954 | |
| 12955 | A tuple ``(p, q)`` where ``p`` is the lower bandwidth and ``q`` is the |
| 12956 | upper bandwidth. |
| 12957 | |
| 12958 | EXAMPLES:: |
| 12959 | |
| 12960 | sage: A = matrix([[2,0],[0,7]]); A |
| 12961 | [2 0] |
| 12962 | [0 7] |
| 12963 | sage: A.get_bandwidth() |
| 12964 | (0, 0) |
| 12965 | sage: A = matrix([[2,0],[3,7]]); A |
| 12966 | [2 0] |
| 12967 | [3 7] |
| 12968 | sage: A.get_bandwidth() |
| 12969 | (1, 0) |
| 12970 | sage: A = matrix([[2,5],[0,7]]); A |
| 12971 | [2 5] |
| 12972 | [0 7] |
| 12973 | sage: A.get_bandwidth() |
| 12974 | (0, 1) |
| 12975 | sage: A = matrix([[2,5],[3,7]]); A |
| 12976 | [2 5] |
| 12977 | [3 7] |
| 12978 | sage: A.get_bandwidth() |
| 12979 | (1, 1) |
| 12980 | |
| 12981 | Two rectangular matrices. :: |
| 12982 | |
| 12983 | sage: B = matrix([[ 0, 1, 2, 3, 4, 0, 0],[ 7, 8, 9,10,11,12, 0],[ 0,15,16,17,18,19,20]]); B |
| 12984 | [ 0 1 2 3 4 0 0] |
| 12985 | [ 7 8 9 10 11 12 0] |
| 12986 | [ 0 15 16 17 18 19 20] |
| 12987 | sage: B.get_bandwidth() |
| 12988 | (1, 4) |
| 12989 | |
| 12990 | sage: C = matrix([[0,1],[2,3],[0,5]]); C |
| 12991 | [0 1] |
| 12992 | [2 3] |
| 12993 | [0 5] |
| 12994 | sage: C.get_bandwidth() |
| 12995 | (1, 1) |
| 12996 | |
| 12997 | Empty matrices behave properly. :: |
| 12998 | |
| 12999 | sage: E = matrix(0, 5, []); E |
| 13000 | [] |
| 13001 | sage: E.get_bandwidth() |
| 13002 | Traceback (most recent call last): |
| 13003 | ... |
| 13004 | ValueError: matrix can't be empty. |
| 13005 | |
| 13006 | AUTHORS: |
| 13007 | |
| 13008 | - Raniere Silva (2012) |
| 13009 | """ |
| 13010 | if not self: |
| 13011 | raise ValueError('matrix can\'t be empty.') |
| 13012 | |
| 13013 | # Compute the lower bandwidth. |
| 13014 | p = self.nrows() - 1 |
| 13015 | band_is_null = True |
| 13016 | while p > 0: |
| 13017 | for a in self.diagonal(-p): |
| 13018 | if a != 0: |
| 13019 | band_is_null = False |
| 13020 | break |
| 13021 | if band_is_null == True: |
| 13022 | p = p - 1 |
| 13023 | else: |
| 13024 | break |
| 13025 | |
| 13026 | # Compute the upper bandwidth. |
| 13027 | q = self.ncols() - 1 |
| 13028 | band_is_null = True |
| 13029 | while q > 0: |
| 13030 | for a in self.diagonal(q): |
| 13031 | if a != 0: |
| 13032 | band_is_null = False |
| 13033 | break |
| 13034 | if band_is_null == True: |
| 13035 | q = q - 1 |
| 13036 | else: |
| 13037 | break |
| 13038 | |
| 13039 | return (p, q) |
| 13040 | |