Ask Your Question
1

Manipulate symbolic vector expressions?

asked 2025-05-01 02:07:55 +0200

monkeyboy gravatar image

Is there a way to manipulate vector symbols instead of instances of vectors...for example u, v, w are vector objects and the vector triple product expanded into u x v x w = (u.w)v - (u.v)w, instead of working with u = [u1, u2, u3] etc and having the calculations done term by term?

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2025-05-13 10:21:54 +0200

Emmanuel Charpentier gravatar image

Do you mean this :

sage: reset()
sage: Z=var("z",n=9)
sage: u=vector(Z[:3]) ; u
(z0, z1, z2)
sage: v=vector(Z[3:6]) ; v
(z3, z4, z5)
sage: w=vector(Z[6:]) ; w
(z6, z7, z8)
sage: (u.dot_product(w))*v - (u.dot_product(v))*w
((z0*z6 + z1*z7 + z2*z8)*z3 - (z0*z3 + z1*z4 + z2*z5)*z6, (z0*z6 + z1*z7 + z2*z8)*z4 - (z0*z3 + z1*z4 + z2*z5)*z7, (z0*z6 + z1*z7 + z2*z8)*z5 - (z0*z3 + z1*z4 + z2*z5)*z8)

$$\left({\left(z_{0} z_{6} + z_{1} z_{7} + z_{2} z_{8}\right)} z_{3} - {\left(z_{0} z_{3} + z_{1} z_{4} + z_{2} z_{5}\right)} z_{6},\,{\left(z_{0} z_{6} + z_{1} z_{7} + z_{2} z_{8}\right)} z_{4} - {\left(z_{0} z_{3} + z_{1} z_{4} + z_{2} z_{5}\right)} z_{7},\,{\left(z_{0} z_{6} + z_{1} z_{7} + z_{2} z_{8}\right)} z_{5} - {\left(z_{0} z_{3} + z_{1} z_{4} + z_{2} z_{5}\right)} z_{8}\right)$$

Note that you have to use the dot_product method, the . alone denoting the use of a method or component of its left "operand) ; similarly, Sage does not allow "implicit multiplication" and forces you to use *. Lfe is hard...

@slelievre's suggestions for function implementation are good ; they might be refined (type checking, error reporting, etc...).

HTH,

edit flag offensive delete link more
0

answered 2025-05-02 13:17:18 +0200

slelievre gravatar image

updated 2025-05-02 13:26:28 +0200

The triple product is not present in Sage, as far as I know.

But it takes one line using cross-product and dot-product, or a determinant.

Defining a triple_product function is also possible.

On the other hand, u * v * w will return $(u \cdot v) w$.

This is because the first multiplication (of two vectors u and v) is interpreted as a dot-product, and the second multiplication (of a scalar and a vector) is then a scalar multiplication.

Triple product using a cross-product and a dot-product:

sage: u = vector((1, 0, 0))
sage: v = vector((0, 1, 0))
sage: w = vector((0, 0, 1))
sage: u.cross_product(v)*w
1

Triple product using a determinant:

sage: matrix([u, v, w]).det()

Function using cross-product and dot-product:

def triple_product(u, v, w):
    r"""
    Return the triple product of these three vectors.
    """
    return u.cross_product(v)*w

Function using determinant:

def triple_product(u, v, w):
    r"""
    Return the triple product of these three vectors.
    """
    return matrix([u, v, w]).det()

Then with either function:

sage: triple_product(u, v, w)
1

If you need it often, you can add the function to your init.sage file.

That makes it available to you in each of your Sage sessions.

EDIT. Sorry, the above does not answer the question.

There is no way to compute with vector symbols in Sage.

Sage's symbolic expressions assumes variables are complex variables.

One can add assumptions that they are real, integer, positive, etc, but that's it.

Maybe Cadabra can do that.

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: 2025-05-01 02:07:55 +0200

Seen: 182 times

Last updated: May 13