5
$\begingroup$

I have a Solve similar to the following:

Solve[e^2 - c^2 == -15, {e, c}, Integers]
(* {{e -> -7, c -> -8}, {e -> -7, c -> 8}, {e -> -1, c -> -4}, 
    {e -> -1, c -> 4}, {e -> 1, c -> -4}, {e -> 1, c -> 4}, 
    {e -> 7, c -> -8}, {e -> 7, c -> 8}} *)

I need to add a region constraint to get the solution I want from the unconstrained list of solutions. I tried the following:

Solve[e^2 - c^2 == -15 ∧ {e, c} ∈ Interval[{0, 4}], {e, c}, Integers]
(* {{e -> {1}, c -> {4}}} *)

However, when I do this it wraps the variable's solutions in List. Is there a way to turn this off so I just get {{e -> 1, c -> 4}} or {e -> 1, c -> 4} as the result? The current result is a pain as I have to massage it for use with Replace. Also, can any explain why it is doing this when I constrain the variables?

$\endgroup$
8
  • $\begingroup$ Whether constrained or not, Solve returns a list of solutions (and this is quite clear from the refguide page). To do otherwise would be a huge incompatibility in form of result. Extracting the first solution, say, is readily done with First or Part. Again, all well documented. $\endgroup$ Commented Nov 18, 2015 at 21:40
  • $\begingroup$ @DanielLichtblau You've obviously not read the question. $\endgroup$
    – Edmund
    Commented Nov 18, 2015 at 22:05
  • 1
    $\begingroup$ Actually I read it twice. But it did not improve with the second go round. $\endgroup$ Commented Nov 18, 2015 at 22:33
  • $\begingroup$ e.g. the constrained one gives e -> {1} instead of e -> 1 as the unconstrained one gives. $\endgroup$
    – Edmund
    Commented Nov 18, 2015 at 22:35
  • $\begingroup$ Ah. I should have read it a third time. Not sure what is going on here, it might be a bug. $\endgroup$ Commented Nov 18, 2015 at 22:41

3 Answers 3

8
$\begingroup$

Interval is a 1D region, so Element[e, Interval[...]] makes e a 1D vector not a scalar. If you want e to be a scalar use Element[{e}, Interval[...]].

In[1]:= Solve[e^2 - c^2 == -15 && Element[{e}|{c}, Interval[{0, 4}]], {e, c}, Integers]

Out[1]= {{e -> 1, c -> 4}}

Compare to:

In[2]:= Solve[Element[e, Disk[]], e, Integers]

Out[2]= {{e -> {-1, 0}}, {e -> {0, -1}}, {e -> {0, 0}}, {e -> {0, 1}},
>    {e -> {1, 0}}}

In[3]:= Solve[Element[{x, y}, Disk[]], {x, y}, Integers]

Out[3]= {{x -> -1, y -> 0}, {x -> 0, y -> -1}, {x -> 0, y -> 0},
>    {x -> 0, y -> 1}, {x -> 1, y -> 0}}
$\endgroup$
1
  • $\begingroup$ This was my first thought. Thanks for the confirmation. $\endgroup$
    – Michael E2
    Commented Nov 18, 2015 at 23:31
2
$\begingroup$

The Interval seems to be the problem, it returns an "Interval Object" and, rather than figure out what that is, just use the <= operator to state the conditions explicitly

Solve[
 e^2 - c^2 == -15 && 0 <= e <= 4 && 0 <= c <= 4, {e, c}, Integers]
(* {{e -> 1, c -> 4}} *)

or

Solve[{e^2 - c^2 == -15, 0 <= e <= 4, 0 <= c <= 4}, {e, 
  c}, Integers]
(* {{e -> 1, c -> 4}} *)
$\endgroup$
1
$\begingroup$

The unwanted List also can be replaced after completing Solve.

Solve[e^2 - c^2 == -15 ∧ {e, c} ∈ Interval[{0, 4}], {e, c}, Integers] 
    /. Rule[z1_, {z2_}] -> Rule[z1, z2]

or

Replace[Solve[e^2 - c^2 == -15 ∧ {e, c} ∈ Interval[{0, 4}], {e, c}, Integers], 
    List[z_] -> z, -1]

(* {{e -> 1, c -> 4}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.