⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cmatrix.c

📁 c 编写的矩阵运算包括加减乘
💻 C
字号:
#include "stdio.h"
#include "stdlib.h"
#include "math.h"
#include <malloc.h>
#include "cmatrix.h"


//----------------------------------------------------------------//
//name: initMatrix (int row, int line)
//function: initialize a Matrix
//input :  row,line
//output : the pointer of Matrix
//----------------------------------------------------------------//

sqMatrix initMatrix( int row, int line )
{
         int i;
         sqMatrix m = (sqMatrix  )malloc(sizeof(matrix));
         if(! m ) exit(OVERFLOW);
         m->aline=line;
         m->arow=row;
         m->length=row*line;
         for (i=0; i<m->length; i++){
             //get x,y and stored in spot
             m->elem[i].n.x=i/line+1; 
             m->elem[i].n.y=i%line+1;
             //printf("%d,%d,%d\n",i,m->elem[i].n.x,m->elem[i].n.y);
             }//for
         return m;
}//initMatrix

//------------------DONE-------------------------------------//
status va(sqMatrix m,int x,int y,ElemType value){
       //evaluate m(x,y)
       int t,line;
       line=m->aline;
       t=(x-1)*line+y-1;//printf("t=%d,length=%d\n",t,m->length);
       if( t> m->length-1 ) return SPECIAL;
       m->elem[t].num=value;
       //printf("elem=%f,value=%f\n",m->elem[t].num,value);
       return TRUE;
}//va
//-----------------------------------------------------------//
status ott_x(int length,int row,int line){
       return length/line+1;
       }//ott
//-----------------------------------------------------------//
status ott_y(int length,int row,int line){
       return length%line+1;
       }//ott
//-----------------------------------------------------------//
status tto(int x,int y,int line){
       return (x-1)*line+y-1;
       }//tto
//-----------------------------------------------------------//
sqMatrix transpose(sqMatrix n){
         int i,j;
         ElemType t;
         sqMatrix m;
         
         m=initMatrix(n->aline,n->arow);//initialize a matrix
         //printf("aroW = %d",n->arow);
         //transpose matrix below
         for( i=1 ; i <= n->arow  ; i++)
              {//printf("\n");
                 for( j=1 ; j<=n->aline ; j++){
                         t=n->elem[tto(i,j,n->aline)].num;
                         va(m,j,i,t);
                         //printf(" %f ",m->elem[tto(i,j,n->arow)].num);
                         }//for
              }//for
         return m;
         }//transpose
//---------------------------------------------------------------------//
status inputMatrix(sqMatrix m){
       //input a Matrix
       int i,j;
       ElemType t;
       printf("\nhere you will input the matrix ,please input as the matrix like by lines:\n");
       for(i=1 ; i <= m->arow ; i++){
               printf("this is %d line:\n",i);
               for(j=1 ; j<= m->aline ; j++){
                   scanf("%lf",&t);
                   //printf("-------------test---------------\n");
                   //printf("t=%f\n",t);
                   va(m,i,j,t);
                   //printf("m(%d,%d)=%lf\n",i,j,m->elem[tto(i,j,m->aline)].num);
                   //printf("-------------testover------------\n");
                   //printf("  ");
                   }//for
       }//for
       return TRUE;
       }//inputMatrix
//---------------------------------------------------------------------//
status outputMatrix(sqMatrix m){
       //output a matrix
       int i ,j;
       ElemType temp;
       printf("\n");
       for(i=1 ; i<= m->arow ; i++){
               printf("line %d:",i);
               for(j=1 ; j<=m->aline ;j++){
               temp=m->elem[tto(i,j,m->aline)].num;
               printf(" %lf ",temp);}
               printf("\n");
               }//for
       return TRUE;
       }//outputMatrix
//---------------------------------------------------------------------//
sqMatrix copyMatrix(sqMatrix m){
         //copy a matrix m to n
         int i,j;ElemType tem;
         sqMatrix n;
         n=initMatrix(m->arow,m->aline);
         for(i=1; i <= m->arow ;i++)
                  for(j=1;j<= m->aline ; j++)
                    {tem = v(m,i,j);va(n,i,j,tem);}
         return n;
         }//copyMatrix
//---------------------------------------------------------------------//
ElemType v(sqMatrix m,int x,int y){
         return m->elem[tto(x,y,m->aline)].num;
         }//v
//---------------------------------------------------------------------//
/*
  Name: status det(sqMatrix detv)
  Copyright: WakefieldQ.sun
  Author: SunGuoyu
  Date: 22-12-08 17:40
  Description: used for count a determinant.
*/
//---------------------------------------------------------------------//
ElemType det(sqMatrix detv)
{
 int i,j,m,n,s,t,k=1,N;
 ElemType f=1.0,c,x,sn,t1,t2;
 N=detv->arow;
 for (i=1,j=1;i<=N&&j<=N;i++,j++)
 {
  if (v(detv,i,j)==0)
  {
   for (m=i;v(detv,m,j)==0;m++);
   if (m==N+1)
   {
    sn=0;
    //printf("value is =%f\n",sn);
    return sn;
   }
   else
    for (n=j;n<=N;n++)
    {
     c=v(detv,i,n);
     va(detv,i,n,v(detv,m,n));
     va(detv,m,n,c);
    }//if (m==N)
    k*=(-1);
  }//if (v(detv,i,j)==0)
  for (s=N;s>i;s--)
  {
   x=v(detv,s,j);//printf("%lf,j=%d\n",x,j);
   for (t=j;t<=N;t++){
    t1=x/v(detv,i,j);
    //printf("t1=");
    //printf(" %lf  ",t1);
    t2=v(detv,i,t)*t1;
    //printf("t2 = %lf\n",t2);
    va(detv,s,t,v(detv,s,t)-t2);}
  }
 }//outputMatrix(detv);
 for (i=1;i<=N;i++)
  f=f*v(detv,i,i);
 sn=k*f;
 return sn;
}
//---------------------------------------------------------------------//
//name:ElemType adjMatrix_elem(sqMatrix m,int x,int y)
//time:22-12-08 18:31
//function:element of adjoin matrix
//---------------------------------------------------------------------//

ElemType adjMatrix_elem(sqMatrix m,int x,int y){
         sqMatrix temp;
         ElemType back;
         int i,j,N,k=1,l=1;
         N=m->arow;
         temp=initMatrix(N-1,N-1);
         for(i=1 ; i<= N ;i++){
                 if(i==x) continue;
                 //else
                 l=1;
                 for(j=1 ; j<= N ; j++){
                         if(j==y) continue;
                         va(temp,k,l,v(m,i,j));
                         l++;}//for(j=1 ; j<= N ; j++)
                 k++;
                 }
         //outputMatrix(temp);
         back = det(temp);
         //printf("x=%d,y=%d,back=%lf\n",x,y,back);
         return back;
         }//adjMatrix
//---------------------------------------------------------------------//
/*
  Name: sqMatrix adjMatrix(sqMatrix m)
  Copyright: WakefieldQ.SUN!
  Author: SunGuoyu
  Date: 22-12-08 21:25
  Description: used to build a adjoin matrix
*/

sqMatrix adjMatrix(sqMatrix m){
         int i,j,N,k=-1;
         sqMatrix n;
         N=m->arow;
         n=initMatrix(N,N);
         for(i=1;i<=N;i++)
            for(j=1;j<=N;j++){
             k*=(-1);
             va(n,i,j,k*adjMatrix_elem(m,i,j));
             }//for
         //outputMatrix(n);
             //printf("k=%d,value=%lf",k,adjMatrix_elem(m,i,j));
         return transpose(n);
         }//adjMatrix
                         
//---------------------------------------------------------------------//
sqMatrix multiNMatrix(sqMatrix m,ElemType t){
       int i,j,N,M;
	   sqMatrix n;
       N=m->arow;
       M=m->aline;
       //printf("-----------------test------M-----------------\n");
       //outputMatrix(m);
	   n=copyMatrix(m);//temp=copyMatrix(m);
	   //printf("-----------------test-------N----------------\n");
	   //printf("N=%d,M=%d",N,M);
	   //outputMatrix(n);
	   //printf("-----------------test------M-----------------\n");
	   //outputMatrix(m);
       for(i=1 ; i <= N ; i++)
               for( j=1 ; j <= M ; j++)
                  va(n,i,j,v(n,i,j)*t);//printf("v=%lf,t=%lf,value is %lf\n",v(n,i,j),t,v(n,i,j)*t);}
       return n;
       }//multiMatrix
//---------------------------------------------------------------------//
/*
  Name: inv(sqMatrix m)
  Copyright: WakefieldQ.SUN!
  Author: SunGuoyu
  Date: 22-12-08 22:00
  Description: used for matrix inverson
*/

sqMatrix inv(sqMatrix m){
         int N;
         ElemType det1;
         sqMatrix n;
         if(m->arow == m->aline)
            N = m->arow;
         else
            {printf("ERROR : ROW AND LINE IS NOT THE SAME!\n");
             exit(0);
             }
         n=initMatrix(N,N);
         n=adjMatrix(m);
         det1 = det(m);
         det1 = 1.0/det1;
         multiNMatrix(n,det1);
         return n;
         }//inv

//---------------------------------------------------------------------//
/*
  Name: sqMatrix multiMatrix(sqMatrix m,sqMatrix n)
  Copyright: WakefieldQ.SUN!
  Author: SunGuoyu
  Date: 22-12-08 22:29
  Description: used for two matrix multipilated.
*/

sqMatrix multiMatrix(sqMatrix m,sqMatrix n){
         int i,j,N,M,k=1;
         ElemType tem=0;
         sqMatrix r;
         if(m->aline == n->arow && m->arow == n-> aline){
                  N=m->arow; M=n->aline;
                  r=initMatrix(N,M);
                  for(i=1 ; i<= m->arow ; i++)
                      for(j=1 ; j<= m->aline ; j++)
                        {tem+=v(m,i,j)*v(n,j,i);
                         if( j == m->aline ){
                            va(r,i,k,tem);
                            k++;
                            tem=0.0;
                            }//if
                         }//for(j=1 ; j<= m->aline ; j++)
            }//if
         return r;
         }//multiMatrix        
  
//---------------------------------------------------------------------//       
status RMatrix(sqMatrix r,double v,double w,double k){
       double t=cos(v)*cos(k)-sin(v)*sin(w)*sin(k);
       va(r,1,1,cos(v)*cos(k)-sin(v)*sin(w)*sin(k));
       va(r,1,2,-cos(v)*sin(k)-sin(v)*sin(w)*sin(k));
       va(r,1,3,-sin(v)*cos(w));
       va(r,2,1,cos(w)*sin(k));
       va(r,2,2,cos(w)*cos(k));
       va(r,2,3,-sin(w));
       va(r,3,1,sin(v)*cos(k)+cos(v)*sin(w)*sin(k));
       va(r,3,2,-sin(v)*sin(k)+cos(v)*sin(w)*cos(k));
       va(r,3,3,cos(v)*cos(w));
       return TRUE;
       }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -