title: Java-Matrix multiplication failing in loop only
body: <p>I'm a math student writing a program to help with my research, but a small flaw is holding me up and I'm not sure what's going wrong.</p>
  
  <p>In the first two calls to the power method, the computation is correct. In the loop, the first power works fine, but the matrix that is made when j=2 is a 2x2 matrix, instead of the correct one I usually get!</p>
  
  <p>I'm sorry if I haven't included enough information, but the program is becoming quite large and I'm unfamiliar with conventions in CS and on SO. I appreciate any help.</p>
  
  <pre><code>    public Queue&lt;OrdInt&gt; tree(Matrix inputMatr) {
      Queue&lt;OrdInt&gt; holder = new LinkedList&lt;OrdInt&gt;();
      LinkedList&lt;Matrix&gt; powers = new LinkedList&lt;Matrix&gt;();
      powers.add(Matrix.identity(inputMatr.size));
      int Np=0;
      int j=1;
  
      while(!powers.contains(inputMatr.power(j)) &amp;&amp; j&lt;Math.pow(2, inputMatr.size)){
          powers.add(inputMatr.power(j));
  
          try {
              Np = (2*powers.get(j).nullspaceDim() - powers.get(j-1).nullspaceDim() - powers.get(j).matrixMultiply(inputMatr).nullspaceDim());
  
              System.out.println("J+1 dim: " +powers.get(j).matrixMultiply(inputMatr).size);
          } catch (Exception e) {
              e.printStackTrace();
          }
  
          if(Np!=0){
              holder.offer(new OrdInt(j, Np));
          }
          j++;
      }
  
      return holder;
  </code></pre>
  
  <p>}</p>
  
  <p>edit: by request, the code for <code>power</code> and <code>matrix multiply</code>.</p>
  
  <pre><code>public Matrix matrixMultiply(Matrix one) throws Exception{
      if(this.size != one.size){
          throw new Exception("Incompatible Matrices.");
      }
      Matrix total = new Matrix(this.size);
  
      for(int x=0; x&lt;this.size; x++){
          for (int y=0; y&lt;this.size; y++){
              int hold=0;
              for(int z=0; z&lt;this.size; z++){
                  hold += this.get(x,z)*one.get(z,y);
              }
              total.set(x,y,hold%2);
          }
      }
      return total;
  }
  
  public Matrix power(int power) {
      Matrix holder = this;
      if(power==0){
          return identity(this.size);
      }
      if(power==1){
          return this;
      }
      else{
          for(int x=0; x&lt;power; x++){
              try {
                  holder =holder.matrixMultiply(this);
              } catch (Exception e) {
                  e.printStackTrace();
              }
      }
      return holder;
  }
  
  }
  </code></pre>