#29514 closed enhancement (fixed)

implement formatting of elements in RDF/CDF

Reported by: gh-mwageringel Owned by:
Priority: minor Milestone: sage-9.2
Component: misc Keywords:
Cc: Merged in:
Authors: Markus Wageringel Reviewers: Eric Gourgoulhon
Report Upstream: N/A Work issues:
Branch: 51a9736 (Commits, GitHub, GitLab) Commit: 51a9736237f27229e5c9624c7fbb6d53ec898725
Dependencies: Stopgaps:

Status badges

Description (last modified by gh-mwageringel)

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.

Change History (8)

comment:1 Changed 13 months ago by gh-mwageringel

  • Authors set to Markus Wageringel
  • Branch set to u/gh-mwageringel/29514
  • Commit set to f89ae6ebc722d44e732b67972fc23da53c9e3efe
  • Status changed from new to needs_review

New commits:

f89ae6e29514: implement formatting of RDF/CDF elements

comment:2 Changed 13 months ago by git

  • Commit changed from f89ae6ebc722d44e732b67972fc23da53c9e3efe to 829bcc02ed2ef6b3530a27565851edfb01f639a7

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

829bcc029514: implement formatting of RDF/CDF elements

comment:3 Changed 13 months ago by git

  • Commit changed from 829bcc02ed2ef6b3530a27565851edfb01f639a7 to 51a9736237f27229e5c9624c7fbb6d53ec898725

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

51a973629514: implement custom __format__ matching the CDF representation

comment:4 Changed 13 months ago by gh-mwageringel

  • Description modified (diff)

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

comment:5 Changed 13 months ago by gh-mwageringel

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]

comment:6 Changed 11 months ago by egourgoulhon

  • Reviewers set to Eric Gourgoulhon
  • Status changed from needs_review to positive_review

LGTM. Thank you!

comment:7 Changed 11 months ago by gh-mwageringel

Thank you for the review.

comment:8 Changed 10 months ago by vbraun

  • Branch changed from u/gh-mwageringel/29514 to 51a9736237f27229e5c9624c7fbb6d53ec898725
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.