Solve the equation `a x = b` for `x`, assuming a is a triangular matrix. Parameters ---------- a : (M, M) array_like A triangular matrix b : (M,) or (M, N) array_like Right-hand side matrix in `a x = b` lower : bool, optional Use only data contained
(a, b, lower=False)
| 1107 | |
| 1108 | |
| 1109 | def solve_triangular(a, b, lower=False): |
| 1110 | """ |
| 1111 | Solve the equation `a x = b` for `x`, assuming a is a triangular matrix. |
| 1112 | |
| 1113 | Parameters |
| 1114 | ---------- |
| 1115 | a : (M, M) array_like |
| 1116 | A triangular matrix |
| 1117 | b : (M,) or (M, N) array_like |
| 1118 | Right-hand side matrix in `a x = b` |
| 1119 | lower : bool, optional |
| 1120 | Use only data contained in the lower triangle of `a`. |
| 1121 | Default is to use upper triangle. |
| 1122 | |
| 1123 | Returns |
| 1124 | ------- |
| 1125 | x : (M,) or (M, N) array |
| 1126 | Solution to the system `a x = b`. Shape of return matches `b`. |
| 1127 | """ |
| 1128 | |
| 1129 | if a.ndim != 2: |
| 1130 | raise ValueError("a must be 2 dimensional") |
| 1131 | if b.ndim <= 2: |
| 1132 | if a.shape[1] != b.shape[0]: |
| 1133 | raise ValueError("a.shape[1] and b.shape[0] must be equal") |
| 1134 | if a.chunks[1] != b.chunks[0]: |
| 1135 | msg = ( |
| 1136 | "a.chunks[1] and b.chunks[0] must be equal. " |
| 1137 | "Use .rechunk method to change the size of chunks." |
| 1138 | ) |
| 1139 | raise ValueError(msg) |
| 1140 | else: |
| 1141 | raise ValueError("b must be 1 or 2 dimensional") |
| 1142 | |
| 1143 | vchunks = len(a.chunks[1]) |
| 1144 | hchunks = 1 if b.ndim == 1 else len(b.chunks[1]) |
| 1145 | token = tokenize(a, b, lower) |
| 1146 | name = "solve-triangular-" + token |
| 1147 | |
| 1148 | # for internal calculation |
| 1149 | # (name, i, j, k, l) corresponds to a_ij.dot(b_kl) |
| 1150 | name_mdot = "solve-tri-dot-" + token |
| 1151 | |
| 1152 | def _b_init(i, j): |
| 1153 | if b.ndim == 1: |
| 1154 | return b.name, i |
| 1155 | else: |
| 1156 | return b.name, i, j |
| 1157 | |
| 1158 | def _key(i, j): |
| 1159 | if b.ndim == 1: |
| 1160 | return name, i |
| 1161 | else: |
| 1162 | return name, i, j |
| 1163 | |
| 1164 | dsk = {} |
| 1165 | if lower: |
| 1166 | for i in range(vchunks): |
no test coverage detected