Hi community when I run my script I receive the recursion error. I don't know how to go about it since my program is due in less than an hour. If someone can give me some insight that would be great! It is a two part script.
My code
import math
import random
from random import randrange, uniform
def add(u, v):
sum1 = [x + y for x, y in zip(u, v)]
return sum1
def negate(u):
myng = [x - 2*y for x, y in zip(u, u)]
return myng
def sub(u,v):
d = [x - y for x, y in zip(u, v)]
return d
def scalarMult(c, u):
m = [(x+y-y)*c for x, y in zip(u,u)]
return m
def zip(u,v):
mt = [x * y for x, y in zip(u, v)]
return mt
def dot(u, v):
l = zp(u,v)
a = sum(l)
return a
def length(u):
d = dot(u,u)
x = math.sqrt(d)
return x
def unit(v):
l = length(v)
m = [(x+y-y)/l for x,y in zip(v,v)]
return m
def randVector(n, a, b):
l = [] * n
i = 0
while(i<n):
l.append(random.uniform(a,b))
i=i+1
return l
#------------------------------------------------------------------------------
# VectorTest.py
#------------------------------------------------------------------------------
import Vector
A = [-3, -4, 7]
B = [6, -2, 2]
print(A)
print(B)
print(Vector.add(A,B))
print(Vector.negate(B))
print(Vector.sub(A,B))
print(Vector.scalarMult(2.5,A))
print(Vector.scalarMult(-3.5,B))
print(Vector.zip(A,B))
print(Vector.dot(A,B))
print(Vector.length(A))
print(Vector.length(B))
print(Vector.unit(A))
print(Vector.unit(B))
print(Vector.angle(A,B))
C = Vector.randVector(3,-10,10)
print(Vector.sub(C,C))
Your zip function infinitely calls itself, generating an infinite recursion.
def zip(u, v):
mt = [x * y for x, y in zip(u, v)]
return mt
When you call it, it has to call itself to get its value. When it's called this way, it calls itself again and again, never completing.
Related
I am looking for a reduced way of this code. I had to do the division separately because of the multiplicative inverse condition.
"""This code calculates the multiplication Elliptic curves over Zp"""
#Initial values for testing
yq = 3
yp = 3
xq = 8
xp = 8
a = 1
p = 11
#Calculate the Euclid greatest common divisor
def egcd(a, b):
if a == 0:
return (b, 0, 1)
else:
g, y, x = egcd(b % a, a)
return g, x - (b // a) * y, y
#Calculate the multiplicate inverse
def modinv(a, m):
g, x, y = egcd(a, m)
if g != 1:
raise Exception('Mod inverse does not exist')
else:
return x % m
#veces = Number of times for the multiplication
veces = 7
print(f"The next results are the multiplication of {veces}*({xp},{yp})")
z = 1
while z <= (veces - 1):
if xp == xq and yp == yq:
numerador = (3 * pow(xp, 2) + a) % p
denominador = ((2 * yp) % p)
inver = modinv(denominador, p)
landa = (inver * numerador) % p
else:
numerador = (yq - yp) % p
denominador = (xq - xp) % p
inver = modinv(denominador, p)
landa = (inver * numerador) % p
xr = (pow(landa, 2) - xp - xq) % p
yr = (landa * (xp - xr) - yp) % p
z += 1
xp, yp = xr, yr
print(f"The result is ({xp},{yp})")
#Any way to simplify this code? I had to do the division separately but I think we can a reducted code for the division.
I've got a math equation that I want to visually record the substitution of.
The equation is y = h * f(t + h, h + f(t, h)), where f(x, y) = x + y - 1
I want to substitute f into y, such that I get:
y = h * f(t + h, h + (t + h - 1))
y = h * (t + h + h + (t + h - 1) - 1)
I've had issues with replace not allowing me to do multi-parameter substitution
I don't have much code, since I'm not sure how to implement it
from sympy import *
f = Function('f')(x, y)
eqn = h * f(t + h, h + f(t, h))
Thanks
sympy.Function is used for declaring undefined functions but in your case the function is known.
The following code seems to work fine over here
from sympy import *
x,y,t,h = symbols('x y t h')
def f(x,y):
return x + y - 1
y = h * f(t+h,h+f(t,h))
y = expand(y)
display(y)
The role of the expand function was to work out the outer multiplication by h in the definition of y.
You can run it in a Jupyter notebook or as an alternative use the print or display function, I get the following result:
Extending average's answer -- Their solution works perfectly if the function is known.
To make it work for a function that's input from the user, you need to do this:
function = input()
def f(x, y, evaluate = False):
eqn = sympify(function)
if evaluate:
eqn = eqn.subs([("x", x), ("y", y)])
return eqn
y = h + f(h, t, True)
This way, if the user inputs "x ** y" for f, y will expand to h + h ** t
Why both codes below will give the same result if we pass the variables (x,y) to the function and without passing it
First Code
def gen():
yield (x + y)
yield (x - y)
x = int(input("X = "))
y = int(input("Y = "))
g = gen()
while True:
try:
print(next(g))
except:
print("END!")
break
Second Code
def gen(x,y):
yield (x + y)
yield (x - y)
x = int(input("X = "))
y = int(input("Y = "))
g = gen(x,y)
while True:
try:
print(next(g))
except:
print("END!")
break
x = random.randint(1, 100)
y = random.randint(1, 100)
myList = [x, y]
newNumber = random.randint(x, y)
but when...
myList = [(nsmallest(2, guesses, key=lambda x: abs(x-number)))]
I don't know what the numbers are, and how to use them as arguments (x, y) to determine the newNumber
"Unpacking Argument Lists"
random.randint(*myList)
The "main" code works to calculate every single binomial coefficient, unless (n = k + 1). It is mind boggling - it says there is a division by zero error but I cannot see why. the error occurs in the main function ( d = n2 / c ). Any idea why?
def getInteger( prompt ):
while True:
try:
num = int( input(prompt))
except ValueError:
print( "That is not an integer -- please try again")
continue
return num
def factorial(f):
f = f
q = (f - 1)
fac = (f * q)
while (q) > 1:
q -= 1
fac = (fac * q)
return (fac)
def main():
n = getInteger("enter a factor:")
k = getInteger("enter a factor:")
while n >= k :
n2 = factorial(n)
k2 = factorial(k)
a = n - k
b = factorial(a)
c = b * k2
d = n2 / c
print("n2 = {} k2 = {} a = {} b = {} c = {} d = {}" .format(n2, k2, a, b, c, d) )
return
else:
print("n must be larger than k")
if __name__ == '__main__':
main()
main()
Note that I need to implement the calculations myself so I cannot use libraries.
Your factorial function is not correct for the inputs 0,1. It returns 0 for both of them, while it should return 1 for both of them.