MyMath.cs 24.1 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace Misc
{
    public class MyMath
    {


        /// <summary>
        /// 获取x 的下一个 以interval为倍数的 数
        /// </summary>
        /// <param name="x"></param>
        /// <param name="interval"></param>
        /// <returns></returns>
        public static int ROUNDUP2(int x, int interval)
        {
            int i = x % interval;
            x += (interval - i);
            return x;
        }
        /// <summary>
        /// 解方程 ax²+bx+c=0
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <param name="x1"></param>
        /// <param name="x2"></param>
        /// <returns></returns>
        public static bool SolveAX2_BX_C(double a, double b, double c, out double x1, out double x2)
        {
            //ax²+bx+c=0(a≠0)
            //x=[-b±√(b²-4ac)]/2a

            if (a == 0)
            {
                if (b != 0)
                {
                    x1 = x2 = -c / b;
                    return true;
                }
                else
                {
                    x1 = x2 = 0;
                    return false;
                }
            }
            else
            {
                double q2 = b * b - 4 * a * c;
                if (q2 < 0)
                {
                    x1 = x2 = 0;
                    return false;
                }
                else
                {
                    x1 = (-b + Math.Sqrt(q2)) / 2 / a;
                    x2 = (-b - Math.Sqrt(q2)) / 2 / a;
                    return true;
                }
            }
        }
        /// <summary>
        /// 排除 NULL_VALUE, 求最大值
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static int Max(IEnumerable<int> buf)
        {
            return Max(buf, 0, buf.Count() - 1);
        }
75 76 77 78
        public static double Max(IEnumerable<double> buf)
        {
            return Max(buf, 0, buf.Count() - 1);
        }
潘栩锋's avatar
潘栩锋 committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
        /// <summary>
        /// 排除 NULL_VALUE, 求最大值
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="first">开始序号</param>
        /// <param name="last">结束序号</param>
        /// <returns></returns>
        public static int Max(IEnumerable<int> buf, int first, int last)
        {
            int max = MyBase.NULL_VALUE;
            for (int i = first; i <= last; i++)
            {
                if (MyBase.ISVALIDATA(buf.ElementAt(i)))
                {
                    if (max == MyBase.NULL_VALUE)
                        max = buf.ElementAt(i);
                    else if (max < buf.ElementAt(i))
                        max = buf.ElementAt(i);
                }
            }
            return max;
        }
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
        public static double Max(IEnumerable<double> buf, int first, int last)
        {
            double max = double.NaN;
            for (int i = first; i <= last; i++)
            {
                if (!double.IsNaN(buf.ElementAt(i)))
                {
                    if (max == double.NaN)
                        max = buf.ElementAt(i);
                    else if (max < buf.ElementAt(i))
                        max = buf.ElementAt(i);
                }
            }
            return max;
        }
潘栩锋's avatar
潘栩锋 committed
116 117 118 119 120 121 122 123 124
        /// <summary>
        /// 排除 NULL_VALUE, 求最小值
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static int Min(IEnumerable<int> buf)
        {
            return Min(buf, 0, buf.Count() - 1);
        }
125 126 127 128 129 130 131 132 133 134
        public static double Min(IEnumerable<double> buf)
        {
            return Min(buf, 0, buf.Count() - 1);
        }        /// <summary>
                 /// 排除 NULL_VALUE, 求最小值
                 /// </summary>
                 /// <param name="buf"></param>
                 /// <param name="first">开始序号</param>
                 /// <param name="last">结束序号</param>
                 /// <returns></returns>
潘栩锋's avatar
潘栩锋 committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        public static int Min(IEnumerable<int> buf, int first, int last)
        {
            int min = MyBase.NULL_VALUE;

            for (int i = first; i <= last; i++)
            {
                if (MyBase.ISVALIDATA(buf.ElementAt(i)))
                {
                    if (min == MyBase.NULL_VALUE)
                        min = buf.ElementAt(i);
                    else if (min > buf.ElementAt(i))
                        min = buf.ElementAt(i);
                }
            }
            return min;
        }
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
        public static double Min(IEnumerable<double> buf, int first, int last)
        {
            double min = double.NaN;

            for (int i = first; i <= last; i++)
            {
                if (!double.IsNaN(buf.ElementAt(i)))
                {
                    if (min == double.NaN)
                        min = buf.ElementAt(i);
                    else if (min > buf.ElementAt(i))
                        min = buf.ElementAt(i);
                }
            }
            return min;
        }
潘栩锋's avatar
潘栩锋 committed
167 168 169 170 171 172 173 174 175
        /// <summary>
        /// 排除 NULL_VALUE, 求平均值
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static int Avg(IEnumerable<int> buf)
        {
            return Avg(buf, 0, buf.Count() - 1);
        }
176 177 178 179
        public static double Avg(IEnumerable<double> buf)
        {
            return Avg(buf, 0, buf.Count() - 1);
        }
潘栩锋's avatar
潘栩锋 committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        /// <summary>
        /// 排除 NULL_VALUE, 求平均值
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="first">开始序号</param>
        /// <param name="last">结束序号</param>
        /// <returns></returns>
        public static int Avg(IEnumerable<int> buf, int first, int last)
        {
            int sum = 0;
            int cnt = 0;

            for (int i = first; i <= last; i++)
            {
                if ((i >= 0) && (i < buf.Count()))
                {
                    if (MyBase.ISVALIDATA(buf.ElementAt(i)))
                    {
                        sum += buf.ElementAt(i);
                        cnt++;
                    }
                }
            }
            if (cnt != 0)
                return sum / cnt;
            else
                return MyBase.NULL_VALUE;
        }
208 209 210 211
        public static double Avg(IEnumerable<double> buf, int first, int last)
        {
            double sum = 0;
            int cnt = 0;
潘栩锋's avatar
潘栩锋 committed
212

213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
            for (int i = first; i <= last; i++)
            {
                if ((i >= 0) && (i < buf.Count()))
                {
                    if (!double.IsNaN(buf.ElementAt(i)))
                    {
                        sum += buf.ElementAt(i);
                        cnt++;
                    }
                }
            }
            if (cnt != 0)
                return sum / cnt;
            else
                return double.NaN;
        }
潘栩锋's avatar
潘栩锋 committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
        /// <summary>
        /// 排除 NULL_VALUE, 求平均值, 无方向 last &lt; first 也可以
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="first">开始序号</param>
        /// <param name="last">结束序号</param>
        /// <returns></returns>
        public static int AvgNoDirection(IEnumerable<int> buf, int first, int last)
        {
            int temp;
            if (first > last)
            {
                temp = first;
                first = last;
                last = temp;
            }
            return Avg(buf, first, last);
        }

        /// <summary>
        /// 排除 NULL_VALUE, 求Sigma
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="first">开始序号</param>
        /// <param name="last">结束序号</param>
        /// <returns></returns>
        public static int Sigma(IEnumerable<int> buf, int first, int last)
        {
            int avg = Avg(buf, first, last);
            if (!MyBase.ISVALIDATA(avg))
                return MyBase.NULL_VALUE;

            double sum = 0;
            int cnt = 0;

            for (int i = first; i <= last; i++)
            {
                if (MyBase.ISVALIDATA(buf.ElementAt(i)))
                {
                    sum += (buf.ElementAt(i) - avg) * (buf.ElementAt(i) - avg);
                    cnt++;
                }
            }
            if (cnt >= 3)
                return (int)(Math.Sqrt(sum / (cnt - 1)));
            else
                return 0;
        }
277 278 279 280 281 282 283 284 285
        /// <summary>
        /// 排除 NULL_VALUE, 求Sigma
        /// </summary>
        /// <param name="buf"></param>
        /// <param name="first">开始序号</param>
        /// <param name="last">结束序号</param>
        /// <returns></returns>
        public static double Sigma(IEnumerable<double> buf, int first, int last)
        {
286
            
287 288 289 290 291 292
            double avg = Avg(buf, first, last);
            if (double.IsNaN(avg))
                return double.NaN;

            double sum = 0;
            int cnt = 0;
潘栩锋's avatar
潘栩锋 committed
293

294 295
            for (int i = first; i <= last; i++)
            {
296
                if ((i >= 0) && (i < buf.Count()))
297
                {
298 299 300 301 302
                    if (!double.IsNaN(buf.ElementAt(i)))
                    {
                        sum += (buf.ElementAt(i) - avg) * (buf.ElementAt(i) - avg);
                        cnt++;
                    }
303 304 305 306 307 308 309
                }
            }
            if (cnt >= 3)
                return (Math.Sqrt(sum / (cnt - 1)));
            else
                return 0;
        }
潘栩锋's avatar
潘栩锋 committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
        /// <summary>
        /// 排除 NULL_VALUE, 求Sigma
        /// </summary>
        /// <param name="buf"></param>
        /// <returns></returns>
        public static int Sigma(IEnumerable<int> buf)
        {
            return Sigma(buf, 0, buf.Count() - 1);
        }
        static double Relevency(IEnumerable<int> x, IEnumerable<int> y)
        {
            return Relevency(x, 0, y, 0, x.Count());
        }
        static double Relevency(IEnumerable<int> x, int x_offset, IEnumerable<int> y, int y_offset, int len)
        {
            double sum_x = 0;
            double sum_y = 0;
            double sum_x2 = 0;
            double sum_y2 = 0;
            double sum_xy = 0;
            int cnt = 0;
            ////避免溢出
            //int max_x = Max(x, x_offset, x_offset + len - 1);
            //if (max_x > 30000)
            //{ 

            //}

            for (int i = 0; i < len; i++)
            {
                int x_i = i + x_offset;
                int y_i = i + y_offset;

                if (MyBase.ISVALIDATA(x.ElementAt(x_i)) && MyBase.ISVALIDATA(y.ElementAt(y_i)))
                {
                    sum_x += x.ElementAt(x_i);
                    sum_y += y.ElementAt(y_i);
                    sum_x2 += (double)x.ElementAt(x_i) * x.ElementAt(x_i);
                    sum_y2 += (double)y.ElementAt(y_i) * y.ElementAt(y_i);
                    sum_xy += (double)x.ElementAt(x_i) * y.ElementAt(y_i);
                    cnt++;
                }
            }
            if (cnt > 0)
            {
                double s1 = cnt * sum_xy - sum_x * sum_y;
                double s2 = Math.Sqrt(cnt * sum_x2 - sum_x * sum_x);
                double s3 = Math.Sqrt(cnt * sum_y2 - sum_y * sum_y);
                double s4 = s1 / s2 / s3;
                return s4;
            }
            else
                return 0;
        }
364 365 366 367 368 369 370 371 372 373 374


        /// <summary>
        /// 求相关性
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public static double Correl(IEnumerable<double> x, IEnumerable<double> y)
        {
            return Correl(x, 0, y, 0, x.Count());
        }
潘栩锋's avatar
潘栩锋 committed
375

376 377 378 379
        /// <summary>
        /// 求相关性
        /// </summary>
        /// <param name="x"></param>
潘栩锋's avatar
潘栩锋 committed
380
        /// <param name="x_offset"></param>
381
        /// <param name="y"></param>
潘栩锋's avatar
潘栩锋 committed
382 383
        /// <param name="y_offset"></param>
        /// <param name="len"></param>
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
        /// <returns></returns>
        public static double Correl(IEnumerable<double> x, int x_offset, IEnumerable<double> y, int y_offset, int len)
        {
            double sum_x = 0;
            double sum_y = 0;
            double sum_x2 = 0;
            double sum_y2 = 0;
            double sum_xy = 0;
            int cnt = 0;
            ////避免溢出
            //int max_x = Max(x, x_offset, x_offset + len - 1);
            //if (max_x > 30000)
            //{ 

            //}

            for (int i = 0; i < len; i++)
            {
                int x_i = i + x_offset;
                int y_i = i + y_offset;
                double xv = x.ElementAt(x_i);
                double yv = y.ElementAt(y_i);
                if ((!double.IsNaN(xv)) && (!double.IsNaN(yv)))
                {
                    sum_x += xv;
                    sum_y += yv;
                    sum_x2 += xv * xv;
                    sum_y2 += yv * yv;
                    sum_xy += xv * yv;
                    cnt++;
                }
            }
            if (cnt > 0)
            {
                double s1 = cnt * sum_xy - sum_x * sum_y;
                double s2 = Math.Sqrt(cnt * sum_x2 - sum_x * sum_x);
                double s3 = Math.Sqrt(cnt * sum_y2 - sum_y * sum_y);
                double s4 = s1 / s2 / s3;
                return s4;
            }
            else
                return 0;
        }
潘栩锋's avatar
潘栩锋 committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
        /// <summary>
        /// 求相关性
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <returns></returns>
        public static double Correl(IEnumerable<int> x, IEnumerable<int> y)
        {
            return Relevency(x, y);
        }
        /// <summary>
        /// 求相关性
        /// </summary>
        /// <param name="x"></param>
        /// <param name="x_offset"></param>
        /// <param name="y"></param>
        /// <param name="y_offset"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        public static double Correl(IEnumerable<int> x, int x_offset, IEnumerable<int> y, int y_offset, int len)
        {
            return Relevency(x, x_offset, y, y_offset, len);
        }
        /// <summary>
        /// 求2次拟合 a,b
        /// </summary>
        /// <param name="y"></param>
        /// <param name="x"></param>
        /// <param name="a"></param>
        /// <param name="b"></param>
        public static void Linest(IEnumerable<int> y, IEnumerable<int> x, out double a, out double b)
        {
            if (y.Count() != x.Count())
            {
                throw new Exception("Linest() 参数异常 (y.Length=" + y.Count() + ") != (x.Length=" + x.Count() + ")");
            }

            double sum_x = 0;
            double sum_y = 0;
            double sum_x2 = 0;
            double sum_xy = 0;
            int n = y.Count();
            int cnt = 0;
            for (int i = 0; i < n; i++)
            {
                if (MyBase.ISVALIDATA(x.ElementAt(i)) && MyBase.ISVALIDATA(y.ElementAt(i)))
                {
                    sum_xy += y.ElementAt(i) * x.ElementAt(i);
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
                    sum_x2 += x.ElementAt(i) * x.ElementAt(i);
                    sum_x += x.ElementAt(i);
                    sum_y += y.ElementAt(i);
                    cnt++;
                }
            }
            if (cnt > 1)
            {
                a = (n * sum_xy - sum_x * sum_y) / (n * sum_x2 - sum_x * sum_x);
                b = sum_y / n - a * sum_x / n;
            }
            else
            {
                a = 1;
                b = 0;
            }
        }

        public static void Linest(IEnumerable<double> y, IEnumerable<double> x, out double a, out double b)
        {
            if (y.Count() != x.Count())
            {
                throw new Exception("Linest() 参数异常 (y.Length=" + y.Count() + ") != (x.Length=" + x.Count() + ")");
            }

            double sum_x = 0;
            double sum_y = 0;
            double sum_x2 = 0;
            double sum_xy = 0;
            int n = y.Count();
            int cnt = 0;
            for (int i = 0; i < n; i++)
            {
                if (!double.IsNaN(x.ElementAt(i)) && !double.IsNaN(y.ElementAt(i)))
                {
                    sum_xy += y.ElementAt(i) * x.ElementAt(i);
潘栩锋's avatar
潘栩锋 committed
511 512 513 514 515 516 517 518
                    sum_x2 += x.ElementAt(i) * x.ElementAt(i);
                    sum_x += x.ElementAt(i);
                    sum_y += y.ElementAt(i);
                    cnt++;
                }
            }
            if (cnt > 1)
            {
519 520
                a = (cnt * sum_xy - sum_x * sum_y) / (cnt * sum_x2 - sum_x * sum_x);
                b = sum_y / cnt - a * sum_x / cnt;
潘栩锋's avatar
潘栩锋 committed
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
            }
            else
            {
                a = 1;
                b = 0;
            }
        }


        /// <summary>
        /// 对环形数据 分区移位
        /// </summary>
        /// <param name="data">原始数据</param>
        /// <param name="map">key=old_idx, value=new_idx</param>
        /// <returns></returns>
        public static int[] Map(IEnumerable<int> data, Dictionary<int, int> map)
        {
            if (map == null || map.Count() == 0)
                return data.ToArray();
            int[] data_new = new int[data.Count()];
            if (map.Count() == 1)
            {
                //只是平移而已
                int old_idx0 = map.Keys.ElementAt(0);
                int new_idx0 = map[old_idx0];

                for (int i = 0; i < data.Count(); i++)
                {
                    int new_idx = i;
                    int old_idx = (old_idx0 - new_idx0) + new_idx;
                    if (old_idx >= data.Count())
                        old_idx -= data.Count();
                    else if (old_idx < 0)
                        old_idx += data.Count();
                    data_new[new_idx] = data.ElementAt(old_idx);
                }
                return data_new;
            }


            for (int i = 0; i < map.Count(); i++)
            {
                int old_idx0 = map.Keys.ElementAt(i);
                int new_idx0 = map[old_idx0];


                int i_next = i + 1;
                if (i_next >= map.Count())
                    i_next = 0;

                int old_idx1 = map.Keys.ElementAt(i_next);
                int new_idx1 = map[old_idx1];

                int cnt = new_idx1 - new_idx0;
                if (cnt < 0)
                    cnt += data.Count();

                int cnt_old = old_idx1 - old_idx0;
                if (cnt_old < 0)
                    cnt_old += data.Count();

                double w = 1.0 * cnt_old / cnt;
                for (int j = 0; j < cnt; j++)
                {
                    int new_idx = j + new_idx0;
                    if (new_idx >= data.Count())
                        new_idx -= data.Count();

                    double old_idx = j * w + old_idx0;
                    int o1 = (int)Math.Ceiling(old_idx);
                    int o0 = (int)old_idx;

                    if (o0 == o1)
                    {
                        int old_idx_0 = o0;
                        if (old_idx_0 >= data.Count())
                            old_idx_0 -= data.Count();
                        data_new[new_idx] = data.ElementAt(old_idx_0);
                    }
                    else
                    {
                        int old_idx_0 = o0;
                        if (old_idx_0 >= data.Count())
                            old_idx_0 -= data.Count();

                        int old_idx_1 = o1;
                        if (old_idx_1 >= data.Count())
                            old_idx_1 -= data.Count();

                        if (MyBase.ISVALIDATA(data.ElementAt(old_idx_1)) && MyBase.ISVALIDATA(data.ElementAt(old_idx_0)))
                        {
                            data_new[new_idx] = (int)(data.ElementAt(old_idx_1) * (old_idx - o0) + data.ElementAt(old_idx_0) * (o1 - old_idx));
                        }
                        else if (MyBase.ISVALIDATA(data.ElementAt(old_idx_0)))
                        {
                            data_new[new_idx] = data.ElementAt(old_idx_0);
                        }
                        else if (MyBase.ISVALIDATA(data.ElementAt(old_idx_1)))
                        {
                            data_new[new_idx] = data.ElementAt(old_idx_1);
                        }
                        else
                        {
                            data_new[new_idx] = MyBase.NULL_VALUE;
                        }

                    }
                }
            }
            return data_new;
        }
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713


        /// <summary>
        /// 对环形数据 分区移位
        /// </summary>
        /// <param name="data">原始数据</param>
        /// <param name="map">key=old_idx, value=new_idx</param>
        /// <returns></returns>
        public static double[] Map(IEnumerable<double> data, Dictionary<int, int> map)
        {
            if (map == null || map.Count() == 0)
                return data.ToArray();
            double[] data_new = new double[data.Count()];
            if (map.Count() == 1)
            {
                //只是平移而已
                int old_idx0 = map.Keys.ElementAt(0);
                int new_idx0 = map[old_idx0];

                for (int i = 0; i < data.Count(); i++)
                {
                    int new_idx = i;
                    int old_idx = (old_idx0 - new_idx0) + new_idx;
                    if (old_idx >= data.Count())
                        old_idx -= data.Count();
                    else if (old_idx < 0)
                        old_idx += data.Count();
                    data_new[new_idx] = data.ElementAt(old_idx);
                }
                return data_new;
            }


            for (int i = 0; i < map.Count(); i++)
            {
                int old_idx0 = map.Keys.ElementAt(i);
                int new_idx0 = map[old_idx0];


                int i_next = i + 1;
                if (i_next >= map.Count())
                    i_next = 0;

                int old_idx1 = map.Keys.ElementAt(i_next);
                int new_idx1 = map[old_idx1];

                int cnt = new_idx1 - new_idx0;
                if (cnt < 0)
                    cnt += data.Count();

                int cnt_old = old_idx1 - old_idx0;
                if (cnt_old < 0)
                    cnt_old += data.Count();

                double w = 1.0 * cnt_old / cnt;
                for (int j = 0; j < cnt; j++)
                {
                    int new_idx = j + new_idx0;
                    if (new_idx >= data.Count())
                        new_idx -= data.Count();

                    double old_idx = j * w + old_idx0;
                    int o1 = (int)Math.Ceiling(old_idx);
                    int o0 = (int)old_idx;

                    if (o0 == o1)
                    {
                        int old_idx_0 = o0;
                        if (old_idx_0 >= data.Count())
                            old_idx_0 -= data.Count();
                        data_new[new_idx] = data.ElementAt(old_idx_0);
                    }
                    else
                    {
                        int old_idx_0 = o0;
                        if (old_idx_0 >= data.Count())
                            old_idx_0 -= data.Count();

                        int old_idx_1 = o1;
                        if (old_idx_1 >= data.Count())
                            old_idx_1 -= data.Count();

714
                        if (!double.IsNaN(data.ElementAt(old_idx_1)) && !double.IsNaN(data.ElementAt(old_idx_0)))
715 716 717
                        {
                            data_new[new_idx] = (data.ElementAt(old_idx_1) * (old_idx - o0) + data.ElementAt(old_idx_0) * (o1 - old_idx));
                        }
718
                        else if (!double.IsNaN(data.ElementAt(old_idx_0)))
719 720 721
                        {
                            data_new[new_idx] = data.ElementAt(old_idx_0);
                        }
722
                        else if (!double.IsNaN(data.ElementAt(old_idx_1)))
723 724 725 726 727 728 729 730 731 732 733 734 735
                        {
                            data_new[new_idx] = data.ElementAt(old_idx_1);
                        }
                        else
                        {
                            data_new[new_idx] = double.NaN;
                        }

                    }
                }
            }
            return data_new;
        }
潘栩锋's avatar
潘栩锋 committed
736 737
    }
}