zoukankan      html  css  js  c++  java
  • 最小二乘拟合

    本文对最小二乘拟合直线中出现的问题,当直线垂直时参数无法求出,使用ax+by+c=0或者p=xcos(a)+ysin(a)计算量会增加,针对这种情况,本算做了修正可以判断直线垂直情况。

    1. //最小二乘法直线拟合ay = kx + b  
    2. static int _rtCalculateLine(const RtPoint* src, const int num, float* k, float* b, int* a)  
    3. {  
    4.     if (NULL == src || num < 2 ) return -1;  
    5.   
    6.     int i = 0;  
    7.     long x = 0, y = 0, xx = 0, xy = 0, yy = 0, tem = 0;  
    8.   
    9.     while(i < num)  
    10.     {  
    11.         x += (src + i)->x;     
    12.         y += (src + i)->y;  
    13.         xx += (src + i)->x * (src + i)->x;  
    14.         xy += (src + i)->x * (src + i)->y;  
    15.         yy += (src + i)->y * (src + i)->y;  
    16.         i++;  
    17.     }  
    18.   
    19.     tem = xx*num - x*x;  
    20.     if(0 == tem)  
    21.     {  
    22.         tem = yy*num - y*y;  
    23.         if( 0 == tem) return -1;  
    24.           
    25.         *k = (num*xy - x*y)/tem;  
    26.         *b = -(x - (*k)*y)/num;  
    27.         *a = 0;  
    28.         return 0;  
    29.     }  
    30.   
    31.     *k = (num*xy - x*y)/tem;  
    32.     *b = (y - (*k)*x)/num;  
    33.     *a = 1;  
    34.       
    35.     return 0;  
    36. }
  • 相关阅读:
    bzoj 1176 cdq分治套树状数组
    Codeforces 669E cdq分治
    Codeforces 1101D 点分治
    Codeforces 1100E 拓扑排序
    Codeforces 1188D Make Equal DP
    Codeforces 1188A 构造
    Codeforces 1188B 式子转化
    Codeforces 1188C DP 鸽巢原理
    Codeforces 1179D 树形DP 斜率优化
    git commit -m "XX"报错 pre -commit hook failed (add --no-verify to bypass)问题
  • 原文地址:https://www.cnblogs.com/cl1024cl/p/6205277.html
Copyright © 2011-2022 走看看