Display of fractions (list)

Hello. You can use Fraction in Python to work with fractions. When such a fractional value is in the list, I made a function `prettyfrac ()` to display and output a nice print. In the subclassing of the Fraction class, it is similar to Display output of floating point number list prettyfloat ().

The following example is the calculation of the coefficient and its display output in Calculation of elliptical arc length.

print(prettyfrac(...)) # print pretty fractions
# ==>  [[1, 1/4, 1/64, 1/256], [1/2, -1/16, -1/128], ...]
from fractions import Fraction

def prettyfrac(x):
    def pfform(x):
        num, den = str(x.numerator), str(x.denominator)
        return num + ("" if den == "1" else "/" + den)
    pfclas = type('', (Fraction,), {'__repr__': pfform, '__str__': pfform})
    def pf(x):
        if isinstance(x, Fraction):
            return pfclas(x)
        else:
            return x
    return map_recur(pf, x)

def map_recur(func, args):
    if isinstance(args, list):
        return [map_recur(func, x) for x in args]
    return func(args)

def binom(frac, n):
    b = 1
    while (n > 0):
        b *= frac / n
        frac, n = frac - 1, n - 1
    return b

def coef(k, maxorder):
    k1 = k if k > 0 else 1
    half = Fraction(1, 2) # 1/2
    return map(lambda j: binom(half,j)*binom(half,j+k)/k1, range((maxorder-k)/2+1))

def main():
    maxorder = 6
    print(prettyfrac([coef(k, maxorder) for k in range(maxorder+1)]))

# ==>  [[1, 1/4, 1/64, 1/256], [1/2, -1/16, -1/128], [-1/16, 1/64, 5/2048], [1/48, -5/768], [-5/512, 7/2048], [7/1280], [-7/2048]]

Recommended Posts

Display of fractions (list)
Display a list of alphabets in Python 3
List of python modules
Copy of multiple List
Display output of a list of floating point numbers
List of activation functions (2020)
Depth of nested list
Display a list of frequently used commands on Zsh
Summary of Python3 list operations
Japanese display of matplotlib, seaborn
Formatted display of pandas DataFrame
Operation of filter (None, list)
List of nodes in diagrams
List of self-made Docker images
Multidimensional array initialization of list
About approximate fractions of pi
[Python] Copy of multidimensional list
List of useful coding styles
How to display a list of installable versions with pyenv
2015-11-26 python> Display the function list of the module> import math> dir (math)
Judgment of if by list comprehension
[Python] Display only the elements of the list side by side [Vertical, horizontal]
Waveform display of audio in Python
List of packages installed by conda
List of frequently used Linux commands
Separate display of Python graphs (memo)
Generate a list of consecutive characters
Introduction of ls command lsix that can display a list of images
About the basics list of Python basics
Note: List of customized special names
[Linux] List of Linux commands used in practice
Algorithm Gymnastics 24 Middle of the Linked List
Get the column list & data list of CASTable
External display of matplotlib diagrams using tkinter
[Python] Display list elements with variadic arguments
Display the graph of tensorBoard on jupyter
String conversion of a list containing numbers
Basic grammar of Python3 series (list, tuple)
List of Atom packages I really use
Summary of built-in methods in Python list
[Machine learning] List of frequently used packages
Summary of how to use Python list
[Python] Get a list of folders only