# Symbolics.jl problem trying to use solve\_for

**URL:** https://discourse.julialang.org/t/symbolics-jl-problem-trying-to-use-solve-for/105319
**Category:** General Usage
**Created:** [October 23, 2023, 3:54pm UTC](https://discourse.julialang.org/t/symbolics-jl-problem-trying-to-use-solve-for/105319 "2023-10-23T15:54:23Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)
#### Post date: [October 23, 2023, 3:54pm UTC](https://discourse.julialang.org/t/symbolics-jl-problem-trying-to-use-solve-for/105319/1 "2023-10-23T15:54:23Z")

</div>

I’m learning how to use Symbolcs.jl.

My current work involves lines and vectors in three dimensional space.

At the suggestion of, I think Shash, at a CAJUN about a month ago, since the Symbolics variables as introduced by `@variables` are scalars, I’ve defined

```julia
"""
    symbolic_vector(name::Symbol, n::Integer)

Define `n` Symbolics variables hose names are based on `name` and
return a vector of those variables.
"""
function symbolic_vector(name::Symbol, n::Integer)
    eltname(name, i) = Symbol("($name)_$i")
    map(variable, map(i -> eltname(name, i), 1:n))
end

```

to define “vector variables”, and

```julia
"""
    vsubs(v::Vector{Symbolics.Num}, values...)

Return a list of `Pair`s suitable for constructing a `Dict` which is
suitable for passing to `Symbolics.substitute`, which
substitues each of `values` with the corresponding variable in `v`.
"""
function vsubs(v::Vector{Symbolics.Num}, values...)
	Pair.(v, values)
end

```

to swubstitute values for them.

I also provide qa cnvenience function for creating the parameteric formula for a line:

```julia
"""
    line(p1, p2, param)

Return the parametric formula for a point on a line which passes through points `p1` and `p2`, and has `param`.
"""
function line(p1, p2, param)
    p1 + param * (p2 - p1)
end

```

As a simple example, I’m trying to find the point at which two copla\nar lines intersect:

```julia
P1 = symbolic_vector(:P1, 3)
P2 = symbolic_vector(:P2, 3)
P3 = symbolic_vector(:P3, 3)
P4 = symbolic_vector(:P4, 3)
@variables r, s
L1 = line(P1, P2, r)
L2 = line(P3, P4, s)
# L1 and L2 intersect at the point where their formulas are equal:
e1 = Equation(L1, L2)

substitutions = OrderedDict(
    # expect r = s = 0.5.
    vsubs(P1, 0, -1, 0)...,
    vsubs(P2, 2, 1, 0)...,
    vsubs(P3, 0, 0, 0)...,
    vsubs(P4, 2, 0, 0)...)

eq1s = substitute(e1, substitutions)

```

from which I can solve in my head to r = s = 0.5.

How do I get `solve_for` to tell me that though?

```julia
solved = solve_for(eq1s, r)

```

but `solved[1]` is

```julia
(1//0)*(2r - 2s)

```

and `solved[2]`

```julia
(1//0)*(-1 + 2r)

```

show division by zero.

What am I doing wrong?

How do I get `solve_for` to give me solutions?

Should I be doing something else instead?

---

<div class="post-metadata">

### Author: ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)
#### Post date: [October 28, 2023, 12:43pm UTC](https://discourse.julialang.org/t/symbolics-jl-problem-trying-to-use-solve-for/105319/2 "2023-10-28T12:43:20Z")

</div>

What is `variable` in

```julia
function symbolic_vector(name::Symbol, n::Integer)
    eltname(name, i) = Symbol("($name)_$i")
    map(variable, map(i -> eltname(name, i), 1:n))
end

```

---

<div class="post-metadata">

### Author: ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)
#### Post date: [October 29, 2023, 2:32pm UTC](https://discourse.julialang.org/t/symbolics-jl-problem-trying-to-use-solve-for/105319/3 "2023-10-29T14:32:05Z")

</div>

`Symbolics.variable`. I must have forgot to include the "using"s. I’ll check for any more later when I’m at my computer.

---

<div class="post-metadata">

### Author: ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)
#### Post date: [October 29, 2023, 4:06pm UTC](https://discourse.julialang.org/t/symbolics-jl-problem-trying-to-use-solve-for/105319/4 "2023-10-29T16:06:03Z")

</div>

Here’s a file that I’ve tested and is complete:

```julia
using DataStructures
using Symbolics
using Symbolics: variable, solve_for, substitute

"""
    symbolic_vector(name::Symbol, n::Integer)

Define `n` Symbolics variables hose names are based on `name` and
return a vector of those variables.
"""
function symbolic_vector(name::Symbol, n::Integer)
    eltname(name, i) = Symbol("($name)_$i")
    map(variable, map(i -> eltname(name, i), 1:n))
end

"""
    vsubs(v::Vector{Symbolics.Num}, values...)

Return a list of `Pair`s suitable for constructing a `Dict` which is
suitable for passing to `Symbolics.substitute`, which
substitues each of `values` with the corresponding variable in `v`.
"""
function vsubs(v::Vector{Symbolics.Num}, values...)
	Pair.(v, values)
end

"""
    line(p1, p2, param)

Return the parametric formula for a point on a line which passes through points `p1` and `p2`, and has `param`.
"""
function line(p1, p2, param)
    p1 + param * (p2 - p1)
end

P1 = symbolic_vector(:P1, 3)
P2 = symbolic_vector(:P2, 3)
P3 = symbolic_vector(:P3, 3)
P4 = symbolic_vector(:P4, 3)
@variables r, s
L1 = line(P1, P2, r)
L2 = line(P3, P4, s)
# L1 and L2 intersect at the point where their formulas are equal:
e1 = Equation(L1, L2)

substitutions = OrderedDict(
    # expect r = s = 0.5.
    vsubs(P1, 0, -1, 0)...,
    vsubs(P2, 2, 1, 0)...,
    vsubs(P3, 0, 0, 0)...,
    vsubs(P4, 2, 0, 0)...)

eq1s = substitute(e1, substitutions)

solved = solve_for(eq1s, r)

println(solved)

```

---

<div class="post-metadata">

### Author: ![Mark\_Nahabedian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mark_nahabedian/32/16562_2.png) [@Mark\_Nahabedian](https://discourse.julialang.org/u/Mark_Nahabedian)
#### Post date: [October 31, 2023, 2:12pm UTC](https://discourse.julialang.org/t/symbolics-jl-problem-trying-to-use-solve-for/105319/5 "2023-10-31T14:12:33Z")

</div>

If I define

```julia
function vector_solve_for(e::Equation, var)
	if e.lhs isa Vector && e.rhs isa Vector
		result = []
		for lr in zip(e.lhs, e.rhs)
			push!(result,
				solve_for(Equation(lr...), var))
		end
		result
	else
		solve_for(e, var)
	end
end

```

I can use it to get a value for `r`:

```julia
vector_solve_for(eq1s, r)
3-element Vector{Any}:
   s
   0.5
 NaN

```

but this doesn’t get me to automated solutions for both `r` and `s`.
