Multiply numpy int and float arrays: Cannot cast ufunc multiply output from dtype












10














I'd like to multiply an int16 array but a float array, with auto rounding, but this fails :



import numpy

A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)

A *= B


I get:




TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'











share|improve this question
























  • It seems it's possible with numpy.multiply(A, B, out=A, casting='unsafe') but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?
    – Basj
    Jul 30 '16 at 12:10












  • See github.com/numpy/numpy/pull/6499/files
    – Basj
    Jul 30 '16 at 12:11
















10














I'd like to multiply an int16 array but a float array, with auto rounding, but this fails :



import numpy

A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)

A *= B


I get:




TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'











share|improve this question
























  • It seems it's possible with numpy.multiply(A, B, out=A, casting='unsafe') but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?
    – Basj
    Jul 30 '16 at 12:10












  • See github.com/numpy/numpy/pull/6499/files
    – Basj
    Jul 30 '16 at 12:11














10












10








10


1





I'd like to multiply an int16 array but a float array, with auto rounding, but this fails :



import numpy

A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)

A *= B


I get:




TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'











share|improve this question















I'd like to multiply an int16 array but a float array, with auto rounding, but this fails :



import numpy

A = numpy.array([1, 2, 3, 4], dtype=numpy.int16)
B = numpy.array([0.5, 2.1, 3, 4], dtype=numpy.float64)

A *= B


I get:




TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('int16') with casting rule 'same_kind'








python arrays numpy floating-point int






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 22:37









seralouk

5,71522339




5,71522339










asked Jul 30 '16 at 11:39









BasjBasj

5,58729104226




5,58729104226












  • It seems it's possible with numpy.multiply(A, B, out=A, casting='unsafe') but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?
    – Basj
    Jul 30 '16 at 12:10












  • See github.com/numpy/numpy/pull/6499/files
    – Basj
    Jul 30 '16 at 12:11


















  • It seems it's possible with numpy.multiply(A, B, out=A, casting='unsafe') but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?
    – Basj
    Jul 30 '16 at 12:10












  • See github.com/numpy/numpy/pull/6499/files
    – Basj
    Jul 30 '16 at 12:11
















It seems it's possible with numpy.multiply(A, B, out=A, casting='unsafe') but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?
– Basj
Jul 30 '16 at 12:10






It seems it's possible with numpy.multiply(A, B, out=A, casting='unsafe') but that's way much longer syntax! Is there a way to set casting='unsafe' in numpy by default?
– Basj
Jul 30 '16 at 12:10














See github.com/numpy/numpy/pull/6499/files
– Basj
Jul 30 '16 at 12:11




See github.com/numpy/numpy/pull/6499/files
– Basj
Jul 30 '16 at 12:11












3 Answers
3






active

oldest

votes


















6














2 ways to solve this:



You can solve this by replacing



A *= B


with



A = (A * B)


or with



numpy.multiply(A, B, out=A, casting='unsafe')





share|improve this answer































    2














    You could use broadcasting to multiply the two arrays and take only the integer part as follows:



    In [2]: (A*B).astype(int)
    Out[2]: array([ 0, 4, 9, 16])


    Timing Constraints:



    In [8]: %timeit (A*B).astype(int)
    1000000 loops, best of 3: 1.65 µs per loop

    In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
    100000 loops, best of 3: 2.01 µs per loop





    share|improve this answer























    • OP wants to do the multiplication in place
      – ali_m
      Jul 30 '16 at 18:45










    • How fast is it compared to numpy.multiply(A, B, out=A, casting='unsafe') ?
      – Basj
      Jul 30 '16 at 18:45










    • @ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
      – SethMMorton
      Jul 30 '16 at 18:51










    • @SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
      – ali_m
      Jul 30 '16 at 18:56






    • 2




      @ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to type A = , but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
      – SethMMorton
      Jul 30 '16 at 18:59





















    2














    import numpy as np

    A = np.float_(A)
    A *= B


    try this. I think are different array type you get fail.



    Cast






    share|improve this answer





















      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%2f38673531%2fmultiply-numpy-int-and-float-arrays-cannot-cast-ufunc-multiply-output-from-dtyp%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      6














      2 ways to solve this:



      You can solve this by replacing



      A *= B


      with



      A = (A * B)


      or with



      numpy.multiply(A, B, out=A, casting='unsafe')





      share|improve this answer




























        6














        2 ways to solve this:



        You can solve this by replacing



        A *= B


        with



        A = (A * B)


        or with



        numpy.multiply(A, B, out=A, casting='unsafe')





        share|improve this answer


























          6












          6








          6






          2 ways to solve this:



          You can solve this by replacing



          A *= B


          with



          A = (A * B)


          or with



          numpy.multiply(A, B, out=A, casting='unsafe')





          share|improve this answer














          2 ways to solve this:



          You can solve this by replacing



          A *= B


          with



          A = (A * B)


          or with



          numpy.multiply(A, B, out=A, casting='unsafe')






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 22 '18 at 18:14

























          answered Jun 30 '17 at 14:12









          seraloukseralouk

          5,71522339




          5,71522339

























              2














              You could use broadcasting to multiply the two arrays and take only the integer part as follows:



              In [2]: (A*B).astype(int)
              Out[2]: array([ 0, 4, 9, 16])


              Timing Constraints:



              In [8]: %timeit (A*B).astype(int)
              1000000 loops, best of 3: 1.65 µs per loop

              In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
              100000 loops, best of 3: 2.01 µs per loop





              share|improve this answer























              • OP wants to do the multiplication in place
                – ali_m
                Jul 30 '16 at 18:45










              • How fast is it compared to numpy.multiply(A, B, out=A, casting='unsafe') ?
                – Basj
                Jul 30 '16 at 18:45










              • @ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
                – SethMMorton
                Jul 30 '16 at 18:51










              • @SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
                – ali_m
                Jul 30 '16 at 18:56






              • 2




                @ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to type A = , but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
                – SethMMorton
                Jul 30 '16 at 18:59


















              2














              You could use broadcasting to multiply the two arrays and take only the integer part as follows:



              In [2]: (A*B).astype(int)
              Out[2]: array([ 0, 4, 9, 16])


              Timing Constraints:



              In [8]: %timeit (A*B).astype(int)
              1000000 loops, best of 3: 1.65 µs per loop

              In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
              100000 loops, best of 3: 2.01 µs per loop





              share|improve this answer























              • OP wants to do the multiplication in place
                – ali_m
                Jul 30 '16 at 18:45










              • How fast is it compared to numpy.multiply(A, B, out=A, casting='unsafe') ?
                – Basj
                Jul 30 '16 at 18:45










              • @ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
                – SethMMorton
                Jul 30 '16 at 18:51










              • @SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
                – ali_m
                Jul 30 '16 at 18:56






              • 2




                @ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to type A = , but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
                – SethMMorton
                Jul 30 '16 at 18:59
















              2












              2








              2






              You could use broadcasting to multiply the two arrays and take only the integer part as follows:



              In [2]: (A*B).astype(int)
              Out[2]: array([ 0, 4, 9, 16])


              Timing Constraints:



              In [8]: %timeit (A*B).astype(int)
              1000000 loops, best of 3: 1.65 µs per loop

              In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
              100000 loops, best of 3: 2.01 µs per loop





              share|improve this answer














              You could use broadcasting to multiply the two arrays and take only the integer part as follows:



              In [2]: (A*B).astype(int)
              Out[2]: array([ 0, 4, 9, 16])


              Timing Constraints:



              In [8]: %timeit (A*B).astype(int)
              1000000 loops, best of 3: 1.65 µs per loop

              In [9]: %timeit np.multiply(A, B, out=A, casting='unsafe')
              100000 loops, best of 3: 2.01 µs per loop






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Jul 30 '16 at 18:49

























              answered Jul 30 '16 at 18:40









              Nickil MaveliNickil Maveli

              17.4k43448




              17.4k43448












              • OP wants to do the multiplication in place
                – ali_m
                Jul 30 '16 at 18:45










              • How fast is it compared to numpy.multiply(A, B, out=A, casting='unsafe') ?
                – Basj
                Jul 30 '16 at 18:45










              • @ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
                – SethMMorton
                Jul 30 '16 at 18:51










              • @SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
                – ali_m
                Jul 30 '16 at 18:56






              • 2




                @ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to type A = , but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
                – SethMMorton
                Jul 30 '16 at 18:59




















              • OP wants to do the multiplication in place
                – ali_m
                Jul 30 '16 at 18:45










              • How fast is it compared to numpy.multiply(A, B, out=A, casting='unsafe') ?
                – Basj
                Jul 30 '16 at 18:45










              • @ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
                – SethMMorton
                Jul 30 '16 at 18:51










              • @SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
                – ali_m
                Jul 30 '16 at 18:56






              • 2




                @ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to type A = , but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
                – SethMMorton
                Jul 30 '16 at 18:59


















              OP wants to do the multiplication in place
              – ali_m
              Jul 30 '16 at 18:45




              OP wants to do the multiplication in place
              – ali_m
              Jul 30 '16 at 18:45












              How fast is it compared to numpy.multiply(A, B, out=A, casting='unsafe') ?
              – Basj
              Jul 30 '16 at 18:45




              How fast is it compared to numpy.multiply(A, B, out=A, casting='unsafe') ?
              – Basj
              Jul 30 '16 at 18:45












              @ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
              – SethMMorton
              Jul 30 '16 at 18:51




              @ali_m Nowhere does the OP mention they want to do the multiplication in place. Their only comment was that the longer way was "way much longer syntax".
              – SethMMorton
              Jul 30 '16 at 18:51












              @SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
              – ali_m
              Jul 30 '16 at 18:56




              @SethMMorton Well, both the example given in the question and the workaround the OP posted in the comments are explicitly performing in place multiplication.
              – ali_m
              Jul 30 '16 at 18:56




              2




              2




              @ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to type A = , but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
              – SethMMorton
              Jul 30 '16 at 18:59






              @ali_m I don't disagree with that, but based on this question and the other question it seems the OP is more interested in terseness than an in-place operation. Having said that, I think the assumption that they want an in place operation is good because of the examples given and that in-place saves having to type A = , but I always think it is safer to wait for the OP to explicitly make it clear what they want rather than make assumptions.
              – SethMMorton
              Jul 30 '16 at 18:59













              2














              import numpy as np

              A = np.float_(A)
              A *= B


              try this. I think are different array type you get fail.



              Cast






              share|improve this answer


























                2














                import numpy as np

                A = np.float_(A)
                A *= B


                try this. I think are different array type you get fail.



                Cast






                share|improve this answer
























                  2












                  2








                  2






                  import numpy as np

                  A = np.float_(A)
                  A *= B


                  try this. I think are different array type you get fail.



                  Cast






                  share|improve this answer












                  import numpy as np

                  A = np.float_(A)
                  A *= B


                  try this. I think are different array type you get fail.



                  Cast







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jul 30 '16 at 19:18









                  Ahmet İlginAhmet İlgin

                  254




                  254






























                      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%2f38673531%2fmultiply-numpy-int-and-float-arrays-cannot-cast-ufunc-multiply-output-from-dtyp%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