【问题标题】:How to get a logarithmic distribution from an interval如何从区间获得对数分布
【发布时间】:2016-06-05 18:49:24
【问题描述】:

我目前正在尝试将间隔切割成不等宽的切片。事实上,我希望每个切片的宽度遵循对数规则。例如,第一个间隔应该比第二个大,等等。

我很难记住我的数学课。所以假设我知道 ab 分别是我的区间 In 的上下边界是切片数: 如何找到每个切片的上下边界(按照对数刻度)?

换句话说,这是我为获得等宽间隔所做的:

for (i = 1; i< p; i++) {
    start = lower + i -1 + ((i-1) * size_piece);

    if (i == p-1 ) {
      end = upper;
    } else {  
      end = start + size_piece;
    }
    //function(start, end)
  }

其中:p-1= 切片数,size_piece = |b-a|

我现在想要得到的是 startend 值,但遵循对数刻度而不是算术刻度(将在for 循环)。

提前感谢您的帮助。

【问题讨论】:

  • 不太清楚你要做什么

标签: scale distribution intervals logarithm


【解决方案1】:

如果我理解了你的问题,这个 C++ 程序将向你展示一个可以使用的算法的实际示例:

#include <iostream>
#include <cmath>

void my_function( double a, double b ) {
    // print out the lower and upper bounds of the slice
    std::cout << a << " -- " << b << '\n';
}

int main() {

    double start = 0.0, end = 1.0;
    int n_slices = 7;

    // I want to create 7 slices in a segment of length = end - start
    // whose extremes are logarithmically distributed:
    //     |         1       |     2    |   3  |  4 | 5 |6 |7|
    //     +-----------------+----------+------+----+---+--+-+
    //   start                                              end

    double scale = (end - start) / log(1.0 + n_slices);
    double lower_bound = start;
    for ( int i = 0; i < n_slices; ++i ) {
        // transform to the interval (1,n_slices+1):
        //     1                 2          3      4    5   6  7 8
        //     +-----------------+----------+------+----+---+--+-+
        //   start                                              end

        double upper_bound = start + log(2.0 + i) * scale;

        // use the extremes in your function
        my_function(lower_bound,upper_bound);

        // update
        lower_bound = upper_bound;
    }

    return 0;
}

输出(切片的极端)是:

0 -- 0.333333
0.333333 -- 0.528321
0.528321 -- 0.666667
0.666667 -- 0.773976
0.773976 -- 0.861654
0.861654 -- 0.935785
0.935785 -- 1

【讨论】:

  • 谢谢!这是我无法理解的。
猜你喜欢
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
  • 2015-06-05
  • 2017-01-18
  • 2019-04-28
  • 1970-01-01
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多