In meiner Summierung möchte ich diese Aktion ausführen können, bei der die Variable bei jeder Ausführung, aber in jedem Schritt meiner Summierung, aktualisiert wird. Ich zeige Ihnen, wovon ich spreche.
Der Code, den ich verwende, ist:
Code: Select all
from sympy import symbols, summation,Piecewise, invert
x = symbols('n')
def add(P1_c, P2_c, p, k):
if P1_c == 0:
return P2_c # P1 is the point at infinity
if P2_c == 0:
return P1_c # P2 is the point at infinity
x1 = P1_c % k
y1 = P1_c // k
x2 = P2_c % k
y2 = P2_c // k
if x1 == x2 and y1 != y2:
return 0
if x1 == x2 and y1 == y2:
m = (3 * x1 ** 2 * invert(2 * y1, p)) % p
else:
m = ((y2 - y1) * invert(x2 - x1, p)) % p
x3 = (m ** 2 - x1 - x2) % p
y3 = (m * (x1 - x3) - y1) % p
# Return combined number
return x3 + k * y3
def multiply(P_c, scalar, p, k,n):
result= 0 # Point at infinity (neutral element)
current = P_c
result = add(result, current, p, k)
result = add(result, current, p, k)
result = add(result,current,p,k)
# result = point_add(result,current,p,k)
# uncomment to see different results
m = Piecewise(
(result,True)
)
return result
k = 2 ** 256
p = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F
Gx = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)[0]
Gy = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798,
0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8)[1]
G_c = Gx + k * Gy
logic = multiply(G_c, x, p, k, x)
result2 = summation(logic, (x,1, 1)) # Sum of n**2 from n=1 to n=5
print(result2)
Ich möchte, dass ihr mir dabei hilft, die Funktion „Stückweise“ in der Funktion „Multiplizieren“ zu verwenden, die dieses Verhalten für mich ausführt.
Ich möchte, dass die Ergebnisvariable für jeden Schritt in meiner Summierung wiederholt einzeln aktualisiert wird, anstatt es explizit zu sagen, wie ich es in der Multiplikationsfunktion getan habe.