鑑於接觸到統計方面的問題還蠻多的,
又發現 excel 真的很好用 (考慮學 VBA 中...),
所以花了點時間,先將 excel 中的 數學類型 的函數寫出一些函式出來
在此聲明,數學類型像是 sin, cosh, atan 等等函數其實在 math.h 裡面都有了
所以不再特別包出成函數

比較值得注意的,大概是最大公因數和最小公倍數的求法
如果不會輾轉相除法,請先找相關文獻閱讀

對於程式碼有問題者,歡迎進行回覆提問
若需寫好的 .cpp 與 .h ,請回覆告知我
並留下您的 mail ,方便我與您聯絡
當然,請別留作業文還是要作業..

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

#ifndef EXCEL_MATH

#ifndef PI
#define PI (double)(3.14159265358979323846264338327950288419716939937510)
#endif

#ifndef ELUE
#define ELUE (double( 2.7182818284590452353602874)
#endif

// 最大公因數
unsigned GCD(unsigned a,
             unsigned b);

// 最小公倍數
unsigned LCM(unsigned a,
             unsigned b);

// 傳回餘數
unsigned MOD(unsigned a,
             unsigned b);


// 傳回平方和
double SUMSQ(double *a,
             unsigned n);

// 傳回兩陣列之相對數值之平方差總和
double SUMX2MY2(double *a,
                double *b,
                unsigned n);


// 傳回兩陣列中各元素平方和之總和
double SUMX2PY2(double *a,
                unsigned n1,
                double *b,
                unsigned n2);

// 傳回兩陣列中對應數值差之平方和
double SUMXMY2(double *a,
               double *b,
               unsigned n);

// 取陣列總合
double SUM(double *a,
           unsigned n);

// 取陣列平均值
double AVERAGE(double *a,
               unsigned n);

// 取陣列最大值
double MAX(double *a,
           unsigned n);

// 取陣列最小值
double MIN(double *a,
           unsigned n);

#define EXCEL_MATH
#endif

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

#include "excel_math.h"

// ==============================
// 最大公因數

/*
function gcd(a, b) {
    define r as integer;
    if (b == 0)
    return a;
    else {
        r = a mod b;
        return gcd(b, r);
    }
}

  function gcd(a, b) {
      define r as integer;
      while b ≠ 0 {
      r := a mod b;
      a := b;
      b := r;
      }
      return a;
  }
*/

unsigned GCD(unsigned a,
             unsigned b)
{   
    if(a==0 || b==0) return 0;
    unsigned r = 0;
    while(b!=0){
        r = a % b;
        a = b;
        b = r;
    }
    return a;
}

// ==============================
// 最小公倍數
unsigned LCM(unsigned a,
             unsigned b)
{
    if(a==0 || b==0) return 0;
    return ((a*b)/GCD(a, b));
}

// ==============================
// 傳回餘數
unsigned MOD(unsigned a,
             unsigned b)
{
    return (a%b);
}

// ==============================
// 傳回平方和
double SUMSQ(double *a,
             unsigned n)
/*
Σ(a^2)
*/
{
    double ret = 0.0;
    for(unsigned i=0; i<n; i++) ret+= (a[i]*a[i]);
    return ret;
}

// ==============================            
// 傳回兩陣列之相對數值之平方差總和
/*
Σ(a^2-b^2)
*/
double SUMX2MY2(double *a,
                double *b,
                unsigned n)
{
    double ret = 0.0;
    for(unsigned i=0; i<n; i++) ret = ret + (a[i]*a[i]-b[i]*b[i]);
    return ret;
}

// ==============================            
// 傳回兩陣列中各元素平方和之總和
/*
Σa^2 + Σb^2
*/
double SUMX2PY2(double *a,
                unsigned n1,
                double *b,
                unsigned n2)
{
    return ( SUMSQ(a, n1) + SUMSQ(b, n2));
}

// ==============================            
// 傳回兩陣列中對應數值差之平方和
/*
Σ(a-b)^2
*/
double SUMXMY2(double *a,
               double *b,
               unsigned n)
{
    double ret = 0.0;
    for(unsigned i=0; i<n; i++) ret = ret + (a[i]-b[i])*(a[i]-b[i]);
    return ret;
}

// ==============================   
// 取陣列總合
double SUM(double *a,
           unsigned n)
{
    double ret = 0.0;
    for(unsigned i=0; i<n; i++){
        ret += a[i];
    }
    return ret;
}

// ==============================            
// 取陣列平均值
double AVERAGE(double *a,
               unsigned n)
{
    return (SUM(a, n)/(double)(n));
}

// ==============================            
// 取陣列最大值
double MAX(double *a,
           unsigned n)
{
    double ret = a[0];
    for(unsigned i=0; i<n; i++){
        if(a[i]> ret) ret = a[i];
    }
    return ret;
}

// ==============================            
// 取陣列最小值
double MIN(double *a,
           unsigned n)
{
   
    double ret = a[0];
    for(unsigned i=0; i<n; i++){
        if(a[i] < ret) ret = a[i];
    }
    return ret;
}

arrow
arrow
    全站熱搜

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