上一篇主要在介紹基本的列運算
以後的 .h 都不再特別聲明,
同時之前所建構的函數也將一再調用
所以,如果此篇的函數沒有印象,
請再往前翻閱其它文章..

這次介紹的比較簡單,主要是行列式的加、減、乘,
以及取得轉置矩陣,若對於內容有問題,
歡迎回覆詢問

函數說明:
1.MTRANSPOSE   取得轉置矩陣
2.MADD             矩陣相加
3.MSUB             矩陣相減
4.MMULT           矩陣相乘
5.SHOWMATRIX    顯示矩陣內容
6.SHOWMATRIX2   顯示矩陣內容

// =======================================
// author  : edison.shih.
// filename: matrix.h
// date    : 2010/2/7
// mail    : forget72@hotmail.com
// ** all copyright reverve **
// =======================================

#ifndef __MATRIX
#define __MATRIX

// =================================

// 矩陣轉置
void MTRANSPOSE(double **a,
                unsigned m,
                unsigned n,
                double **result);

// 二維陣列相加
void MADD(double **a,
          double **b,
          unsigned m,
          unsigned n,
          double **result);


// 二維陣列減
void MSUB(double **a,
          double **b,
          unsigned m,
          unsigned n,
          double **result);

// 傳回二維陣列乘積
void MMULT(double **a,
           double **b,
           unsigned m,
           unsigned n,
           unsigned p,
           double **result);

// 顯示二維陣列
void SHOWMATRIX(double **a,
                unsigned m,
                unsigned n);

// 顯示二維陣列
void SHOWMATRIX2(double **a,
                 unsigned m,
                 unsigned n);
#endif

// =======================================
// author  : edison.shih.
// filename: matrix.cpp
// date    : 2010/2/7
// mail    : forget72@hotmail.com
// ** all copyright reverve **
// =======================================

#include "matrix.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

//////////////////////////////////////////////////////////////
// ==============================
// 矩陣轉置
void MTRANSPOSE(double **a,
                unsigned m,
                unsigned n,
                double **result)
{
    for(unsigned i=0; i<n;i ++){
        for(unsigned j=0; j<m; j++){
            result[i][j] = a[j][i];
        }
    }
}


// ==============================
// 二維陣列相加
void MADD(double **a,
          double **b,
          unsigned m,
          unsigned n,
          double **result)
{
    for(unsigned i=0; i<m; i++)
    {
        for(unsigned j=0; j<n; j++)
        {
            result[i][j] = a[i][j] + b[i][j];
        }
    }
}

// ==============================
// 二維陣列相減
void MSUB(double **a,
          double **b,
          unsigned m,
          unsigned n,
          double **result)
{
    for(unsigned i=0; i<m; i++)
    {
        for(unsigned j=0; j<n; j++)
        {
            result[i][j] = a[i][j] - b[i][j];
        }
    }
}

// ==============================
// 傳回二陣列乘積
/*
cij = Σ(r=1~n) | Air*Brj
*/
void MMULT(double **a,
           double **b,
           unsigned m,
           unsigned n,
           unsigned p,
           double **result)
{
    for(unsigned i=0; i<m; i++)
    {
        for(unsigned j=0; j<p; j++)
        {
            result[i][j]=0;
            for(unsigned k=0;k<n;k++) result[i][j]+=a[i][k]*b[k][j]; 
        }
    }
}

// ==============================
// 顯示二維陣列

void SHOWMATRIX(double **a,
                unsigned m,
                unsigned n)
{
    for(unsigned i=0; i<m; i++){
        for(unsigned j=0; j<n; j++){
            printf("%5.2lf ", a[i][j]);
        }
        printf("\n");
    }
}


// 顯示二維陣列
void SHOWMATRIX2(double **a,
                 unsigned m,
                 unsigned n)
{
    for(unsigned i=0; i<m; i++){
        for(unsigned j=0; j<n; j++){
            printf("%8.4lf ", a[i][j]);
        }
        printf("\n");
    }
}
// ==============================

創作者介紹
創作者 Edison 的頭像
Edison

藍影

Edison 發表在 痞客邦 留言(0) 人氣()