1个回答
2013-06-07
展开全部
private static TripleSMatrix addTripleMatrix(TripleSMatrix t1,
TripleSMatrix t2) {
if (t1.getRows() != t2.getRows() || t1.getColumns() != t2.getColumns()) {
System.out.println("这两个矩阵不能相加 。");
return null;
}
TripleSMatrix sum = new TripleSMatrix();int count = 1;
int i = 1, j = 1, v;
sum.setRows(t1.getRows());
sum.setColumns(t1.getColumns());// 执行矩阵相加,按行的顺序相加,分为四种情况
while (i <= t1.getNonzeroElements() && j <= t2.getNonzeroElements()) {
if ((t1.triple[i][0] < t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] < t2.triple[j][1])) {sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2];
i++;
count++;
} else if ((t1.triple[i][0] > t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] > t2.triple[j][1])) {
sum.triple[count][0] = t2.triple[j][0];
sum.triple[count][1] = t2.triple[j][1];
sum.triple[count][2] = t2.triple[j][2];
j++;
count++;
}
// 当前结点对应且两元素之和不为零
else if ((t1.triple[i][2] + t2.triple[j][2]) != 0) {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2] + t2.triple[j][2];
count++;
i++;
j++;
} else {// 当前结点对应且两元素之和为零,此种情况在三元组中将不再存储
i++;
j++;
}
}
while (i <= t1.getNonzeroElements()) {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2];
i++;
count++;
}while (j <= t2.getNonzeroElements()) {
sum.triple[count][0] = t2.triple[j][0];
sum.triple[count][1] = t2.triple[j][1];
sum.triple[count][2] = t2.triple[j][2];
j++;
count++;
}sum.setNonzeroElements(count - 1);
sum.sortTriple(sum.triple, 1, count);// 对输入的三元组排序
return sum;
}// 计算两个求稀疏矩阵的和 ,结果用数组表示。和为零的值不用单独做判断了这次
private static int[][] addMatrix(TripleSMatrix t1, TripleSMatrix t2) {
if (t1.getRows() != t2.getRows() || t1.getColumns() != t2.getColumns()) {
System.out.println("这两个矩阵不能相加 。");
return null;
}
int[][] c = new int[t1.getRows()][t2.getColumns()];
int i = 1, j = 1;
// 执行矩阵相加,按行的顺序相加,分为四种情况
while (i <= t1.getNonzeroElements() && j <= t2.getNonzeroElements()) {
if ((t1.triple[i][0] < t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] < t2.triple[j][1])) {c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2];
i++;
} else if ((t1.triple[i][0] > t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] > t2.triple[j][1])) {c[t2.triple[j][0] - 1][t2.triple[j][1] - 1] = t2.triple[j][2];
j++;
} else {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2]
+ t2.triple[j][2];
i++;
j++;
}
}// 将矩阵t1 的剩余元素插入矩阵c
while (i <= t1.getNonzeroElements()) {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2];
i++;
}
// 将矩阵t2 的剩余元素插入矩阵c
while (j <= t2.getNonzeroElements()) {
c[t2.triple[j][0] - 1][t2.triple[j][1] - 1] = t2.triple[i][2];
j++;
}
return c;
}
}
TripleSMatrix t2) {
if (t1.getRows() != t2.getRows() || t1.getColumns() != t2.getColumns()) {
System.out.println("这两个矩阵不能相加 。");
return null;
}
TripleSMatrix sum = new TripleSMatrix();int count = 1;
int i = 1, j = 1, v;
sum.setRows(t1.getRows());
sum.setColumns(t1.getColumns());// 执行矩阵相加,按行的顺序相加,分为四种情况
while (i <= t1.getNonzeroElements() && j <= t2.getNonzeroElements()) {
if ((t1.triple[i][0] < t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] < t2.triple[j][1])) {sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2];
i++;
count++;
} else if ((t1.triple[i][0] > t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] > t2.triple[j][1])) {
sum.triple[count][0] = t2.triple[j][0];
sum.triple[count][1] = t2.triple[j][1];
sum.triple[count][2] = t2.triple[j][2];
j++;
count++;
}
// 当前结点对应且两元素之和不为零
else if ((t1.triple[i][2] + t2.triple[j][2]) != 0) {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2] + t2.triple[j][2];
count++;
i++;
j++;
} else {// 当前结点对应且两元素之和为零,此种情况在三元组中将不再存储
i++;
j++;
}
}
while (i <= t1.getNonzeroElements()) {
sum.triple[count][0] = t1.triple[i][0];
sum.triple[count][1] = t1.triple[i][1];
sum.triple[count][2] = t1.triple[i][2];
i++;
count++;
}while (j <= t2.getNonzeroElements()) {
sum.triple[count][0] = t2.triple[j][0];
sum.triple[count][1] = t2.triple[j][1];
sum.triple[count][2] = t2.triple[j][2];
j++;
count++;
}sum.setNonzeroElements(count - 1);
sum.sortTriple(sum.triple, 1, count);// 对输入的三元组排序
return sum;
}// 计算两个求稀疏矩阵的和 ,结果用数组表示。和为零的值不用单独做判断了这次
private static int[][] addMatrix(TripleSMatrix t1, TripleSMatrix t2) {
if (t1.getRows() != t2.getRows() || t1.getColumns() != t2.getColumns()) {
System.out.println("这两个矩阵不能相加 。");
return null;
}
int[][] c = new int[t1.getRows()][t2.getColumns()];
int i = 1, j = 1;
// 执行矩阵相加,按行的顺序相加,分为四种情况
while (i <= t1.getNonzeroElements() && j <= t2.getNonzeroElements()) {
if ((t1.triple[i][0] < t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] < t2.triple[j][1])) {c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2];
i++;
} else if ((t1.triple[i][0] > t2.triple[j][0])
|| (t1.triple[i][0] == t2.triple[j][0] && t1.triple[i][1] > t2.triple[j][1])) {c[t2.triple[j][0] - 1][t2.triple[j][1] - 1] = t2.triple[j][2];
j++;
} else {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2]
+ t2.triple[j][2];
i++;
j++;
}
}// 将矩阵t1 的剩余元素插入矩阵c
while (i <= t1.getNonzeroElements()) {
c[t1.triple[i][0] - 1][t1.triple[i][1] - 1] = t1.triple[i][2];
i++;
}
// 将矩阵t2 的剩余元素插入矩阵c
while (j <= t2.getNonzeroElements()) {
c[t2.triple[j][0] - 1][t2.triple[j][1] - 1] = t2.triple[i][2];
j++;
}
return c;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询