# Symbolically solving system of linear equations

**URL:** <https://discourse.julialang.org/t/symbolically-solving-system-of-linear-equations/101397>\
**Category:** New to Julia\
**Tags:** symbolics\
**Created:** [July 9, 2023, 1:44pm UTC](https://discourse.julialang.org/t/symbolically-solving-system-of-linear-equations/101397 "2023-07-09T13:44:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![maliesen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maliesen/32/51389_2.png) [@maliesen](https://discourse.julialang.org/u/maliesen)\
**Post date:** [July 9, 2023, 1:44pm UTC](https://discourse.julialang.org/t/symbolically-solving-system-of-linear-equations/101397/1 "2023-07-09T13:44:28Z")

</div>

Hello,

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

```julia
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

```julia
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?

---

<div class="post-metadata">

**Author:** ![MarcBerliner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marcberliner/32/14883_2.png) [@MarcBerliner](https://discourse.julialang.org/u/MarcBerliner)\
**Post date:** [July 9, 2023, 2:29pm UTC](https://discourse.julialang.org/t/symbolically-solving-system-of-linear-equations/101397/2 "2023-07-09T14:29:51Z")

</div>

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.

```julia
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

```julia
simplified = simplify.(solution .* det(A))

```

---

<div class="post-metadata">

**Author:** ![maliesen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maliesen/32/51389_2.png) [@maliesen](https://discourse.julialang.org/u/maliesen)\
**Post date:** [July 9, 2023, 11:10pm UTC](https://discourse.julialang.org/t/symbolically-solving-system-of-linear-equations/101397/3 "2023-07-09T23:10:20Z")

</div>

Thanks a lot! 🙂
