[Python] How can I use the values in my sympy summation in my piecewise properly?

Discussão em 'Python' iniciado por Stack, Dezembro 19, 2024.

  1. Stack

    Stack Membro Participativo

    I have a sympy summation in python and I want to add the summation value that my sympy symbol is currently to a variable in python so it can use numbers to run non symbolic functions.

    The code I'm using is:

    from sympy import symbols, summation, Piecewise, Eq

    # Define the symbols
    n = symbols('n', integer=True, nonnegative=True)
    x = symbols('x') # Example formula variable

    # Define your formula (e.g., x = 2 * n)
    def formula(count,symbol):
    first = count.to_bytes(32,byteorder='big')
    second = first.from_bytes(32, byteorder="big")
    third = second **2
    return third
    # Define the number you're looking for (e.g., find first occurrence of 6)
    target = 1

    # Define a Piecewise condition to detect the first occurrence
    piecewise_logic = Piecewise(
    (formula(0 + target ,n),Eq(n, target)), # If the formula equals the target, use it
    (0, True) # Else, return 0
    )

    # Perform the summation (simulate finding the first occurrence)
    result = summation(piecewise_logic, (n, 0, 10)) # Summing over range 0 to 10

    print(result)


    As you can see in my code the sympy summation is from 0 to 10.

    In my piecewise, target is just a placeholder, it should be whatever value the symbol is in the summation 0 through 10.

    Then it should add to 0 each time n changes its summation value.

    I'm not looking for any loops I want everything instant like this code.

    So basically my question is how can I use each value of the summation at every step of n and add that to 0 in my piecewise?

    Continue reading...

Compartilhe esta Página