Matrix multiplication in pythonwithout numpy

# write your python code here
# you can take the above example as sample input for your program to test
# it should work for any general input try not to hard code for only given input examples


# you can feel free to change all these codes/structure
# here A and B are list of lists
import numpy as np
def matrix_mul():
    
    a=[]
    ac=0
    ace=0
    bc=0
    bce=0
    result=[]
    

  
    A   = [[1,3,4],
             [2,5,7],
             [5,9,6]]
    B   = [[1,0,0],
             [0,1,0],
             [0,0,1]]
  
    
    print(A,B,len(A[0]),len(B[0]),len(A),len(B))
    for i in range(len(A)):
        ac+=1
    for j in range(len(A[0])):
        ace+=1
    for i in range(len(B)):
        bc+=1
    for j in range(len(B[0])):
        bce+=1
    if ace==bc:
        
        
        
            for b in range(bce):
                ele=0
                a.append(ele)
            for c in range(len(A)):
                result.append(a)
            print(result)   
            print(A,B,len(A[0]),len(B[0]),len(A),len(B))

            
            for i in range(len(A)): 
                # iterating by coloum by B  
                for j in range(len(B[0])): 
                    # iterating by rows of B 
                    for k in range(len(B)): 
                        result[i][j] += A[i][k] * B[k][j]   
                        
                        
            print(result)            
            result=np.dot(A,B)
            print(result)
            print(len(A),len(B),ac,ace,bc,bce)
            
    else:
        
        
        print("Can't multiply",A,B)
        print(len(A),len(B),ac,ace,bc,bce)

        
matrix_mul()

thats my code for matrix multiplication in which the key code is

            for i in range(len(A)): 
                # iterating by coloum by B  
                for j in range(len(B[0])): 
                    # iterating by rows of B 
                    for k in range(len(B)): 
                        result[i][j] += A[i][k] * B[k][j]   

It works fine on geeksforgeeks,programmiz and for people on stackoverflow but doesnt work on my jupyter notebook. I don’ t understand the reason. Please help.

It produces output:

 [[8, 17, 17], [8, 17, 17], [8, 17, 17]]

Output It should produce:

[[1 3 4]
 [2 5 7]
 [5 9 6]]

also it should also work for inputs:

A   = [[1,2],
             [3,4]]
      B   = [[1,2,3,4,5],
             [5,6,7,8,9]]

Thanks.

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

Pre-formatted-text

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

You probably have different versions of Python in each of those environments. I would explicitly preallocate your matrix in the desired shape. You are getting lucky using += on uninitialized entries anyway.

Noted with Thanks

ya actually my jupyter notebook is using 2.7 ver
while my pc python is 3.7 I tried upgrading my jpnb but it’s still the same.
Although I don’t understand how does that affect matrix multiplication operation.

You should never use Python 2. Python 2 is dead.

The difference in versions isn’t ‘affecting matrix multiplication’; the difference in version is affecting the shape of the multiply indexed array.

You should

  1. Upgrade and never, ever use Python 3

  2. Initialize your array

You should have something like

array = [[0 for i in range(....)] for j in range(...)]

with the correct sizes for numbers of rows and columns filled in for the ranges.

thanks but now how do i update my jpnb python, pandas

It depends upon how you installed it in the first place. I would uninstall jupyter and reinstall, making sure that you use Python 3 or pip3.