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)
| 1881 | return expr |
| 1882 | |
| 1883 | def 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 | |
| 1929 | def ilt(expr, s, t, integrate=False): |
| 1930 | """ |
no test coverage detected