1
$\begingroup$

I want to get the graphic of a 3D-straight line:

First:

Clear["Global`*"];
FindInstance[1/4 (-1 + x) == -y == 1/3 (-2 - z), {x, y, z}, Reals, 2]

(*{{x -> 1/2, y -> 1/8, z -> -(13/8)}, {x -> 3/5, y -> 1/10, 
  z -> -(17/10)}}*)

Then the " x-> y-> z->" need to be deleted manually and get two point {1/2, 1/8, -(13/8)}, {3/5, 1/10, -(17/10)}.

Finally:

Graphics3D[InfiniteLine[{{1/2, 1/8, -(13/8)}, {3/5, 1/10, -(17/10)}}],
  PlotRange -> {{-2, 2}, {-2, 2}, {-2, 2}}, Axes -> True, 
 AspectRatio -> 1, AxesOrigin -> {0, 0, 0}, AxesLabel -> {x, y, z}]

How to combine these two pieces of code into one piece? Or, How to automatically get {a,b,c} from {x -> a, y -> b, z -> c}?

$\endgroup$
4
  • 3
    $\begingroup$ You are looking for Values. $\endgroup$
    – MarcoB
    Feb 17, 2022 at 1:45
  • 1
    $\begingroup$ @MarcoB great. This is a 3rd way to do it. May be you can make this an answer also. We now only need 7 more ways to get to 10 :) $\endgroup$
    – Nasser
    Feb 17, 2022 at 1:54
  • $\begingroup$ OK. Thank you. :-) @MarcoB $\endgroup$
    – lotus2019
    Feb 17, 2022 at 2:03
  • $\begingroup$ @Nasser an update: only three to reach your claimed number of alternatives $\endgroup$
    – user49048
    Feb 17, 2022 at 2:51

8 Answers 8

4
$\begingroup$

There are probably many ways to do this. (in Mathematica, the rule of thumb is that there are at least 10 ways to do the same thing :)

But one way could be

Clear["Global`*"];
sol = FindInstance[1/4 (-1 + x) == -y == 1/3 (-2 - z), {x, y, z},  Reals, 2];
pts = sol /. Rule[x_, y_] :> y;
Graphics3D[InfiniteLine[pts], 
 PlotRange -> {{-2, 2}, {-2, 2}, {-2, 2}}, Axes -> True, 
 AspectRatio -> 1, AxesOrigin -> {0, 0, 0}, AxesLabel -> {x, y, z}]

Mathematica graphics

$\endgroup$
1
  • $\begingroup$ Thank you. :-) @Nasser $\endgroup$
    – lotus2019
    Feb 17, 2022 at 2:04
6
$\begingroup$

If you want to get {a,b,c} from {x -> a, y -> b, z -> c} you should use Values:

In[10]:= Values[{x -> a, y -> b, z -> c}]

Out[10]= {a, b, c}

For what it's worth I only read the post title, so there may be some context I'm missing here.

$\endgroup$
4
$\begingroup$
sol = FindInstance[1/4 (-1 + x) == -y == 1/3 (-2 - z), {x, y, z}, 
  Reals, 2];
{x,y,z}/.sol
sol[[;; , ;; , 2]]
$\endgroup$
1
  • $\begingroup$ Thank you. :-) @cvgmt $\endgroup$
    – lotus2019
    Feb 17, 2022 at 2:04
2
$\begingroup$

Another way would be

sol = FindInstance[1/4 (-1 + x) == -y == 1/3 (-2 - z), {x, y, z}, 
  Reals, 2]

sol /. Rule[a_, b_] :> b
$\endgroup$
2
$\begingroup$

I am still a bit jealous of the amazing performance given by @cvgmt the other day, so the following also works:

sol = FindInstance[1/4 (-1 + x) == -y == 1/3 (-2 - z), {x, y, z}, 
  Reals, 2]

Apply[#2 &, sol, {2}]
$\endgroup$
2
$\begingroup$

Another possible way to do it:

{Last @@@ sol[[1]], Last @@@ sol[[2]]}
$\endgroup$
2
$\begingroup$

One more

FoldList[#1 /. #2 &, {x, y, z}, sol][[2 ;; 3]]
$\endgroup$
1
$\begingroup$

Not a surprise that you can use Fold as well in the following manner

Table[Fold[#1 /. #2 &, {x, y, z}, sol[[i]]], {i, 1, 2}]
$\endgroup$

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