当前位置:首页 » 编程语言 » c语言实现两个矩阵除法
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言实现两个矩阵除法

发布时间: 2023-01-15 19:39:36

c语言 用类编程实现两个矩阵的和、差、积,N×N,N可变 急!!!在线等

//原来写着玩儿的。还多一个求幂功能,我在devcpp 4.9.9.2 下编译运行通过测试。
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <vector>
#include <windows.h>
using namespace std ;

const int maxR = 100 , maxC = 100 ;
typedef double mat_element ;

struct Matr{ //Matrix
int n , m ; //number of row / col

mat_element e[maxR][maxC] ;

Matr (int _n = 1 , int _m = 1): n(_n) , m(_m) {} ;

Matr operator + (const Matr A) const{
assert(n == A.n && m == A.m) ;

Matr M(n , m) ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
M.e[i][j] = e[i][j] + A.e[i][j] ;
}
}
return M ;
}

Matr operator - (const Matr A) const{
assert(n == A.n && m == A.m) ;

Matr M(n , m) ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
M.e[i][j] = e[i][j] - A.e[i][j] ;
}
}
return M ;
}

Matr operator * (const Matr A) const{
assert(m == A.n) ;

Matr M(n , A.m) ;
memset(M.e , 0 , sizeof(M.e) ) ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < A.m ; j ++) {
for(int k = 0 ; k < m ; k++) {
M.e[i][j] += e[i][k] * A.e[k][j] ;
}
}
}
return M ;
}

Matr operator * (const mat_element k) const{
Matr M(n , m) ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
M.e[i][j] = e[i][j] * k ;
}
}
return M ;
}

Matr operator ^ (const int k) const{
assert(n == m) ;

Matr M(n , m) , A = *this ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < n ; j++) {
M.e[i][j] = (i == j) ;
}
}

for(int i = 1 ; i > 0 ; i <<= 1){
if(k & i) M = M * A ;
A = A * A ;
}
return M ;
}

void input() {
cout << "please input a " << n << "*" << m << " matrix" << endl ;
mat_element tmp ;
for(int i = 0 ; i < n ; i++) {
for(int j = 0 ; j < m ; j++) {
cin >> tmp ;
e[i][j] = tmp ;
}
}
cout << "matrix inputed !" << endl ;
}
void output(){
for(int i = 0 ; i < n ; i++){
for(int j = 0 ; j < m ; j++) {
cout << e[i][j] << '\t' ;
}
printf("\n") ;
}
printf("\n") ;
}
} ;

int main(){
Matr A(2 , 2) , B(2 , 2) ;
/*
A.e[0][0] = 1 ;
A.e[0][1] = 2 ;
A.e[1][0] = 4 ;
A.e[1][1] = 8 ;

B.e[0][0] = 16 ;
B.e[0][1] = 32 ;
B.e[1][0] = 64 ;
B.e[1][1] = 128 ;
*/
A.input() ;
B.input() ;
(A + B).output() ;
(A - B).output() ;
(A * B).output() ;
(A * 2).output() ;

system("pause") ;
return 0 ;
}

② C语言编程:编写一个函数求两个矩阵的差

intfunction(double**p,intp_r,intp_c,double**q,intq_r,intq_c,double**re){
inti,j;
if(p==NULL||q==NULL||p_r!=q_r||p_c!=q_c){
re=NULL;
return1;
}
if(p_r*p_c>1024){
re=NULL;
return2;
}
for(i=0;i<p_r;++i)
for(j=0;j<p_c;++j)
re[i][j]=p[i][j]-q[i][j];
return0;
}

③ C语言怎么实现矩阵左除

C语言怎么实现矩阵左除
在做矩阵的逆运算(也就是出除法运算)时,分母的左右取决余原乘式左右;
右除式A/B,相当于A*inv(B)即A右乘B的逆矩阵;
左除式A\B,相当于inv(A)*B即A的逆矩阵左乘B

④ 用c语言编写矩阵的加减乘除运算

#include <iostream>
#include <iomanip>
using namespace std;
template <typename T1,typename T2>
void inverse(T1*mat1,T2 *mat2,int a,int b);
template <typename T1,typename T2>
void multi(T1*mat1,T2*mat2,T2*result,int a,int b,int c);
template <typename T>
void output(T*mat,char *s,int a,int b);
int main(){
int middle[6][3],result[6][4];
int matrix1[3][6]={8,10,12,23,1,3,5,7,9,2,4,6,34,45,56,2,4,6};
int matrix2[3][4]={3,2,1,0,-1,-2,9,8,7,6,5,4};
char*s1="result";
char*s2="middle";
//inverse(matrix1,middle,6,3);
inverse<int[6],int[3]>(matrix1,middle,6,3);
//multi(middle,matrix2,result,6,3,4);
multi<int[3],int[4]>(middle,matrix2,result,6,3,4);
output(matrix1,"matrix1",3,6);
output(middle,s2,6,3);
output(matrix2,"matrix2",3,4);
output(result,s1,6,4);
return 0;
}
template <typename T1,typename T2>
void inverse(T1*mat1,T2*mat2,int a,int b){
int i,j;
for(i=0;i<b;i++)
for(j=0;j<a;j++)
mat2[j][i]=mat1[i][j];
return;

}
template <typename T1,typename T2>
void multi(T1*mat1,T2*mat2,T2*result,int a,int b,int c){
int i,j,k;
for(i=0;i<a;i++){
for(j=0;j<c;j++){
result[i][j]=0;
for(k=0;k<b;k++)
result[i][j]+=mat1[i][k]*mat2[k][j];
}
}
return;
}
template<typename T>
void output(T*mat,char*s,int a,int b){
int i,j;
cout<<s<<endl;
for(i=0;i<a;i++){
for(j=0;j<b;j++)
cout<<setw(6)<<mat[i][j];
cout<<endl;
}
return;
}

⑤ C语言求矩阵运算,急

可以用二维数组模拟矩阵,乘法就是A的第一行乘对应B的第一列放在(1,1)位置,然后继续往后乘。其实进行矩阵计算用MATLAB简单的多或者C++的Armadillo。