Difficulty in implementing Strassen's algorithm in Python - python
I don't understand how to call my code recursively. Here is my code so far:
import numpy
B = [[5,5,5,5,5,5,5,5],[6,6,6,6,6,6,6,6],[7,7,7,7,7,7,7,7],[8,8,8,8,8,8,8,8],
[9,9,9,9,9,9,9,9], [10,10,10,10,10,10,10,10],[11,11,11,11,11,11,11,11], [12,12,12,12,12,12,12,12]]
A = [[5,5,5,5,5,5,5,5],[6,6,6,6,6,6,6,6],[7,7,7,7,7,7,7,7],[8,8,8,8,8,8,8,8],
[1,1,1,1,1,1,1,1], [2,2,2,2,2,2,2,2],[3,3,3,3,3,3,3,3],[4,4,4,4,4,4,4,4]]
def main():
strassen(A,B)
def strassen(A, B):
A = numpy.asarray(A)
B = numpy.asarray(B)
lengthA = len(A)
lengthB = len(B)
if lengthA == 2:
print "will calculate"
else:
a, b = strassen(A[:lengthA//2, :lengthA//2], B[:lengthB//2, :lengthB//2])
lengthA = lengthA//2
lengthB = lengthB//2
print a
print b
return a, b
I'm trying to reduce a to [[5,5],[6,6]] and b to [[5,5],[6,6]] but I'm getting an error:
a, b = strassen(A[:lengthA//2, :lengthA//2], B[:lengthB//2, :lengthB//2])
TypeError: 'NoneType' object is not iterable.
a and b are the 1st 2x2 matrices that will be formed after the 2nd whole matrix division for A and B. Please can someone explain this to me. Thanks
You have no return value in your recursion terminating condition. When I run your code, it prints "will calculate" before giving the error. The error happens after that because there is no return value from the strassen function on the last call (when lengthA == 2).
Related
Unable to crack Python code for GCD of 3 numbers?
I am trying to find the GCD of 3 numbers but have been unable to get around much success till now. I am using the following code def gcd(a, b,c): if sum(np.array(list([a,b,c]))%min(a,b,c))==0: return min(a,b,c) else: x = np.array(list([a,b,c]))%min(a,b,c) if sum((list([x]))%min(x))==0: return min(x) When I run this on an example say gcd(21,45,60), it gives me the below error C:\Users\mmt8091\Anaconda3\lib\site-packages\ipykernel_launcher.py:6: RuntimeWarning: divide by zero encountered in remainder What am i doing wrong here? Have been unable to find any other solutions on net as well. Please help
You do not need np. Try this: def gcd(*args): a, b, *c = args a, b = min(a, b), max(a, b) gcd1 = b if not a else gcd(b % a, a) for i in c: gcd1 = gcd(gcd1, i) return gcd1 print(gcd(21, 45, 60))
finding the LCM using python
def multiple(a, b): """so I'm trying to return the smallest number n that is a multiple of both a and b. for example: multiple(3, 4) 12 multiple(14, 21) 42 """ def gcd (a,b): if a < b : a , b = b,a while b: a , b = b , a % b return a def lcm (a , b): n= (a*b) / gcd(a,b) return n it keeps throwing errors about indentation and logic. I don't understand why. I've tried changing the variables around too.
No need to find GCD, we can directly find LCM. Below code works def lcmof(x,y): res=0 mx=max(x,y) mn=min(x,y) for i in range(1,mx+1,1): temp=mx*i try: if(temp%mn==0): res=temp break except ZeroDivisionError: res=0 break return res
Python GCD - errors
I have an issue with my code whose purpose is to find the GCD of two inputs. When I try to run the module it tells me that 'gcd' is not defined. def GCD(12,4): gcd = 1 for i in range(2, max(12,4)/2): if((12 % i == 0) and (4 % i == 0)): gcd = i return gcd
You are not calling the GCF function. You have just defined your function. You need to add a line gcf = GCF(a,b) after the place where you accept the input. That is after b = int(input('denomenator: ')) Edit: Change the input statements to a = float(input('numerator: ')) b = float(input('denomenator: '))
You can use Euclid division algorithm to find gcd in less time. Take floating point numbers to a and b. def gcd(a,b): c = 1 a,b = max(a,b),min(a,b) while c != 0: c = a%b a,b = b,c return a print gcd(12,5)
Middle value using python 3
UPDATED a = int(input("Give a value: ")) b = int(input("Give a value: ")) c = int(input("Give a value: ")) def middle(a, b ,c) : m = min(a,b,c) M = max(a,b,c) return a+b+c-m-M This is where im at. It takes my numbers into the data. How would I get it to display the middle one?! Sorry I'm so terrible at this. Way in over my head on this intro course. #CommuSoft #Zorg #paxdiablo and everyone else
Like others mentioned, you're missing a colon, but for simplicity sake: def middle(a, b, c): return sorted([a, b, c])[1]
You should put a colon (:) on the first line (def) as well. This works for the online python environment: def input(a, b, c) : if a <= b <= c or c <= b <= a : return b elif b <= a <= c or c <= a <= b : return a else: return c Furthermore it is more advisable to make use of min and max I guess. Min and max are sometimes directly supported by a CPU and there are implementations that avoid branching (if-then-else's): def input(a, b, c) : m = min(a,b,c) M = max(a,b,c) return a+b+c-m-M or: def input(a, b, c) : return min(max(a,b),max(b,c),max(a,c)) The last one is also numerically stable. In most cases if-then-else clauses should be avoided. They reduce the amount of pipelining although in interpreted languages this might not increase performance. Based on the comments, I guess you want to write an interactive program. This can be done like: def middle(a, b, c) : #defining a method return min(max(a,b),max(b,c),max(a,c)) a = int(input("Give a value: ")) b = int(input("Give b value: ")) c = int(input("Give c value: ")) print("The requested value is ") print(middle(a,b,c)) #calling a method Defining a method will never result in Python using that method. The a, b and c in the def block are not the a, b and c in the rest of your program. These are "other variables that happen to have the same name". In order to call a method. You write the methods name and between brackets the parameters with which you wish to call your method.
Post your full syntax error (or any other full traceback) whenever you're having trouble. And your def line needs a colon.
You could do: def middle(a,b,c): s={a,b,c} s-={min(s),max(s)} return s.pop() What this does: Create a set of the unduplicated values: >>> a,b,c=1,2,3 >>> s={a,b,c} >>> s {1, 2, 3} Remove the max and the min, leaving the middle: >>> s-={min(s), max(s)} >>> s {2} Pop the only remaining value: >>> s.pop() 2
Python programming beginner difficulties
I am trying to write a program in Python, but I am stuck in this piece of code: def function(): a=[3,4,5,2,4] b=1 c=0 for x in range(5): if a[x-1]>b: c=c+1 return c print(function()) It gives me value 1 instead of 5. Actually the function I am trying to write is a little bit more complicated, but the problem is actually the same, it doesn't give me the right result. def result(): r=[0]*len(y) a=2 b=an_integer while b>0: for x in range(len(y)) : if y[x-1] > 1/a and b>0: r[x-1]=r[x-1]+1 b=b-1 a=a+1 return r print(result()) v is a list of values smaller than 1 and b has an integer as value. If some values x in v are bigger than 1/a then the values x in r should get 1 bigger, then it should repeat a=a+1 until b becomes 0. I want this function to give a result of the type for ex. [7,6,5,4,3] where the sum of the elements in this list is equal to b. Sometimes it gives me the right value, sometimes not and when the elements in v are equal for example v=[0.33333,0.33333,0.33333] it gets stuck and doesn't give me a result. I don't know what I am doing wrong !
Your return statements are incorrectly indented. You want to return after the loop ends, not inside the loop. def function(): a = [3, 4, 5, 2, 4] b = 1 c = 0 for x in range(5): if a[x-1] > b: c = c + 1 return c Also, a couple of optimizations to the code: def function(a, b): c = 0 for x in a: if x > b: c += 1 return c or further: def function(a, b): return sum(x > b for x in a)
return; only inside the fun in the end it. and name the Variable v