How to implement numpy broadcast mechanism with mkl?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















How to implement numpy broadcast mechanism with mkl?



  I have been confused, how to use mkl to efficiently implement the broadcast mechanism in numpy (Element wise operator "+","-","*")?



such as
2-D array sub 1-D array



[[1,2,3],              [[0,0,0],
[4,5,6], - [1,2,3] = [3,3,3],
[7,8,9]] [6,6,6]]


And the second operation (can be understood as a matrix multiplied by a diagonal matrix)
2-D array multiply 1-D array(Element wise multiply )



[[1,2,3],               [[1,4,9],
[4,5,6], * [1,2,3] = [4,10,18],
[7,8,9]] [7,16,27]]


I tried to implement with the for loop +cblas_dscal/vdSub.
But I think this is not efficient, I don't know if there is any better implementation.










share|improve this question

























  • The mkl code is called by compiled numpy code. You don't invoke it directly from Python. It's a library that is linked to numpy during setup or compilation, not something you consciously use while running interpreted code.

    – hpaulj
    Jan 4 at 21:24











  • Sorry, I didn’t say it clearly. I am using c++ and mkl now, not python...

    – 梁先锋
    Jan 5 at 12:24


















-1















How to implement numpy broadcast mechanism with mkl?



  I have been confused, how to use mkl to efficiently implement the broadcast mechanism in numpy (Element wise operator "+","-","*")?



such as
2-D array sub 1-D array



[[1,2,3],              [[0,0,0],
[4,5,6], - [1,2,3] = [3,3,3],
[7,8,9]] [6,6,6]]


And the second operation (can be understood as a matrix multiplied by a diagonal matrix)
2-D array multiply 1-D array(Element wise multiply )



[[1,2,3],               [[1,4,9],
[4,5,6], * [1,2,3] = [4,10,18],
[7,8,9]] [7,16,27]]


I tried to implement with the for loop +cblas_dscal/vdSub.
But I think this is not efficient, I don't know if there is any better implementation.










share|improve this question

























  • The mkl code is called by compiled numpy code. You don't invoke it directly from Python. It's a library that is linked to numpy during setup or compilation, not something you consciously use while running interpreted code.

    – hpaulj
    Jan 4 at 21:24











  • Sorry, I didn’t say it clearly. I am using c++ and mkl now, not python...

    – 梁先锋
    Jan 5 at 12:24














-1












-1








-1








How to implement numpy broadcast mechanism with mkl?



  I have been confused, how to use mkl to efficiently implement the broadcast mechanism in numpy (Element wise operator "+","-","*")?



such as
2-D array sub 1-D array



[[1,2,3],              [[0,0,0],
[4,5,6], - [1,2,3] = [3,3,3],
[7,8,9]] [6,6,6]]


And the second operation (can be understood as a matrix multiplied by a diagonal matrix)
2-D array multiply 1-D array(Element wise multiply )



[[1,2,3],               [[1,4,9],
[4,5,6], * [1,2,3] = [4,10,18],
[7,8,9]] [7,16,27]]


I tried to implement with the for loop +cblas_dscal/vdSub.
But I think this is not efficient, I don't know if there is any better implementation.










share|improve this question
















How to implement numpy broadcast mechanism with mkl?



  I have been confused, how to use mkl to efficiently implement the broadcast mechanism in numpy (Element wise operator "+","-","*")?



such as
2-D array sub 1-D array



[[1,2,3],              [[0,0,0],
[4,5,6], - [1,2,3] = [3,3,3],
[7,8,9]] [6,6,6]]


And the second operation (can be understood as a matrix multiplied by a diagonal matrix)
2-D array multiply 1-D array(Element wise multiply )



[[1,2,3],               [[1,4,9],
[4,5,6], * [1,2,3] = [4,10,18],
[7,8,9]] [7,16,27]]


I tried to implement with the for loop +cblas_dscal/vdSub.
But I think this is not efficient, I don't know if there is any better implementation.







intel-mkl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 5 at 12:22







梁先锋

















asked Jan 4 at 14:39









梁先锋梁先锋

1




1













  • The mkl code is called by compiled numpy code. You don't invoke it directly from Python. It's a library that is linked to numpy during setup or compilation, not something you consciously use while running interpreted code.

    – hpaulj
    Jan 4 at 21:24











  • Sorry, I didn’t say it clearly. I am using c++ and mkl now, not python...

    – 梁先锋
    Jan 5 at 12:24



















  • The mkl code is called by compiled numpy code. You don't invoke it directly from Python. It's a library that is linked to numpy during setup or compilation, not something you consciously use while running interpreted code.

    – hpaulj
    Jan 4 at 21:24











  • Sorry, I didn’t say it clearly. I am using c++ and mkl now, not python...

    – 梁先锋
    Jan 5 at 12:24

















The mkl code is called by compiled numpy code. You don't invoke it directly from Python. It's a library that is linked to numpy during setup or compilation, not something you consciously use while running interpreted code.

– hpaulj
Jan 4 at 21:24





The mkl code is called by compiled numpy code. You don't invoke it directly from Python. It's a library that is linked to numpy during setup or compilation, not something you consciously use while running interpreted code.

– hpaulj
Jan 4 at 21:24













Sorry, I didn’t say it clearly. I am using c++ and mkl now, not python...

– 梁先锋
Jan 5 at 12:24





Sorry, I didn’t say it clearly. I am using c++ and mkl now, not python...

– 梁先锋
Jan 5 at 12:24












1 Answer
1






active

oldest

votes


















0














You can just broadcast the second array to two dimensions with arr2[None, :]. This yields the following code:



arr1 = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
arr2 = np.array([1,2,3])

print(arr1 - arr2[None, :])
# Out: [[0 0 0]
# [3 3 3]
# [6 6 6]]
print(arr1 * arr2[None, :])
# Out: [[ 1, 4, 9],
# [ 4, 10, 18],
# [ 7, 16, 27]]


If you broadcast your arrays like this, numpy will use optimizations like mkl to perform the requested operations like multiplications.

More information on broadcasting and expanding array dimensions with None or np.newaxis can be found here:




  • Broadcasting

  • Expancing dimensions






share|improve this answer


























  • Thanks. but I am using c++ and mkl now, not python

    – 梁先锋
    Jan 5 at 12:24











  • You may still upvote and accept my answer, since it answered your question. :)

    – Scotty1-
    Jan 5 at 17:21












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%2f54041051%2fhow-to-implement-numpy-broadcast-mechanism-with-mkl%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









0














You can just broadcast the second array to two dimensions with arr2[None, :]. This yields the following code:



arr1 = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
arr2 = np.array([1,2,3])

print(arr1 - arr2[None, :])
# Out: [[0 0 0]
# [3 3 3]
# [6 6 6]]
print(arr1 * arr2[None, :])
# Out: [[ 1, 4, 9],
# [ 4, 10, 18],
# [ 7, 16, 27]]


If you broadcast your arrays like this, numpy will use optimizations like mkl to perform the requested operations like multiplications.

More information on broadcasting and expanding array dimensions with None or np.newaxis can be found here:




  • Broadcasting

  • Expancing dimensions






share|improve this answer


























  • Thanks. but I am using c++ and mkl now, not python

    – 梁先锋
    Jan 5 at 12:24











  • You may still upvote and accept my answer, since it answered your question. :)

    – Scotty1-
    Jan 5 at 17:21
















0














You can just broadcast the second array to two dimensions with arr2[None, :]. This yields the following code:



arr1 = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
arr2 = np.array([1,2,3])

print(arr1 - arr2[None, :])
# Out: [[0 0 0]
# [3 3 3]
# [6 6 6]]
print(arr1 * arr2[None, :])
# Out: [[ 1, 4, 9],
# [ 4, 10, 18],
# [ 7, 16, 27]]


If you broadcast your arrays like this, numpy will use optimizations like mkl to perform the requested operations like multiplications.

More information on broadcasting and expanding array dimensions with None or np.newaxis can be found here:




  • Broadcasting

  • Expancing dimensions






share|improve this answer


























  • Thanks. but I am using c++ and mkl now, not python

    – 梁先锋
    Jan 5 at 12:24











  • You may still upvote and accept my answer, since it answered your question. :)

    – Scotty1-
    Jan 5 at 17:21














0












0








0







You can just broadcast the second array to two dimensions with arr2[None, :]. This yields the following code:



arr1 = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
arr2 = np.array([1,2,3])

print(arr1 - arr2[None, :])
# Out: [[0 0 0]
# [3 3 3]
# [6 6 6]]
print(arr1 * arr2[None, :])
# Out: [[ 1, 4, 9],
# [ 4, 10, 18],
# [ 7, 16, 27]]


If you broadcast your arrays like this, numpy will use optimizations like mkl to perform the requested operations like multiplications.

More information on broadcasting and expanding array dimensions with None or np.newaxis can be found here:




  • Broadcasting

  • Expancing dimensions






share|improve this answer















You can just broadcast the second array to two dimensions with arr2[None, :]. This yields the following code:



arr1 = np.array([[1,2,3],
[4,5,6],
[7,8,9]])
arr2 = np.array([1,2,3])

print(arr1 - arr2[None, :])
# Out: [[0 0 0]
# [3 3 3]
# [6 6 6]]
print(arr1 * arr2[None, :])
# Out: [[ 1, 4, 9],
# [ 4, 10, 18],
# [ 7, 16, 27]]


If you broadcast your arrays like this, numpy will use optimizations like mkl to perform the requested operations like multiplications.

More information on broadcasting and expanding array dimensions with None or np.newaxis can be found here:




  • Broadcasting

  • Expancing dimensions







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 15:01

























answered Jan 4 at 14:56









Scotty1-Scotty1-

2,2551424




2,2551424













  • Thanks. but I am using c++ and mkl now, not python

    – 梁先锋
    Jan 5 at 12:24











  • You may still upvote and accept my answer, since it answered your question. :)

    – Scotty1-
    Jan 5 at 17:21



















  • Thanks. but I am using c++ and mkl now, not python

    – 梁先锋
    Jan 5 at 12:24











  • You may still upvote and accept my answer, since it answered your question. :)

    – Scotty1-
    Jan 5 at 17:21

















Thanks. but I am using c++ and mkl now, not python

– 梁先锋
Jan 5 at 12:24





Thanks. but I am using c++ and mkl now, not python

– 梁先锋
Jan 5 at 12:24













You may still upvote and accept my answer, since it answered your question. :)

– Scotty1-
Jan 5 at 17:21





You may still upvote and accept my answer, since it answered your question. :)

– Scotty1-
Jan 5 at 17:21




















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%2f54041051%2fhow-to-implement-numpy-broadcast-mechanism-with-mkl%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

Angular Downloading a file using contenturl with Basic Authentication

Olmecas

Can't read property showImagePicker of undefined in react native iOS