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

📄 lowertriangle.java

📁 JAVA 数学程序库 提供常规的数值计算程序包
💻 JAVA
字号:
package jmathlib.toolbox.jmathlib.matrix;

import jmathlib.core.tokens.numbertokens.DoubleNumberToken;
import jmathlib.core.tokens.Token;
import jmathlib.core.tokens.OperandToken;
import jmathlib.core.functions.ExternalFunction;

/**An external function for converting a matrix into lower 
triangular form*/
public class lowertriangle extends ExternalFunction
{
	public OperandToken evaluate(Token[] operands)
	{
		OperandToken result = null;
		Token operand = operands[0];
		
		if(operand instanceof DoubleNumberToken)
		{
			DoubleNumberToken matrix = ((DoubleNumberToken)operand);
			
			if(matrix.getSizeX() == matrix.getSizeY())
			{
				int size = matrix.getSizeX();
				
				result = new DoubleNumberToken(calcLowerTriangle(matrix.getReValues(), size));
			}
	        else
	        {
	            jmathlib.core.interpreter.Errors.throwMathLibException(ERR_NOT_SQUARE_MATRIX);
	        }			
		}
		
		return result;		
	}
	
	/**Convert the matrix into lower triangular form by swapping
	rows then adding rows together*/
	private double[][] calcLowerTriangle(double[][] values, int size)
	{
		//swap the rows so that the values on the 
		//primary diagonal are non zero
		double[][] result = checkRows(values, size);
		
		for(int i = size-2; i >= 0; i--)
		{
			for(int j = size-1; j > i; j--)
			{
				//find the factor needed to zero this cell
				double factor = result[i][j] / result[j][j];

				//calculate the new values for the row i
				//after adding row j * factor
				for(int k = 0; k < size; k++)
				{
					result[i][k] = result[i][k] - result[j][k] * factor;
				}
			}
		}
		
		return result;		
	}

	/**makes sure that the values on the primary diagonal
	are not zero. swapping rows if nescessary*/	
	private double[][] checkRows(double[][] values, int size)
	{
		for(int i = size-1; i >= 0; i--)
		{	
			if(values[i][i] == 0)	//then swap this row with anothre
			{
				boolean found = false;
				//find a row that fits
				for(int j = size-1; j >= 0; j--)
				{
					if(values[j][i] != 0 && values[i][j] != 0)
					{
						//swap row i and j
						double temp[] = values[i];
						values[i] = values[j];
						values[j] = temp;
						found = true;
						break;
					}
				}
				
				//if a matching row wasnt't found then move
				//the row to the top of the matrix
				if(!found)
				{
					double temp[] = values[i];
					values[i] = values[0];
					values[0] = temp;					
				}
			}
		}
		
		return values;
	}
}

/*
@GROUP
matrix
@SYNTAX
answer = LOWERTRIANGLE(matrix)
@DOC
Converts a square matrix into it's lower triangular form.
@NOTES
@EXAMPLES
LOWERTRIANGLE([1,2;3,4]) = [-0.5, 0; 3, 4]
@SEE
uppertriangle

*/

⌨️ 快捷键说明

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