Expanding abstract symbols in terms of their values

21 преглед
Пређи на прву непрочитану поруку

Simon Hirscher

непрочитано,
11. 9. 2014. 09:42:4611.9.14.
– sy...@googlegroups.com
Hi,

I'm working on an extension for sympy.diffgeom. Since expressions
often get very complicated in differential geometry, it sometimes
makes sense to e.g. simply keep a tensor as the indexed symbol it
is, sometimes one inserts its actual components' values (and
tries to simplify further). Put differently, my mathematical
objects (mostly tensors) carry names but also have values and I'd
like to let the user decide when he wants use what and enable him
to replace names by their values later on (without the need for
him to use replace()/subs() because this requires way too much
manual effort).

Basically, this is the analogy of the famous pitfall:

>>> x, y = symbols('x, y')
>>> y = x + 2
>>> x = 2
>>> y
x + 2

whereas I would actually like to provide a way to do exactly
this: Replace the symbol 'x' by its value later on.

(Obviously, this would allow lazy calculation of tensor
components. Hence, the user could build expressions involving
tensors without needing to calculate all of their components
first – after all, he might only need some of them.)

So: Are there any best practices in this regard or is there even
a general function in SymPy to expand/evaluate such abstract
symbols in terms of their (still abstract/non-numerical) values?

I imagine something like this:

>>> R[0, 1, 0, 1]
R_{0101}
>>> R[0, 1, 0, 1].expand() (or .eval()/.doit()/.compute() or something)
<some complicated expression in terms of coordinate functions>

Thank you for your time!

Aaron Meurer

непрочитано,
11. 9. 2014. 11:48:0911.9.14.
– sy...@googlegroups.com
doit is a pretty standard way to implement this pattern. You can make
it work by implementing _eval_doit. expand is also OK (just implement
_eval_expand_hint for whatever hint name makes sense, see the
docstring of expand().

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at http://groups.google.com/group/sympy.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/878ulr1q9u.fsf%40simonhirscher.de.
> For more options, visit https://groups.google.com/d/optout.

F. B.

непрочитано,
11. 9. 2014. 16:58:4011.9.14.
– sy...@googlegroups.com, pub...@simonhirscher.de
We need to make sympy.diffgeom and sympy.tensor.tensor work together. Unfortunately the way is still long.

Basically, sympy.tensor.tensor is meant to represent tensors by a symbol. It currently supports tensor polynomials in the abstract index notation, but I am (slowly) working on expressing every kind of expression and in the future I also plan to add the standard Einstein index notation.

A tensor is represented by a tensor head (say, A) and its indices (say, i0, i1, i2)

A(i0) is a contravariant vector, A(-i0) is a covariant vector, A(i0)*A(-i0) is a contraction.

I added the possibility of injecting components data by a command such as A(i0).data = [E, px, py, pz], unfortunately that module does not handle the change of coordinates nor binds that components to a particular basis. That should eventually be rewritten.

If you are interested to contribute, feel free to join!
Одговори свима
Одговори аутору
Проследи
0 нових порука