Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[RFC] Add latex representation to DenseMatrix #370

Merged

Conversation

eendebakpt
Copy link
Contributor

@eendebakpt eendebakpt commented Sep 24, 2021

  • Add a _repr_latex_ method to the DenseMatrix class.
  • Add truncation for large matrices

image

Related to symengine/symengine#1701

@eendebakpt eendebakpt changed the title [WIP] Add latex representation to DenseMatrix [RFC] Add latex representation to DenseMatrix Sep 24, 2021
@certik
Copy link
Contributor

certik commented Sep 24, 2021

Nice!

I think in general these things should go into the C++ symengine, and we should just call it from the Python wrappers. That way any language will benefit and we only have to maintain this at one place.

Let me know what you think. I can help you get started in the C++ codebase if you are interested. Essentially this should go here: https://github.com/symengine/symengine/blob/88cf2e2ff482f64fae6c18ae93b8a30b6adef632/symengine/printers/latex.cpp. I don't know if we can simply add a method or figure out a way to handle matrices (I think they might be separate classes), but either way, I would do this in C++.

@eendebakpt
Copy link
Contributor Author

Nice!

I think in general these things should go into the C++ symengine, and we should just call it from the Python wrappers. That way any language will benefit and we only have to maintain this at one place.

Let me know what you think. I can help you get started in the C++ codebase if you are interested.

If there would be other languages that benefit from latex it might indeed be good to move this into the symengine part. Otherwise it is probably easier to implement it on the python side.
I am comfortable with C++, but some pointers into the symengine structure would be helpful. In particular: I think the basic elements of a DenseMatrix are Basic objects. Where is the (latex) printing for the DenseMatrix and Basic objects? And which other structures to consider.

The latex printing of simple objects from the python side also seems not to be implemented, although a _repr_latex_ is present:
image

@isuruf
Copy link
Member

isuruf commented Sep 24, 2021

The latex printing of simple objects from the python side also seems not to be implemented, although a repr_latex is present:

You need to do init_printing() to make it work,

@eendebakpt
Copy link
Contributor Author

eendebakpt commented Sep 24, 2021

The latex printing of simple objects from the python side also seems not to be implemented, although a repr_latex is present:

You need to do init_printing() to make it work,

Thanks for the pointer. On my system it is not working:
image

Can be fixed by commenting out the lines

# To not expose internals
del lib.symengine_wrapper
del lib

from the symengine __init__.py. See #371.

@isuruf

@certik
Copy link
Contributor

certik commented Sep 25, 2021

Try this in a jupyter notebook. The init_printing should work.

@certik
Copy link
Contributor

certik commented Sep 25, 2021

@isuruf, since DenseMatrix is not part of the SymEngine visitor pattern, and currently does not have a latex printer, what do you think is the best design for printing latex? In symengine/printers.h we have std::string latex(const Basic &x); but that accepts Basic. I wonder if we can add another overload like latex(const DenseMatrix &x);, add an implementation into symengine/printers/latex.cpp and then just call it from the Python wrappers.

@isuruf
Copy link
Member

isuruf commented Sep 26, 2021

In symengine/printers.h we have std::string latex(const Basic &x); but that accepts Basic. I wonder if we can add another overload like latex(const DenseMatrix &x);, add an implementation into symengine/printers/latex.cpp and then just call it from the Python wrappers.

Yes, that sounds good.

@eendebakpt
Copy link
Contributor Author

@isuruf I updated this PR based on symengine/symengine#1847. Could you help me out with the cython part where the python object is mapped to a c++ symengine object?

@peendebak
Copy link

@isuruf Could you have a look at the cython part of the PR? With the c++ part merged, this is should be quite small.

@peendebak
Copy link

@isuruf The CI is failing because a python object cannot be assigned to the ctypes DenseMatrix. Could you have a look at it?

@isuruf isuruf merged commit 1ef1c87 into symengine:master Nov 26, 2021
@isuruf
Copy link
Member

isuruf commented Nov 26, 2021

Thanks

@eendebakpt eendebakpt deleted the feat/densematrix_latex_representation branch December 9, 2021 21:37
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants