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 formatting of elements in RDF/CDF #29514

Closed
mwageringel opened this issue Apr 15, 2020 · 15 comments
Closed

implement formatting of elements in RDF/CDF #29514

mwageringel opened this issue Apr 15, 2020 · 15 comments

Comments

@mwageringel
Copy link

This ticket implements __format__ for elements in RDF and CDF. See also #29212 for a related discussion.

The real number format immediately delegates to float, whereas the complex format matches the usual repr of elements in CDF as closely as possible (unlike Python's type complex).

sage: '{:.4e}'.format(RDF(2/3))
'6.6667e-01'

sage: format(CDF(32/3, 0), ' .4f')
' 10.6667 + 0.0000*I'

sage: format(CDF(-2/3, -2/3), '.4e')
'-6.6667e-01 - 6.6667e-01*I'

sage: format(CDF(0, 2/3), '.4')
'0.6667*I'

sage: format(CDF(2, 0), '.4')
'2.0'

For reference: the format specification in Python.

Component: misc

Author: Markus Wageringel

Branch/Commit: 51a9736

Reviewer: Eric Gourgoulhon

Issue created by migration from https://trac.sagemath.org/ticket/29514

@mwageringel mwageringel added this to the sage-9.2 milestone Apr 15, 2020
@mwageringel
Copy link
Author

Author: Markus Wageringel

@mwageringel
Copy link
Author

Branch: u/gh-mwageringel/29514

@mwageringel
Copy link
Author

New commits:

f89ae6e29514: implement formatting of RDF/CDF elements

@mwageringel
Copy link
Author

Commit: f89ae6e

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 15, 2020

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

829bcc029514: implement formatting of RDF/CDF elements

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 15, 2020

Changed commit from f89ae6e to 829bcc0

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 16, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

51a973629514: implement custom `__format__` matching the CDF representation

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Apr 16, 2020

Changed commit from 829bcc0 to 51a9736

@mwageringel

This comment has been minimized.

@mwageringel
Copy link
Author

comment:4

Actually, here is a much better version that very closely matches the usual representation of elements in CDF.

@mwageringel
Copy link
Author

comment:5

A possible use case is reducing the number of digits in matrices:

sage: print(matrix.random(CDF, 4).str(' {:.4f}'.format))
[ -0.6482 + 0.4684*I   0.7523 + 0.9738*I   0.0076 - 0.5470*I   0.4949 + 0.5821*I]
[  0.6205 + 0.5834*I  -0.0394 + 0.3852*I  -0.3764 - 0.9199*I   0.3162 - 0.2032*I]
[ -0.0436 + 0.5112*I  -0.3949 + 0.2829*I   0.2733 + 0.4865*I   0.3938 - 0.0825*I]
[  0.0763 + 0.6577*I  -0.9444 + 0.4760*I   0.0951 + 0.6043*I  -0.4464 + 0.4081*I]

@egourgoulhon
Copy link
Member

comment:6

LGTM. Thank you!

@egourgoulhon
Copy link
Member

Reviewer: Eric Gourgoulhon

@mwageringel
Copy link
Author

comment:7

Thank you for the review.

@vbraun
Copy link
Member

vbraun commented Jul 8, 2020

Changed branch from u/gh-mwageringel/29514 to 51a9736

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants