如何从区间获取对数分布 [英] How to get a logarithmic distribution from an interval
问题描述
我目前正在尝试将间隔切成不等宽的切片。实际上,我希望每个切片的宽度遵循对数规则。例如,第一个间隔应该大于第二个间隔,等等。
I am currently trying to cut an interval into not equal-width slices. In fact I want the width of each slice to follow a logarithmic rule. For instance the first interval is supposed to be bigger than the second one, etc.
我很难记住自己的数学讲座。所以假设我知道 a 和 b 分别是我区间 I 和 n 的上下边界的数量是多少:
如何找到每个切片的上下边界(遵循对数刻度)?
I have a hard time remembering my mathematics lectures. So assuming I know a and b which are respectively the lower and upper boundaries of my interval I, and n is the number of slices: how can I find the lower and upper boundaries of each slice (following a logarithmic scale)?
换句话说,这就是我已经完成了获得等宽间隔的操作:
In other word, here's what I have done to get equal-width interval:
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 = | ba | 。
Where: p-1= number of slices, and size_piece = |b-a|.
我现在想要得到的是 start 和 end 值,但是遵循对数刻度而不是算术刻度(这将在某些函数中调用)在 for 循环中)。
What I want to get now is start and end values, but following a logarithmic scale instead of an arithmetic scale (which are going to be called in some function in the for loop).
在此先感谢您的帮助。
推荐答案
如果我已理解您的问题,则此C ++程序将向您展示可以使用的算法的实际示例:
If I have understood your question, this C++ program will show you a practical example of the algorithm that can be used:
#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;
}
输出(切片的极值)为:
The output (the extremes of the slices) is:
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
这篇关于如何从区间获取对数分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!