日撸java 三百行 (day 20 )辅助矩阵类

本文介绍了一个名为IntMatrix的Java类,用于处理矩阵的基本操作,如构造、获取单位矩阵、设置和获取元素值、矩阵相加和相乘。此外,还复习了异常处理,为后续学习图的代码打下基础。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

接下来的几天进入了图的代码学习,今天是准备工作,需要写出一个辅助类IntMatrix,里面包含了矩阵的基本操作。

package com.day20;

import java.security.PublicKey;
import java.util.Arrays;

public class IntMatrix {
    /**
     * the data
     */
    int data[][];


    /**
     * ********************
     * The first constructor.
     *
     * @param paraRows    The number of rows.
     * @param paraColumns The number of columns.
     * ********************
     */
    public IntMatrix(int paraRows, int paraColumns) {
        data = new int[paraRows][paraColumns];
    }// Of the first constructor

    /**
     * ********************
     * The second constructor. Construct a copy of the given matrix.
     *
     * @param paraMatrix The given matrix.
     * ********************
     */
    public IntMatrix(int paraMatrix[][]) {
        data = new int[paraMatrix.length][paraMatrix[0].length];
        for (int i = 0; i < paraMatrix.length; ++i) {
            for (int j = 0; j < paraMatrix[0].length; j++) {
                data[i][j] = paraMatrix[i][j];
            }// Of for j
        }// Of for i
    }// Of the second constructor

    /**
     * ********************
     * Get my data. Warning, the reference to the data instead of a copy of the
     * data is returned.
     *
     * @return The data matrix.
     * ********************
     */
    public int[][] getData() {
        return data;
    }// Of getData

    /**
     * ********************
     * The third constructor. Construct a copy of the given matrix.
     *
     * @param paraMatrix The given matrix.
     * ********************
     */
    public IntMatrix(IntMatrix paraMatrix) {
        this(paraMatrix.getData());
    }// Of the third constructor

    /**
     * ********************
     * Get identity matrix. The values at the diagonal are all 1.
     *
     * @param paraRows The given rows.
     * ********************
     */
    public static IntMatrix getIdentityMatrix(int paraRows) {
        IntMatrix resultMatrix = new IntMatrix(paraRows, paraRows);
        for (int i = 0; i < paraRows; ++i) {
            resultMatrix.data[i][i] = 1;
        }// Of for i
        return resultMatrix;
    }// Of getIdentityMatrix

    /**
     * ********************
     * Overrides the method claimed in Object, the superclass of any class.
     * ********************
     */
    public String toString() {
        return Arrays.deepToString(data);
    }// Of toString

    /**
     * ********************
     * Getter.
     *
     * @return The number of rows.
     * ********************
     */
    public int getRows() {
        return data.length;
    }// Of getRows

    /**
     * ********************
     * Getter.
     *
     * @return The number of columns.
     * ********************
     */
    public int getColumns() {
        return data[0].length;
    }// Of getColumns

    /**
     * ********************
     * Set one the value of one element.
     *
     * @param paraRow    The row of the element.
     * @param paraColumn The column of the element.
     * @param paraValue  The new value.
     * ********************
     */
    public void setValue(int paraRow, int paraColumn, int paraValue) {
        data[paraRow][paraColumn] = paraValue;
    }// Of setValue

    /**
     * ********************
     * Get the value of one element.
     *
     * @param paraRow    The row of the element.
     * @param paraColumn The column of the element.
     * ********************
     */
    public int getValue(int paraRow, int paraColumn) {
        return data[paraRow][paraRow];
    }// Of getValue

    /**
     * ********************
     * Add another matrix to me.
     *
     * @param paraMatrix The other matrix.
     * ********************
     */
    public void add(IntMatrix paraMatrix) throws Exception {
        int[][] tempData = paraMatrix.getData();
        if (tempData.length != data.length) {
            throw new Exception("Cannot add matrices. Rows not match: " + data.length + " vs. "
                    + tempData.length + ".");
        }// Of if
        if (tempData[0].length != data[0].length) {
            throw new Exception("Cannot add matrices. Rows not match: " + data[0].length + " vs. "
                    + tempData[0].length + ".");
        }// Of if
        for (int i = 0; i < data.length; ++i) {
            for (int j = 0; j < data[0].length; j++) {
                data[i][j] += tempData[i][j];
            }// Of for j
        }// Of for i

    }// Of add

    /**
     * ********************
     * Add two existing matrices.
     *
     * @param paraMatrix1 The first matrix.
     * @param paraMatrix2 The second matrix.
     * @return A new matrix.
     * ********************
     */
    public static IntMatrix add(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
        IntMatrix resultMatrix = new IntMatrix(paraMatrix1);
        resultMatrix.add(paraMatrix2);
        return resultMatrix;
    }// Of add


    /**
     * ********************
     * Multiply two existing matrices.
     *
     * @param paraMatrix1 The first matrix.
     * @param paraMatrix2 The second matrix.
     * @return A new matrix.
     * ********************
     */
    public static IntMatrix multiply(IntMatrix paraMatrix1, IntMatrix paraMatrix2) throws Exception {
        int[][] tempData1 = paraMatrix1.getData();
        int[][] tempData2 = paraMatrix2.getData();
        if (tempData1[0].length != tempData2.length) {
            throw new Exception("Cannot multiply matrices: " + tempData1[0].length + " vs. "
                    + tempData2.length + ".");
        }// Of if
        int[][] resultData = new int[tempData1.length][tempData2[0].length];
        for (int i = 0; i < tempData1.length; ++i) {
            for (int j = 0; j < tempData2[0].length; ++j) {
                for (int k = 0; k < tempData1[0].length; ++k) {
                    resultData[i][j] += tempData1[i][k] * tempData2[k][j];
                }// Of for k
            }// Of for j
        }// Of for i
        IntMatrix resultMatrix = new IntMatrix(resultData);
        return resultMatrix;
    }// Of multiply


    public static void main(String[] args) {
        //int data[][]=new int[][]{{1,2},{2,1}};
        IntMatrix tempMatrix1 = new IntMatrix(3, 3);
        tempMatrix1.setValue(0, 1, 1);
        tempMatrix1.setValue(1, 0, 1);
        tempMatrix1.setValue(1, 2, 1);
        tempMatrix1.setValue(2, 1, 1);
        System.out.println("The original matrix is: " + tempMatrix1);
        IntMatrix tempMatrix2 = null;
        //IntMatrix tempMatrix3 =new IntMatrix(data);
        try {
            tempMatrix2 = IntMatrix.multiply(tempMatrix1, tempMatrix1);
        } catch (Exception ee) {
            System.out.println(ee);
        } // Of try
        System.out.println("The square matrix is: " + tempMatrix2);

        IntMatrix tempMatrix3 = new IntMatrix(tempMatrix2);
        try {
            tempMatrix3.add(tempMatrix1);
        } catch (Exception ee) {
            System.out.println(ee);
        } // Of try
        System.out.println("The connectivity matrix is: " + tempMatrix3);

    }// Of main

}// Of class IntMatrix

我预感接下来几天会高频率用到这个类来创建对象,所以先罗列一下里面的重要方法:

  • IntMatrix(int paraMatrix[][]) 构造方法把一个二维数组封装到对象中
  • getData()导出当前对象的二维数组
  • getIdentityMatrix(int paraRows)创建一个单位阵
  • getRows()、getColumns()返回行数、列数 
  • setValue、getValue 对应给定位置设值、取值
  • add、multiply 对应矩阵相加、相乘

总结:今天的代码总体上难度不大,顺便回顾了异常的捕抛,接下来几天就得面对难度相对较大的图。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值