算法名称:带状对角矩阵乘法
带状对角矩阵定义:
带状对角系统紧靠在对角线左边(下边)有m1≥0个非零元素,紧靠其右边(上边)有m2≥0个非零元素。当然,这仅在m1和m2<<N时才有意义。这种情况下,用LU分解求解线性系统可以比通用N×N的情况完成的更快,占用空间更少。
用aij精确定义带状对角矩阵,表示为
aij = 0 当 j > i + m2 或 i > j + m1
带状对角矩阵用一种称为压缩形式的方式进行存储及操作,把矩阵顺时针旋转45°,是非零元素排成一个细长的矩阵,它有m1+1+m2列和N行。例如,
其中N=7,m1=2,m2=1,存储在压缩形式为7×4矩阵
其中x表示压缩格式中浪费的空间,它们在运算中不被引用,因而其值可随意。
注:原矩阵对角线元素现在第m1列中,对角线左下的元素在其左侧,对角线右上的元素在其右侧。
运行示例:
The origin left matrix (before compression):
3.0 1.0 0.0 0.0 0.0 0.0 0.0
4.0 1.0 5.0 0.0 0.0 0.0 0.0
9.0 2.0 6.0 5.0 0.0 0.0 0.0
0.0 3.0 5.0 8.0 9.0 0.0 0.0
0.0 0.0 7.0 9.0 3.0 2.0 0.0
0.0 0.0 0.0 3.0 8.0 4.0 6.0
0.0 0.0 0.0 0.0 2.0 4.0 4.0
----------------------------------
The new left matrix (after compression):
0.0 0.0 3.0 1.0
0.0 4.0 1.0 5.0
9.0 2.0 6.0 5.0
3.0 5.0 8.0 9.0
7.0 9.0 3.0 2.0
3.0 8.0 4.0 6.0
2.0 4.0 4.0 0.0
----------------------------------
The right matrix:
1.0
2.0
3.0
4.0
5.0
6.0
7.0
----------------------------------
Solution matix:
5.0
21.0
51.0
98.0
84.0
118.0
62.0
示例程序:
package com.nc4nr.chapter02.banmul;


public class BanMul ...{


double[][] a = ...{

...{ 3.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 },

...{ 4.0, 1.0, 5.0, 0.0, 0.0, 0.0, 0.0 },

...{ 9.0, 2.0, 6.0, 5.0, 0.0, 0.0, 0.0 },

...{ 0.0, 3.0, 5.0, 8.0, 9.0, 0.0, 0.0 },

...{ 0.0, 0.0, 7.0, 9.0, 3.0, 2.0, 0.0 },

...{ 0.0, 0.0, 0.0, 3.0, 8.0, 4.0, 6.0 },

...{ 0.0, 0.0, 0.0, 0.0, 2.0, 4.0, 4.0 }
};
int anrow = 7,
m1 = 2, // 对角线下有m1行
m2 = 1; // 对角线上有m2行
double[][] ca = new double[7][4];

double[][] x = ...{

...{1.0},

...{2.0},

...{3.0},

...{4.0},

...{5.0},

...{6.0},

...{7.0}
};
double[] b = new double[anrow];

private void cmpban() ...{
int n = anrow;

for (int i = 0; i < n; i++) ...{
int k = i - m1;
int tmploop = Math.min(m1+m2+1,n - k);
for (int j = Math.max(0, -k); j < tmploop; j++) ca[i][j] = a[i][j+k];
}
}

private void banmul() ...{
int n = anrow;

for (int i = 0; i < n; i++) ...{
int k = i - m1,
tmploop = Math.min(m1+m2+1, n-k);
b[i] = 0.0;

for (int j=Math.max(0, -k); j < tmploop; j++) ...{
b[i] += ca[i][j]*x[j+k][0];
}
}
System.out.println("Solution matix:");
for (int i = 0; i < n; i++)
System.out.println(b[i]);
}

private void output(double[][] a, int nrow, int ncol) ...{

for (int i = 0; i < nrow; i++) ...{
String str = "";

for (int j = 0; j < ncol; j++) ...{
str += a[i][j] + " ";
}
System.out.println(str);
}
System.out.println("----------------------------------");
}

public BanMul() ...{
System.out.println("The origin left matrix (before compression):");
output(a,anrow,anrow);
cmpban();
System.out.println("The new left matrix (after compression):");
output(ca,anrow,4);
System.out.println("The right matrix:");
output(x,anrow,1);
banmul();
}

public static void main(String[] args) ...{
new BanMul();
}

}
发表于 @ 2007年12月06日 13:36:00|评论(loading...)|编辑