GSL:错误报告

发布于 2024-11-01 23:35:41 字数 2075 浏览 2 评论 0原文

我想使用 GSL 进行集成 http://www.gnu.org/software/gsl/manual /html_node/Numerical-Integration.html 但是,我没有找到如何集成函数的便捷方法

(示例中的函数 f http://www.gnu.org/software/gsl/manual/html_node/Numerical-integration-examples.html

可以向集成器报告错误。我想集成一个函数,该函数本身是由可能失败的集成产生的。这是我的示例程序

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>

double f (double x, void * params) {
    GSL_ERROR("test error",GSL_FAILURE);
    return 0.0;
}



int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);

double result, error;

gsl_function F;
F.function = &f;

gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                     w, &result, &error); 

printf ("status          = %d\n", status);
status  = GSL_FAILURE;
printf ("status          = %d\n", status);


gsl_integration_workspace_free (w);

return 0;
}

的输出 状态=0 status = -1

我认为集成商应该停止并返回我的错误代码。我怎样才能实现这个目标?

非常感谢您的帮助!!!

2011-04-27:我也尝试过这个变体,在Brian Gough告诉我之后,

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>

double f (double x, void * params) {
    GSL_ERROR("test error",GSL_FAILURE);
    return GSL_NAN;
}



int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);

double result, error;

gsl_function F;
F.function = &f;

gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                     w, &result, &error); 

printf ("status          = %d\n", status);
status  = GSL_FAILURE;
printf ("status          = %d\n", status);


gsl_integration_workspace_free (w);

return 0;
}

它也没有帮助。我现在将填写一份错误报告。

I want to use the GSL for integration
http://www.gnu.org/software/gsl/manual/html_node/Numerical-Integration.html
However, I find no convenient way how the integrated function

(the function f in the example http://www.gnu.org/software/gsl/manual/html_node/Numerical-integration-examples.html)

can report an error to the integrator. I want to integrate a function which itself results from an integration that could fail. This is my sample program

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>

double f (double x, void * params) {
    GSL_ERROR("test error",GSL_FAILURE);
    return 0.0;
}



int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);

double result, error;

gsl_function F;
F.function = &f;

gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                     w, &result, &error); 

printf ("status          = %d\n", status);
status  = GSL_FAILURE;
printf ("status          = %d\n", status);


gsl_integration_workspace_free (w);

return 0;
}

resulting in the output
status = 0
status = -1

I think the integrator should rather stop and return my error code. How can I achieve this?

Thank you very much for your help!!!

2011-04-27: I also tried this variant, after Brian Gough told me,

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>

double f (double x, void * params) {
    GSL_ERROR("test error",GSL_FAILURE);
    return GSL_NAN;
}



int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);

double result, error;

gsl_function F;
F.function = &f;

gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                     w, &result, &error); 

printf ("status          = %d\n", status);
status  = GSL_FAILURE;
printf ("status          = %d\n", status);


gsl_integration_workspace_free (w);

return 0;
}

it did not help either. I will now fill out a bug report.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

睫毛上残留的泪 2024-11-08 23:35:41

感谢 GSL 邮件列表中的 Xubin Wu,问题得到了解决:

嗨,

GSL_ERROR 本身是一个宏,看起来

      gsl_error (reason, __FILE__, __LINE__, gsl_errno);
      return gsl_errno;

函数在返回 NAN 之前就已经返回了,因为 GSL_ERROR
已被调用。关闭处理程序只需让第一行执行
没有什么。默认错误处理程序在打印后中止程序
错误信息。

我不认为这是一个错误。也许你可以编写自己的错误处理程序
来解决你的问题。例如,可以使用“goto”跳出
gsl_integration_qags,或设置一些全局变量来指示
积分结果不正确。

PS:我相信这个宏就是您所需要的,

宏:GSL_ERROR_VAL(原因,gsl_errno,值)
该宏与 GSL_ERROR 相同,但返回用户定义的值
值而不是错误代码。它可以用于数学
返回浮点值的函数。

以下示例显示如何在数学上返回 NaN
使用 GSL_ERROR_VAL 宏的奇点,

 if (x == 0)
   {
     GSL_ERROR_VAL("argument lies on singularity",
                   GSL_ERANGE, GSL_NAN);
   }

所以我根据调整了代码

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>

double f (double x, void * params) {
//  return GSL_NAN;
    GSL_ERROR_VAL ("argument lies on singularity", GSL_ERANGE, GSL_NAN);
}



int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);

double result, error;

gsl_function F;
F.function = &f;

gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                     w, &result, &error); 

printf ("status          = %d\n", status);
status  = GSL_FAILURE;
printf ("status          = %d\n", status);


gsl_integration_workspace_free (w);

return 0;
}

,一切都按预期工作......

Thanks to Xuebin Wu from the GSL Mailing list the problem is solved:

Hi,

GSL_ERROR itself is a macro, it looks like

      gsl_error (reason, __FILE__, __LINE__, gsl_errno);
      return gsl_errno;

The function already returns before you return NAN, because GSL_ERROR
has been called. Turning the handler off just let the first line do
nothing. The default error handler abort the program after printing
error message.

I do not think it is a bug. Maybe you can write your own error handler
to solve your problem. For example, you can use "goto" to jump out of
gsl_integration_qags, or set some global variable to indicate the
integration result is incorrect.

PS: I believe this macro is what you need,

Macro: GSL_ERROR_VAL (reason, gsl_errno, value)
This macro is the same as GSL_ERROR but returns a user-defined value
of value instead of an error code. It can be used for mathematical
functions that return a floating point value.

The following example shows how to return a NaN at a mathematical
singularity using the GSL_ERROR_VAL macro,

 if (x == 0)
   {
     GSL_ERROR_VAL("argument lies on singularity",
                   GSL_ERANGE, GSL_NAN);
   }

So I adjusted the code according to

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_errno.h>

double f (double x, void * params) {
//  return GSL_NAN;
    GSL_ERROR_VAL ("argument lies on singularity", GSL_ERANGE, GSL_NAN);
}



int main (void)
{
gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);

double result, error;

gsl_function F;
F.function = &f;

gsl_set_error_handler_off();
int status = gsl_integration_qags (&F, 0, 1, 0, 1e-7, 1000,
                     w, &result, &error); 

printf ("status          = %d\n", status);
status  = GSL_FAILURE;
printf ("status          = %d\n", status);


gsl_integration_workspace_free (w);

return 0;
}

and everything works as expected...

何以畏孤独 2024-11-08 23:35:41

有点黑客,但我可能会让你的函数存储一些标志。当遇到错误时,它会设置标志并为所有后续评估返回零。然后,在集成之后,您可以检查此标志以查看结果是否有效。

A bit hackish, but I'd probably have your function store some flag. When it encounters an error it sets the flag and returns zero for all subsequent evaluations. Then, after you've integrated it you can check this flag to see if the result is valid.

听闻余生 2024-11-08 23:35:41

为函数编写一个包装器,返回指向包含函数结果和错误状态的结构体的指针,怎么样?或者,如果您使用 C++,则可以使用对象来进行这种封装......

What about to write a wrapper for the function which returns pointer to a structure, containing function results and error status ? Or if you use c++, this encapsulation can be made with use of objects ....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文