我如何继续对我的syspy总和的所有值进行下一个数字?

问题描述 投票:0回答:1

from sympy import symbols, summation, gcdex, Piecewise, Eq, Abs, Mul, Le, mod_inverse, solve, invert, Mod, Max, log, \ floor, simplify, And, Ge import random x = symbols('x') #just a placeholder function that I was trying to change how next() operates so it can go up by 1 each tine i call next() def callFunc(args): if args: return args return args class DynamicIterator: def __init__(self, func, a, b): self.func = func # Function to generate values self.counter = 0 # Keeps track of the current index self.a = a self.b = b self.can_advance = True # Default to allowing automatic advancement self.advanced = False # Flag to control when to advance def __iter__(self): return self def __next__(self, ): if not self.can_advance: raise StopIteration # Stop iteration if cannot advance value = self.func(self.counter, self.a, self.b) # Generate the value self.counter += 1 # Increment the counter return value def generate_combinations(n, a, c): range_size = c - a + 1 l = a + (n % range_size) return l # Only return `l` # Create an instance of the iterator expressions = DynamicIterator(generate_combinations, 1, 2 ** 256 + 1) def next_expr(): iterate = next(expressions) return iterate def formula109(l, integer): random_number = random.randint(integer, integer+1) + l random_number2 = random_number - l # i removed the x so you can see how the values are the same each time without me using x. return random_number2 logic = formula109(x, callFunc(next_expr())) total = summation(logic, (x, 1, 900000)) print(total) #see what numbers are for each value of x terms = [logic.subs(x, k) for k in range(1, 10)] print(terms)

在运行此操作时,您可以看到,值一直保持在2。我知道,如果我将X添加到它,那将会上升,但这不是我想要的。我希望每次都可以每次使用next()来迭代x的每个值。有人可以告诉我该怎么办吗?

输出为:

1800000
[2, 2, 2, 2, 2, 2, 2, 2, 2]

如果我将您的代码复制到

ipython
会话中(就像我上个月一样),然后查看
logic
python random sympy
1个回答
0
投票
In [193]: logic = formula109(x, callFunc(next_expr())) In [194]: logic Out[194]: 2

是一个数字,而不是一个表达式(剥离
x
脱落可以做到这一点)。  再次查看它不会改变价值。
In [195]: logic Out[195]: 2

next查看制作
logic

的组件:

In [196]: next_expr()
Out[196]: 2

In [197]: next_expr()
Out[197]: 3

In [198]: next_expr()
Out[198]: 4

ok,这是一个基础。
In [199]: callFunc(next_expr()) Out[199]: 5 In [200]: callFunc(next_expr()) Out[200]: 6 In [201]: callFunc(next_expr()) Out[201]: 7 In [202]: callFunc(next_expr()) Out[202]: 8

callFunc

包装器一样。
In [203]: formula109(x, callFunc(next_expr()))
Out[203]: 
9

In [204]: formula109(x, callFunc(next_expr()))
Out[204]: 
10

In [205]: formula109(x, callFunc(next_expr()))
Out[205]: 
12

In [206]: formula109(x, callFunc(next_expr()))
Out[206]: 
13

formula109
包装器。
In [207]: logic = formula109(x, callFunc(next_expr())) In [208]: logic Out[208]: 13 In [209]: logic = formula109(x, callFunc(next_expr())) In [210]: logic Out[210]: 15

新的分配获得了最新的数字。
,但尝试用

logic
迭代
sums

不会再次分配

logic

In [211]: [logic.subs(x, k) for k in range(1, 4)] Out[211]: [15, 15, 15] In [212]: [logic.subs(x, k) for k in range(1, 4)] Out[212]: [15, 15, 15]

summation

之前是
Sum().doit()

In [213]: Sum(logic, (x,1,5)) Out[213]: Sum(15, (x, 1, 5)) In [214]: Sum(logic, (x,1,5)) Out[214]: Sum(15, (x, 1, 5))

llet的put the返回中,所以
x
是一个。
logic

sympy.Expr

In [215]: def formula109(l, integer):
     ...:     random_number = random.randint(integer, integer+1) + l
     ...:     return random_number
     ...:     

In [216]: logic = formula109(x, callFunc(next_expr()))

In [217]: logic
Out[217]: 
x + 16
加上一个数字,而不是
logic
加上函数调用! 列表理解是

x

x

基础
1+16, 2+16, ...
,最后一个值形式
In [218]: [logic.subs(x, k) for k in range(1, 4)]
Out[218]: [17, 18, 19]
保留在

16
callFunc(next_expr())

中:

Sum
现在,如果我抛弃
summation
In [219]: Sum(logic, (x,1,5))
Out[219]: 
Sum(x + 16, (x, 1, 5))

In [220]: summation(logic, (x,1,5))
Out[220]: 
95

,然后将您的“迭代器”作为普通的python:
sympy

In [221]: [formula109(x, callFunc(next_expr())) for x in range(1,5)]
Out[221]: [17, 20, 22, 24]

In [222]: [formula109(x, callFunc(next_expr())) for x in range(1,5)]
Out[222]: [22, 24, 26, 28]

In [223]: callFunc(next_expr())
Out[223]: 24

In [224]: callFunc(next_expr())
Out[224]: 25

In [225]: [formula109(x, callFunc(next_expr())) for x in range(1,5)]
Out[225]: [27, 30, 32, 33]
每次称为步骤。  但是请注意,我必须将其包括在列表中。
我不带有简单的价值分配
next_expr()
如果我将其包含在功能或lambda
中,我确实会踏得

In [226]: foobar = callFunc(next_expr()); foobar Out[226]: 30 In [227]: [foobar for i in range(3)] Out[227]: [30, 30, 30]
但是,如果我尝试将其包含在

In [231]: foobar = lambda k: callFunc(next_expr()) In [232]: foobar(1) Out[232]: 31 In [233]: foobar(1) Out[233]: 32 In [234]: [foobar(i) for i in range(4)] Out[234]: [33, 34, 35, 36] In [235]: [foobar(i) for i in range(4)] Out[235]: [37, 38, 39, 40]

中,我会遇到错误。  任何功能都可能是正确的。
Sum

我得出的结论与上个月相同。  
In [239]: Sum(foobar, (x,1,5)) C:\Users\14256\miniconda3\lib\site-packages\sympy\concrete\expr_with_limits.py:26: SymPyDeprecationWarning: The string fallback in sympify() is deprecated. To explicitly convert the string form of an object, use sympify(str(obj)). To add define sympify behavior on custom objects, use sympy.core.sympify.converter or define obj._sympy_ (see the sympify() docstring). sympify() performed the string fallback resulting in the following string: '<function <lambda> at 0x000001B818FCE290>' See https://docs.sympy.org/latest/explanation/active-deprecations.html#deprecated-sympify-string-fallback for details. This has been deprecated since SymPy version 1.6. It will be removed in a future version of SymPy. function = sympify(function) ValueError: Error from parse_expr with transformed code: "<Symbol ('function' )<lambda >Symbol ('at' )Integer (0x000001B818FCE290 )>" The above exception was the direct cause of the following exception: File <string>:1 <Symbol ('function' )<lambda >Symbol ('at' )Integer (0x000001B818FCE290 )> ^ SyntaxError: invalid syntax During handling of the above exception, another exception occurred: SympifyError: Sympify of expression 'could not parse '<function <lambda> at 0x000001B818FCE290>'' failed, because of exception being raised: SyntaxError: invalid syntax (<string>, line 1)

根本不像Python列表理解或

Sum/summation
。 看起来它可以进行重复

map

,但是

expr.subs(x)
不能包含函数。  这不是一个
expr

lambda

在Python内(或顶部)运行。 它不会重写或重新定义Python语法或解释。

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.