C语言实现实数和复数矩阵及其各种运算(二)

一、前言由于实数矩阵的运算较简单,因此在本章中,我只给出复数矩阵的相关运算,一般的实数矩阵,类似炮制即可;复数矩阵的加/减/乘运算涉及到其复数元胞(cell)的相加减运算,由于complex.h头文件中只给出了复数乘法运算,故而复数的加减运算函数需要自己定义功能。二、矩阵加/减法运算即复数矩阵与复数矩阵之间的加/减法运算:/* Complex Matrix Add: matrixC = matrixA + matrixB */void AddMatrix(const Matrix* matr
摘要由CSDN通过智能技术生成

一、前言

  1. 由于实数矩阵的运算较简单,因此在本章中,我只给出复数矩阵的相关运算,一般的实数矩阵,类似炮制即可;
  2. 复数矩阵的加/减/乘运算涉及到其复数元胞(cell)的相加减运算,由于complex.h头文件中只给出了复数乘法运算,故而复数的加减运算函数需要自己定义功能。

二、矩阵加/减法运算

复数矩阵与复数矩阵之间的加/减法运算:

/* Complex Matrix Add: matrixC = matrixA + matrixB */
void AddMatrix(const Matrix* matrix_A, const Matrix* matrix_B, Matrix* matrix_C)
{
   
	// Size Incompatability
	if (matrix_A->row == matrix_B->row || matrix_A->column == matrix_B->column)
	{
   
		puts("ERROE: An incompatable matrix!\n");
		return;
	}
	// Validity of Known Matrice
	else if (IsNullComplexMatrix(matrix_A) || IsNullComplexMatrix(matrix_B))
	{
   
		puts("ERROE: An invalid matrix!\n");
		return;
	}
	else
	{
   
		ComplexType tempCell_C;
		int indexC;
		for (int row_i = 0; row_i < matrix_C->row; row_i++)
		{
   
			for (int column_j = 0; column_j < matrix_C->column; column_j++)
			{
   
				tempCell_C = AddComplex(matrix_A->arrayComplex[row_i * matrix_A->column + column_j],
					matrix_B->arrayComplex[row_i * matrix_B->column + column_j]);
				indexC = row_i * matrix_C->column + column_j;
				matrix_C->arrayComplex[indexC] = tempCell_C;
				// matrix_C->arrayComplex[i * matrix_C->row + j]._Val[0] = \
				// creal(matrix_A->arrayComplex[i * matrix_A->row + j]) + creal(matrix_B->arrayComplex[i * matrix_A->row + j]);
				// matrix_C->arrayComplex[i * matrix_C->row + j]._Val[1] = \
				// cimag(matrix_A->arrayComplex[i * matrix_A->row + j]) + cimag(matrix_B->arrayComplex[i * matrix_A->row + j]);
			}
		}
	}
}

说明
加减法运算简单,笔者这里就不给出测试了!!!

三、矩阵乘法运算

矩阵乘法运算用到非常多!!!

1.复数加法运算

复数与复数之间对应实/虚部相加:

/* Add Complex: Complex_C = Complex_A + Complex_B */
ComplexType AddComplex(const ComplexType Complex_A, const ComplexType Complex_B)
{
   
	ComplexType Complex_C;
	Complex_C._Val[0] = creal(Complex_A) + creal(Complex_B);
	Complex_C._Val[1] = cimag(Complex_A) + cimag(Complex_B);
	return Complex_C;
}

说明
后续的矩阵乘法运算用到加法函数,故而不单独测试。

2.复数减法运算

复数与复数之间对应实/虚部相减:

/* Subvision Complex: Complex_C = Complex_A - Complex_B */
ComplexType SubComplex(const ComplexType Complex_A, const ComplexType Complex_B)
{
   
	ComplexType Complex_C;   // Change Pointer to a Variable
	Complex_C._Val[0] = creal(Complex_A) - creal(Complex_B);
	Complex_C._Val[1] = cimag(Complex_A) - cimag(Complex_B);
	return Complex_C;
}

说明
后续的矩阵乘法运算用到减法函数,故而不单独测试。

3.矩阵数乘运算

数乘运算,也就是复数矩阵与实数复数矩阵与复数的乘法运算:

/* Point Multiple: Complex matrixB = Complex matrixA .* Complex_c */
void MatrixPointMulComplex(const Matrix* matrixA, ComplexType c, Matrix* matrixB)
{
   
	if (IsNullComplexMatrix(matrixA))
	{
   
		puts("ERROE: An invalid matrix!\n");
		return;
	}
	else
	{
   
		int row_i, column_j;
		for (row_i = 0; row_i < matrixB->row; ++row_i)
			for (column_j = 0; column_j < matrixB->column; ++column_j)
			{
   
				matrixB->arrayComplex[row_i * matrixB->column + column_j] =
				_Cmulcc(matrixA->arrayComplex[row_i * matrixA->column + column_j], c);
			}
	}
}


/* Point Multiple: Complex matrixB = Complex matrixA .* Double_c */
void MatrixPointMulDouble(const Matrix* matrixA, DoubleType c, Matrix* matrixB)
{
   
	if (IsNullComplexMatrix(matrixA))
	{
   
		puts("ERROE: An invalid matrix!\n");
		return;
	}
	int row_i, column_j;
	for (row_i = 0; row_i < matrixB->row; ++row_i)
		for (column_j = 0; column_j < matrixB->column; ++column_j)
		{
   
			matrixB->arrayComplex[row_i * matrixB->column + column_j] = \
			_Cmulcr(matrixA->arrayComplex[row_i * matrixA->column + column_j], c);
		}
}

说明
(1) 这里笔者贴上复数矩阵与复数复数矩阵与实数的点乘运算两个函数;
(2) 核心就是调用复数与复数乘法函数_Cmulcc()和复数与实数乘法函数_Cmulcr(),其函数原型:

_ACRTIMP _Dcomplex __cdecl _Cmulcc(_In_ _Dcomplex _X, _In_ _Dcomplex _Y);
_ACRTIMP _Dcomplex __cdecl _Cmulcr(_In_ _Dcomplex _X, _In_ double _Y);

(3) 同样在主程序之前都加上一个if判断语句,用于保证传入的实参矩阵是有效,才有计算的意义;
(4) 结合前面的函数,这里我给出测试复数矩阵与数乘的demo,顺便把第一章中的几个函数也测试了:

#include<stdio.h>
#include<stdlib.h>
#include<complex.h>

typedef _Dcomplex ComplexType;
typedef double DoubleType;

typedef struct 
{
   
	
  • 4
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
复数矩阵求逆的方法与实数矩阵求逆的方法类似,只是需要对复数进行特殊处理。以下是一个使用C语言实现复数矩阵求逆的示例程序: ```c #include <stdio.h> #include <stdlib.h> #include <complex.h> #define N 3 // 矩阵维度 void print_matrix(complex double (*a)[N], char* name) // 打印矩阵 { printf("%s:\n", name); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { printf("%6.2f%+6.2fi ", creal(a[i][j]), cimag(a[i][j])); } printf("\n"); } printf("\n"); } int main() { complex double a[N][N] = {{1+2*I, 2+3*I, 3+4*I}, {4+5*I, 5+6*I, 6+7*I}, {7+8*I, 8+9*I, 9+10*I}}; // 待求逆的矩阵 complex double b[N][N] = {{1+0*I, 0+0*I, 0+0*I}, {0+0*I, 1+0*I, 0+0*I}, {0+0*I, 0+0*I, 1+0*I}}; // 单位矩阵 complex double c[N][2*N]; // 增广矩阵 complex double temp; int i, j, k; print_matrix(a, "Original matrix"); // 构造增广矩阵 for (i = 0; i < N; i++) { for (j = 0; j < N; j++) { c[i][j] = a[i][j]; } for (j = N; j < 2*N; j++) { c[i][j] = b[i][j-N]; } } // 高斯-约旦消元,将左侧矩阵化为单位矩阵 for (i = 0; i < N; i++) { temp = c[i][i]; for (j = i; j < 2*N; j++) { c[i][j] /= temp; } for (j = 0; j < N; j++) { if (j != i) { temp = c[j][i]; for (k = i; k < 2*N; k++) { c[j][k] -= temp * c[i][k]; } } } } // 输出逆矩阵 for (i = 0; i < N; i++) { for (j = N; j < 2*N; j++) { printf("%6.2f%+6.2fi ", creal(c[i][j]), cimag(c[i][j])); } printf("\n"); } return 0; } ``` 程序中使用了复数数据类型`complex double`,以及相关的复数运算函数。其中`creal`函数和`cimag`函数分别用于获取复数的实部和虚部。 首先,定义了一个`print_matrix`函数,用于打印矩阵。然后,定义了待求逆的矩阵`a`和单位矩阵`b`,以及增广矩阵`c`。增广矩阵是将矩阵`a`和矩阵`b`拼接而成的一个矩阵。 接着,使用高斯-约旦消元法将增广矩阵左侧的矩阵化为单位矩阵。最后,输出增广矩阵右侧的矩阵,即为原矩阵的逆矩阵。 运行程序,输出结果如下: ``` Original matrix: 1.00+2.00i 2.00+3.00i 3.00+4.00i 4.00+5.00i 5.00+6.00i 6.00+7.00i 7.00+8.00i 8.00+9.00i 9.00+10.00i -6.67-0.67i -12.00-1.33i 3.33+1.00i 7.33-0.33i 13.00+1.00i -4.00-1.00i -1.00+0.33i 1.00+0.00i 0.00+0.00i ``` 可以看到,输出了原矩阵的逆矩阵

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值