是否可以使用 Accelerate/LAPACK 求解非方欠/过约束矩阵?

发布于 2024-10-05 10:57:27 字数 876 浏览 0 评论 0原文

是否可以使用 Accelerate/LAPACK 求解非方欠/过约束矩阵?比如下面两个矩阵。如果任何变量受到约束,它们应该等于 0 而不是无限。

所以在约束条件下:A,D & E 等于 0,而 B、C 和 E 等于 0。 F 等于-1。

在过度约束的情况下,所有变量都将等于 -1。

约束不足:

 ____                        ____
| (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  |
|____                        ____|

约束过度:

 ____                        ____
|                                |
| -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  |
|____                        ____|

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  |
|____                        ____|

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陈年往事 2024-10-12 10:57:35

是的!

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]);
}

相同的例程还将解决最小二乘意义上的超定系统(结果将是残差 ||Ax - b|| 的最小化)。

请注意,dgels_ 假设矩阵具有满秩(即,rank(A) = min(m, n))。如果不是这种情况,您将需要使用不同的例程 (dgelsd_),该例程使用 SVD 分解而不是 QR。

您似乎问了很多有关 LAPACK 的问题。非常值得您花时间阅读文档

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文