1. 程式人生 > >js高斯消元法解決n元一次方程組--算等額分期

js高斯消元法解決n元一次方程組--算等額分期

fine 計算 calc 賦值 行合並 change amp block 有效

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;
    continue;
    }
    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;
    }
    }
    return determinant;
    }

    /**

  • 將系數矩陣和方程值的矩陣進行合並成增廣矩陣
  • @param coefficient
  • 系數矩陣
  • @param value
  • 方程值
  • @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];
    }
    }
    for (let i = 0; i < value.length; i++) {
    temp[i][temp[i].length - 1] = value[i];
    }
    return temp;
    }

    /**

  • 檢查有效的行數,看非零行的個數
  • @param value
  • 需要檢查的數組
  • @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];
    continue;
    }
    }
    }
    }
    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
    /
    /

    // 方程的未知數的個數
    int n = 4;
    // 系數矩陣
    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) {
    toastLog("未知數有" + n + "個,消元法後獲取的階梯方程組有"
  • checkMatrixRow + "個方程,多於未知數個數,所以該方程有無解");
    } else {
    toastLog("未知數有" + n + "個,消元法後獲取的階梯方程組有"
  • checkMatrixRow + "個方程,等於未知數個數,所以該方程有解");
    let result = calculationResult(mathMatrix);
    for (let i = 0; i < result.length; i++) {
    toastLog("方程組的解為x" + (i + 1) + "="+
    df.format(result[i]));
    }
    }

    } catch (e) {  
        // TODO Auto-generated catch block  
        log(e.name + ": " + e.message);  
    }  

    }
    main();

js高斯消元法解決n元一次方程組--算等額分期