значение next() в моем суммировании увеличивается только на 1 один раз, а затем использует это же значение для всего суммирования. Я хочу иметь возможность использовать следующее значение каждый раз при суммировании, как если бы я сказал в Python: next("iterable") next("iterable") next("iterable")
Код, который я сейчас использую:
Код: Выделить всё
from sympy import summation, Eq, symbols
x = symbols('x')
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
if x != 0:
self.counter += 1
return value
def control_advance(self, can_advance):
"""Control whether the iterator can advance or not."""
self.can_advance = can_advance
# Define the function that returns the value of `l`
def generate_combinations(n, a, c):
l = 0
l += 1
return l # Only return `l`
# Create an instance of the iterator
expressions = DynamicIterator(generate_combinations, 1, 200)
# Define a lambda to get the next value of `l`
next_expr = lambda: next(expressions) + x
total = summation(next_expr(), (x, 1, 3))
print(total)
Может кто-нибудь помочь мне решить эту проблему?
Подробнее здесь: https://stackoverflow.com/questions/793 ... -the-summa