📄 matrix.c
字号:
/**
* File : Matrix.c
* Author : Wind
* Email : zealotwjr@163.com
* Date : 2005-9-30
* Purpose :
* */
#include <stdlib.h>
#include <stdio.h>
#include "../head/Global.h"
#include "../head/Matrix.h"
/**
* @describ Initial Matrix
* @return Status indicating whether matrix initializtion is successful.
* If success return SUCCESS,otherwise return FAIL
* */
Status initialMatrix(Matrix * m,int row,int col){
if(!m){
MessageLine("Error:Encounter NULL Matrix at -- initMatrix(Matrix * m,int row,int col).");
return FAIL;
}
m->row=row;
m->col=col;
m->array=(double*) malloc (sizeof(double) * row * col);
if(!m->array){
return FAIL;
}
return SUCCESS;
}
double * M(Matrix * m ,int row ,int col){
if(m&&m->array){
return &(m->array[row*m->col + col]);
}else{
MessageLine("Error:Encounter NULL Matrix or NULL Matrix array at -- M(Matrix * m,int row,int col).");
return NULL;
}
}
void printMatrix(Matrix * m){
int i=0;
int j=0;
for (i = 0; i < m->row; ++i) {
for (j = 0; j < m->col; ++j) {
printf("%6.3lf ",*M(m,i,j));
}
printf("\n");
}
}
/**
* @describ destroy the matrix->array
* @param Matrix * m
* @return STATUS
* */
Status destroyMatrix(Matrix * m){
if(!m){
return FAIL;
}
free(m->array);
m->array=NULL;
m->row=0;
m->col=0;
return SUCCESS;
}
/**
* @describ Matrix multiply
* @param Matrix * a
* @param Matrix * b
* @param Matrix * c
* @return STATUS
* */
Status matrixMultiply(Matrix * a,Matrix * b ,Matrix * c){
if(!a||!b||!c){
MessageLine("Error encountered at -- matrixMultiply (Matrix * a,Matrix * b ,Matrix * c).");
MessageLine("Error = !a||!b||!c");
}
if(a->col != b->row){
MessageLine("Error encountered at -- matrixMultiply (Matrix * a,Matrix * b ,Matrix * c).");
MessageLine("Error = a->col != b->row ");
return FAIL;
}
if(!initialMatrix(c,a->row,b->col)){
MessageLine("Error encountered at -- matrixMultiply (Matrix * a,Matrix * b ,Matrix * c).");
MessageLine("Error = can't init Matrix *c .");
return FAIL;
}
int i=0;
int j=0;
int k=0;
for (i = 0; i < a->row; ++i) {
for (j = 0; j < b->col; ++j) {
for ( k = 0; k < a->col; ++k) {
double * temp= M(c,i,j);
*temp += *M(a,i,k) * (*M(b,k,j));
}
}
}
return SUCCESS;
}
Status matrixTranspose(Matrix * m){
int i=0;
int j=0;
double temp=0.0;
if(!m){
return FAIL;
}
for(i=0;i<m->row;i++){
for(j=i+1;j<m->col;j++){
temp=*M(m,i,j);
*M(m,i,j)=*M(m,j,i);
*M(m,j,i)=temp;
}
}
return SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -