Helper function to normalize matrices and vectors.
(matrix_str: str)
| 182 | return entry |
| 183 | |
| 184 | def normalize_matrix(matrix_str: str) -> str: |
| 185 | """Helper function to normalize matrices and vectors.""" |
| 186 | logger.debug(f"Normalizing matrix input: {repr(matrix_str)}") |
| 187 | try: |
| 188 | # Remove all whitespace |
| 189 | matrix_str = ''.join(matrix_str.split()) |
| 190 | |
| 191 | # Extract the matrix content |
| 192 | match = re.match(r'^\\begin\{pmatrix\}(.*?)\\end\{pmatrix\}$', matrix_str) |
| 193 | if not match: |
| 194 | return matrix_str |
| 195 | |
| 196 | content = match.group(1) |
| 197 | rows = content.split('\\\\') |
| 198 | |
| 199 | # Normalize each entry in each row |
| 200 | normalized_rows = [] |
| 201 | for row in rows: |
| 202 | if '&' in row: |
| 203 | entries = [normalize_matrix_entry(entry) for entry in row.split('&')] |
| 204 | else: |
| 205 | entries = [normalize_matrix_entry(row)] |
| 206 | normalized_rows.append('&'.join(entries)) |
| 207 | |
| 208 | # Reconstruct the matrix |
| 209 | result = "\\begin{pmatrix}" + "\\\\".join(normalized_rows) + "\\end{pmatrix}" |
| 210 | logger.debug(f"Normalized matrix result: {repr(result)}") |
| 211 | return result |
| 212 | |
| 213 | except Exception as e: |
| 214 | logger.debug(f"Failed to normalize matrix: {str(e)}") |
| 215 | return matrix_str |
| 216 | |
| 217 | def normalize_algebraic_expression(expr: str) -> str: |
| 218 | """Helper function to normalize algebraic expressions.""" |
no test coverage detected