Numpy dot() function equivalent
up vote
-2
down vote
favorite
This question is just out of pure curiosity. Suppose I have 2 matrices a and b.
a=np.array([[1, 2],
[2, 3],
[4, 5]])
b=np.array([[1, 2, 3, 4],
[2, 3, 4, 5]])
To find their dot product, I might use np.dot(a,b). But is there any other way to do this? I am not asking for any other alias functions. But maybe another way to do this like np.sum(a*b, axis=1) (I know that doesn't work, it is just an example). And what if I have a 3-D matrix? Is there any other way to compute their dot product as well (without using any functions)?
Thanks in advance!
python numpy matrix matrix-multiplication
|
show 2 more comments
up vote
-2
down vote
favorite
This question is just out of pure curiosity. Suppose I have 2 matrices a and b.
a=np.array([[1, 2],
[2, 3],
[4, 5]])
b=np.array([[1, 2, 3, 4],
[2, 3, 4, 5]])
To find their dot product, I might use np.dot(a,b). But is there any other way to do this? I am not asking for any other alias functions. But maybe another way to do this like np.sum(a*b, axis=1) (I know that doesn't work, it is just an example). And what if I have a 3-D matrix? Is there any other way to compute their dot product as well (without using any functions)?
Thanks in advance!
python numpy matrix matrix-multiplication
You can use the@operator:a@b.
– Willem Van Onsem
Nov 11 at 19:51
Furthermorenp.multiplydoes not produces a matrix multiplication.
– Willem Van Onsem
Nov 11 at 19:52
@Willem Van Onsem I was asking for a mathematical equivalent, not another operator or function
– ѕняєє ѕιиgнι
Nov 11 at 19:57
It's easy with an added bit of broadcasting. Think about the dimensions you need to multiply and sum
– hpaulj
Nov 11 at 19:57
@hpaulj Could you please be a bit more specific and post your answer?
– ѕняєє ѕιиgнι
Nov 11 at 19:58
|
show 2 more comments
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
This question is just out of pure curiosity. Suppose I have 2 matrices a and b.
a=np.array([[1, 2],
[2, 3],
[4, 5]])
b=np.array([[1, 2, 3, 4],
[2, 3, 4, 5]])
To find their dot product, I might use np.dot(a,b). But is there any other way to do this? I am not asking for any other alias functions. But maybe another way to do this like np.sum(a*b, axis=1) (I know that doesn't work, it is just an example). And what if I have a 3-D matrix? Is there any other way to compute their dot product as well (without using any functions)?
Thanks in advance!
python numpy matrix matrix-multiplication
This question is just out of pure curiosity. Suppose I have 2 matrices a and b.
a=np.array([[1, 2],
[2, 3],
[4, 5]])
b=np.array([[1, 2, 3, 4],
[2, 3, 4, 5]])
To find their dot product, I might use np.dot(a,b). But is there any other way to do this? I am not asking for any other alias functions. But maybe another way to do this like np.sum(a*b, axis=1) (I know that doesn't work, it is just an example). And what if I have a 3-D matrix? Is there any other way to compute their dot product as well (without using any functions)?
Thanks in advance!
python numpy matrix matrix-multiplication
python numpy matrix matrix-multiplication
edited Nov 11 at 19:55
asked Nov 11 at 19:50
ѕняєє ѕιиgнι
30014
30014
You can use the@operator:a@b.
– Willem Van Onsem
Nov 11 at 19:51
Furthermorenp.multiplydoes not produces a matrix multiplication.
– Willem Van Onsem
Nov 11 at 19:52
@Willem Van Onsem I was asking for a mathematical equivalent, not another operator or function
– ѕняєє ѕιиgнι
Nov 11 at 19:57
It's easy with an added bit of broadcasting. Think about the dimensions you need to multiply and sum
– hpaulj
Nov 11 at 19:57
@hpaulj Could you please be a bit more specific and post your answer?
– ѕняєє ѕιиgнι
Nov 11 at 19:58
|
show 2 more comments
You can use the@operator:a@b.
– Willem Van Onsem
Nov 11 at 19:51
Furthermorenp.multiplydoes not produces a matrix multiplication.
– Willem Van Onsem
Nov 11 at 19:52
@Willem Van Onsem I was asking for a mathematical equivalent, not another operator or function
– ѕняєє ѕιиgнι
Nov 11 at 19:57
It's easy with an added bit of broadcasting. Think about the dimensions you need to multiply and sum
– hpaulj
Nov 11 at 19:57
@hpaulj Could you please be a bit more specific and post your answer?
– ѕняєє ѕιиgнι
Nov 11 at 19:58
You can use the
@ operator: a@b.– Willem Van Onsem
Nov 11 at 19:51
You can use the
@ operator: a@b.– Willem Van Onsem
Nov 11 at 19:51
Furthermore
np.multiply does not produces a matrix multiplication.– Willem Van Onsem
Nov 11 at 19:52
Furthermore
np.multiply does not produces a matrix multiplication.– Willem Van Onsem
Nov 11 at 19:52
@Willem Van Onsem I was asking for a mathematical equivalent, not another operator or function
– ѕняєє ѕιиgнι
Nov 11 at 19:57
@Willem Van Onsem I was asking for a mathematical equivalent, not another operator or function
– ѕняєє ѕιиgнι
Nov 11 at 19:57
It's easy with an added bit of broadcasting. Think about the dimensions you need to multiply and sum
– hpaulj
Nov 11 at 19:57
It's easy with an added bit of broadcasting. Think about the dimensions you need to multiply and sum
– hpaulj
Nov 11 at 19:57
@hpaulj Could you please be a bit more specific and post your answer?
– ѕняєє ѕιиgнι
Nov 11 at 19:58
@hpaulj Could you please be a bit more specific and post your answer?
– ѕняєє ѕιиgнι
Nov 11 at 19:58
|
show 2 more comments
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
In [66]: a=np.array([[1, 2],
...: [2, 3],
...: [4, 5]])
...:
...: b=np.array([[1, 2, 3, 4],
...: [2, 3, 4, 5]])
...:
...:
In [67]: np.dot(a,b)
Out[67]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [68]: a@b
Out[68]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [69]: np.einsum('ij,jk',a,b)
Out[69]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
Broadcasted multiply and sum:
In [71]: (a[:,:,None]*b[None,:,:]).sum(axis=1)
Out[71]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [72]: (a[:,:,None]*b[None,:,:]).shape
Out[72]: (3, 2, 4)
add a comment |
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',
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252571%2fnumpy-dot-function-equivalent%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
In [66]: a=np.array([[1, 2],
...: [2, 3],
...: [4, 5]])
...:
...: b=np.array([[1, 2, 3, 4],
...: [2, 3, 4, 5]])
...:
...:
In [67]: np.dot(a,b)
Out[67]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [68]: a@b
Out[68]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [69]: np.einsum('ij,jk',a,b)
Out[69]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
Broadcasted multiply and sum:
In [71]: (a[:,:,None]*b[None,:,:]).sum(axis=1)
Out[71]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [72]: (a[:,:,None]*b[None,:,:]).shape
Out[72]: (3, 2, 4)
add a comment |
up vote
1
down vote
accepted
In [66]: a=np.array([[1, 2],
...: [2, 3],
...: [4, 5]])
...:
...: b=np.array([[1, 2, 3, 4],
...: [2, 3, 4, 5]])
...:
...:
In [67]: np.dot(a,b)
Out[67]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [68]: a@b
Out[68]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [69]: np.einsum('ij,jk',a,b)
Out[69]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
Broadcasted multiply and sum:
In [71]: (a[:,:,None]*b[None,:,:]).sum(axis=1)
Out[71]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [72]: (a[:,:,None]*b[None,:,:]).shape
Out[72]: (3, 2, 4)
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
In [66]: a=np.array([[1, 2],
...: [2, 3],
...: [4, 5]])
...:
...: b=np.array([[1, 2, 3, 4],
...: [2, 3, 4, 5]])
...:
...:
In [67]: np.dot(a,b)
Out[67]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [68]: a@b
Out[68]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [69]: np.einsum('ij,jk',a,b)
Out[69]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
Broadcasted multiply and sum:
In [71]: (a[:,:,None]*b[None,:,:]).sum(axis=1)
Out[71]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [72]: (a[:,:,None]*b[None,:,:]).shape
Out[72]: (3, 2, 4)
In [66]: a=np.array([[1, 2],
...: [2, 3],
...: [4, 5]])
...:
...: b=np.array([[1, 2, 3, 4],
...: [2, 3, 4, 5]])
...:
...:
In [67]: np.dot(a,b)
Out[67]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [68]: a@b
Out[68]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [69]: np.einsum('ij,jk',a,b)
Out[69]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
Broadcasted multiply and sum:
In [71]: (a[:,:,None]*b[None,:,:]).sum(axis=1)
Out[71]:
array([[ 5, 8, 11, 14],
[ 8, 13, 18, 23],
[14, 23, 32, 41]])
In [72]: (a[:,:,None]*b[None,:,:]).shape
Out[72]: (3, 2, 4)
answered Nov 12 at 3:38
hpaulj
109k674140
109k674140
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252571%2fnumpy-dot-function-equivalent%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You can use the
@operator:a@b.– Willem Van Onsem
Nov 11 at 19:51
Furthermore
np.multiplydoes not produces a matrix multiplication.– Willem Van Onsem
Nov 11 at 19:52
@Willem Van Onsem I was asking for a mathematical equivalent, not another operator or function
– ѕняєє ѕιиgнι
Nov 11 at 19:57
It's easy with an added bit of broadcasting. Think about the dimensions you need to multiply and sum
– hpaulj
Nov 11 at 19:57
@hpaulj Could you please be a bit more specific and post your answer?
– ѕняєє ѕιиgнι
Nov 11 at 19:58