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

📄 matrix.java

📁 用java实现浮点数加减乘除四则混合运算
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/**
 * This is the class for the matrix. 
 * It defines matrix arithmetic and floating-number arithmetic including
 * addition, subtraction, multiplication, division,transposition,inversion
 * and so on.
 * 
 * @author Rachel Chen 0122070
 * @version 1.0     2003/3/22
 */
public class Matrix
{
	/** The row number of the matrix. */
	private int row;

	/** The column number of the matrix. */
	private int column;

	/** The elements in the matrix. */
	private Object[] elements;

	/** The values of the matrix denoted by a two-dimension array. */
	private double[][] value;

	/** 
	 * The constructor for the Matrix class.
	 * It is only used to parse the initial situation.<p>
	 * When get the appropriate matrices,it will call the private
	 * method parseMatrix() <p> to parse it into one matrix.
	 *
	 * @param r The row number of the matrix.
	 * @param c The column number of the matrix.
	 * @param e The matrix elements for the matrix.
	 */
  public Matrix( int r, int c, Matrix[] e )
	{
		row = r;
		column = c;

		elements = new Object[e.length];

		for ( int i = 0; i < e.length; i++ )
		{
			elements[i] = e [i];
		}

		parseMatrix();
	}

  /** 
	 * The constructor for the Matrix class.<p>
	 * It is used to make the matrix from the given Double Float array paramater.
	 *	 
	 * @param e The Double Float elements for the matrix.
	 */
	public Matrix( Double[] e )
	{
		row = 1;
		column = e.length;

		elements = new Double[e.length];

		for ( int i = 0; i < e.length; i++ )
		{
			elements[i] = e [i];
		}		
		parseMatrix();
	}

  /** 
	 * The constructor for the Matrix class. <p>
	 * It is used to get the matrix from the given float array.
	 *
	 * @param e The float elements for the matrix.
	 */
	public Matrix( double[][] e )
	{
		row = e.length;
		column = e[0].length;	
		value = new double[row][column];

		for ( int i = 0; i < row ; i++ )
		{		
			for ( int j = 0; j < column; j++ )
			{						
				value[i][j] = e[i][j];				
			}
		}	
	}

	/** 
	 * The constructor for the Matrix class. <p>
	 * It initializes  an empty matrix of the fixed row and column.
	 *
	 * @param r The row number of the matrix.
	 * @param c The column number of the matrix.	 
	 */
	public Matrix( int r, int c )
	{
		row = r;
		column = c;

		value= new double[row][column];
	}

	/** 
	 * Return the row of the matrix.
	 *
	 * @return The row of the matrix.
	 */
	public int row()
	{
		return row;
	}
  
	/** 
	 * Return the column of the matrix.
	 *
	 * @return The column of the matrix.
	 */
	public int column()
	{
		return column;
	}

	/**
	 * Do the floating-number addition.
	 *
	 * @param x The left operand for addition.
	 * @param y The right operand for addition.
	 * @return The sum of x and y.
	 */
	public static Double add( double x, double y )
	{
		return new Double( x + y );
	}
  
	/**
	 * Do the matrix addition.
	 * It first checks the validity of the two matrices. <P>If they are in the
	 * different dimension,give the error information. <p>Then do the addition
	 * and return the answer.
	 *
	 * @param x The left operand for addition.
	 * @param y The right operand for addition.
	 * @return The sum of x and y.
	 */
	public static Matrix add(Matrix x, Matrix y )
	{ 
		if ( x.row()!= y.row() || x.column() != y.column() )
	  {
			System.out.println( 
				"Error: The two matrices are not in the same dimention.\n" );
			return null;
	  }

		Matrix result = new Matrix( x.row(), x.column());
	  result.value= new double[x.row()][x.column()];		

		for ( int i = 0; i < x.row(); i++ )
		{
			for ( int j = 0; j < x.column(); j++ )
			{
				result.value[i][j] = x.value[i][j] + y.value[i][j];	
			}
		}

    return result;
	}
  
	/**
	 * Do the floating-number subtraction.
	 *
	 * @param x The left operand for subtraction.
	 * @param y The right operand for subtraction.
	 * @return The result of x - y.
	 */
	public static Double minus( double x, double y )
	{
		return new Double( x - y );
	}

	/**
	 * Do the matrix subtraction.
	 * It first checks the validity of the two matrices.  <p>
	 * If they are in the different dimension,give the error information. <p>
	 * Then do the subtraction and return the answer.<p>
	 *
	 * @param x The left operand for subtraction.
	 * @param y The right operand for subtraction.
	 * @return The result of x - y.
	 */
	public static Matrix minus( Matrix x, Matrix y )
	{
		if ( x.row()!= y.row() || x.column() != y.column() )
	  {
			System.out.println( 
				"Error: The two matrices are not in the same dimention.\n" );
			return null;
	  }

		Matrix result = new Matrix( x.row(), x.column());
	  result.value= new double[x.row()][x.column()];		

		for ( int i = 0; i < x.row(); i++ )
		{
			for ( int j = 0; j < x.column(); j++ )
			{
				result.value[i][j] = x.value[i][j] - y.value[i][j];	
			}
		}
    return result;
	}
	
  /**
	 * Do the floating-number multiplication.
	 *
	 * @param x The left operand for multiplication.
	 * @param y The right operand for multiplication.
	 * @return The product of x and y.
	 */
	public static Double multi( double x, double y )
	{
		return new Double( x * y );
	}
  
	/**
	 * Do the multiplication between floating-number and matrix.
	 *
	 * @param x The left operand for multiplication.
	 * @param y The right operand for multiplication.
	 * @return The product of x and y.
	 */
	public static Matrix multi( Matrix x, double y )
	{		
		Matrix result = new Matrix( x.row(), x.column());
	  result.value= new double[x.row()][x.column()];		

		for ( int i = 0; i < x.row(); i++ )
		{
			for ( int j = 0; j < x.column(); j++ )
			{
				result.value[i][j] = x.value[i][j] * y;	
			}
		}
    return result;
	}

  /**
	 * Do the multiplication between floating-number and matrix.
	 *
	 * @param x The left operand for multiplication.
	 * @param y The right operand for multiplication.
	 * @return The product of x and y.
	 */
	public static Matrix multi( double y, Matrix x )
	{
		return multi( x, y );
	}

	/**
	 * Do the matrix multiplication.
	 * It first checks the validity of the two matrices. 
	 * If the column of the first  matrix doesn't equal the row 
	 * of the second matrix, give the error information. 
	 * Then do the multiplication and return the answer. 
	 *
	 * @param x The left operand for multiplication.
	 * @param y The right operand for multiplication.
	 * @return The product of x and y. If it a 1 * 1 matrix return the Double 
	 * float object for it. <p> Otherwise return the matrix object.
	 */
	public static Object multi( Matrix x, Matrix y )
	{
		if ( x.column() != y.row() )
		{
			System.out.println( 
				"Error: The two matrices can't multiply!" );
			return null;
		}

		Matrix result = new Matrix( x.row(), y.column() );
    int temp = 0;

    for ( int i = 0; i < x.row(); i++ )
    {
			for ( int j = 0; j < y.column(); j++ )
			{
				for ( int k = 0; k < x.column(); k++ )
				{
				   temp += x.value[i][k] * y.value[k][j] ;
				}
				result.value[i][j] = temp;
				temp = 0;
			}
		}
		if ( result.row() == 1 && result.column() == 1)
		{
			return new Double( result.value[0][0] );
		}
		return result;
	}

  /**
	 * Do the floating-number division.
	 *
	 * @param x The left operand for division.
	 * @param y The right operand for division.
	 * @return The quotient of x and  y.
	 */
	public static Double divide( double x, double y )
	{
		return new Double( x / y );
	}
  
	/**
	 * Do the division between floating-number and matrix. <p>
	 * Call the multi( x, 1/y ) to do the operation.
	 *
	 * @param x The left operand for division.
	 * @param y The right operand for division.
	 * @return The quotient of x and y.
	 */
	public static Matrix divide ( Matrix x, double y )
	{
    return multi( x, 1/y );
	}

  /**
	 * Give the identity matrix of dimension x * x.
	 *
	 * @param x The dimension of the matrix.
	 * @return The identity matrix of the required dimension.
	 */
  public static Matrix identityMatrix( int x )
	{
		Matrix matrix = new Matrix( x, x );
		
		for ( int i = 0; i < x; i++ )
		{
			for ( int j = 0; j < x; j++ )
			{
				if ( i == j )
				{

⌨️ 快捷键说明

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