Skip to content
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

Support for single indexes in MatrixExpr #7883

Merged
merged 1 commit into from Aug 21, 2014
Merged

Conversation

jcrist
Copy link
Member

@jcrist jcrist commented Aug 17, 2014

This allows MatrixSymbols to be iterated over, and gives a similar
interface to DenseMatrix. MatExpr are treated as row-major order arrays.
This only works if both the matrix shape and index are Integer/int.

Demo:

>>> qs = symbols('q0:5')
>>> q = MatrixSymbol('q', 5, 1)
>>> expr = sum(qs); expr
q0 + q1 + q2 + q3 + q4

# Can be used to create a sub_dict, with all symbols in a vector representation.
# This is useful for code-generation or numerical evaluation
>>> sub_dict = dict(zip(qs, q)); sub_dict
{q4: q[4, 0], q3: q[3, 0], q0: q[0, 0], q1: q[1, 0], q2: q[2, 0]}
>>> expr.subs(sub_dict)
q[0, 0] + q[1, 0] + q[2, 0] + q[3, 0] + q[4, 0]

# Can also iterate over slices. This will sum just the fourth row of matrix A
>>> A = MatrixSymbol('A', 5, 5)
>>> sum(A[3, :])
A[3, 0] + A[3, 1] + A[3, 2] + A[3, 3] + A[3, 4]

This allows MatrixSymbols to be iterated over, and gives a similar
interface to DenseMatrix. MatExpr are treated as row-major order arrays.
This only works if both the matrix shape and index are Integer/int.
@jcrist
Copy link
Member Author

jcrist commented Aug 17, 2014

@mrocklin : Please review.

@jcrist
Copy link
Member Author

jcrist commented Aug 19, 2014

Ping for review: @mrocklin.

@jcrist
Copy link
Member Author

jcrist commented Aug 21, 2014

Can I get a review on this from someone? This will really help make codegen for mechanics stuff alot easier to express using vectors. @sympy/mechanics

@moorepants
Copy link
Member

This seems fine. +1

I can't see why this would cause any issues. It is just a simple feature addition.

@mrocklin
Copy link
Member

Sorry for being absent here. We had this at one point and I took it out. Perhaps it was because it was causing infinite loops on symbolic shapes. I see that you've handled that. No clear objection from me. I haven't inspected this that deeply though.

@jcrist
Copy link
Member Author

jcrist commented Aug 21, 2014

No worries @mrocklin. We could make it work with symbolics by changine the indices to be symbolic expressions between the dimensions and the single index, but I don't really see the use of that (unless someone wanted to check C array index arithmetic). Thanks for the review. Merging.

jcrist added a commit that referenced this pull request Aug 21, 2014
Support for single indexes in MatrixExpr
@jcrist jcrist merged commit 71e7438 into sympy:master Aug 21, 2014
@jcrist jcrist deleted the matsym_iter branch August 21, 2014 21:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants