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

Implement matrix truncation for latex to enhance the display of large matrices #26284

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

mohamedrezk122
Copy link
Contributor

@mohamedrezk122 mohamedrezk122 commented Feb 27, 2024

References to other Issues or PRs

Fixes #16251

Brief description of what is fixed or changed

The _print_matrix_contents method now includes options for compactifying and setting maximum visible rows and columns, providing a more readable representation. This improvement prevents printing slowdowns for large matrices and improves overall user experience.
Basically, the method looks if the number of rows or number of columns exceeds certain thresholds, which are predefined in the _settingsdictionary under the names mat_visible_rows and mat_visible_cols respectively, with 15 as a default value. If so, the method avoids iterating over all elements of the matrix and adds dots to imply compaction.

The compaction feature is also predefined in _settings dictionary with default value True to avoid large matrix printing slowdown.

Here is the behavior after enabling the compaction feature:
image
And here is the behavior with it:
image

Also, compaction of rows and cols are independent based on the dimension of each. The following two examples illustrates this better:

image
image

Finally, if the matrix dimensions are small (below thresholds) in both axes then no compaction takes place like so :
image

Other comments

Release Notes

  • printing
    • Added a truncation feature to latex for large matrices printing.

…ting

The `_print_matrix_contents` method now includes options for compactifying
and setting maximum visible rows and columns, providing a more readable
representation. This improvement prevents printing slowdowns for large
matrices and improves overall user experience. This modification shall
solve sympy#16251.
@sympy-bot
Copy link

sympy-bot commented Feb 27, 2024

Hi, I am the SymPy bot. 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.13.

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. -->
Fixes #16251 


#### Brief description of what is fixed or changed
The `_print_matrix_contents` method now includes options for compactifying and setting maximum visible rows and columns, providing a more readable representation. This improvement prevents printing slowdowns for large matrices and improves overall user experience. 
Basically, the method looks if the number of rows or number of columns exceeds certain thresholds, which are predefined in the `_settings`dictionary under the names `mat_visible_rows` and `mat_visible_cols` respectively, with `15` as a default value. If so, the method avoids iterating over all elements of the matrix and adds dots to imply compaction.

The compaction feature is also predefined in `_settings` dictionary with default value `True` to avoid large matrix printing slowdown.    

**Here is the behavior after enabling the compaction feature:**  
![image](https://github.com/sympy/sympy/assets/59314933/00021ae5-425e-4295-a450-a29fb14242a0)
**And here is the behavior with it:**
![image](https://github.com/sympy/sympy/assets/59314933/79be328d-d597-46f7-9b77-9c528f0d0d31)

**Also, compaction of rows and cols are independent based on the dimension of each. The following two examples illustrates this better:**

![image](https://github.com/sympy/sympy/assets/59314933/3b9fe932-d723-4cd9-870e-a787cef5691d)
![image](https://github.com/sympy/sympy/assets/59314933/b8c1fa3e-2fa0-4ef0-acb1-aa987088d9ef)

**Finally, if the matrix dimensions are small (below thresholds) in both axes then no compaction takes place like so :**
![image](https://github.com/sympy/sympy/assets/59314933/61cbeea8-27a0-4d01-a26c-46179cf01c64)



#### Other comments


#### 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. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

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
  * Added a truncation feature to latex  for large matrices printing.


<!-- END RELEASE NOTES -->

Copy link

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)

| Change   | Before [a00718ba]    | After [24792d5f]    |   Ratio | Benchmark (Parameter)                                                |
|----------|----------------------|---------------------|---------|----------------------------------------------------------------------|
| -        | 68.6±0.5ms           | 44.3±0.2ms          |    0.65 | integrate.TimeIntegrationRisch02.time_doit(10)                       |
| -        | 66.5±1ms             | 43.8±0.2ms          |    0.66 | integrate.TimeIntegrationRisch02.time_doit_risch(10)                 |
| +        | 18.5±0.5μs           | 30.0±0.5μs          |    1.62 | integrate.TimeIntegrationRisch03.time_doit(1)                        |
| -        | 5.20±0.02ms          | 2.90±0.03ms         |    0.56 | logic.LogicSuite.time_load_file                                      |
| -        | 72.2±0.6ms           | 28.4±0.2ms          |    0.39 | polys.TimeGCD_GaussInt.time_op(1, 'dense')                           |
| -        | 25.4±0.4ms           | 16.7±0.05ms         |    0.66 | polys.TimeGCD_GaussInt.time_op(1, 'expr')                            |
| -        | 73.0±0.9ms           | 28.6±0.2ms          |    0.39 | polys.TimeGCD_GaussInt.time_op(1, 'sparse')                          |
| -        | 249±2ms              | 123±0.9ms           |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'dense')                           |
| -        | 252±3ms              | 123±2ms             |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'sparse')                          |
| -        | 643±9ms              | 364±8ms             |    0.57 | polys.TimeGCD_GaussInt.time_op(3, 'dense')                           |
| -        | 643±9ms              | 368±2ms             |    0.57 | polys.TimeGCD_GaussInt.time_op(3, 'sparse')                          |
| -        | 483±8μs              | 282±2μs             |    0.58 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(1, 'dense')            |
| -        | 1.75±0ms             | 1.04±0.01ms         |    0.59 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(2, 'dense')            |
| -        | 5.78±0.03ms          | 3.08±0.01ms         |    0.53 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(3, 'dense')            |
| -        | 448±2μs              | 225±1μs             |    0.5  | polys.TimeGCD_QuadraticNonMonicGCD.time_op(1, 'dense')               |
| -        | 1.44±0.02ms          | 654±20μs            |    0.46 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(2, 'dense')               |
| -        | 4.80±0.09ms          | 1.63±0.01ms         |    0.34 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(3, 'dense')               |
| -        | 374±2μs              | 206±0.8μs           |    0.55 | polys.TimeGCD_SparseGCDHighDegree.time_op(1, 'dense')                |
| -        | 2.39±0.05ms          | 1.22±0.01ms         |    0.51 | polys.TimeGCD_SparseGCDHighDegree.time_op(3, 'dense')                |
| -        | 9.84±0.1ms           | 4.30±0.03ms         |    0.44 | polys.TimeGCD_SparseGCDHighDegree.time_op(5, 'dense')                |
| -        | 356±1μs              | 169±2μs             |    0.48 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(1, 'dense')            |
| -        | 2.50±0.03ms          | 893±0.4μs           |    0.36 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(3, 'dense')            |
| -        | 9.70±0.06ms          | 2.68±0.01ms         |    0.28 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(5, 'dense')            |
| -        | 1.02±0.01ms          | 424±1μs             |    0.42 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'dense')           |
| -        | 1.74±0.02ms          | 516±4μs             |    0.3  | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'sparse')          |
| -        | 5.85±0.03ms          | 1.79±0.01ms         |    0.31 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'dense')           |
| -        | 8.35±0.03ms          | 1.52±0ms            |    0.18 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'sparse')          |
| -        | 285±0.9μs            | 65.0±0.8μs          |    0.23 | polys.TimePREM_QuadraticNonMonicGCD.time_op(1, 'sparse')             |
| -        | 3.42±0.02ms          | 396±2μs             |    0.12 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'dense')              |
| -        | 3.92±0.03ms          | 282±2μs             |    0.07 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'sparse')             |
| -        | 6.98±0.08ms          | 1.26±0.01ms         |    0.18 | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'dense')              |
| -        | 8.65±0.07ms          | 858±3μs             |    0.1  | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'sparse')             |
| -        | 5.04±0.03ms          | 2.99±0.01ms         |    0.59 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(2, 'sparse') |
| -        | 12.0±0.09ms          | 6.55±0.1ms          |    0.54 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'dense')  |
| -        | 22.4±0.2ms           | 9.12±0.09ms         |    0.41 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'sparse') |
| -        | 5.16±0.02ms          | 869±4μs             |    0.17 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(1, 'sparse')    |
| -        | 12.7±0.1ms           | 7.10±0.03ms         |    0.56 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(2, 'sparse')    |
| -        | 101±0.4ms            | 25.5±0.1ms          |    0.25 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'dense')     |
| -        | 165±2ms              | 54.3±0.1ms          |    0.33 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'sparse')    |
| -        | 174±3μs              | 111±0.9μs           |    0.64 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'dense')      |
| -        | 359±0.9μs            | 217±1μs             |    0.61 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'sparse')     |
| -        | 4.25±0.1ms           | 838±6μs             |    0.2  | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'dense')      |
| -        | 5.25±0.08ms          | 384±2μs             |    0.07 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'sparse')     |
| -        | 19.7±0.2ms           | 2.81±0ms            |    0.14 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'dense')      |
| -        | 22.5±0.1ms           | 637±5μs             |    0.03 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'sparse')     |
| -        | 482±2μs              | 136±1μs             |    0.28 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(1, 'sparse') |
| -        | 4.51±0.1ms           | 623±3μs             |    0.14 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'dense')  |
| -        | 5.23±0.03ms          | 140±0.3μs           |    0.03 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'sparse') |
| -        | 13.1±0.06ms          | 1.28±0ms            |    0.1  | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'dense')  |
| -        | 13.5±0.4ms           | 141±1μs             |    0.01 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'sparse') |
| -        | 132±3μs              | 75.5±0.3μs          |    0.57 | solve.TimeMatrixOperations.time_rref(3, 0)                           |
| -        | 249±1μs              | 88.4±0.4μs          |    0.35 | solve.TimeMatrixOperations.time_rref(4, 0)                           |
| -        | 24.0±0.06ms          | 10.0±0.06ms         |    0.42 | solve.TimeSolveLinSys189x49.time_solve_lin_sys                       |
| -        | 27.8±0.4ms           | 15.4±0.1ms          |    0.55 | solve.TimeSparseSystem.time_linsolve_Aaug(20)                        |
| -        | 54.4±0.2ms           | 24.4±0.4ms          |    0.45 | solve.TimeSparseSystem.time_linsolve_Aaug(30)                        |
| -        | 27.9±0.1ms           | 15.3±0.09ms         |    0.55 | solve.TimeSparseSystem.time_linsolve_Ab(20)                          |
| -        | 54.7±0.4ms           | 24.6±0.1ms          |    0.45 | solve.TimeSparseSystem.time_linsolve_Ab(30)                          |

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

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.

Having an option to truncate large array printing.
2 participants