Ask Your Question
0

3D line from equations to parametric

asked 2019-03-22 19:16:55 +0100

updated 2019-03-23 12:19:21 +0100

Emmanuel Charpentier gravatar image

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 flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
1

answered 2019-03-23 13:18:05 +0100

rburing gravatar image

You don't need the name of the parameter in order to use parametric_plot3d:

sage: sol = solve(eqns, [x,y,z])[0]
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)[0]; parameter
r5
edit flag offensive delete link more
0

answered 2019-03-23 12:16:10 +0100

Emmanuel Charpentier gravatar image

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[0].get(k).variables())
                             for k in Sol[0].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: load('/tmp/sage_shell_modepFOyEq/sage_shell_mode_temp.sage') # Load code above...
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}]

Seems to work as advertised...

HTH,

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2019-03-22 19:16:55 +0100

Seen: 777 times

Last updated: Mar 23 '19