# Inverting a matrix of symbolic equations

**URL:** https://discourse.julialang.org/t/inverting-a-matrix-of-symbolic-equations/79083
**Category:** General Usage
**Tags:** linearalgebra, symbolics
**Created:** [April 6, 2022, 6:03am UTC](https://discourse.julialang.org/t/inverting-a-matrix-of-symbolic-equations/79083 "2022-04-06T06:03:34Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Isaiah\_Warnke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/isaiah_warnke/32/15908_2.png) [@Isaiah\_Warnke](https://discourse.julialang.org/u/Isaiah_Warnke)
#### Post date: [April 6, 2022, 6:03am UTC](https://discourse.julialang.org/t/inverting-a-matrix-of-symbolic-equations/79083/1 "2022-04-06T06:03:34Z")

</div>

I’d like to invert a 4x4 matrix of symbolic equations.

I’m able to successfully invert a matrix of symbolic expressions like this:

```julia
using Symbolics
using LinearAlgebra
@variables x y[1:16] z
Symbolics.scalarize(y)

eq11 = x
eq12 = x^2
eq13 = x+15
eq14 = cos(x)
eq21 = log(x)
eq22 = sqrt(x)
eq23 = x^3
eq24 = -x
eq31 = 1/x
eq32 = (x+4)^3
eq33 = 69*x
eq34 = 42
eq41 = 14/x
eq42 = x^(3/5)
eq43 = sin(x)
eq44 = csc(x)

M = [eq11 eq12 eq13 eq14;
     eq21 eq22 eq23 eq24;
     eq31 eq32 eq33 eq34;
     eq41 eq42 eq43 eq44]

inv(M)

```

but if I change the expressions to equations

```julia
eq11 = y[1] ~ x
eq12 = y[2] ~ x^2
eq13 = y[3] ~ x+15
eq14 = y[4] ~ cos(x)
eq21 = y[5] ~ log(x)
eq22 = y[6] ~ sqrt(x)
eq23 = y[7] ~ x^3
eq24 = y[8] ~ -x
eq31 = y[9] ~ 1/x
eq32 = y[10] ~ (x+4)^3
eq33 = y[11] ~ 69*x
eq34 = y[12] ~ 42
eq41 = y[13] ~ 14/x
eq42 = y[14] ~ x^(3/5)
eq43 = y[15] ~ sin(x)
eq44 = y[16] ~ csc(x)

```

the inversion fails.

Is there a way around this?

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [April 6, 2022, 6:46am UTC](https://discourse.julialang.org/t/inverting-a-matrix-of-symbolic-equations/79083/2 "2022-04-06T06:46:46Z")

</div>

What is the inverse of a single equation like a = b?

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 6, 2022, 8:12am UTC](https://discourse.julialang.org/t/inverting-a-matrix-of-symbolic-equations/79083/3 "2022-04-06T08:12:37Z")

</div>

If `=(a, b)` is a function that maps two numbers to a boolean, then `=^{-1}(c)` should be a function that takes a boolean `c` and returns (the) two numbers for which `=(a, b) = c`. It will not in general be well-defined since the equality operator cannot be bijective by the pidgeonhole principle, but if we settle for _an_ inverse then maybe

```julia
julia> ==⁻¹(c) = (1, Int(c))
==⁻¹ (generic function with 1 method)

julia> ==⁻¹(true)
(1, 1)

julia> ==⁻¹(false)
(1, 0)

```

could be an acceptable implementation.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 6, 2022, 8:43am UTC](https://discourse.julialang.org/t/inverting-a-matrix-of-symbolic-equations/79083/4 "2022-04-06T08:43:50Z")

</div>

Arguably, the inverse of an equation is the tuple (LHS, RHS):

```julia
julia> macro eqinv(ex)
    return esc(:(($(ex.args[2]), $(ex.args[3]))))
end;
julia> a = 2; b = 4;
julia> @eqinv a==b
(2, 4)
julia> ==(@eqinv(a==b)...)
false
julia> ==(@eqinv(a==a)...)
true

```

i.e. the inverted function (macro) “undoes” the original. Can’t imagine this is very useful though.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 6, 2022, 1:10pm UTC](https://discourse.julialang.org/t/inverting-a-matrix-of-symbolic-equations/79083/5 "2022-04-06T13:10:08Z")

</div>

Sure, open an issue. But this is… really weird 😅 . I’m not sure of a use case where you wouldn’t just invert the matrix of expressions instead of equations, so I don’t think it would get prioritized without a clear idea of a use case.

---

<div class="post-metadata">

### Author: ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)
#### Post date: [April 6, 2022, 1:11pm UTC](https://discourse.julialang.org/t/inverting-a-matrix-of-symbolic-equations/79083/6 "2022-04-06T13:11:42Z")

</div>

Yeah I can almost guarantee my solutions are not what op wants.

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [April 6, 2022, 1:14pm UTC](https://discourse.julialang.org/t/inverting-a-matrix-of-symbolic-equations/79083/7 "2022-04-06T13:14:10Z")

</div>

Almost. I’ve seen odder requests.
