# Matrix multiplication in pythonwithout numpy

**URL:** <https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781>\
**Category:** Curriculum Help\
**Tags:** python\
**Created:** [September 20, 2020, 9:54am UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781 "2020-09-20T09:54:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![harshadkoli7](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/harshadkoli7/32/150355_2.png) [@harshadkoli7](https://forum.freecodecamp.org/u/harshadkoli7)\
**Post date:** [September 20, 2020, 9:54am UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781/1 "2020-09-20T09:54:29Z")

</div>

```python
# 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

```python
            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:

```python
 [[8, 17, 17], [8, 17, 17], [8, 17, 17]]

```

Output It should produce:

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

```

also it should also work for inputs:

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

```

Thanks.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [September 20, 2020, 9:58am UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781/2 "2020-09-20T09:58:38Z")

</div>

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](https://global.discourse-cdn.com/freecodecamp/original/3X/1/b/1b18854f1059217f29370444396145cc3a0cd8ae.gif)

See [this post](http://www.freecodecamp.com/forum/t/forum-code-formatting/25574) to find the backtick on your keyboard.  
**Note:** Backticks (`) are not single quotes (’).

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 20, 2020, 4:17pm UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781/3 "2020-09-20T16:17:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![harshadkoli7](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/harshadkoli7/32/150355_2.png) [@harshadkoli7](https://forum.freecodecamp.org/u/harshadkoli7)\
**Post date:** [September 21, 2020, 6:52am UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781/4 "2020-09-21T06:52:39Z")

</div>

`Noted with Thanks `

---

<div class="post-metadata">

**Author:** ![harshadkoli7](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/harshadkoli7/32/150355_2.png) [@harshadkoli7](https://forum.freecodecamp.org/u/harshadkoli7)\
**Post date:** [September 21, 2020, 6:54am UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781/5 "2020-09-21T06:54:39Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 21, 2020, 1:08pm UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781/6 "2020-09-21T13:08:15Z")

</div>

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

```auto
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.

---

<div class="post-metadata">

**Author:** ![harshadkoli7](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/harshadkoli7/32/150355_2.png) [@harshadkoli7](https://forum.freecodecamp.org/u/harshadkoli7)\
**Post date:** [September 27, 2020, 2:49pm UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781/7 "2020-09-27T14:49:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [September 27, 2020, 4:14pm UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781/8 "2020-09-27T16:14:00Z")

</div>

> **[Project Jupyter](https://jupyter.org/install)**
>
> The Jupyter Notebook is a web-based interactive computing platform. The notebook combines live code, equations, narrative text, visualizations, interactive dashboards and other media.

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.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:29pm UTC](https://forum.freecodecamp.org/t/matrix-multiplication-in-pythonwithout-numpy/421781/9 "2021-06-01T18:29:20Z")

</div>

Closed on June 1, 2021, 6:29pm UTC


