Is it possible to solve a non-square under/over constrained matrix using Accelerate/LAPACK?

眉间皱痕 提交于 2020-01-05 19:22:59

问题


Is it possible to solve a non-square under/over constrained matrix using Accelerate/LAPACK? Such as the following two matrices. If any variables are under constrained they should equal 0 instead of being infinite.

So in the under constrained case: A, D & E would equal 0, while B, C & F equal -1.

In the over constrained case all variables would be equal to -1.

Under Constrained:

 ____                        ____
| (A) (B) (C) (D) (E) (F)        |
| -1   0   0   1   0   0   |  0  |
|  1   0   0   0  -1   0   |  0  |
|  0  -1   1   0   0   0   |  0  |
|  0   1   0   0   0  -1   |  0  |
|  0   1   0   0   0   0   | -1  |
|____                        ____|

Over Constrained:

 ____                        ____
|                                |
| -1   0   0   1   0   0   |  0  |
|  1   0   0   0  -1   0   |  0  |
|  0  -1   1   0   0   0   |  0  |
|  0   1   0   0   0  -1   |  0  |
|  0   1   0   0   0   0   | -1  |
|  0   0   1  -1   0   0   |  0  |
|  1  -1   0   0   0   0   |  0  |
|____                        ____|

回答1:


Yes!

void SolveUnderdeterminedSystem() {

    __CLPK_integer m = 5;
    __CLPK_integer n = 6;
    __CLPK_integer nrhs = 1;
    double A[30] = {
        -1.0,  1.0,  0.0,  0.0,  0.0,
         0.0,  0.0, -1.0,  1.0,  1.0,
         0.0,  0.0,  1.0,  0.0,  0.0,
         1.0,  0.0,  0.0,  0.0,  0.0,
         0.0, -1.0,  0.0,  0.0,  0.0,
         0.0,  0.0,  0.0, -1.0,  0.0
    };
    __CLPK_integer lda = 5;
    double x[6] = { 0.0, 0.0, 0.0, 0.0, -1.0, 0.0 };
    __CLPK_integer ldb = 6;
    /* Need to allocate at least 2*min(m,n) workspace. */
    double work[12];
    __CLPK_integer workSize = 12;
    __CLPK_integer info;

    dgels_("N", &m, &n, &nrhs, A, &lda, x, &ldb, work, &workSize, &info);

    if (info)
        printf("Could not solve system; dgels exited with error %d\n", info);
    else
        printf("Solution is [%f, %f, %f, %f, %f, %f]\n",
               x[0], x[1], x[2], x[3], x[4], x[5]);
}

The same routine will also solve over-determined systems in the least-squares sense (the result will be a minimizer of the residual ||Ax - b||).

Note that dgels_ assumes that the matrix has full rank (i.e., rank(A) = min(m, n)). If this is not the case, you will need to use a different routine (dgelsd_) that uses an SVD factorization instead of QR.

You seem to be asking a lot of questions about LAPACK. It would be well worth your time to read the documentation.



来源:https://stackoverflow.com/questions/4319246/is-it-possible-to-solve-a-non-square-under-over-constrained-matrix-using-acceler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!