亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

基于線性方程計算值的算法

基于線性方程計算值的算法

ibeautiful 2022-11-02 15:09:21
我正在使用java基于線性方程計算PayStructure中各種Paycodes的值。我的不同方程如下:CTC = Fixed ValueBasic = CTC * 0.4HRA = Basic/2ConveyanceAllowance = Fixed ValueProvidentFund = Basic * 0.12Gratuity = Basic * .0481OtherAllowance = (CTC - (Basic + HRA + ConveyanceAllowance + ProvidentFund + Gratuity))我試過使用這里給出的解決方案。但是這個解決方案只有在所有計算值都是整數的情況下才有效,在我的情況下,這些值也可以包含十進制數字。我根據上述條件修改的代碼如下:public class PayStructure {    public static void main(String[] args) {        findAndprintSolutions(1, 1000000);    }    private static void findAndprintSolutions(int from, int to) {        for (int a = from; a < to; a++) {            for (int b = from; b < to; b++) {                for (int c = from; c < to; c++) {                    for (int d = from; d < to; d++) {                        for (int e = from; e < to; e++) {                            for (int f = from; f < to; f++) {                                for (int g = from; g < to; g++) {                                    if (isSolution(a, b, c, d, e, f, g))                                        printSolution(new int[] { a, b, c, d, e, f, g });                                }                            }                        }                    }                }            }        }    }    private static boolean isSolution(int a, int b, int c, int d, int e, int f, int g) {        if (a != 100000)            return false;        if (b != a * (.4))            return false;        if (c != b / 2)            return false;        if (d != 10000)            return false;        if (e != b * (.12))            return false;        if (f != b * (.0481))            return false;        if (g != (a - (b + c + d + e + f)))            return false;        return true;    }此外,上述代碼將被終止,因為 CTC 的最大值可能是數百萬,并且根據變量的數量,時間復雜度最終會達到millions^NumberOfVariables. 是否有任何其他可能性來計算基于給定方程的值?方程和變量的數量可能會有所不同,但會有一個解決方案來計算每個變量的值,因此通用解決方案的任何輸入都會更好。
查看完整描述

3 回答

?
慕的地8271018

TA貢獻1796條經驗 獲得超4個贊

也許您最好的選擇是弄清楚如何將其轉化為形式的線性方程組的形式c[1]x[1] + c[2]x[2] … + c[n]x[n] = 0。從那里,您可以使用廣泛建立的線性系統技術來求解系統。有關大量信息,請參閱Wikipedia頁面。您可以讓用戶以這種形式為您的方法提供輸入,或者您可以對每個方程進行少量處理以對其進行轉換(例如,如果所有方程在 LHS 上都有一個變量,如您的示例所示,則翻轉簽名并將其放在 RHS 的末尾)。

解釋求解線性方程組的理論超出了這個答案的范圍,但基本上,如果有一個有效的分配,你的系統要么是唯一確定的,如果不存在有效的分配是過度確定的,或者如果有無限多的分配是可能的. 如果有一個獨特的任務,你會得到數字;如果系統未確定,您至少會得到一組約束,這些約束必須包含無限多個解決方案中的任何一個;如果不確定,您將一無所獲并且知道原因。


查看完整回答
反對 回復 2022-11-02
?
隔江千里

TA貢獻1906條經驗 獲得超10個贊

使用 Java 的一些線性代數庫。例如,使用此處記錄的矩陣運算求解線性方程組。您的深層嵌套循環太慢了,有更好的算法可用。



查看完整回答
反對 回復 2022-11-02
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

這就是我解決這個問題的方法。首先,我通過將所有變量放在左側并將值 & 0 放在右側來創建方程:


CTC = 1000000

(0.4)CTC - Basic = 0

(0.5)Basic-HRA = 0

ConvAll = 10000

(0.12)Basic-PF = 0

(0.0481)Basic - Gratuity = 0

CTC - (Basic + HRA + ConvAll + PF+ Gratuity + OtherAll) = 0

然后我創建了一個這樣的矩陣:


|1          0     0    0   0   0   0| |CTC     | = |1000000|

|0.4       -1     0    0   0   0   0| |Basic   | = |0      |

|0         0.5   -1    0   0   0   0| |HRA     | = |0

|0          0     0    1   0   0   0| |ConvAll | = |10000  |

|0         0.12   0    0  -1   0   0| |PF      | = |0      |

|0        0.0481  0    0   0  -1   0| |Gratuity| = |10000  |

|1         -1    -1   -1  -1  -1  -1| |OtherAll| = |0      |

在此之后,我計算了(上面第一個矩陣的逆)和(最右邊的矩陣)的乘積,并使用下面的代碼得到了每個分量的相應值:


public class Matrix


{

    static int n = 0;


    public static void main(String argv[]) {

        Scanner input = new Scanner(System.in);

        System.out.println("Enter the dimension of square matrix: ");


        n = input.nextInt();

        double a[][] = new double[n][n];

        System.out.println("Enter the elements of matrix: ");

        for (int i = 0; i < n; i++)

            for (int j = 0; j < n; j++)

                a[i][j] = input.nextDouble();

        double d[][] = invert(a);

        System.out.println();

        System.out.println("Enter the equation values: ");

        System.out.println();

        double b[][] = new double[n][1];

        for (int i = 0; i < n; i++) {

            b[i][0] = input.nextDouble();

        }


        double e[][] = multiplyMatrix(d, b);

        System.out.println();

        System.out.println("The final solution is: ");

        System.out.println();

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < 1; j++) {

                System.out.printf(e[i][j] + " ");

            }

            System.out.println();

        }

        input.close();

    }


    public static double[][] invert(double a[][]) {

        int n = a.length;

        double x[][] = new double[n][n];

        double b[][] = new double[n][n];

        int index[] = new int[n];

        for (int i = 0; i < n; ++i)

            b[i][i] = 1;


        // Transform the matrix into an upper triangle

        gaussian(a, index);


        // Update the matrix b[i][j] with the ratios stored

        for (int i = 0; i < n - 1; ++i)

            for (int j = i + 1; j < n; ++j)

                for (int k = 0; k < n; ++k)

                    b[index[j]][k] -= a[index[j]][i] * b[index[i]][k];


        // Perform backward substitutions

        for (int i = 0; i < n; ++i) {

            x[n - 1][i] = b[index[n - 1]][i] / a[index[n - 1]][n - 1];

            for (int j = n - 2; j >= 0; --j) {

                x[j][i] = b[index[j]][i];

                for (int k = j + 1; k < n; ++k) {

                    x[j][i] -= a[index[j]][k] * x[k][i];

                }

                x[j][i] /= a[index[j]][j];

            }

        }

        return x;

    }


    // Method to carry out the partial-pivoting Gaussian


    // elimination. Here index[] stores pivoting order.


    public static void gaussian(double a[][], int index[]) {

        int n = index.length;

        double c[] = new double[n];


        // Initialize the index

        for (int i = 0; i < n; ++i)

            index[i] = i;


        // Find the rescaling factors, one from each row

        for (int i = 0; i < n; ++i) {

            double c1 = 0;

            for (int j = 0; j < n; ++j) {

                double c0 = Math.abs(a[i][j]);

                if (c0 > c1)

                    c1 = c0;

            }

            c[i] = c1;

        }


        // Search the pivoting element from each column

        int k = 0;


        for (int j = 0; j < n - 1; ++j) {

            double pi1 = 0;

            for (int i = j; i < n; ++i) {

                double pi0 = Math.abs(a[index[i]][j]);

                pi0 /= c[index[i]];

                if (pi0 > pi1) {

                    pi1 = pi0;

                    k = i;

                }

            }


            // Interchange rows according to the pivoting order

            int itmp = index[j];

            index[j] = index[k];

            index[k] = itmp;

            for (int i = j + 1; i < n; ++i) {

                double pj = a[index[i]][j] / a[index[j]][j];

                // Record pivoting ratios below the diagonal

                a[index[i]][j] = pj;

                // Modify other elements accordingly

                for (int l = j + 1; l < n; ++l)

                    a[index[i]][l] -= pj * a[index[j]][l];

            }

        }

    }


    public static double[][] multiplyMatrix(double a[][], double b[][]) {

        double c[][] = new double[n][1];

        for (int i = 0; i < n; i++) {

            for (int j = 0; j < 1; j++) {

                for (int k = 0; k < n; k++) {

                    c[i][j] = c[i][j] + a[i][k] * b[k][j];

                }

            }

        }

        return c;

    }

}

謝謝大家的線索。


查看完整回答
反對 回復 2022-11-02
  • 3 回答
  • 0 關注
  • 164 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號