# How to solve equations combined with vector variables with unknown length

I want to solve the following equaitions combined with vector variables and scalar variables. How can I use it in MMA?

\begin{aligned} \mathbf{x}+w\mathbf{a}-\mathbf{v}&=0\\ \mathbf{v}&\ge 0\\ \mathbf{a}^T\mathbf{x}&=b\\ \mathbf{x}&\ge 0\\ \mathbf{v}*\mathbf{x}&=\mathbf{0} \end{aligned}

In which $w$ is scalar and others are vectors. $\mathbf{a},b$ is known and we want to calculate $\mathbf{x,v},w$ in terms of $\mathbf{a},b$. $\mathbf{v}*\mathbf{x}=\mathbf{0}$ means that the product of each element of $x$ and $v$ is 0, i.e. $x_iv_i=0$.

I do know that we can use

x = Array[m, 3]


to declear x as a vector with length 3. I wonder whether we can solve it directly without declear the length? Also, If we have multi-variables, how can we model this problem?

• Your notation is not entirely clear to me. What do $\mathbf{v} \ge 0$ and $\mathbf{x} \ge 0$ conditions mean? Is $\mathbf{a}^T\mathbf{x}$ matrix product? Is $\mathbf{b}$ a vector or a scalar? What do you mean by "multi-variables"? Jun 1 '18 at 15:38
• @jkuczm, I made some modification to make the notation be more clear. $\mathbf{x} \ge 0$ means that each element of $\mathbf{x}$ is greater than 0. Jun 4 '18 at 12:03
• @xzczd Thanks for your edit. Could you please tell me why the system always force me to type latex in code format? How to avoid that? Jun 4 '18 at 12:09
• I guess you've (somewhat unintentionally) select the $\LaTeX$ code and press Ctrl+K? $\LaTeX$ code should not start with 4 spaces. Jun 4 '18 at 12:13
• @xzczd I just click edit and then change nothing it will also say that I contain code. Where can I solve this problem? Jun 4 '18 at 13:21

You can't get solutions with only vector expressions, since there are individual conditions on vector components.

Edit: Update after major changes of equations by OP.

General expression for equations

weqs[i_] :=
With[{x = Array[x, i], v = Array[v, i], a = Array[a, i],
zero = ConstantArray[0, i]}, {Thread[x + w a - v == zero],
a.x == b, Thread[v >= zero], Thread[x >= zero], a.a != 0,

var[i_] := Join[Array[x, i], Array[v, i], {w}]

sol[i_] := Solve[weqs[i], var[i], Reals] // Simplify;

tfsol[i_] :=
sol[i] //.
Or -> Composition[(Column[#, Right,
Background -> {{White, LightGray}}, Frame -> All] &), List]]

tfsol[1]


tfsol[2]


tfsol[3]


Edit 2: Appendix

It turnes out, that the solutions for w are combinations of the a-vector with tuples of {0,1} and b

ww[i_] := -b/Rest[Plus @@ (Array[a, i]*Array[a, i]*#) & /@ Tuples[{0, 1}, i]]

ww[1]

(*   {-(b/a[1]^2)}   *)

ww[3]

(*    {-(b/a[3]^2), -(b/a[2]^2), -(b/(a[2]^2 + a[3]^2)), -(b/a[1]^2),
-(b/(a[1]^2 + a[3]^2)), -(b/(a[1]^2 + a[2]^2)),
-(b/(a[1]^2 + a[2]^2 + a[3]^2))}   *)


You can use that knowlewdge to get x and v more easily for higher dimensions. With this changed code

weqs[i_] :=
With[{x = Array[x, i], v = Array[v, i], a = Array[a, i],
zero = ConstantArray[0, i]}, {Thread[x + w a - v == zero],

ww[i_] := -b/
Rest[Plus @@ (Array[a, i]*Array[a, i]*#) & /@ Tuples[{0, 1}, i]]

var[i_] := Join[Array[x, i], Array[v, i]]

sol[i_] := {w -> #,
Solve[weqs[i] /. w -> #, var[i], Reals] // Simplify} & /@ ww[i];

tfsol[i_] :=
sol[i] //.
Or -> Composition[(Column[#, Right,
Background -> {{White, LightGray}}, Frame -> All] &), List]]

tfsol[4]


or try to find also algorithms for x and v.

• Dear @Akku14 , Thanks very much for your detailed answer. But there seems some confusion due to different understanding of the notations. I made some changes of the notation to make it more clear. The main issue is that b is a scalar (sorry to make it unclear. so, Thread[ax == b] should be a.x == b). Also Thread[vx >= zero] should to be v*x == zero. w is unknown and should be calculate. I made some change of your code but seems something wrong, Could you please help to run this code again? Thanks Jun 4 '18 at 11:53

I don't know any way to solve this problem completely automatically in Mathematica, but it can definitely help in semi-automatic solution.

Let's start with two vector equations since they can be easily written as equation for arbitrary vector component:

\begin{aligned} \mathbf{x}_i + w \, \mathbf{a}_i - \mathbf{v}_i&=0\\ \mathbf{v}_i \, \mathbf{x}_i&=0 \end{aligned}

In Mathematica we can solve those equations using:

cond1 = Reduce[x@i + w a@i - v@i == 0 && v@i x@i == 0, {x@i, v@i}]
(* (x[i] == 0 && v[i] == w a[i]) || (x[i] == -w a[i] && v[i] == 0) *)


Now we focus on equation $\sum_i \mathbf{a}_i \mathbf{x}_i = b$. Since, from above solution, we know that $\mathbf{x}_i$ is either zero or $-w \, \mathbf{a}_i$ we can insert it and sum only over non-zero $\mathbf{x}_i$:

\begin{aligned} -w \, \sum_{i:\mathbf{x}_i \ne 0} \mathbf{a}_i^2 &= b \end{aligned}

Let's define $A = \sum_{i:\mathbf{x}_i \ne 0} \mathbf{a}_i^2$. $A$ is zero when all $\mathbf{x}_i$ are zero and, since it's sum of squares, it's positive otherwise. We also know that if $w$ is zero, then all $\mathbf{x}_i$ are zero, so $A$ is zero.

Now our equation is: $-w \, A = b$. Let's add it to cond1 with all other conditions:

cond2 = Reduce[
cond1 &&
x@i >= 0 && v@i >= 0 && -w A == b &&
(A > 0 || A == 0 && x@i == 0) && (w == 0 \[Implies] A == 0),
{w, x@i, v@i},
Reals
]


To make result more readable let's expand it, sort and present alternatives as a table:

order = PositionIndex@{b, A, w, a@i, x@i, v@i};
Sort[SortBy[order@First@# &] /@ List @@ LogicalExpand@cond2] // TableForm
(* b == 0 && A == 0           && a[i] == 0 && x[i] == 0 && v[i] == 0
b == 0 && A == 0 && w >= 0 && a[i] > 0  && x[i] == 0 && v[i] == w a[i]
b == 0 && A == 0 && w <= 0 && a[i] < 0  && x[i] == 0 && v[i] == w a[i]

b > 0 && A > 0 && w == -(b/A) && a[i] == 0 && x[i] == 0 && v[i] == 0
b > 0 && A > 0 && w == -(b/A) && a[i] > 0 && x[i] == -w a[i] && v[i] == 0
b > 0 && A > 0 && w == -(b/A) && a[i] < 0 && x[i] == 0 && v[i] == w a[i]

b < 0 && A > 0 && w == -(b/A) && a[i] == 0 && x[i] == 0 && v[i] == 0
b < 0 && A > 0 && w == -(b/A) && a[i] > 0 && x[i] == 0 && v[i] == w a[i]
b < 0 && A > 0 && w == -(b/A) && a[i] < 0 && x[i] == -w a[i] && v[i] == 0 *)


Now we can assemble final result remembering that conditions on b, A, and w are "global" and conditions on a[i], x[i], and v[i] apply to specific components, so different lines can be true for different components of vectors in one solution as long as conditions on b, A, and w are compatible.

1. $b = 0$ (first three lines of our solution).

From first line we get: \begin{aligned} \mathbf{a} = \mathbf{0} &\implies w \in \mathbb{R} \land \mathbf{x} = \mathbf{v} = \mathbf{0} \end{aligned}

First and second line gives: \begin{aligned} \mathbf{a} \ge \mathbf{0} &\implies w \ge 0 \land \mathbf{x} = \mathbf{0} \land \mathbf{v} = w \, \mathbf{a} \end{aligned}

First and third line gives: \begin{aligned} \mathbf{a} \le \mathbf{0} &\implies w \le 0 \land \mathbf{x} = \mathbf{0} \land \mathbf{v} = w \, \mathbf{a} \end{aligned}

Second and third line gives: \begin{aligned} \left(\exists_{i,j} \, \mathbf{a}_i > 0 \land \mathbf{a}_j < 0\right) &\implies w = 0 \land \mathbf{x} = \mathbf{v} = \mathbf{0} \end{aligned}

2. $b > 0$ (lines 4-6)

Fourth and sixth line gives (since all $\mathbf{x}_i$ are zero, thus $A$ is zero, which contradicts $A > 0$ condition): \begin{aligned} \mathbf{a} \le \mathbf{0} &\implies \text{no solutions} \end{aligned}

Any combination with fifth line: \begin{aligned} \left(\exists_i \, \mathbf{a}_i > 0\right) &\implies w = \frac{-b}{\sum_{i : \mathbf{a}_i > 0} \mathbf{a}_i^2} \land \mathbf{x}_i = \begin{cases} -w \, \mathbf{a}_i &: \mathbf{a}_i > 0 \\ 0 &: \mathbf{a}_i \le 0\end{cases} \land \mathbf{v}_i = \begin{cases} 0 &: \mathbf{a}_i \ge 0 \\ w \, \mathbf{a}_i &: \mathbf{a}_i < 0\end{cases} \end{aligned}

3. $b < 0$ (lines 7-9)

Seventh and eighth line gives (since all $\mathbf{x}_i$ are zero, thus $A$ is zero, which contradicts $A > 0$ condition): \begin{aligned} \mathbf{a} \ge \mathbf{0} &\implies \text{no solutions} \end{aligned}

Any combination with nineth line: \begin{aligned} \left(\exists_i \, \mathbf{a}_i < 0\right) &\implies w = \frac{-b}{\sum_{i : \mathbf{a}_i < 0} \mathbf{a}_i^2} \land \mathbf{x}_i = \begin{cases} 0 &: \mathbf{a}_i \ge 0 \\ -w \, \mathbf{a}_i &: \mathbf{a}_i < 0\end{cases} \land \mathbf{v}_i = \begin{cases} w \, \mathbf{a}_i &: \mathbf{a}_i > 0 \\ 0 &: \mathbf{a}_i \le 0 \end{cases} \end{aligned}