Symbolically solving system of linear equations

Hello,

I want to solve a system of linear equations, so I wrote:

using Symbolics

@variables a11, a12, a21, a22
@variables x1, x2
@variables b1, b2

solution = Symbolics.solve_for([a11*x1+a12*x2~b1, a21*x1+a22*x2~b2],[x1, x2])
println(solution)
  1. Is it possible to achieve the same with something similar to
using Symbolics

@variables A[1:2, 1:2]
@variables x[1:2]
@variables b[1:2]

solution = Symbolics.solve_for(A*x~b,x)
println(solution)
  1. I want to rewrite the solution as to pull out 1/det(A). For doing so I tried to expand the expression, but solution = simplify(solution, expand=true) did not work. How can the solution expression be expanded?
  1. Add a collect after evaluating @variables with arrays to get them to work like the scalarized version. Additionally, you need to replace ~ with .~ to set the equals signs on the elements of the vectors, not the vectors themselves.
using Symbolics

@variables A[1:2, 1:2]
@variables x[1:2]
@variables b[1:2]

A = collect(A)
x = collect(x)
b = collect(b)

solution = Symbolics.solve_for(A*x .~ b,x)
  1. One way to remove the 1/det(A) term is by multiplying through by det(A) and simplifying each element
simplified = simplify.(solution .* det(A))
3 Likes

Thanks a lot! :slight_smile: