4
$\begingroup$

I want to put the solutions of the equation $y = (x + 3)/(x - 3)$, where $x$, $y$ are integers, into the form $(x,y)$. For example, $(2, -5)$ instead of $\{2,-5\}$. When I evaluated

{x, y} /. Solve[y == (x + 3)/(x - 3), {x, y}, Integers]

I got

{{-3, 0}, {0, -1}, {1, -2}, {2, -5}, {4, 7}, {5, 4}, {6, 3}, {9, 2}}

How do I get $(-3,0),\,(0,-1),\,\ldots$?

$\endgroup$
2
  • 2
    $\begingroup$ Can you please explain why you'd want that? Is it purely to display the list "nicely"? $\endgroup$ Commented Oct 3, 2013 at 8:11
  • 1
    $\begingroup$ In my country, the pair of solution must be formated like that. After Mathematica out put, I use TeXform to convert them. $\endgroup$ Commented Oct 3, 2013 at 8:50

4 Answers 4

7
$\begingroup$

I assume you know (1,2) will result in an error while interpreting by FE and that form you need is for output/presentation purposes:

StringForm["(``,``)", ##] & @@@ {{-3, 0}, {0, -1}, {1, -2}, {2, -5}, {4, 7}, {5, 4}, 
                                 {6, 3}, {9, 2}}

{(-3,0),(0,-1),(1,-2),(2,-5),(4,7),(5,4),(6,3),(9,2)}

$\endgroup$
5
$\begingroup$
StringReplace[#, {"{" -> "(", 
    "}" -> ")"}] & /@ (ToString /@ {{-3, 
     0}, {0, -1}, {1, -2}, {2, -5}, {4, 7}, {5, 4}, {6, 3}, {9, 2}})
$\endgroup$
2
  • $\begingroup$ +1, notice that StringReplace automatically threads over list so you do not have to Map with it. $\endgroup$
    – Kuba
    Commented Oct 3, 2013 at 12:25
  • $\begingroup$ @Kuba thank you, should have known but always nice to learn $\endgroup$
    – ubpdqn
    Commented Oct 3, 2013 at 12:27
4
$\begingroup$

You could use Row to predefine output format and again to display it as you want with ", " as the separator between Row elements

Row[Row@{"(", x, ", ", y, ")"} /.Solve[y == (x + 3)/(x - 3), {x, y}, Integers], ", "]

(-3, 0), (0, -1), (1, -2), (2, -5), (4, 7), (5, 4), (6, 3), (9, 2)
$\endgroup$
3
$\begingroup$

You may only obtain it if you agree to have the strings of the desired form, since otherwise the "(" and ")" parentheses are reserved for a different purpose. Try this:

This is your list of solutions

 lst = {x, y} /. Solve[y == (x + 3)/(x - 3), {x, y}, Integers]

(*
{{-3, 0}, {0, -1}, {1, -2}, {2, -5}, {4, 7}, {5, 4}, {6, 3}, {9, 2}} *)

and this makes the string of solution:

Map["(" <> ToString[#[[1]]] <> "," <> ToString[#[[2]]] <> ")" &, lst]

{"(-3,0)", "(0,-1)", "(1,-2)", "(2,-5)", "(4,7)", "(5,4)", "(6,3)", \
"(9,2)"}
$\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.