“TypeError: only integer scalar arrays can be converted to a scalar index” on Hill-RSA cryptography












0














I'm trying to code a Hill-RSA cryptography program that you can see a part of here:



q2=31
alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",","," ",".",";","_"]
X=np.zeros((m,1),dtype=np.int32)
Y=np.zeros((m,1),dtype=np.int32)
Texte_decode="";
for i in range(1,(len(Texte_code)/m)+1):
for k in range(0,m):
j=0
while (Texte_code[k+m*(i-1)]<>alphabet[j+1]):
j=j+1
X[k]=j
X=X.transpose()
A2=np.zeros((m,m),dtype=np.int32)
for u in range(0,m):
for l in range(0,m):
A2[u,l]=A[u,l]
Y=X.dot(A2)
Y=Y.transpose()
pprint(Y)
Y2=np.zeros((m,1),dtype=np.int32)
for ind in range(0,m):
Y2[ind]=Y[ind]%q2
pprint(Y2)
for k in range(0,m):
Texte_decode=Texte_decode+alphabet[Y2[k]+1]
for i in range(len(Texte_decode),len(Texte_decode)-m+1,-1):
if Texte_decode[i]=="." and Texte_decode[i-1]==".":
Texte_decode=Texte_decode[1,i-1]
print Texte_decode


When i execute this part, I get




"TypeError: only integer scalar arrays can be converted to a scalar index"




on the line



Texte_decode=Texte_decode+alphabet[Y2[k]+1]


Can anyone help me get rid of this error?



Thanks in advance










share|improve this question





























    0














    I'm trying to code a Hill-RSA cryptography program that you can see a part of here:



    q2=31
    alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",","," ",".",";","_"]
    X=np.zeros((m,1),dtype=np.int32)
    Y=np.zeros((m,1),dtype=np.int32)
    Texte_decode="";
    for i in range(1,(len(Texte_code)/m)+1):
    for k in range(0,m):
    j=0
    while (Texte_code[k+m*(i-1)]<>alphabet[j+1]):
    j=j+1
    X[k]=j
    X=X.transpose()
    A2=np.zeros((m,m),dtype=np.int32)
    for u in range(0,m):
    for l in range(0,m):
    A2[u,l]=A[u,l]
    Y=X.dot(A2)
    Y=Y.transpose()
    pprint(Y)
    Y2=np.zeros((m,1),dtype=np.int32)
    for ind in range(0,m):
    Y2[ind]=Y[ind]%q2
    pprint(Y2)
    for k in range(0,m):
    Texte_decode=Texte_decode+alphabet[Y2[k]+1]
    for i in range(len(Texte_decode),len(Texte_decode)-m+1,-1):
    if Texte_decode[i]=="." and Texte_decode[i-1]==".":
    Texte_decode=Texte_decode[1,i-1]
    print Texte_decode


    When i execute this part, I get




    "TypeError: only integer scalar arrays can be converted to a scalar index"




    on the line



    Texte_decode=Texte_decode+alphabet[Y2[k]+1]


    Can anyone help me get rid of this error?



    Thanks in advance










    share|improve this question



























      0












      0








      0


      0





      I'm trying to code a Hill-RSA cryptography program that you can see a part of here:



      q2=31
      alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",","," ",".",";","_"]
      X=np.zeros((m,1),dtype=np.int32)
      Y=np.zeros((m,1),dtype=np.int32)
      Texte_decode="";
      for i in range(1,(len(Texte_code)/m)+1):
      for k in range(0,m):
      j=0
      while (Texte_code[k+m*(i-1)]<>alphabet[j+1]):
      j=j+1
      X[k]=j
      X=X.transpose()
      A2=np.zeros((m,m),dtype=np.int32)
      for u in range(0,m):
      for l in range(0,m):
      A2[u,l]=A[u,l]
      Y=X.dot(A2)
      Y=Y.transpose()
      pprint(Y)
      Y2=np.zeros((m,1),dtype=np.int32)
      for ind in range(0,m):
      Y2[ind]=Y[ind]%q2
      pprint(Y2)
      for k in range(0,m):
      Texte_decode=Texte_decode+alphabet[Y2[k]+1]
      for i in range(len(Texte_decode),len(Texte_decode)-m+1,-1):
      if Texte_decode[i]=="." and Texte_decode[i-1]==".":
      Texte_decode=Texte_decode[1,i-1]
      print Texte_decode


      When i execute this part, I get




      "TypeError: only integer scalar arrays can be converted to a scalar index"




      on the line



      Texte_decode=Texte_decode+alphabet[Y2[k]+1]


      Can anyone help me get rid of this error?



      Thanks in advance










      share|improve this question















      I'm trying to code a Hill-RSA cryptography program that you can see a part of here:



      q2=31
      alphabet=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",","," ",".",";","_"]
      X=np.zeros((m,1),dtype=np.int32)
      Y=np.zeros((m,1),dtype=np.int32)
      Texte_decode="";
      for i in range(1,(len(Texte_code)/m)+1):
      for k in range(0,m):
      j=0
      while (Texte_code[k+m*(i-1)]<>alphabet[j+1]):
      j=j+1
      X[k]=j
      X=X.transpose()
      A2=np.zeros((m,m),dtype=np.int32)
      for u in range(0,m):
      for l in range(0,m):
      A2[u,l]=A[u,l]
      Y=X.dot(A2)
      Y=Y.transpose()
      pprint(Y)
      Y2=np.zeros((m,1),dtype=np.int32)
      for ind in range(0,m):
      Y2[ind]=Y[ind]%q2
      pprint(Y2)
      for k in range(0,m):
      Texte_decode=Texte_decode+alphabet[Y2[k]+1]
      for i in range(len(Texte_decode),len(Texte_decode)-m+1,-1):
      if Texte_decode[i]=="." and Texte_decode[i-1]==".":
      Texte_decode=Texte_decode[1,i-1]
      print Texte_decode


      When i execute this part, I get




      "TypeError: only integer scalar arrays can be converted to a scalar index"




      on the line



      Texte_decode=Texte_decode+alphabet[Y2[k]+1]


      Can anyone help me get rid of this error?



      Thanks in advance







      python arrays numpy matrix hill-climbing






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 20 at 14:16









      James K Polk

      29.7k106795




      29.7k106795










      asked Nov 20 at 7:15









      Victor Jacquens

      33




      33
























          1 Answer
          1






          active

          oldest

          votes


















          1














          What debugging have you done? Did you review the nature of the elements of the problem line?



          Texte_decode=Texte_decode+alphabet[Y2[k]+1]


          k comes from for k in range(0,m): so that shouldn't be the problem. It's clearly an integer.



          Your printed Y2. It's initialed as a (m,1) array. So Y2[k] will be a (1,) array, right?



          alphabet is a list.



          In an interactive shell let's try a test case:



          In [70]: [1,2,3,4][np.array([1])]
          ---------------------------------------------------------------------------
          TypeError Traceback (most recent call last)
          <ipython-input-70-4ad73b219fa3> in <module>()
          ----> 1 [1,2,3,4][np.array([1])]

          TypeError: only integer scalar arrays can be converted to a scalar index


          Same error message!



          If we start with a 1d array, and select an element, the indexing works:



          In [71]: [1,2,3,4][np.arange(4)[1]]
          Out[71]: 2


          Now that we understand the problem, the solution should be obvious, right?






          share|improve this answer





















          • I'm not sure I understood your answer. m is declared before and is equal to 5, which makes Y2 a (1,5) array
            – Victor Jacquens
            Nov 21 at 8:38












          • Y2=np.zeros((m,1),dtype=np.int32)
            – hpaulj
            Nov 21 at 10:45










          • This is how I declared Y2. What improvment do you suggest me to do?
            – Victor Jacquens
            Nov 21 at 14:38










          • Explain why you chose to initial Y2 with that shape? What's different in my 2 test examples?
            – hpaulj
            Nov 21 at 15:41










          • So I was supposed to translate a Maple code to python and that's how I figured it would work. Here is the original code if you want to look at it link
            – Victor Jacquens
            Nov 21 at 16:53











          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%2f53387978%2ftypeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-on-hi%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









          1














          What debugging have you done? Did you review the nature of the elements of the problem line?



          Texte_decode=Texte_decode+alphabet[Y2[k]+1]


          k comes from for k in range(0,m): so that shouldn't be the problem. It's clearly an integer.



          Your printed Y2. It's initialed as a (m,1) array. So Y2[k] will be a (1,) array, right?



          alphabet is a list.



          In an interactive shell let's try a test case:



          In [70]: [1,2,3,4][np.array([1])]
          ---------------------------------------------------------------------------
          TypeError Traceback (most recent call last)
          <ipython-input-70-4ad73b219fa3> in <module>()
          ----> 1 [1,2,3,4][np.array([1])]

          TypeError: only integer scalar arrays can be converted to a scalar index


          Same error message!



          If we start with a 1d array, and select an element, the indexing works:



          In [71]: [1,2,3,4][np.arange(4)[1]]
          Out[71]: 2


          Now that we understand the problem, the solution should be obvious, right?






          share|improve this answer





















          • I'm not sure I understood your answer. m is declared before and is equal to 5, which makes Y2 a (1,5) array
            – Victor Jacquens
            Nov 21 at 8:38












          • Y2=np.zeros((m,1),dtype=np.int32)
            – hpaulj
            Nov 21 at 10:45










          • This is how I declared Y2. What improvment do you suggest me to do?
            – Victor Jacquens
            Nov 21 at 14:38










          • Explain why you chose to initial Y2 with that shape? What's different in my 2 test examples?
            – hpaulj
            Nov 21 at 15:41










          • So I was supposed to translate a Maple code to python and that's how I figured it would work. Here is the original code if you want to look at it link
            – Victor Jacquens
            Nov 21 at 16:53
















          1














          What debugging have you done? Did you review the nature of the elements of the problem line?



          Texte_decode=Texte_decode+alphabet[Y2[k]+1]


          k comes from for k in range(0,m): so that shouldn't be the problem. It's clearly an integer.



          Your printed Y2. It's initialed as a (m,1) array. So Y2[k] will be a (1,) array, right?



          alphabet is a list.



          In an interactive shell let's try a test case:



          In [70]: [1,2,3,4][np.array([1])]
          ---------------------------------------------------------------------------
          TypeError Traceback (most recent call last)
          <ipython-input-70-4ad73b219fa3> in <module>()
          ----> 1 [1,2,3,4][np.array([1])]

          TypeError: only integer scalar arrays can be converted to a scalar index


          Same error message!



          If we start with a 1d array, and select an element, the indexing works:



          In [71]: [1,2,3,4][np.arange(4)[1]]
          Out[71]: 2


          Now that we understand the problem, the solution should be obvious, right?






          share|improve this answer





















          • I'm not sure I understood your answer. m is declared before and is equal to 5, which makes Y2 a (1,5) array
            – Victor Jacquens
            Nov 21 at 8:38












          • Y2=np.zeros((m,1),dtype=np.int32)
            – hpaulj
            Nov 21 at 10:45










          • This is how I declared Y2. What improvment do you suggest me to do?
            – Victor Jacquens
            Nov 21 at 14:38










          • Explain why you chose to initial Y2 with that shape? What's different in my 2 test examples?
            – hpaulj
            Nov 21 at 15:41










          • So I was supposed to translate a Maple code to python and that's how I figured it would work. Here is the original code if you want to look at it link
            – Victor Jacquens
            Nov 21 at 16:53














          1












          1








          1






          What debugging have you done? Did you review the nature of the elements of the problem line?



          Texte_decode=Texte_decode+alphabet[Y2[k]+1]


          k comes from for k in range(0,m): so that shouldn't be the problem. It's clearly an integer.



          Your printed Y2. It's initialed as a (m,1) array. So Y2[k] will be a (1,) array, right?



          alphabet is a list.



          In an interactive shell let's try a test case:



          In [70]: [1,2,3,4][np.array([1])]
          ---------------------------------------------------------------------------
          TypeError Traceback (most recent call last)
          <ipython-input-70-4ad73b219fa3> in <module>()
          ----> 1 [1,2,3,4][np.array([1])]

          TypeError: only integer scalar arrays can be converted to a scalar index


          Same error message!



          If we start with a 1d array, and select an element, the indexing works:



          In [71]: [1,2,3,4][np.arange(4)[1]]
          Out[71]: 2


          Now that we understand the problem, the solution should be obvious, right?






          share|improve this answer












          What debugging have you done? Did you review the nature of the elements of the problem line?



          Texte_decode=Texte_decode+alphabet[Y2[k]+1]


          k comes from for k in range(0,m): so that shouldn't be the problem. It's clearly an integer.



          Your printed Y2. It's initialed as a (m,1) array. So Y2[k] will be a (1,) array, right?



          alphabet is a list.



          In an interactive shell let's try a test case:



          In [70]: [1,2,3,4][np.array([1])]
          ---------------------------------------------------------------------------
          TypeError Traceback (most recent call last)
          <ipython-input-70-4ad73b219fa3> in <module>()
          ----> 1 [1,2,3,4][np.array([1])]

          TypeError: only integer scalar arrays can be converted to a scalar index


          Same error message!



          If we start with a 1d array, and select an element, the indexing works:



          In [71]: [1,2,3,4][np.arange(4)[1]]
          Out[71]: 2


          Now that we understand the problem, the solution should be obvious, right?







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 at 17:34









          hpaulj

          110k774140




          110k774140












          • I'm not sure I understood your answer. m is declared before and is equal to 5, which makes Y2 a (1,5) array
            – Victor Jacquens
            Nov 21 at 8:38












          • Y2=np.zeros((m,1),dtype=np.int32)
            – hpaulj
            Nov 21 at 10:45










          • This is how I declared Y2. What improvment do you suggest me to do?
            – Victor Jacquens
            Nov 21 at 14:38










          • Explain why you chose to initial Y2 with that shape? What's different in my 2 test examples?
            – hpaulj
            Nov 21 at 15:41










          • So I was supposed to translate a Maple code to python and that's how I figured it would work. Here is the original code if you want to look at it link
            – Victor Jacquens
            Nov 21 at 16:53


















          • I'm not sure I understood your answer. m is declared before and is equal to 5, which makes Y2 a (1,5) array
            – Victor Jacquens
            Nov 21 at 8:38












          • Y2=np.zeros((m,1),dtype=np.int32)
            – hpaulj
            Nov 21 at 10:45










          • This is how I declared Y2. What improvment do you suggest me to do?
            – Victor Jacquens
            Nov 21 at 14:38










          • Explain why you chose to initial Y2 with that shape? What's different in my 2 test examples?
            – hpaulj
            Nov 21 at 15:41










          • So I was supposed to translate a Maple code to python and that's how I figured it would work. Here is the original code if you want to look at it link
            – Victor Jacquens
            Nov 21 at 16:53
















          I'm not sure I understood your answer. m is declared before and is equal to 5, which makes Y2 a (1,5) array
          – Victor Jacquens
          Nov 21 at 8:38






          I'm not sure I understood your answer. m is declared before and is equal to 5, which makes Y2 a (1,5) array
          – Victor Jacquens
          Nov 21 at 8:38














          Y2=np.zeros((m,1),dtype=np.int32)
          – hpaulj
          Nov 21 at 10:45




          Y2=np.zeros((m,1),dtype=np.int32)
          – hpaulj
          Nov 21 at 10:45












          This is how I declared Y2. What improvment do you suggest me to do?
          – Victor Jacquens
          Nov 21 at 14:38




          This is how I declared Y2. What improvment do you suggest me to do?
          – Victor Jacquens
          Nov 21 at 14:38












          Explain why you chose to initial Y2 with that shape? What's different in my 2 test examples?
          – hpaulj
          Nov 21 at 15:41




          Explain why you chose to initial Y2 with that shape? What's different in my 2 test examples?
          – hpaulj
          Nov 21 at 15:41












          So I was supposed to translate a Maple code to python and that's how I figured it would work. Here is the original code if you want to look at it link
          – Victor Jacquens
          Nov 21 at 16:53




          So I was supposed to translate a Maple code to python and that's how I figured it would work. Here is the original code if you want to look at it link
          – Victor Jacquens
          Nov 21 at 16:53


















          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.





          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53387978%2ftypeerror-only-integer-scalar-arrays-can-be-converted-to-a-scalar-index-on-hi%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

          "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

          Alcedinidae

          Origin of the phrase “under your belt”?