其他#

Miscellaneous stuff that does not really fit anywhere else.

sympy.utilities.misc.as_int(n, strict=True)[源代码]#

将参数转换为内置整数。

保证返回值等于输入值。如果输入具有非整数值,则引发ValueError。什么时候? strict 是真的,这使用 __index__ 当它是假的,它使用 int .

实例

>>> from sympy.utilities.misc import as_int
>>> from sympy import sqrt, S

该函数主要用于清理需要使用内置整数的函数的输入,因此任何明确为整数的函数都应作为int返回:

>>> as_int(S(3))
3

由于精度有限,浮动不被认为是精确的,并且会引起错误,除非 strict 标志为False。对于大浮点数,这种精度问题变得很明显:

>>> big = 1e23
>>> type(big) is float
True
>>> big == int(big)
True
>>> as_int(big)
Traceback (most recent call last):
...
ValueError: ... is not an integer
>>> as_int(big, strict=False)
99999999999999991611392

默认情况下,可能是整数值的复杂表示形式的输入也会被拒绝:

>>> one = sqrt(3 + 2*sqrt(2)) - sqrt(2)
>>> int(one) == 1
True
>>> as_int(one)
Traceback (most recent call last):
...
ValueError: ... is not an integer
sympy.utilities.misc.debug(*args)[源代码]#

打印 *args 如果SYMPY_DEBUG为真,则不执行任何操作。

sympy.utilities.misc.debug_decorator(func)[源代码]#

如果SYMPY_DEBUG为真,它将打印一个漂亮的执行树,其中包含所有修饰函数的参数和结果,否则什么都不做。

sympy.utilities.misc.debugf(string, args)[源代码]#

Print string%args if SYMPY_DEBUG is True, else do nothing. This is intended for debug messages using formatted strings.

sympy.utilities.misc.filldedent(s, w=70, **kwargs)[源代码]#

Strips leading and trailing empty lines from a copy of s, then dedents, fills and returns it.

空行剥离用于处理像这样的docstring,它在初始三重引号后以新行开头,在字符串的开头插入空行。

Additional keyword arguments will be passed to textwrap.fill().

参见

strlines, rawlines

sympy.utilities.misc.find_executable(executable, path=None)[源代码]#

尝试在“path”中列出的目录中查找“executable”(一个用“path”分隔的目录的字符串操作系统路径';默认为操作系统环境 ['路径'] ). 返回完整的文件名,如果找不到,则不返回

sympy.utilities.misc.func_name(x, short=False)[源代码]#

返回函数名 \(x\) (如有定义)否则 \(type(x)\) . 如果short为True,并且结果的别名较短,则返回别名。

实例

>>> from sympy.utilities.misc import func_name
>>> from sympy import Matrix
>>> from sympy.abc import x
>>> func_name(Matrix.eye(3))
'MutableDenseMatrix'
>>> func_name(x < 1)
'StrictLessThan'
>>> func_name(x < 1, short=True)
'Lt'
sympy.utilities.misc.ordinal(num)[源代码]#

第1个字符串的第1个数字变为序号1。

sympy.utilities.misc.rawlines(s)[源代码]#

返回一个剪切可粘贴的字符串,当打印时,该字符串与输入等效。当字符串中有多行时使用此选项。返回的字符串经过格式化,因此可以在测试中很好地缩进;在某些情况下,它被包装在dedent函数中,而dedent函数必须从textwrap导入。

实例

注意:因为下面的示例中有一些字符需要转义,因为它们本身都在三引号的docstring中,所以下面的表达式看起来比在解释器窗口中打印时复杂得多。

>>> from sympy.utilities.misc import rawlines
>>> from sympy import TableForm
>>> s = str(TableForm([[1, 10]], headings=(None, ['a', 'bee'])))
>>> print(rawlines(s))
(
    'a bee\n'
    '-----\n'
    '1 10 '
)
>>> print(rawlines('''this
... that'''))
dedent('''\
    this
    that''')
>>> print(rawlines('''this
... that
... '''))
dedent('''\
    this
    that
    ''')
>>> s = """this
... is a triple '''
... """
>>> print(rawlines(s))
dedent("""\
    this
    is a triple '''
    """)
>>> print(rawlines('''this
... that
...     '''))
(
    'this\n'
    'that\n'
    '    '
)
sympy.utilities.misc.replace(string, *reps)[源代码]#

返回 string 所有钥匙都插好了 reps 替换为相应的值,先替换较长的字符串,而不考虑给定的顺序。 reps 可以作为元组或单个映射传递。

实例

>>> from sympy.utilities.misc import replace
>>> replace('foo', {'oo': 'ar', 'f': 'b'})
'bar'
>>> replace("spamham sha", ("spam", "eggs"), ("sha","md5"))
'eggsham md5'

如果映射中的键重叠(即长度相同,并且在开始/结束处具有相同的序列),则无法保证将获得唯一答案:

>>> reps = [
...     ('ab', 'x'),
...     ('bc', 'y')]
>>> replace('abc', *reps) in ('xc', 'ay')
True

工具书类

sympy.utilities.misc.strlines(s, c=64, short=False)[源代码]#

返回一个剪切可粘贴的字符串,当打印时,该字符串与输入等效。这些行将被圆括号包围,并且任何行的长度都不得超过c(默认为64个)字符。如果行包含换行符,则 \(rawlines\) 结果将返回。如果 short 为True(默认值为False),则如果有一行,则返回时将不带边框。

实例

>>> from sympy.utilities.misc import strlines
>>> q = 'this is a long string that should be broken into shorter lines'
>>> print(strlines(q, 40))
(
'this is a long string that should be b'
'roken into shorter lines'
)
>>> q == (
... 'this is a long string that should be b'
... 'roken into shorter lines'
... )
True
sympy.utilities.misc.translate(s, a, b=None, c=None)[源代码]#

返回 s 其中字符已被替换或删除。

句法

翻译(s,None,deletechars):

中的所有字符 deletechars 被删除

翻译,地图 [,删除字符] ):

中的所有字符 deletechars (如果提供)被删除,那么将执行map定义的替换;如果map的键是字符串,则首先处理较长的键。多字符删除的值应为“”。

翻译(s,oldchars,newchars,deletechars)

中的所有字符 deletechars 然后删除每个字符 oldchars 替换为中的相应字符 newchars

实例

>>> from sympy.utilities.misc import translate
>>> abc = 'abc'
>>> translate(abc, None, 'a')
'bc'
>>> translate(abc, {'a': 'x'}, 'c')
'xb'
>>> translate(abc, {'abc': 'x', 'a': 'y'})
'x'
>>> translate('abcd', 'ac', 'AC', 'd')
'AbC'

如果映射重叠中的键的长度相同,并且在开始/结束处具有相同的序列,则无法保证将获得唯一答案:

>>> translate(abc, {'ab': 'x', 'bc': 'y'}) in ('xc', 'ay')
True