-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Added ArraySlice class so that ArraySymbol accepts slices #22265
Conversation
Pins down existing behaviour so we can safely refactor it
This helps static code analysers like mypy and code navigators like pylance
This gives a better overview of the way in which self._args should be interpreted. Several other modules also adopted this style.
All tests that involve ArrayElement use an ArraySymbol for the "name" argument. For the implementation, it's also safer to have a reference to the parent ArraySymbol, like when comparing ArrayElements. Note that MatrixElement also treats the name argument as a parent (although it doesn't perform a type check): https://github.com/sympy/sympy/blob/6de9857/sympy/matrices/expressions/matexpr.py#L719
Same as with Python list and numpy.array, something like the following should be allowed: >>> some_list = [0, 1, 2] >>> some_list[:5] [0, 1, 2]
✅ Hi, I am the SymPy bot (v162). I'm here to help you write a release notes entry. Please read the guide on how to write release notes. Your release notes are in good order. Here is what the release notes will look like:
This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.10. Click here to see the pull request description that was parsed.
|
🟠Hi, I am the SymPy bot (v162). I've noticed that some of your commits add or delete files. Since this is sometimes done unintentionally, I wanted to alert you about it. This is an experimental feature of SymPy Bot. If you have any feedback on it, please comment at sympy/sympy-bot#75. The following commits add new files:
The following commits delete files:
If these files were added/deleted on purpose, you can ignore this message. |
Is there a 'sympy way' to parametrize tests? Apparently can't do this through pytest: |
I am interested in writing the code printers for array like objects. In particular, I am currently working on trying to print Indexed objects with numpy and tensorflow. While some pattern matching is needed for Indexed compared to Arrays (e.g. x_i-x_j would need x[:,newaxis]-x[newaxis,:] as the array expression), I'll gladly write the code printers for the Array objects and have the printer for Indexed objects use the printing routines for Arrays when appropriate to avoid code duplication. Please also see my ideas in: #22219 For this I would however, like to see a newaxis object and if possible the option to select specific indices through an array of integers (numpy supports this as far as I understand and tensorflow allows this through tf.gather and tf.scatter). Would you consider adding those options to this merge request? I also have a question about this merge request (for my future reference). Is it prefered to have many small commits in a merge request or only a single large commit? I could argue it either way. Also, is it appropriate to ask for additional features in a merge request? Or should I have written it as a separate issue? |
Sounds good, yes! While working on this PR, I was already trying to implement some more arithmetic (only addition etc), but I got the impression that
Not sure either; I was trying some sort of TDD here, and perhaps it helps when reviewing the PR. In the end, the commits can be squashed. (Personally, I'm in favour of squash-merging, so that the commit history within the PR doesn't matter anymore).
Idk, not a sympy developer ;) It sounds like a bigger idea, so indeed worth to post an issue on it (perhaps just linking to the discussion suffices). |
In the case of trying to sum over an axis, what would the sympy expression be? |
Beats me 🤷♂️ Afaics there are no sympy classes that lambdify to Perhaps something can be defined through Lines 246 to 299 in 3abd23d
|
I think einsum can be correctly used for summing over the axis of a single Array. E.g. import numpy as np
A=np.ones((3,))
np.einsum('i->',A) Would yield a scalar=3. I would imagine that it would shortcut to a special case and just run np.sum if it detects this is possible. One could also imagine something like If you need any help with the printing routines, or want me to give them an attempt that is no problem. I'm not very experienced with the Array part of sympy though, so I'm looking for your help on that side e.g giving an example of what sympy expression should lead to what print statement, or even simply the sympy expression (if it is clear what it should mean) that should be supported for printing. |
Thanks, let's move this discussion to #22269 |
Would you consider ArraySlice to allow None (both numpy and tensorflow allow this as an alternative to newaxis), ArraySymbols and Arrays as valid slices? to replicate this behaviour: Considering #22269 (reply in thread) allowing symbols as axes would be nice too |
So guys, is there anything about the PR that needs to be changed? Sounds like the other points raised here are larger issues. |
This PR makes the shape of |
I added a small unit test (837d238) that pinned down existing behaviour. At the time, an
sympy/sympy/tensor/array/expressions/tests/test_array_symbol.py Lines 8 to 19 in 837d238
Update: following behaviour is indeed defined in the latest master (ef37f78) >>> from sympy.tensor.array.expressions.array_expressions import ArraySymbol
>>> A = ArraySymbol("A")
>>> A[0, 0]
A[0, 0] |
Probably this is a bug. If you don't pass a shape to |
Then a test should be written to prove it. I think this should be addressed elsewhere, not in this PR. |
I think generally this points in the direction of a larger issue: too much logics going on in the constructors/new methods. For instance, |
Yes, it's related to the problem of automatic normalization. Actually, all of SymPy is affected by this problem. If you call I would put the logic in a There is already a function meant to perform contractions, it's |
I do admit though that I indulged in some other clean-ups. So what I could do is clean up this PR (rebase) and extract some of those commits that were not immediately related to |
Remember that the array expressions module has not been tested that much with undefined shapes. It could require some work to make array symbols with undefined shape work with all expression-tree objects. |
Okay so sounds like those issues have to be addressed first. I could write some tests and so to enforce the use of a non-empty shape and PR that separately first. I'll also clean up this PR and have it focus only on defining an |
Side note: I mainly have an interest in code generation, so I can't help much with the math :S |
IMO using |
|
It can still be fixed. |
Many points raised here, so I tried to collect them in #22321. |
References to other Issues or PRs
Brief description of what is fixed or changed
Added an
ArraySlice
class that allows taking slices from anArraySymbol
:It's now also possible to take a single index from an array:
Negative indices are also allowed (normalized in the same way as
MatrixSymbol
does):Other changes are related to typing and making array expressions more like matrix expressions. See commit history.
Other comments
Motivation is to have
ArraySymbol
behave more likenumpy.array
ortensorflow.Tensor
, so that you can use SymPy expressions as a template for these different computational back-ends (throughlambdify
).Release Notes