importClass(java.text.DecimalFormat);
let df = new DecimalFormat("0.##");
/***
增廣矩陣機型初等行變化的算法
@param value
需要算的增廣矩陣
@return 計算的結果
*/
function mathDeterminantCalculation(value) {
// 當矩陣的行數大于2時
for (let i = 0; i < value.length; i++) {
// 檢查數組對角線位置的數值是否是0,如果是零則對該數組進行調換,查找到一行不為0的進行調換
if (value[i][i] == 0) {
value = changeDeterminantNoZero(value, i, i);
}
for (let j = 0; j < i; j++) {
// 讓開始處理的行的首位為0處理為三角形式
// 如果要處理的列為0則和自己調換一下位置,這樣就省去了計算
if (value[i][j] == 0) {
continue;
// 如果要是要處理的行是0則和上面的一行進行調換
if (value[j][j] == 0) {
let temp = value[i];
value[i] = value[i - 1];
value[i - 1] = temp;
let ratio = -(value[i][j] / value[j][j]);
value[i] = addValue(value[i], value[j], ratio);
return value;
将i行之前的每一行乘以一個系數,使得從i行的第i列之前的數字置換為0
@param currentRow
目前要處理的行
@param frontRow
i行之前的周遊的行
@param ratio
要乘以的系數
@return 将i行i列之前數字置換為0後的新的行
/
function addValue(currentRow,frontRow,
ratio){
for (let i = 0; i < currentRow.length; i++) {
currentRow[i] += frontRow[i] ratio;
currentRow[i] =parseFloat(currentRow[i]);
//( df.format(currentRow[i]));
//Double.parseDouble(df.format(currentRow[i]));
return currentRow;
/**
指定列的位置是否為0,查找第一個不為0的位置的行進行位置調換,如果沒有則傳回原來的值
@param determinant
需要處理的行列式
@param line
要調換的行
@param row
要判斷的列
function changeDeterminantNoZero(determinant,
line, row){
for (let j = line; j < determinant.length; j++) {
// 進行行調換
if (determinant[j][row] != 0) {
let temp = determinant[line];
determinant[line] = determinant[j];
determinant[j] = temp;
return determinant;
将系數矩陣和方程值的矩陣進行合并成增廣矩陣
@param coefficient
系數矩陣
方程值
@return 增廣矩陣
function transferMatrix(coefficient,
value) {
let temp = new Array(coefficient.length);
if (coefficient.length != value.length) {
return temp;
// 将方程值添加到系數矩陣中
for (let i = 0; i < coefficient.length; i++) {
temp[i] =new Array(coefficient[0].length + 1);
for (let j = 0; j < coefficient[0].length; j++) {
temp[i][j] = coefficient[i][j];
temp[i][temp[i].length - 1] = value[i];
檢查有效的行數,看非零行的個數
需要檢查的數組
@return 非零行的個數
function effectiveMatrix(value) {
for (let i = value.length - 1; i > -1; i--) {
for (let j = 0; j < value[i].length; j++) {
if (value[i][j] != 0) {
return i + 1;
return 0;
當方程組有解的時候計算方程組的解
@param mathMatrix
方程組的增廣矩陣
@return 方程組的解
function calculationResult(mathMatrix) {
// 有解時方程組的個數等于方程組的未知數
let result = new Array(mathMatrix.length);
log(mathMatrix);
for (let i = mathMatrix.length - 1; i > -1; i--) {
let temp = 0;
for (let j = mathMatrix[i].length; j > 0; j--) {
// 第一個為方程的解,需要将解指派給臨時變量
if (mathMatrix[i][j - 1] != 0) {
if (j == mathMatrix[i].length) {
temp = mathMatrix[i][j - 1];
} else if ((j - 1 > -1 )&& ((result[j - 1])!=undefined)) {
temp -= mathMatrix[i][j - 1] result[j - 1];
//log((j - 1 > -1 )&&(typeof(result[j - 1])!=undefined)+";j="+j+";undefined="+(typeof(result[j - 1])!=undefined));
} else {
result[i] = temp / mathMatrix[i][j - 1];
return result;
function main() {
// 方程的未知數的個數
int n = 3;
// 系數矩陣
double[][] test = { { 2, 3, 1 }, { 1,1, 1 }, { 1, 2, -1 } };
// 方程的解
double[] value = { 11, 6, 2 };
方程組的解為x1=1.0 方程組的解為x2=2.0 方程組的解為x3=3.0
/*
int n = 4;
double[][] test = { { 3, 1, -1, 1 },{ 1, -1, 1,2 }, {2,1,2,-1},{ 1,0, 2, 1 } };
double[] value ={ -3, 4, 7,6 };
方程組的解為x1=1.0 方程組的解為x2=-2.0 方程組的解為x3=3.0 方程組的解為x4=-1.0
double[][] test = { { 1, -3, 4, -5 },{ 1, -1, -1,2 }, {1,2,0,5},{ 2,-1, 3, -2 } };
double[] value ={ 0, 0, 0,0 };
方程組有零解
let n = 5;
let test = [[2,1, 1,1,1 ],[ 1, 2, 1,1,1 ], [1,1,3,1,1],[ 1,1,1,4,1 ],[1,1,1,1,5]];
let value =[ 4,5,9,0,-5 ];
n=3;
value=[11,0,-2];
test=[[3,1,-2],[1,1,-1],[-1,-1,-3]]
try {
// 轉換成增廣矩陣并進行初等行變化
let mathMatrix = mathDeterminantCalculation(transferMatrix(
test, value));
// 找出非零行的個數
let checkMatrixRow = effectiveMatrix(mathMatrix);
// 根據未知數的個數和方程組非零行的個數來判斷目前方程組的解的情況
if (n > checkMatrixRow) {
toastLog("未知數有" + n + "個,消元法後擷取的階梯方程組有"
checkMatrixRow + "個方程,少于未知數個數,是以該方程有無數組解");
} else if (n < checkMatrixRow) {
checkMatrixRow + "個方程,多于未知數個數,是以該方程有無解");
checkMatrixRow + "個方程,等于未知數個數,是以該方程有解");
let result = calculationResult(mathMatrix);
for (let i = 0; i < result.length; i++) {
toastLog("方程組的解為x" + (i + 1) + "="+
df.format(result[i]));
main();