def solution_details(augmented_matrix, vars=None):
num_coeff_cols = augmented_matrix.subdivisions()[1][0]
if not num_coeff_cols > 0:
raise ValueError("Subdivided augmented matrix required.")
except (AttributeError, IndexError):
raise ValueError("Subdivided augmented matrix required.")
pivots = augmented_matrix.pivots()
const_col = num_coeff_cols + 1
print("Matrix and RREF:")
u = [augmented_matrix, augmented_matrix.rref()]
show(augmented_matrix, augmented_matrix.rref())
is_zero = all(component == 0 for component in augmented_matrix[:, -1])
print('Matrix is homogeneous, must be consistent (always at least one soln - the 0 vector).')
print('Matrix is not homogeneous - can be inconsistent.')
if (const_col - 1) in pivots:
print('No Solution (Inconsistent - const col has pivot)')
if len(pivots) == num_coeff_cols:
print("Unique Solution (Consistent, pivot position in each col)")
elif len(pivots) < num_coeff_cols:
print('Infinitely Many Solutions (Consistent, >= 1 coeff col with no pivots)')
solution, X, X_pivots, X_free, param_sol_dict = my_solve(augmented_matrix, vars)
print("Pivots (leading) variables: ", X_pivots)
print("Free variables: ", X_free)
solution = reduce(operator.concat, solution)
[print(f' {s}') for s in solution if len(solution)]
print("Parametized solution vectors")
print("(particular + unrestricted combination)")
for key, value in param_sol_dict.items():
print(f"{key}: {str(value[0]).rjust(10)} {' '.join(str(v).rjust(10) for v in value[1])}")
def my_solve(augmented_matrix, vars=None):
A = augmented_matrix[:, :-1]
Y = augmented_matrix[:, -1]
raise RuntimeError("The matrices have different numbers of rows")
if vars and len(vars) != n:
raise RuntimeError(f"Provided variables '{vars}' != number of columns '{n}'")
X = vector([var(vars[i]) for i in range(n)])
X = vector([var(f"x_{i}") for i in range(n)])
X_pivots = vector([var(X[i]) for i in range(n) if i in A.pivots()])
X_free = vector([var(X[i]) for i in range(n) if i not in A.pivots()])
system = [A[i] * X == Y[i, j] for i in range(m)]
sol = solve(system, *X_pivots)
coefficients = [s.rhs().coefficient(var) for var in X_free]
constant_term = s.rhs() - sum(coeff * var for coeff, var in zip(coefficients, X_free))
coeff_var_pairs = [(coeff, var) for coeff, var in zip(coefficients, X_free)]
coeff_var_strings = [f"{coeff}{var}" for coeff, var in coeff_var_pairs]
param_sol_dict[str(s.lhs())] = [constant_term, coeff_var_strings]
param_sol_dict[str(free_var)] = [0, [f'1{var}' if var == free_var else f'0{var}' for var in X_free]]
return sols, X, X_pivots, X_free, param_sol_dict