⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pnmatrix.java

📁 Petri网分析工具PIPE is open-source
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      }    }    return h;  }//######################################################################################  /**   * Set a single element.   * @param i    Row index.   * @param j    Column index.   * @param s    A(i,j).   * @exception  ArrayIndexOutOfBoundsException   */  public void set (int i, int j, int s) {    A[i][j] = s;  }//######################################################################################  /**   * Set a submatrix.   * @param i0   Initial row index   * @param i1   Final row index   * @param j0   Initial column index   * @param j1   Final column index   * @param X    A(i0:i1,j0:j1)   * @exception  ArrayIndexOutOfBoundsException Submatrix indices   */  public void setMatrix (int i0, int i1, int j0, int j1, PNMatrix X) {    try {      for (int i = i0; i <= i1; i++) {        for (int j = j0; j <= j1; j++) {          A[i][j] = X.get(i-i0,j-j0);        }      }    } catch(ArrayIndexOutOfBoundsException e) {      throw new ArrayIndexOutOfBoundsException("Submatrix indices");    }  }//######################################################################################  /**   * Set a submatrix.   * @param r    Array of row indices.   * @param c    Array of column indices.   * @param X    A(r(:),c(:))   * @exception  ArrayIndexOutOfBoundsException Submatrix indices   */  public void setMatrix (int[] r, int[] c, PNMatrix X) {    try {      for (int i = 0; i < r.length; i++) {        for (int j = 0; j < c.length; j++) {          A[r[i]][c[j]] = X.get(i,j);        }      }    } catch(ArrayIndexOutOfBoundsException e) {      throw new ArrayIndexOutOfBoundsException("Submatrix indices");    }  }//######################################################################################  /**   * Set a submatrix.   * @param r    Array of row indices.   * @param j0   Initial column index   * @param j1   Final column index   * @param X    A(r(:),j0:j1)   * @exception  ArrayIndexOutOfBoundsException Submatrix indices   */  public void setMatrix (int[] r, int j0, int j1, PNMatrix X) {    try {      for (int i = 0; i < r.length; i++) {        for (int j = j0; j <= j1; j++) {          A[r[i]][j] = X.get(i,j-j0);        }      }    } catch(ArrayIndexOutOfBoundsException e) {      throw new ArrayIndexOutOfBoundsException("Submatrix indices");    }  }//######################################################################################  /**   * Set a submatrix.   * @param i0 Initial row index   * @param i1 Final row index   * @param c Array of column indices.   * @param X A(i0:i1,c(:))   * @exception ArrayIndexOutOfBoundsException Submatrix indices   */  public void setMatrix (int i0, int i1, int[] c, PNMatrix X) {    try {      for (int i = i0; i <= i1; i++) {        for (int j = 0; j < c.length; j++) {          A[i][c[j]] = X.get(i-i0,j);        }      }    } catch(ArrayIndexOutOfBoundsException e) {      throw new ArrayIndexOutOfBoundsException("Submatrix indices");    }  }//######################################################################################  /**   * Matrix transpose.   * @return    A'   */  public PNMatrix transpose () {    PNMatrix X = new PNMatrix(n,m);    int[][] C = X.getArray();    for (int i = 0; i < m; i++) {      for (int j = 0; j < n; j++) {        C[j][i] = A[i][j];      }    }    return X;  }//######################################################################################  /**   * Find the greatest common divisor of a column matrix (vector) of integers.   * @return     The gcd of the column matrix.   */  public int gcd () {    int gcd = A[0][0];    for (int i = 1; i < m; i++)      if ((A[i][0] != 0) || (gcd != 0))        gcd = gcd2(gcd, A[i][0]);    return gcd; // this should never be zero  }//######################################################################################  /**   * Find the greatest common divisor of 2 integers.   * @param a    The first integer.   * @param b    The second integer.   * @return     The gcd of the column   */  private int gcd2 (int a, int b) {    int gcd;    a = Math.abs(a);    b = Math.abs(b);    // ensure b > a    if (b <= a){      int tmp = b;      b = a;      a = tmp;    }    if ( a!=0 ) {      for ( int tmp ; (b %= a) != 0; ) {        tmp = b;        b = a;        a = tmp;      }      gcd = a;    } else if (b != 0) {      gcd = b;    } else {      // both args == 0, return 0, but this shouldn't happen      gcd = 0;    }    return gcd;  }//######################################################################################  /**   * Unary minus   * @return - A   */  public PNMatrix uminus () {    PNMatrix X = new PNMatrix(m,n);    int[][] C = X.getArray();    for (int i = 0; i < m; i++) {      for (int j = 0; j < n; j++) {        C[i][j] = -A[i][j];      }    }    return X;  }//######################################################################################  /**   * C = A + B   * @param B another matrix   * @return A + B   */  public PNMatrix plus (PNMatrix B) {    checkMatrixDimensions(B);    PNMatrix X = new PNMatrix(m,n);    int[][] C = X.getArray();    for (int i = 0; i < m; i++) {      for (int j = 0; j < n; j++) {        C[i][j] = A[i][j] + B.A[i][j];      }    }    return X;  }//######################################################################################  /**   * A = A + B   * @param B another matrix   * @return A + B   */  public PNMatrix plusEquals (PNMatrix B) {    checkMatrixDimensions(B);    for (int i = 0; i < m; i++) {      for (int j = 0; j < n; j++) {        A[i][j] = A[i][j] + B.A[i][j];      }    }    return this;  }//######################################################################################  /**   * C = A - B   * @param B another matrix   * @return A - B   */  public PNMatrix minus (PNMatrix B) {    checkMatrixDimensions(B);    int[][] C = new int[m][n]; //= X.getArray();    for (int i = 0; i < m; i++) {      for (int j = 0; j < n; j++) {        C[i][j] = A[i][j] - B.A[i][j];      }    }    PNMatrix X = new PNMatrix(C);    return X;  }//######################################################################################  /**   * A = A - B   * @param B another matrix   * @return A - B   */  public PNMatrix minusEquals (PNMatrix B) {    checkMatrixDimensions(B);    for (int i = 0; i < m; i++) {      for (int j = 0; j < n; j++) {        A[i][j] = A[i][j] - B.A[i][j];      }    }    return this;  }//######################################################################################  /**   * Multiply a matrix by an int in place, A = s*A   * @param s int multiplier   * @return replace A by s*A   */  public PNMatrix timesEquals (int s) {    for (int i = 0; i < m; i++) {      for (int j = 0; j < n; j++) {        A[i][j] = s*A[i][j];      }    }    return this;  }//######################################################################################  /**   * Multiply a row matrix by a column matrix, A = s*A   * @param B column vector   * @return product of row vector A by column vector B   */  public int vectorTimes (PNMatrix B) {    int product = 0;    for (int j = 0; j < n; j++) {      product += A[0][j] * B.get(j, 0);    }    return product;  }//######################################################################################  /**   * Divide a matrix by an int in place, A = s*A   * @param s int divisor   * @return replace A by A/s   */  public PNMatrix divideEquals (int s) {    for (int i = 0; i < m; i++) {      for (int j = 0; j < n; j++) {        A[i][j] = A[i][j]/s;      }    }    return this;  }//######################################################################################  /**   * Generate identity matrix]   * @param m Number of rows.   * @param n Number of colums.   * @return An m-by-n matrix with ones on the diagonal and zeros elsewhere.   */  public static PNMatrix identity (int m, int n) {    PNMatrix A = new PNMatrix(m,n);        int[][] X = A.getArray();    for (int i = 0; i < m; i++) {      for (int j = 0; j < n; j++) {        X[i][j] = (i == j ? 1 : 0);      }    }    return A;  }//######################################################################################  /**   * Print the matrix to stdout.   Line the elements up in columns   * with a Fortran-like 'Fw.d' style format.   * @param w    Column width.   * @param d    Number of digits after the decimal.   */  public void print (int w, int d) {      print(new PrintWriter(System.out,true),w,d);  }//######################################################################################  	/**    * Print the matrix to a string.   Line the elements up in columns    * with a Fortran-like 'Fw.d' style format.    * @param w    Column width.    * @param d    Number of digits after the decimal.    * @return     The formated string to output.    */  public String printString (int w, int d) {    if (isZeroMatrix())      return "\nNone\n\n";    ByteArrayOutputStream arrayStream = new ByteArrayOutputStream();    print(new PrintWriter(arrayStream,true),w,d);    String output = arrayStream.toString();    return output;  }//######################################################################################  /**   * Print the matrix to the output stream.   Line the elements up in   * columns with a Fortran-like 'Fw.d' style format.   * @param output Output stream.   * @param w Column width.   * @param d Number of digits after the decimal.   */  public void print (PrintWriter output, int w, int d) {    DecimalFormat format = new DecimalFormat();    format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.UK));    format.setMinimumIntegerDigits(1);    format.setMaximumFractionDigits(d);    format.setMinimumFractionDigits(d);    format.setGroupingUsed(false);    print(output,format,w+2);  }//######################################################################################  /**   * Print the matrix to stdout.  Line the elements up in columns.   * Use the format object, and right justify within columns of width   * characters.   * Note that if the matrix is to be read back in, you probably will want   * to use a NumberFormat that is set to UK Locale.   * @param format A Formatting object for individual elements.   * @param width Field width for each column.   * @see java.text.DecimalFormat#setDecimalFormatSymbols   */  public void print (NumberFormat format, int width) {      print(new PrintWriter(System.out,true),format,width); }//######################################################################################  // DecimalFormat is a little disappointing coming from Fortran or C's printf.  // Since it doesn't pad on the left, the elements will come out different  // widths.  Consequently, we'll pass the desired column width in as an  // argument and do the extra padding ourselves.  /**   * Print the matrix to the output stream.  Line the elements up in columns.   * Use the format object, and right justify within columns of width   * characters.   * Note that is the matrix is to be read back in, you probably will want   * to use a NumberFormat that is set to US Locale.   * @param output the output stream.   * @param format A formatting object to format the matrix elements   * @param width Column width.   * @see java.text.DecimalFormat#setDecimalFormatSymbols   */      public void print (PrintWriter output, NumberFormat format, int width) {        output.println();  // start on new line.        for (int i = 0; i < m; i++) {          for (int j = 0; j < n; j++) {            String s = format.format(A[i][j]); // format the number            int padding = Math.max(1,width-s.length()); // At _least_ 1 space            for (int k = 0; k < padding; k++)              output.print(' ');            output.print(s);          }          output.println();        }        output.println();   // end with blank line.      }//######################################################################################      /**       * Throws IllegalArgumentException if dimensions of A and B differ.       *  @param   B   The matrix to check the dimensions.       */      private void checkMatrixDimensions (PNMatrix B) {        if (B.m != m || B.n != n) {          throw new IllegalArgumentException("Matrix dimensions must agree.");        }      }//######################################################################################      /**       * Used to display intermediate results for checking       * @param a  The array of integers to print.       */      public void printArray(int[] a){        int n = a.length;      }//    ######################################################################################      public void setToZero() {        for(int i = 0 ; i < m ; i++)          for(int j = 0 ; j < n ; j++)            A[i][j] = 0;      }//######################################################################################}//######################################################################################

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -