有什么方法可以在 sympy 中进行整数除法? [英] Any way to do integer division in sympy?

查看:34
本文介绍了有什么方法可以在 sympy 中进行整数除法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的表达式,我认为可以简化,我认为 sympy 将是实现它的完美方式.不幸的是,该公式依赖于几个整数除法,我找不到任何方法来表示 sympy 中的那些.

<预><代码>>>>x=符号('x')>>>(x+1)/2x/2 + 1/2

显然不是我想要的,1/2 不是整数.

<预><代码>>>>(x+1)//2类型错误:不支持的操作数类型//: 'Add' 和 'int'

显然sympy 不处理//.

<预><代码>>>>整数((x+1)/2)# 一长串错误信息,以...结尾TypeError: Integer 只能用于整数表达式.

似乎 Integer 只适用于常数,而不是公式.

有一个函数 trunc 但它似乎没有做任何与我想要的类似的事情.

有没有办法在sympy中表示整数除法?

解决方案

标准

我假设您想要一个通过以下测试的函数 div:

from sympy import sympify,简化,符号def test_div(div):# 检查 div 对整数的行为是否符合预期对于范围内的 i (-5,5):对于范围内的 j(-5,5):如果 j==0:继续断言 i//j == div(sympify(i),sympify(j))# 检查div的输出是否可以简化x = Symbol("x", 整数=真)断言简化( div(x+1,2) - div(x-1,2) ) == 1

取模

您可以使用模运算符实现整数除法,如下所示:

div = lambda x,y: (x-x%y)/y

由于 SymPy 支持模运算并且能够对其进行简化,因此该函数通过了上述测试.但是,如果无法进行完全简化,您最终会得到可能不受欢迎的模表达式.

地板

正如评论中已经提到的,SymPy 提供了一个 floor 函数,该函数可用于获取整数除法(这也是 // 运算符用于表达式已实现):

div = lambda x,y: sympy.floor(x/y)

然而,floor 不支持简化,因此第二次测试失败.

I have a very long expression that I think can be simplified, and I thought sympy would be the perfect way to do it. Unfortunately the formula relies on a couple of integer divides, and I can't find any way to represent those in sympy.

>>> x=Symbol('x')
>>> (x+1)/2
x/2 + 1/2

Clearly not what I want, 1/2 isn't an integer.

>>> (x+1)//2
TypeError: unsupported operand type(s) for //: 'Add' and 'int'

Obviously sympy doesn't handle //.

>>> Integer((x+1)/2)
#   A long list of error messages, ending with ...
TypeError: Integer can only work with integer expressions.

It seems that Integer is only intended to work on constant numbers, not formulas.

There's a function trunc but it doesn't seem to do anything similar to what I want.

Is there any way to represent an integer division in sympy?

解决方案

Criteria

I assume that you want a function div that passes the following tests:

from sympy import sympify, simplify, Symbol

def test_div(div):
    # check that div behaves as intended for integers
    for i in range(-5,5):
        for j in range(-5,5):
            if j==0: continue
            assert i//j == div(sympify(i),sympify(j))

    # check that div’s output can be simplified
    x = Symbol("x", integer=True)
    assert simplify( div(x+1,2) - div(x-1,2) ) == 1

Modulo

You can realise an integer division using the modulo operator as follows:

div = lambda x,y: (x-x%y)/y

As SymPy supports modulo arithmetics and is capable of simplifying it, this function passes the above tests. However, if no full simplification is possible, you will end up with modulo expressions that may be undesired.

Floor

As already mentioned in the comments, SymPy provides a floor function, which could be used to acquire an integer division as (which is also how the // operator for expressions is implemented):

div = lambda x,y: sympy.floor(x/y)

However, floor does not support simplifications and therefore fails the second test.

这篇关于有什么方法可以在 sympy 中进行整数除法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆