- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个使用“odeint”模拟种群动态的程序。我想设置一个 if 条件来禁止我的颂歌的结果为负。这是我的代码摘要:
class Communities
{
public :
typedef boost::array< double , 22 > state_type;
Communities(...);
~Communities();
void operator()(state_type &x , state_type &dxdt , double t);
void operator()(state_type &x , const double t );
void integration(par::Params parParam);
private:
...
};
void Communities :: operator ()( state_type &x , state_type &dxdt , double t )
{
for (int i=0; i<nb ; ++i)
{
dxdt[i] = ...
}
for (int j=0; j<g ; ++j)
{
dxdt[j] += ...
}
for (int k=0; k<nb+g ; ++k)
{
if (x[k] <0)
{
x[k] = 0;
}
}
}
...
void Communities :: integration(par::Params parParam)
{
...
integrate_adaptive(make_controlled( 1E-12 , 1E-12 , runge_kutta_fehlberg78< state_type >()) , boost::ref(*this), B , 0.0 , 10.0 , 0.1 , boost::ref(*this));
}
int main(int argc, const char * argv[])
{
...
Com1.integration(ParamText);
return 0;
}
正如所写,下面的循环是无用的:
for (int k=0; k<nb+g ; ++k)
{
if (x[k] <0)
{
x[k] = 0;
}
}
你对这个程序有足够的了解吗?您知道我如何让它发挥作用吗?
谢谢!
integrate_adaptive的代码
template< class Stepper , class System , class State , class Time , class Observer >
size_t integrate_adaptive(
Stepper stepper , System system , State &start_state ,
Time &start_time , Time end_time , Time &dt ,
Observer observer , controlled_stepper_tag
)
{
typename odeint::unwrap_reference< Observer >::type &obs = observer;
typename odeint::unwrap_reference< Stepper >::type &st = stepper;
const size_t max_attempts = 1000;
const char *error_string = "Integrate adaptive : Maximal number of iterations reached. A step size could not be found.";
size_t count = 0;
while( less_with_sign( start_time , end_time , dt ) )
{
obs( start_state , start_time );
if( less_with_sign( end_time , static_cast<Time>(start_time + dt) , dt ) )
{
dt = end_time - start_time;
}
size_t trials = 0;
controlled_step_result res = success;
do
{
res = st.try_step( system , start_state , start_time , dt );
++trials;
}
while( ( res == fail ) && ( trials < max_attempts ) );
if( trials == max_attempts ) BOOST_THROW_EXCEPTION( std::overflow_error( error_string ) );
++count;
}
obs( start_state , start_time );
return count;
}
最佳答案
是的,这个循环是无用的,因为它与求解 ODE 无关。 ODE 是 dx/dt = f(x,t)
并且通过计算 f(x)
和更新 x
以数值方式求解 ODE数值方法。你的循环打破了这个算法。详细地说,odeint 假定 x
是一个输入参数。
您需要的是一个特殊的集成例程。您可以查看 integrate_adaptive
的实现并在那里介绍您的外观。 integrate_adaptive
的代码基本是
template< typename Stepper , typename System , typename State , typename Time , typename Obs >
void integrate_adaptive( Stepper stepper , System system , State &x , Time &start_time , Time end_time , Time dt , Obs obs )
{
const size_t max_attempts = 1000;
size_t count = 0;
while( ( start_time + dt ) < end_time )
{
obs( start_state , start_time );
if( ( start_time + dt ) > end_time )
{
dt = end_time - start_time;
}
size_t trials = 0;
controlled_step_result res = success;
do
{
res = st.try_step( system , start_state , start_time , dt );
++trials;
}
while( ( res == fail ) && ( trials < max_attempts ) );
if( trials == max_attempts ) throw std::overflow_error( "error" );
}
obs( start_state , start_time );
}
您可以在最大尝试次数条件之后直接引入循环。
关于c++ - odeint 禁止负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26269359/
我有一个带有标志属性的枚举,我用它来表示权限。我用它来比较 if (CurrentPermissions & Permission1 == Permission1) 等... [FlagsAttrib
我在使用具有两个不同日期的 TIMEDIFF 时遇到问题。以下查询“应该”返回00:04:51 mysql> SELECT TIMEDIFF(TIME('2013-07-21 00:04:50'),T
我有一个页面抛出 JavaScript 异常: Unhandled exception at line 5144, column 13 in raphael.js0x80048270 - JavaSc
我有一个大整数,比如说 BigInteger a=Biginteger.valueOf(50); 除此之外 BigInteger a=(BigInteger.ZERO).subtract(BigInt
我正在使用 CoreLocation 框架获取我的速度和距离来计算平均速度。 在 CoreLocation 发出的第一个更新中,它显示了速度和行进距离的负值。我该如何解决这个问题? 速度是 locat
我有一个数据框“df”: x y 0 1 -1 1 -2 -3 2 3 4 3 4 5 4 9 6 我正在尝试确定 x 和 y 值的百分比是正数还是负数。所
引用自:http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html 'd' '\u0054' Formats the a
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Negative ASCII value int main() { char b = 8-'3';
发现了一个令人费解的指标——事件消息计数为负值。我已确认 ServiceBus Explorer (v3.0.4) 和 Azure 门户报告相同的负值。 怎么会发生这种事? 我为 SB 队列启用了以下
我正在尝试编译一个很大程度上依赖于 libcurl 和 pcap 的自定义包,在我的机器上它工作得很好,但是当我尝试使用工具链编译它时,我收到此错误: $ /home/kavastudios/site
我正在开发一个桌面软件,它向用户每次执行主要操作收费。例如,每个 PDF 打印将向用户收取 0.1 美元的费用。 我的软件提供多线程。 . 所以,如果它运行单线程,它就可以正常工作:) 但问题是如果用
我有一个用户模型和一个工作场所模型。用户有一个字段性别(男/女),每个工作场所有很多用户。我想要选择工作场所的用户总数以及按工作场所名称分组的工作场所的女性用户总数。 这是我尝试过的。 User.se
我正在尝试在 D3 中创建一个复制 this design 的条形图.这个想法是值的范围可以从 -100 到 100 并且彼此并排显示。比例必须保持在 0-100,并使用颜色来指示数字是高于还是低于
我在使用 gcc4.4 的 Ubuntu 10.04 中遇到同样的问题,相同的代码有效使用 gcc4.1 在 RH 5.5 上很好 #include #include int main(int a
size_t 被声明为 unsigned int 所以它不能表示负值。 所以有 ssize_t 这是signed 类型的 size_t 对吗? 这是我的问题: #include #include
我正在尝试确定 x 列对于这些列中的值是否具有相同的方向(正或负)或者它们是否具有不同的方向(例如,一个为正,另一个为负)。 我目前正在使用 with确定列中的值是否为 > 0 , 0 & coun
我的 Firebird 过程采用了几个具有 bigint 值的参数。 当我从 uint64 类型的 go 程序参数调用此过程时,值大于 max int32/2 存储为负数。 如何将 bigint/ui
我正在考虑是否可以消除编译器警告。警告来自将 uint32 与 -1 进行比较。 现在只看一眼,这似乎是一件不明智的事情,因为 uint32 永远不应该为负,但我没有编写这段代码,也不熟悉 c++ 的
我有以下数据框(由负数和正数组成): df.head() Out[39]: Prices 0 -445.0 1 -2058.0 2 -954.0 3 -520.0 4 -73
我正在尝试使用库WPF Metro UI Charts,它派生自Modern UI Charts。但是,当我尝试在 Page 而不是 Window 中使用图表时,我遇到了 ClusteredColum
我是一名优秀的程序员,十分优秀!