MCPcopy Index your code
hub / github.com/SLiCAP/SLiCAP_python / roundN

Function roundN

SLiCAP/SLiCAPmath.py:1883–1927  ·  view source on GitHub ↗

Rounds all float and rational numbers in an expression to ini.disp digits, and converts integers into floats if their number of digits exceeds ini.disp :param expr: Input expression :type expr: sympy.Expr :param numeric: True if numeric evaluation (pi = 3.14...) must be done

(expr, numeric=False)

Source from the content-addressed store, hash-verified

1881 return expr
1882
1883def roundN(expr, numeric=False):
1884 """
1885 Rounds all float and rational numbers in an expression to ini.disp digits,
1886 and converts integers into floats if their number of digits exceeds ini.disp
1887
1888 :param expr: Input expression
1889 :type expr: sympy.Expr
1890
1891 :param numeric: True if numeric evaluation (pi = 3.14...) must be done
1892 :type numeric: Bool
1893
1894 :return: modified expression
1895 :rtype: sympy.Expr
1896 """
1897 if not isinstance(expr, sp.core.Basic):
1898 try:
1899 expr = sp.sympify(str(expr))
1900 except sp.SympifyError:
1901 print("Error in expression:", expr)
1902 return None
1903 if numeric:
1904 expr = sp.N(expr, ini.disp)
1905 else:
1906 # Convert rationals into floats
1907 expr = rational2float(expr)
1908 # Clean-up the expression
1909 try:
1910 # Round floats to display accuracy
1911 expr = expr.xreplace({n: sp.Float(n, ini.disp)
1912 for n in expr.atoms(sp.Float)})
1913 # Convert floats to int if they can be displayed as such
1914 maxInt = 10**ini.disp
1915 floats = expr.atoms(sp.Float)
1916 for flt in floats:
1917 intNumber = int(flt)
1918 if float(intNumber) == float(flt) and sp.Abs(flt) < maxInt:
1919 expr = expr.xreplace({flt: intNumber})
1920 # Replace large integers with floats
1921 ints = expr.atoms(sp.Integer)
1922 for integer in ints:
1923 if sp.Abs(integer) >= maxInt:
1924 expr = expr.xreplace({integer: sp.Float(integer, ini.disp)})
1925 except AttributeError:
1926 pass
1927 return expr
1928
1929def ilt(expr, s, t, integrate=False):
1930 """

Callers 15

eqnMethod · 0.90
matrixEqnMethod · 0.90
exprMethod · 0.90
eqnInlineMethod · 0.90
_TEXcreateCSVtableFunction · 0.90
_numRoots2TEXFunction · 0.90
_symRoots2TEXFunction · 0.90
_htmlLineMethod · 0.90
_latex_ENGFunction · 0.90
eqn2htmlFunction · 0.90
matrices2htmlFunction · 0.90
pz2htmlFunction · 0.90

Calls 1

rational2floatFunction · 0.85

Tested by

no test coverage detected