# 3D line from equations to parametric

In 3D space, given a line defined as the solution of two equations (two planes intersection) like in:

sage: x,y,z=var('x y z')
sage: eqns = [x + y + 2*z - 25 == 0,  -x + y - 25 == 0]


how to obtain the direction vector and one (any) line point (parametric form) ?

This solution from solve:

sage: solve( eqns, [x,y,z] )
[[x == -r13, y == -r13 + 25, z == r13]]


has an answer in parametric form, but with parameter "r13" that has a name unpredictable and not usable in next steps.

This solution from solve:

sage: solve( eqns, [y,z] )
[[y == x + 25, z == -x]]


solves the issues of the previous, but it has been assumed that "x" is a valid parameter for the line ( something not true, by example, in case of vertical line: [ x==10 , y==2 ] )

The target is, by example, to obtain a parametric expression of any line that after can be used in a call to "parametric_plot3d".

edit retag close merge delete

Sort by » oldest newest most voted You don't need the name of the parameter in order to use parametric_plot3d:

sage: sol = solve(eqns, [x,y,z])
sage: parametric_line = [eqn.rhs() for eqn in sol]; parametric_line
[-r5, -r5 + 25, r5]
sage: parametric_plot3d(parametric_line, (-10, 10))


But still you can get it if you want (it's not "unusable in next steps" as you claimed):

sage: parameters = set(sum([list(eq.variables()) for eq in parametric_line], []))
sage: assert len(parameters) == 1
sage: parameter = list(parameters); parameter
r5

more

Try this :

def param_form(eqns, unkns, params=None):
## Variables involved in the system of equations
L1=reduce(union, [[s for s in E.variables()] for E in eqns], {})
## Solve in a dictionary (eliminates special-case handling of
## unique solutions or unique unknowns...)
Sol=solve(eqns, unkns, solution_dict=True)
## Variables involved in solutions
L2=set(s for s in reduce(union,
[set(Sol.get(k).variables())
for k in Sol.keys()], {}))
## "New" variables introduced by solve
D=L2.difference(L1)
if params is None:
## Default behaviour : declare them globally
var(", ".join([repr(s) for s in D]))
else:
## Peruse an option list of parameter names
## Create local symbolic variables
LD=SR.var(", ".join([repr(s) for s in D]))
## Again a single-variable special case. Grrrr...
if len(LD)==0:
LD=(LD,)
## Substitution dictionnary.
SD=dict(zip(LD, params))
## Too few parameter names ? Declare them globally.
## (We might alternatively raise an error)
RD=set(LD)-set(SD.keys())
if len(RD)>0:
var(", ".join([repr(s) for s in RD]))
## Substitute our params in the solution.
for S in Sol:
for k in S.keys():
S.update({k:S.get(k).subs(SD)})
return Sol


Test that (very quick and rough) :

sage: reset()
sage: x,y,z=var('x y z')
sage: eqns = [x + y + 2*z - 25 == 0,  -x + y - 25 == 0]
sage: param_form(eqns, [x,y,z])
[{z: r16, y: -r16 + 25, x: -r16}]


Test that a symbolic variable r16 has been correctly created globally

sage: r16
r16


We wish to name our parameter name t. We have to create it first:

sage: t=var("t")
sage: param_form(eqns, [x,y,z],[t])
[{z: t, y: -t + 25, x: -t}]


HTH,

more