Skip to content

Conversation

oscargus
Copy link
Contributor

@oscargus oscargus commented Dec 31, 2021

References to other Issues or PRs

Closes #22763

Brief description of what is fixed or changed

Corrected LaTeX-printing of Determinant.

Other comments

I seem to recall that there was some discussion about this elsewhere, but cannot really recall where...

Release Notes

  • printing
    • Corrected LaTeX- and pretty-printing of Adjoint, Determinant, Inverse, and Transpose.

@sympy-bot
Copy link

sympy-bot commented Dec 31, 2021

Hi, I am the SymPy bot (v163). 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:

  • printing
    • Corrected LaTeX- and pretty-printing of Adjoint, Determinant, Inverse, and Transpose. (#22773 by @dispasha and @oscargus)

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.11.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->

Closes #22763

#### Brief description of what is fixed or changed

Corrected LaTeX-printing of Determinant.

#### Other comments

I seem to recall that there was some discussion about this elsewhere, but cannot really recall where...

#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
* printing
   * Corrected LaTeX- and pretty-printing of Adjoint, Determinant, Inverse, and Transpose. 
<!-- END RELEASE NOTES -->

Update

The release notes on the wiki have been updated.

@oscargus
Copy link
Contributor Author

There are things that must be fixed here...

@oscargus
Copy link
Contributor Author

Now it should be working correctly. I noted that this happens:

In [23]: Inverse(m)
Out[23]: 
        -1
⎛⎡1  2⎤⎞  
⎜⎢    ⎥⎟  
⎝⎣3  4⎦⎠  

so an extra parenthesis for Inverse. This is fixed here so that it is printed as

      -1
⎡1  2⎤  
⎢    ⎥  
⎣3  4⎦  

(similar for LaTeX) and should be fixed similarly for Adjoint and Transpose (at least), but since #22772 changes them, it will have to wait.

@github-actions
Copy link

github-actions bot commented Dec 31, 2021

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@oscargus oscargus changed the title Fixed LaTeX-printing of Determinant Fixed LaTeX- and pretty-printing of Determinant Dec 31, 2021
@smichr
Copy link
Member

smichr commented Dec 31, 2021

Is this ready if #22772 is in?

@oscargus
Copy link
Contributor Author

Is this ready if #22772 is in?

I think/hope so. As far as I know, the combination should not break anything. (If it does, I'll update the tests ASAP.)

else:
mat_delim_backup = self._settings['mat_delim']
self._settings['mat_delim'] = ''
tex = r"\left|{%s}\right|" % self._print(expr.args[0])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you mentioned this on the issue, but why not just set the delimiter to | and recursively print expr.args[0]? The result would be the same but would be less convoluted.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Main reason is that it was like that in the code.

The corresponding code would be:

            self._settings['mat_delim'] = '|'
            tex = self._print(expr.args[0])

and changing

        self._delim_dict = {'(': ')', '[': ']'}

to

        self._delim_dict = {'(': ')', '[': ']', '|': '|'}

so to me it seemed about the same.

But I can definitely change it. One should probably check how block matrices print though... Things like

X = MatrixSymbol('X', 2, 2)
m = Matrix(((1, 2), (3, 4)))
BlockMatrix(((OneMatrix(2, 2), X), (m, ZeroMatrix(2, 2))))

Will probably not print in a sensible way independent of solution...

(Right now:

⎡  𝟙     X⎤
⎢         ⎥
⎢⎡1  2⎤   ⎥
⎢⎢    ⎥  𝟘⎥
⎣⎣3  4⎦   ⎦

)

tex = r"\left|{%s}\right|" % self._print(expr.args[0])
self._settings['mat_delim'] = mat_delim_backup
if exp is not None:
return r"%s^{%s}" % (tex, exp)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Pow printer only uses the exp argument if the argument is a Function. Maybe this could be generalized to allow other classes, although in this case, do we really want powers of determinants to print like |A|^2? A test you added below seems to indicate not. Personally I'd say we should not print determinant powers like that because it makes it unclear if an expression is actually Determinant(MatPow) or Pow(Determinant).

Copy link
Contributor Author

@oscargus oscargus Jan 1, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The plan was that it should be printed as |A|^2, yes. And the other way around as |A^2| (or |(A)^2| if A is a Matrix rather than a MatrixSymbol).

Probably adding the exp-argument was a bit too much copy and paste...

@oscargus oscargus marked this pull request as draft January 1, 2022 21:30
@oscargus oscargus force-pushed the latexdeterminant branch 10 times, most recently from 1431de1 to c87299c Compare April 3, 2022 12:42
@oscargus oscargus marked this pull request as ready for review April 3, 2022 12:52
@oscargus
Copy link
Contributor Author

oscargus commented Apr 3, 2022

This is now refactored to not fiddle with the settings and handle block matrices in a much better way now.

@oscargus oscargus merged commit c8ca7fa into sympy:master Apr 25, 2022
@oscargus oscargus deleted the latexdeterminant branch April 25, 2022 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect LaTeX display of a determinant
5 participants