Python, Class, Call function inside the class, Operator Overloading (Matrix Multiplication)












1















I am trying to write a class that has a mutiplication operator overloading function that is used to handle matrix multiplcation.
My method is




  1. Transpose a matrix(I write a transpose function inside the class) which is called T

  2. Write a dot product function which handle the mutiplication for Row A and Row B(Since I already transposed matrix B).

  3. Write the multiplication operator overloading function which call the transpose and dot product function.


My problem:




  1. So the problem I have is when I try to call the function dot, it reminds me that "dot() takes 2 positional arguments but 3 were given". I kind of knowing why as dot(self,other) can only take one more argument besides itself? However, my method requires me to sepcifically tell the dot function that I want to input specific row of the Matrix. That's why I used self.g[i]. The thing I don't understand is my function dot is able to take two inputs which is self and other, when I specify in mul function, I also give two inputs "self.dot(self.g[i],other.g[j])", why the program counts as three arguments.

  2. My second question is about the "self", I know when I want to call a function inside the class, I need to use self.functionname syntax, I was really confused here, self is the object, to me "self" represents Matrix A and "other" represents Matrix B, does the program get confused if I use self to call the function?


Honestly, I am pretty confused about the utilization of self in the class, is there any examples recommended that could help beginner like me to grasp this concept?



Thanks in advance guys.



Here are my codes



Constructor



# Constructor
def __init__(self, grid): # I don't know if i need another input here or not like def __init__(self, grid , matrixB)
self.g = grid
self.h = len(grid)
self.w = len(grid[0])


Mutiplication operator overloading



def __mul__(self, other):

af_trans = other.T() # Transpose the second matrix
for i in range(0,self.h):
every_row=
for j in range(0,self.h):
element_product = self.dot(self.g[i],other.g[j])
every_row.append(element_product)
return every_row


Transpose function



def T(self):

T_matrix =
for i in range(0,self.h):
new_row =
for j in range(0,self.w):
T_element = self.g[j][i]
new_row.append(T_element)
T_matrix.append(new_row)
return T_matrix


Dotproduct



def dot(self,other):  
sumproduct=0
for i in range(0,self.h):
element_product = self.g[i][i]*other.g[i][i] #The first element
of Matrix A times the first element of Matrix B
sum_product = sum_product+element_product
return sum_product









share|improve this question























  • how do you call the dot function? should be A.dot(B)...

    – hiro protagonist
    Nov 25 '18 at 9:31











  • Are you sure you are not reinventing numpy?

    – DeepSpace
    Nov 25 '18 at 9:39











  • I think I'm supposed to use A.dot(B),however I need to let dot function to run the A matrix row by row, so I think I have to pass something like A[i] to indicate it is running row by row.

    – MOSOON-E
    Nov 25 '18 at 21:43
















1















I am trying to write a class that has a mutiplication operator overloading function that is used to handle matrix multiplcation.
My method is




  1. Transpose a matrix(I write a transpose function inside the class) which is called T

  2. Write a dot product function which handle the mutiplication for Row A and Row B(Since I already transposed matrix B).

  3. Write the multiplication operator overloading function which call the transpose and dot product function.


My problem:




  1. So the problem I have is when I try to call the function dot, it reminds me that "dot() takes 2 positional arguments but 3 were given". I kind of knowing why as dot(self,other) can only take one more argument besides itself? However, my method requires me to sepcifically tell the dot function that I want to input specific row of the Matrix. That's why I used self.g[i]. The thing I don't understand is my function dot is able to take two inputs which is self and other, when I specify in mul function, I also give two inputs "self.dot(self.g[i],other.g[j])", why the program counts as three arguments.

  2. My second question is about the "self", I know when I want to call a function inside the class, I need to use self.functionname syntax, I was really confused here, self is the object, to me "self" represents Matrix A and "other" represents Matrix B, does the program get confused if I use self to call the function?


Honestly, I am pretty confused about the utilization of self in the class, is there any examples recommended that could help beginner like me to grasp this concept?



Thanks in advance guys.



Here are my codes



Constructor



# Constructor
def __init__(self, grid): # I don't know if i need another input here or not like def __init__(self, grid , matrixB)
self.g = grid
self.h = len(grid)
self.w = len(grid[0])


Mutiplication operator overloading



def __mul__(self, other):

af_trans = other.T() # Transpose the second matrix
for i in range(0,self.h):
every_row=
for j in range(0,self.h):
element_product = self.dot(self.g[i],other.g[j])
every_row.append(element_product)
return every_row


Transpose function



def T(self):

T_matrix =
for i in range(0,self.h):
new_row =
for j in range(0,self.w):
T_element = self.g[j][i]
new_row.append(T_element)
T_matrix.append(new_row)
return T_matrix


Dotproduct



def dot(self,other):  
sumproduct=0
for i in range(0,self.h):
element_product = self.g[i][i]*other.g[i][i] #The first element
of Matrix A times the first element of Matrix B
sum_product = sum_product+element_product
return sum_product









share|improve this question























  • how do you call the dot function? should be A.dot(B)...

    – hiro protagonist
    Nov 25 '18 at 9:31











  • Are you sure you are not reinventing numpy?

    – DeepSpace
    Nov 25 '18 at 9:39











  • I think I'm supposed to use A.dot(B),however I need to let dot function to run the A matrix row by row, so I think I have to pass something like A[i] to indicate it is running row by row.

    – MOSOON-E
    Nov 25 '18 at 21:43














1












1








1








I am trying to write a class that has a mutiplication operator overloading function that is used to handle matrix multiplcation.
My method is




  1. Transpose a matrix(I write a transpose function inside the class) which is called T

  2. Write a dot product function which handle the mutiplication for Row A and Row B(Since I already transposed matrix B).

  3. Write the multiplication operator overloading function which call the transpose and dot product function.


My problem:




  1. So the problem I have is when I try to call the function dot, it reminds me that "dot() takes 2 positional arguments but 3 were given". I kind of knowing why as dot(self,other) can only take one more argument besides itself? However, my method requires me to sepcifically tell the dot function that I want to input specific row of the Matrix. That's why I used self.g[i]. The thing I don't understand is my function dot is able to take two inputs which is self and other, when I specify in mul function, I also give two inputs "self.dot(self.g[i],other.g[j])", why the program counts as three arguments.

  2. My second question is about the "self", I know when I want to call a function inside the class, I need to use self.functionname syntax, I was really confused here, self is the object, to me "self" represents Matrix A and "other" represents Matrix B, does the program get confused if I use self to call the function?


Honestly, I am pretty confused about the utilization of self in the class, is there any examples recommended that could help beginner like me to grasp this concept?



Thanks in advance guys.



Here are my codes



Constructor



# Constructor
def __init__(self, grid): # I don't know if i need another input here or not like def __init__(self, grid , matrixB)
self.g = grid
self.h = len(grid)
self.w = len(grid[0])


Mutiplication operator overloading



def __mul__(self, other):

af_trans = other.T() # Transpose the second matrix
for i in range(0,self.h):
every_row=
for j in range(0,self.h):
element_product = self.dot(self.g[i],other.g[j])
every_row.append(element_product)
return every_row


Transpose function



def T(self):

T_matrix =
for i in range(0,self.h):
new_row =
for j in range(0,self.w):
T_element = self.g[j][i]
new_row.append(T_element)
T_matrix.append(new_row)
return T_matrix


Dotproduct



def dot(self,other):  
sumproduct=0
for i in range(0,self.h):
element_product = self.g[i][i]*other.g[i][i] #The first element
of Matrix A times the first element of Matrix B
sum_product = sum_product+element_product
return sum_product









share|improve this question














I am trying to write a class that has a mutiplication operator overloading function that is used to handle matrix multiplcation.
My method is




  1. Transpose a matrix(I write a transpose function inside the class) which is called T

  2. Write a dot product function which handle the mutiplication for Row A and Row B(Since I already transposed matrix B).

  3. Write the multiplication operator overloading function which call the transpose and dot product function.


My problem:




  1. So the problem I have is when I try to call the function dot, it reminds me that "dot() takes 2 positional arguments but 3 were given". I kind of knowing why as dot(self,other) can only take one more argument besides itself? However, my method requires me to sepcifically tell the dot function that I want to input specific row of the Matrix. That's why I used self.g[i]. The thing I don't understand is my function dot is able to take two inputs which is self and other, when I specify in mul function, I also give two inputs "self.dot(self.g[i],other.g[j])", why the program counts as three arguments.

  2. My second question is about the "self", I know when I want to call a function inside the class, I need to use self.functionname syntax, I was really confused here, self is the object, to me "self" represents Matrix A and "other" represents Matrix B, does the program get confused if I use self to call the function?


Honestly, I am pretty confused about the utilization of self in the class, is there any examples recommended that could help beginner like me to grasp this concept?



Thanks in advance guys.



Here are my codes



Constructor



# Constructor
def __init__(self, grid): # I don't know if i need another input here or not like def __init__(self, grid , matrixB)
self.g = grid
self.h = len(grid)
self.w = len(grid[0])


Mutiplication operator overloading



def __mul__(self, other):

af_trans = other.T() # Transpose the second matrix
for i in range(0,self.h):
every_row=
for j in range(0,self.h):
element_product = self.dot(self.g[i],other.g[j])
every_row.append(element_product)
return every_row


Transpose function



def T(self):

T_matrix =
for i in range(0,self.h):
new_row =
for j in range(0,self.w):
T_element = self.g[j][i]
new_row.append(T_element)
T_matrix.append(new_row)
return T_matrix


Dotproduct



def dot(self,other):  
sumproduct=0
for i in range(0,self.h):
element_product = self.g[i][i]*other.g[i][i] #The first element
of Matrix A times the first element of Matrix B
sum_product = sum_product+element_product
return sum_product






python class operator-overloading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 9:29









MOSOON-EMOSOON-E

163




163













  • how do you call the dot function? should be A.dot(B)...

    – hiro protagonist
    Nov 25 '18 at 9:31











  • Are you sure you are not reinventing numpy?

    – DeepSpace
    Nov 25 '18 at 9:39











  • I think I'm supposed to use A.dot(B),however I need to let dot function to run the A matrix row by row, so I think I have to pass something like A[i] to indicate it is running row by row.

    – MOSOON-E
    Nov 25 '18 at 21:43



















  • how do you call the dot function? should be A.dot(B)...

    – hiro protagonist
    Nov 25 '18 at 9:31











  • Are you sure you are not reinventing numpy?

    – DeepSpace
    Nov 25 '18 at 9:39











  • I think I'm supposed to use A.dot(B),however I need to let dot function to run the A matrix row by row, so I think I have to pass something like A[i] to indicate it is running row by row.

    – MOSOON-E
    Nov 25 '18 at 21:43

















how do you call the dot function? should be A.dot(B)...

– hiro protagonist
Nov 25 '18 at 9:31





how do you call the dot function? should be A.dot(B)...

– hiro protagonist
Nov 25 '18 at 9:31













Are you sure you are not reinventing numpy?

– DeepSpace
Nov 25 '18 at 9:39





Are you sure you are not reinventing numpy?

– DeepSpace
Nov 25 '18 at 9:39













I think I'm supposed to use A.dot(B),however I need to let dot function to run the A matrix row by row, so I think I have to pass something like A[i] to indicate it is running row by row.

– MOSOON-E
Nov 25 '18 at 21:43





I think I'm supposed to use A.dot(B),however I need to let dot function to run the A matrix row by row, so I think I have to pass something like A[i] to indicate it is running row by row.

– MOSOON-E
Nov 25 '18 at 21:43












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466203%2fpython-class-call-function-inside-the-class-operator-overloading-matrix-mult%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53466203%2fpython-class-call-function-inside-the-class-operator-overloading-matrix-mult%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks