快速显式解决方案A x = bAx=b, , 低条件数b ∈R3b∈R3

计算科学 线性求解器 参考请求
2021-12-21 06:47:58

我正在寻找一个快速(我敢说是最优的吗?)显式解决 3x3 线性实际问题,Ax=bAR3×3,bR3

矩阵是通用的,但接近于条件数接近 1 的单位矩阵。因为实际上是精度约为 5 位的传感器测量值,所以我不介意由于数字而丢失几位问题。Ab

当然,基于任意数量的方法提出一个明确的解决方案并不难,但如果有一些东西在 FLOPS 计数方面被证明是最优的,那将是理想的(毕竟,整个问题可能适合 FP 寄存器!)。

(是的,这个例程经常被调用。我已经摆脱了低垂的果实,这是我的分析列表中的下一个......)

4个回答

你不能打败一个明确的公式。你可以在一张纸上写下解让编译器为您优化。任何其他方法几乎都不可避免地具有语句或循环(例如,对于迭代方法),这会使您的代码比任何直线代码都慢。x=A1biffor

由于矩阵非常接近恒等式,以下 Neumann 级数将很快收敛:

A1=k=0(IA)k

根据所需的准确性,它甚至可能足以在 2 个术语后截断:

A1I+(IA)=2IA.

这可能比直接公式稍快(如 Wolfgang Bangerth 的回答中所建议的那样),但准确性要低得多。


您可以使用 3 个术语获得更高的准确性:

A1I+(IA)+(IA)2=3I3A+A2

但是如果你写出逐个条目的公式(3I3A+A2)b,您正在查看与直接 3x3 矩阵逆公式相当数量的浮点运算(尽管您不必进行除法,这有点帮助)。

FLOPS 计数基于上述建议:

  • LU,无旋转:

    • Mul = 11,Div/Recip = 6,Add/Sub = 11,Total = 28;或者
    • Mul = 16,Div/Recip = 3,Add/Sub = 11,Total = 30
  • 带回代的高斯消元法,无旋转:

    • Mul = 11,Div/Recip = 6,Add/Sub = 11,Total = 28;或者
    • Mul = 16,Div/Recip = 3,Add/Sub = 11,Total = 30
  • 通过辅因子扩展的克莱默规则

    • Mul = 24,Div = 3,加/减 = 15,总计 = 42;或者
    • Mul = 27,Div = 1,加/减 = 15,总计 = 43
  • 显式逆然后相乘:

    • Mul = 30,Div = 3,加/减 = 17,总计 = 50;或者
    • Mul = 33,Div = 1,加/减 = 17,总计 = 51

MATLAB 概念验证:

通过辅因子扩展的克莱默规则

function k = CramersRule(A, m)
%
% FLOPS:
%
% Multiplications:        24
% Subtractions/Additions: 15
% Divisions:               3
%
% Total:                  42

a = A(1,1);
b = A(1,2);
c = A(1,3);

d = A(2,1);
e = A(2,2);
f = A(2,3);

g = A(3,1);
h = A(3,2);
i = A(3,3);

x = m(1);
y = m(2);
z = m(3);

ei = e*i;
fh = f*h;

di = d*i;
fg = f*g;

dh = d*h;
eg = e*g;

ei_m_fh = ei - fh;
di_m_fg = di - fg;
dh_m_eg = dh - eg;

yi = y*i;
fz = f*z;

yh = y*h;
ez = e*z;

yi_m_fz = yi - fz;
yh_m_ez = yh - ez;

dz = d*z;
yg = y*g;

dz_m_yg = dz - yg;
ez_m_yh = ez - yh;


det_a = a*ei_m_fh - b*di_m_fg + c*dh_m_eg;
det_1 = x*ei_m_fh - b*yi_m_fz + c*yh_m_ez;
det_2 = a*yi_m_fz - x*di_m_fg + c*dz_m_yg;
det_3 = a*ez_m_yh - b*dz_m_yg + x*dh_m_eg;


p = det_1 / det_a;
q = det_2 / det_a;
r = det_3 / det_a;

k = [p;q;r];

LU(无旋转)和反向替换:

function [x, y, L, U] = LUSolve(A, b)
% Total FLOPS count:     (w/ Mods)
%
% Multiplications:  11    16
% Divisions/Recip:   6     3
% Add/Subtractions: 11    11
% Total =           28    30
%

A11 = A(1,1);
A12 = A(1,2);
A13 = A(1,3);

A21 = A(2,1);
A22 = A(2,2);
A23 = A(2,3);

A31 = A(3,1);
A32 = A(3,2);
A33 = A(3,3);

b1 = b(1);
b2 = b(2);
b3 = b(3);

L11 = 1;
L22 = 1;
L33 = 1;

U11 = A11;
U12 = A12;
U13 = A13;

L21 = A21 / U11;
L31 = A31 / U11;

U22 = (A22 - L21*U12);
L32 = (A32 - L31*U12) / U22;

U23 = (A23 - L21*U13);

U33 = (A33 - L31*U13 - L32*U23);

y1 = b1;
y2 = b2 - L21*y1;
y3 = b3 - L31*y1 - L32*y2;

x3 = (y3                  ) / U33;
x2 = (y2 -          U23*x3) / U22;
x1 = (y1 - U12*x2 - U13*x3) / U11;

L = [ ...
    L11,   0,   0;
    L21, L22,   0;
    L31, L32, L33];

U = [ ...
    U11, U12, U13;
      0, U22, U23;
      0,   0, U33];

x = [x1;x2;x3];
y = [y1;y2;y3];

显式逆然后相乘:

function x = ExplicitInverseMultiply(A, m)
%
% FLOPS count:                  Alternative
%
% Multiplications:        30            33
% Divisions:               3             1
% Additions/Subtractions: 17            17
% Total:                  50            51


a = A(1,1);
b = A(1,2);
c = A(1,3);

d = A(2,1);
e = A(2,2);
f = A(2,3);

g = A(3,1);
h = A(3,2);
i = A(3,3);

ae = a*e;
af = a*f;
ah = a*h;
ai = a*i;

bd = b*d;
bf = b*f;
bg = b*g;
bi = b*i;

cd = c*d;
ce = c*e;
cg = c*g;
ch = c*h;

dh = d*h;
di = d*i;

eg = e*g;
ei = e*i;

fg = f*g;
fh = f*h;

dh_m_eg = (dh - eg);
ei_m_fh = (ei - fh);
fg_m_di = (fg - di);

A = ei_m_fh;
B = fg_m_di;
C = dh_m_eg;
D = (ch - bi);
E = (ai - cg);
F = (bg - ah);
G = (bf - ce);
H = (cd - af);
I = (ae - bd);

det_A = a*ei_m_fh + b*fg_m_di + c*dh_m_eg;

x1 =  (A*m(1) + D*m(2) + G*m(3)) / det_A;
x2 =  (B*m(1) + E*m(2) + H*m(3)) / det_A;
x3 =  (C*m(1) + F*m(2) + I*m(3)) / det_A;

x = [x1;x2;x3];

高斯消除:

function x = GaussianEliminationSolve(A, m)
%
% FLOPS Count:      Min   Alternate
%
% Multiplications:  11    16
% Divisions:         6     3
% Add/Subtractions: 11    11
% Total:            28    30
%

a = A(1,1);
b = A(1,2);
c = A(1,3);

d = A(2,1);
e = A(2,2);
f = A(2,3);

g = A(3,1);
h = A(3,2);
i = A(3,3);

b1 = m(1);
b2 = m(2);
b3 = m(3);

% Get to echelon form

op1 = d/a;

e_dash  = e  - op1*b;
f_dash  = f  - op1*c;
b2_dash = b2 - op1*b1;

op2 = g/a;

h_dash  = h  - op2*b;
i_dash  = i  - op2*c;
b3_dash = b3 - op2*b1; 

op3 = h_dash / e_dash;

i_dash2  = i_dash  - op3*f_dash;
b3_dash2 = b3_dash - op3*b2_dash;

% Back substitution

x3 = (b3_dash2                  ) / i_dash2;
x2 = (b2_dash        - f_dash*x3) / e_dash;
x1 = (b1      - b*x2 -      c*x3) / a;

x = [x1 ; x2 ; x3];

注意:请随意在这篇文章中添加您自己的方法和计数。

可能是克莱默法则。如果你可以避免旋转,也许是 LU 分解;它是一个 3x3 矩阵,因此手动展开循环很容易。其他任何事情都可能涉及分支,我怀疑 Krylov 子空间方法是否会在 1 或 2 次迭代中足够频繁地收敛以使其值得。