sympy / sympy

A computer algebra system written in pure Python
https://sympy.org/
Other
12.46k stars 4.31k forks source link

objects with indices (tensors) #3115

Closed certik closed 4 years ago

certik commented 17 years ago

bc.. Let's get inspired by ginac. This code should make it easy to compute for example einstein equations from the (symbolic) metric tensor. We can do it already now, but it would be nice to type in equations in the index form as found in the general relativity books and then just plug in a symbolic matrix for the metric tensor, and it would figure all the rest by itself.

p. Original issue for "#3115":https://github.com/sympy/sympy/issues/3115: "http://code.google.com/p/sympy/issues/detail?id=16":http://code.google.com/p/sympy/issues/detail?id=16

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

certik commented 17 years ago

bc.. Owner: ---

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c1":http://code.google.com/p/sympy/issues/detail?id=16#c1

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

certik commented 17 years ago

bc.. see also examples/relativity.py

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c2":http://code.google.com/p/sympy/issues/detail?id=16#c2

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

certik commented 17 years ago

bc.. I am struggling with the right interface to this. The relativity.py is working nice. I studied how grtensors, maple, ginac is doing tensors:

maple is just handling matrices

ginac is just handling the indexed objects, and has some support for computing with matrices using indices, however, we need up and down indices with matrices as well (inverting the matrix if needed)

grtensors have a weird notation, but that's because it's in the maple language.

What we need is a way to write equations like:

for the christoffel symbol:

Chr = g[i.up, m.up]/2 * (g[m.dn, k.dn].diff(l.up) + g[m.dn,l.dn].diff(k.up) \

and then assign some particular matrix for "g":

gdd=Matrix(( (-exp(nu(r)),0,0,0), (0, exp(lam(r)), 0, 0), (0, 0, r2, 0), (0, 0, 0, r2sin(theta)*2) ))

And the Chr above should calculate all (nonzero) components. Working example is in relativity.py, however, there I just implement the equation for Chr by hand in the "G" class.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c3":http://code.google.com/p/sympy/issues/detail?id=16#c3

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

certik commented 17 years ago

bc.. But I don't like the way this equation is written:

Chr = g[i.up, m.up]/2 * (g[m.dn, k.dn].diff(l.up) + g[m.dn,l.dn].diff(k.up) \

If you find some more intuitive and clear approach, let me know. BTW, the equation above is the equation (3) in: http://mathworld.wolfram.com/ChristoffelSymboloftheSecondKind.html

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c4":http://code.google.com/p/sympy/issues/detail?id=16#c4

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

egao1980 commented 16 years ago

bc.. IMHO Maxima's way is better: Chr = g([],[i,m])/2 * (g([m,k]).diff([], [l]) + g([m,l]).diff([],[k]) - g([k,l]).diff([], [m]))

i.e. I suggest to treat tensors as function of index sets as it provide a way to handle both covariant and contravariant index and other indexes like point index and time slice index needed in finite difference method equation. For example: g([j],[i],[x+1/2], [n+1]) -> n+1 i g x+1/2 j

Also, this approach allows us to handle summation over dummy indexes when co- and contravariant index sets use same symbols.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c5":http://code.google.com/p/sympy/issues/detail?id=16#c5

p. Original author: "https://code.google.com/u/116624277117698429559/":https://code.google.com/u/116624277117698429559/

certik commented 15 years ago

bc.. Some more thoughts:

I will implement the same as GiNaC has here: http://www.ginac.de/tutorial/Indexed-objects.html#Indexed%20objects That way you will just write something like

var("e D i j k l") Indexed(D, i, j, k, l) * Indexed(e, k, l)

and it will do the right thing. Then you will tell it to expand itself and it will probably call your routine to return something to be substituted for

D_0000 D_0100 ... e_00 e_01 e_11 ...

Plus I will implement Matrix support so that for vectors and 2 rank tensors this works out of the box.

We need this in SfePy, so I'll give it a shot.

Owner: ondrej.certik

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c6":http://code.google.com/p/sympy/issues/detail?id=16#c6

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

certik commented 15 years ago

bc.. Actually, I also like the comment #5. We'll see what I can do.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c7":http://code.google.com/p/sympy/issues/detail?id=16#c7

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

certik commented 15 years ago

bc.. I did some work, attached. I got busy with other things since then. Anyone feel free to continue. I'll get back to it later if I find time.

Owner: ---

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c8":http://code.google.com/p/sympy/issues/detail?id=16#c8

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

sympy-issue-migrator commented 15 years ago

bc.. Hi, I've been aware of Sympy for about a year and have kept an eye on it since then. I need to do some (and eventually lots) of calculations involving indices. Playing around a bit with some of the ideas on this page, I have written some prototype code that lets Sympy manipulate indices.

I've attached a patch, and I hope I got the format etc right.

I've used the notation ([i], [j,k]) for example to denote an indexed object with one lower index i and two upper indices, j and k.

Example usage:

from sympy.indexed import from sympy import i, j, k, l = symbols('i', 'j', 'k', 'l') theta, phi = symbols(r"\theta", r"\phi") g = Indexed('g', [i,j], components = ((1,0), (0, sin(theta)2))) g([2,2]) sin(\theta)2 g([i,j]) g([i, j]) einstein_sum(g([i,i])) 1 + sin(\theta)_2 einstein_sum(g([i,j]) * g([], [j,k])) # Second list of indices are superscript g([i, 1])_g([], [1, k]) + g([i, 2])g([], [2, k])

There is a fair amount of framework there but not much usability yet. I'm having trouble figuring out how to handle differentiation since \delta1(g{ij}) really means the derivative of g^{ij} in the first coordinate direction, but substituting the indices in when calculating the einstein sum would give diff(g([i,j]), 1) which doesn't work as needed.

Is there a neat way I can intercept diff(g([i,j]), 1) to replace 1 with the required coordinate? I think anything else is going to be ugly.

The other next step is to allow an expression like einstein_sum(g([i,j]) * g([], [j,k])) to be turned into another indexed object with indices, ([i],[j])

I'm not sure if this sort of implementation fits the requirements for a system in Sympy but I need it for my own work anyway so I'm happy to develop it regardless. Having it able to integrate well into Sympy would be a definite plus though, so feedback is appreciated.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c9":http://code.google.com/p/sympy/issues/detail?id=16#c9

p. Original author: "https://code.google.com/u/102177944399983481853/":https://code.google.com/u/102177944399983481853/

certik commented 15 years ago

bc.. Thanks for the work! I'll look at it soon. Other's, please review the patch as well.

Labels: NeedsReview

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c10":http://code.google.com/p/sympy/issues/detail?id=16#c10

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

sympy-issue-migrator commented 15 years ago

bc.. No problem! There is still a good amount of work to do before this becomes usable, so I don't know if you'd prefer to wait until I have it in a more working state before reviewing, or if you prefer to get code in quickly?

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c11":http://code.google.com/p/sympy/issues/detail?id=16#c11

p. Original author: "https://code.google.com/u/102177944399983481853/":https://code.google.com/u/102177944399983481853/

certik commented 15 years ago

bc.. Like Robert, you wanted tensors in the first place. :)

Cc: robert.cimrman

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c12":http://code.google.com/p/sympy/issues/detail?id=16#c12

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

certik commented 15 years ago

bc.. Let's get it in soon and improve further.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c13":http://code.google.com/p/sympy/issues/detail?id=16#c13

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

sympy-issue-migrator commented 15 years ago

bc.. I've done a bit more work on this and updated the code to a stage where some values can actually be calculated!

I'm still getting used to Mercurial, so apologies if this doesn't work, but applying the attached patch after the one above should take you to the right version.

I hope this isn't too much code to throw at you at once.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c14":http://code.google.com/p/sympy/issues/detail?id=16#c14

p. Original author: "https://code.google.com/u/102177944399983481853/":https://code.google.com/u/102177944399983481853/

certik commented 15 years ago

bc.. Thanks again. Would you like to just push your patches to some repository and host it on github for example (and write logs to the commits)? If all is ok, I'll push it in and be done with it. I am currently at the google mentors summit, so I am quite busy.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c15":http://code.google.com/p/sympy/issues/detail?id=16#c15

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

sympy-issue-migrator commented 15 years ago

bc.. When you say 'push your patches to some repository' do you mean I should set up a new repository and push my patches to that, and they can be reviewed from there? Or is there an existing repository that I should push them to? I'm happy to do what ever makes life easiest for you.

No worries about being busy, I hope you're not too run off your feet!

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c16":http://code.google.com/p/sympy/issues/detail?id=16#c16

p. Original author: "https://code.google.com/u/102177944399983481853/":https://code.google.com/u/102177944399983481853/

certik commented 15 years ago

bc.. I suggest you learn git and then setup a repo at github.com. Yes, you just pull our repo, commit your patches and push evetything in. In the long run I want to setup something like this: http://review.source.android.com/ where one submit patches, and it automatically creates a git repo which one can pull.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c17":http://code.google.com/p/sympy/issues/detail?id=16#c17

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

certik commented 15 years ago

bc.. Ok, you already use mercurial, so just create a repo at: http://www.bitbucket.org/ and push all your changes in.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c18":http://code.google.com/p/sympy/issues/detail?id=16#c18

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

aterrel commented 15 years ago

bc.. I haven't reviewed anything here yet, but I'll give it a shot. Just so you know my tensor calculus is fuzzy so if I say something silly let me know, I mostly have some comments about Sympy code and style. I only had time to get through the first patch I will look at the next one a bit later.

As far as the repo goes, I believe Ondrej is saying to make a repo (such as at github.org) and then we can pull from it, as explained at http://www.kernel.org/pub/software/scm/git/docs/user-manual.html#public- repositories ,

AFAIK, we are suppose to use PEP 8 ( http://www.python.org/dev/peps/pep-0008/ ) for python style. With that your code has quite a few lines over 80 characters and trailing whitespaces. The trailing whitespace actually break the tests.

For /sympy/indexed/indexed.py

Indexed and Indexed_Components overload init instead of new. This is out of sync with the rest of the code and also causes errors for objects. For example:

Indexed('a',[k],[m,n]) == Indexed('b',[k],[m,n])

causes some errors.

Also you are putting dimensions and components in the keywords, why not make these explicit and pass on the assumptions to the basic class handler? Right now you get them leaked to the assumptions:

In [13]: g = Indexed('g', [i, j], components = ((r2, 1, 1), (0, t3, 1))) In [19]: g.assumptions0 Out[19]:
{'components': ((r2 , 1, 1), (0, t3 , 1))}

Also it would be nice to document how the components are suppose to work. For the above example is it that g{r**2,1} = 1 or g{1,1} = r**2? Maybe there is more room for what is going on here or perhaps a reference for the mathematically challenged (like me).

I also wonder if we should derive the Indexed from Basic instead of Atom, since it can be broken down into other parts. Perhaps a design similar to the Function class would be more appropriate.

Sympy are suppose to be immutable, so setting new attributes should return new versions of the class. For example:

def set_components(self, components):
    ...
    self.components = components

needs to be rethought. I'm actually not sure what is going on here for example why would you do this instead of create a new object.

I'm not sure why this function is needed, just calling components should be sufficient:

def get_components(self):
    return self.components

Could you add an examples to free_indices and einstein_sum? In all the other functions it really helps.

Other than that I it looks good to me, but I need to go review some of the rules of tensors to check if things are correct algorithmically. This looks really cool and I would love to have it in.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c19":http://code.google.com/p/sympy/issues/detail?id=16#c19

p. Original author: "https://code.google.com/u/106486100774697058597/":https://code.google.com/u/106486100774697058597/

sympy-issue-migrator commented 15 years ago

bc.. Thanks for the feedback, it's really helpful. Most of the issues seem to stem from the fact that I've only just delved into the internals of Sympy, and fortunately none of them seem too hard to fix. Also I wasn't expecting my code to be reviewed so soon!

I'm afraid I might not have time to work on this again until next weekend. Thanks for any feedback in the meantime.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c20":http://code.google.com/p/sympy/issues/detail?id=16#c20

p. Original author: "https://code.google.com/u/102177944399983481853/":https://code.google.com/u/102177944399983481853/

certik commented 15 years ago

bc.. Thanks for the work and especially thanks Andy for reviewing it! I am really glad that it is not difficult for you to dig into sympy internals and to review code.

We just need to figure out some easier ways to do reviews, I think gerrit will do it.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c21":http://code.google.com/p/sympy/issues/detail?id=16#c21

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

sympy-issue-migrator commented 15 years ago

bc.. In the long I'd prefer to use git, so I've cloned the main repository and set up my own one at http://github.com/jpallen/sympy . I've started to address some of the things that Andy mentioned.

set_components and get_component are there to wrap the work that needs done to get the dimensions and do some sanity checking on components. I left them public as they seemed useful and I didn't realise objects were meant to be immutable. Does simply making them private sound reasonable?

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c22":http://code.google.com/p/sympy/issues/detail?id=16#c22

p. Original author: "https://code.google.com/u/102177944399983481853/":https://code.google.com/u/102177944399983481853/

certik commented 15 years ago

bc.. Yes, it seems reasonable to make them private. Please ping us if you have something to review/merge.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c23":http://code.google.com/p/sympy/issues/detail?id=16#c23

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

aterrel commented 15 years ago

bc.. Labels: -NeedsReview

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c24":http://code.google.com/p/sympy/issues/detail?id=16#c24

p. Original author: "https://code.google.com/u/106486100774697058597/":https://code.google.com/u/106486100774697058597/

aterrel commented 15 years ago

bc.. any updates on this work?

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c25":http://code.google.com/p/sympy/issues/detail?id=16#c25

p. Original author: "https://code.google.com/u/106486100774697058597/":https://code.google.com/u/106486100774697058597/

sympy-issue-migrator commented 15 years ago

bc.. Not for a while, I've been pretty busy recently, sorry. I'll hopefully get another chance to look at it soon.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c26":http://code.google.com/p/sympy/issues/detail?id=16#c26

p. Original author: "https://code.google.com/u/102177944399983481853/":https://code.google.com/u/102177944399983481853/

aterrel commented 15 years ago

bc.. very cool. I might play around with it a bit, I would like to get some non-Newtonian fluid models going in sympy without having to carry all the tensor bits around myself.

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c27":http://code.google.com/p/sympy/issues/detail?id=16#c27

p. Original author: "https://code.google.com/u/106486100774697058597/":https://code.google.com/u/106486100774697058597/

certik commented 15 years ago

bc.. Yeah, I want this too, for playing with tensors in differential geometry and other places. We all need this, someone just needs to do it. :)

p. Original comment: "http://code.google.com/p/sympy/issues/detail?id=16#c28":http://code.google.com/p/sympy/issues/detail?id=16#c28

p. Original author: "https://code.google.com/u/104039945248245758823/":https://code.google.com/u/104039945248245758823/

asmeurer commented 13 years ago
**Labels:** Matrices  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c29 Original author: https://code.google.com/u/asmeurer@gmail.com/

ketch commented 13 years ago
+1 for getting this functionality into sympy...

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c30 Original author: https://code.google.com/u/116021515467243730737/

vks commented 13 years ago
The Indexed class and friends implement some tensor stuff: http://docs.sympy.org/dev/modules/tensor.html

**Status:** Started  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c31 Original author: https://code.google.com/u/Vinzent.Steinberg@gmail.com/

smichr commented 12 years ago
See what functionality (if any) in the patch above is not handled with the Indexed class.

**Labels:** CodeInCategory-Documentation CodeInDifficulty-Easy  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c32 Original author: https://code.google.com/u/117933771799683895267/

asmeurer commented 12 years ago
**Labels:** EasyToFix  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c33 Original author: https://code.google.com/u/asmeurer@gmail.com/

Krastanov commented 12 years ago
Should this be GCI documentation? For the moment I'm moving it to the spreadsheet under documentation. Change also there if needed.

**Labels:** CodeInImportedIntoSpreadsheet  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c34 Original author: https://code.google.com/u/100157245271348669141/

asmeurer commented 12 years ago
**Labels:** -codeinimportedintospreadsheet CodeInImportedIntoMelange  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c35 Original author: https://code.google.com/u/asmeurer@gmail.com/

goodok commented 12 years ago
This issue has no plan.

In present days this module exists http://docs.sympy.org/dev/modules/tensor/indexed.html#examples We must decide what to do further with it, estimate the list of defects (e.g. indexes are not sympyfied, they are pythonic ints).

And there can be similar implementation in physics or matrices modules.

**Status:** NeedsDecision  
**Labels:** -EasyToFix  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c36 Original author: https://code.google.com/u/109448925098397033296/

asmeurer commented 11 years ago
**Labels:** -CodeInImportedIntoMelange  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c37 Original author: https://code.google.com/u/asmeurer@gmail.com/

Krastanov commented 11 years ago
**Labels:** CodeInImportedIntoGoogleDocs  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c38 Original author: https://code.google.com/u/100157245271348669141/

asmeurer commented 11 years ago
**Labels:** -CodeInCategory-Documentation -CodeInDifficulty-Easy -CodeInImportedIntoGoogleDocs  

Original comment: http://code.google.com/p/sympy/issues/detail?id=16#c39 Original author: https://code.google.com/u/asmeurer@gmail.com/

asmeurer commented 4 years ago

Things are always improving, but we have indexed object and mathematical tensor support now. New issues should be opened for anything that isn't yet implemented.