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

Add "format string" support for tensors #655

Merged
merged 1 commit into from
Oct 11, 2024

Commits on Oct 7, 2024

  1. Add "format string" support for tensors

    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
    AngelEzquerra committed Oct 7, 2024
    Configuration menu
    Copy the full SHA
    545491d View commit details
    Browse the repository at this point in the history