Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "format string" support for tensors (mratsim#655)
This comes in 2 forms: 1. Add a version of the pretty function that takes a format string as its input instead of a precision value. Also add a new "showHeader" argument to the original pretty function. - This new version of pretty lets you specify the format string that must be used to display each element. It also adds some new tensor-specific format string "tokens" that are used to control how tensors are displayed (beyond the format of each element). See below for more details. 2. Add a formatValue procedure that takes a tensor as its first input. This makes it possible to control how tensors are displayed in format strings, in the exact same way as if you were using the new pretty(specifier) procedure. This new formatValue procedure takes all of the standard format specifiers, such as "f", "g", "+", etc. (and including, in nim 2.2 and above, the 'j' specifier for complex tensors) which are used to configure of each tensor element is displayed. In addition to those standard format specifiers you can use a few, tensor specific modifiers which can be combined to achieve different results: - "[:]": Display the tensor as if it were a nim "array". This makes it easy to use the representation of a tensor in your own code. No header is shown. - "<>[:]": Same as "[:]" but displays a header describing the tensor type and shape. - "[]": Same as "[:]" but displays the tensor in a single line. No header is shown. - "<>[:]": Same as "[]" but displays a header describing the tensor type and shape. - "||": "Pretty-print" the tensor _without_ a header. - "<>||": Same as "||" but displays a header describing the tensor type and shape. This is the default display mode. - "<>": When used on its own (i.e. not combined with "[:]", "[]" and "||" as shown above) it is equivalent to "<>[]" (i.e. single-line, nim-like representation with a header). Cannot wrap the element specifier. Notes: - The default format (i.e. when none of these tensor-specific tokens is used) is pretty printing with a header (i.e. "<>||"). - These tensor specific modifiers can placed at the start or at the end of the format specifier (e.g. "<>[:]06.2f", or "06.2f<>[:]"), or they can "wrap" the element specifier (e.g. "<>[:06.2f]"). - When wrapping you cannot use the "compact forms" of these specifiers (i.e. "<:>" and "<|>"). - When using the wrapping form of "[:]", start with "[:" and end with either "]" or ":]" (i.e. both "[:06.2f]" and "[:06.2f:]" are valid). - This version of this function does not have a `showHeader` argument because to disable the header you can simply select the right format specifier. Fixes after PR comments More fixes
- Loading branch information