9 Replies - 271 Views - Last Post: 10 November 2017 - 09:59 AM Rate Topic: -----

#1 esadri21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-November 17

multiplication of matrics

Posted 10 November 2017 - 07:10 AM

from math import *
import numpy as np
m=n=None 

x = np.array([12,23,52,32,12,12,23,52,32,12])
y = np.array([43,34,11,67,44,43,34,11,67,44])

m = []

for k in range (1,4):        
    m.append(x/y+k)

    flat_list = [item for sublist in m for item in sublist]

    q=[[fl[2:3]],[fl[3:4]]]   #2*1 non square matrix 
    p=[[fl[2:3],fl[3:4]],[fl[2:3],fl[3:4]]]    #2*2 matrix
    mm=np.dot(q,p)

    print (mm)    # It gets error, ValueError: shapes (2,1,1) and (2,2,1) not aligned






1- k must be change from 1 to 3 (k=1,2,3) but it would not change the final answer but it just repeat the out put 3 times and they are identical and just use k=1. why? we need to print answer with k=1 and k=2 and k=3

2- 2- how can multiply two or more matrix with this way:
q=[[fl[2:3]],[fl[3:4]]] #2*1 non square matrix
p=[[fl[2:3],fl[3:4]],[fl[2:3],fl[3:4]]] #2*2 matrix
mm=np.dot(q,p)

Is This A Good Question/Topic? 0
  • +

Replies To: multiplication of matrics

#2 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 321
  • View blog
  • Posts: 1,056
  • Joined: 27-December 13

Re: multiplication of matrics

Posted 10 November 2017 - 08:16 AM

Many issues here:

In line 13 you write 'flat_list', in line 15/16 only 'fl'; please make sure to copy the running code, not re-type as this may create errors like this.

In line 15 and 16 you do fl[2:3] etc. This creates a list of length 1
fl[2:3] == [fl[2]]

This means that your matrices are actually 3D, not 2D
Use this:
q=fl[2],fl[3]   #2*1 non square matrix 
p=[[fl[2],fl[3]],[fl[2],fl[3]]]    #2*2 matrix


Finally; as you keep appending to m the flat list grows - but you ALWAYS use the same first list items in line 15/16.
I am not sure what you want, maybe don't append to m but create a new m in each pass of the loop.
Was This Post Helpful? 1
  • +
  • -

#3 esadri21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-November 17

Re: multiplication of matrics

Posted 10 November 2017 - 08:23 AM

Really thank you for your help. I'm beginner and something like this make me confused. But what about K? range of K does not make different out puts, it just repeat one out put

This post has been edited by andrewsw: 10 November 2017 - 08:39 AM
Reason for edit:: removed previous quote, use the REPLY button

Was This Post Helpful? 0
  • +
  • -

#4 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 321
  • View blog
  • Posts: 1,056
  • Joined: 27-December 13

Re: multiplication of matrics

Posted 10 November 2017 - 08:26 AM

This is because the first elements of 'm' are always the same.
I think you should not append to 'm' in line 11, but create a new list in each pass. If you do this the output becomes
[ 41.26433946  10.64602361]
[ 61.92376958  22.80613216]
[ 86.5831997   38.96624071]

EDIT: Grammar correction

This post has been edited by DK3250: 10 November 2017 - 08:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 321
  • View blog
  • Posts: 1,056
  • Joined: 27-December 13

Re: multiplication of matrics

Posted 10 November 2017 - 08:36 AM

Oh, - one more thing; The list comprehension in line 13 is convoluted, I'm not sure what you try to obtain.
As far as I can see, it is currently equivalent to
flat_list = m[0]

Was This Post Helpful? 0
  • +
  • -

#6 esadri21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-November 17

Re: multiplication of matrics

Posted 10 November 2017 - 08:39 AM

View PostDK3250, on 10 November 2017 - 08:26 AM, said:

This is because the first elements of 'm' is always the same.
I think you should not append to 'm' in line 11, but create a new list in each pass. If you do this the output becomes
[ 41.26433946  10.64602361]
[ 61.92376958  22.80613216]
[ 86.5831997   38.96624071]




Thank you, but I have to write append becaus I want to call arrays everywhere i want in calculations. Now is there any solution for k? if we consider fl[0], for all value of K(in range(1,4)) we must have:
[1+(12/43)] for k=1
[2+(12/431)] for k=2
[3+(12431))] for k=3
Was This Post Helpful? 0
  • +
  • -

#7 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 321
  • View blog
  • Posts: 1,056
  • Joined: 27-December 13

Re: multiplication of matrics

Posted 10 November 2017 - 08:47 AM

Try to print 'm' and 'fl' in the loop.
You will see that they both grow.
But fl[2] and fl[3] never changes, thus the output never changes.
Was This Post Helpful? 0
  • +
  • -

#8 esadri21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-November 17

Re: multiplication of matrics

Posted 10 November 2017 - 09:07 AM

View PostDK3250, on 10 November 2017 - 08:47 AM, said:

Try to print 'm' and 'fl' in the loop.
You will see that they both grow.
But fl[2] and fl[3] never changes, thus the output never changes.





Oh god im gonna kill my self. I don't understand. I just want to have different values of k in my outputs. Is it complicated? I thought it is very easy and it is because of my lacking of knowledge.
Was This Post Helpful? 0
  • +
  • -

#9 esadri21  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-November 17

Re: multiplication of matrics

Posted 10 November 2017 - 09:30 AM

View Postesadri21, on 10 November 2017 - 09:07 AM, said:

View PostDK3250, on 10 November 2017 - 08:47 AM, said:

Try to print 'm' and 'fl' in the loop.
You will see that they both grow.
But fl[2] and fl[3] never changes, thus the output never changes.





Oh god im gonna kill my self. I don't understand. I just want to have different values of k in my outputs. Is it complicated? I thought it is very easy and it is because of my lacking of knowledge.



I found the answer, we need to move m=[ ] into the loop. then we have different values of k. thank you my friend for your great help.
Was This Post Helpful? 0
  • +
  • -

#10 DK3250  Icon User is offline

  • Pythonian
  • member icon

Reputation: 321
  • View blog
  • Posts: 1,056
  • Joined: 27-December 13

Re: multiplication of matrics

Posted 10 November 2017 - 09:59 AM

Good!
I didn't want just to show a working code - I'm sure you learn much more this way.
If you want, show the final code - there might be room for further improvements.
Happy coding!
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1