Dlib库【10】——计算积分,读取xml显示

本文演示了使用Dlib库进行数值积分的方法,并通过几个具体函数实例展示了积分过程及结果。此外,还提供了一个简单的XML文件解析示例,介绍了如何读取XML文件并打印其内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


配置Dlib环境:  链接


1.计算积分


// The contents of this file are in the public domain.  See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

This example demonstrates the usage of the numerical quadrature function
integrate_function_adapt_simp().  This function takes as input a single variable
function, the endpoints of a domain over which the function will be integrated, and a
tolerance parameter.  It outputs an approximation of the integral of this function over
the specified domain.  The algorithm is based on the adaptive Simpson method outlined in:

Numerical Integration method based on the adaptive Simpson method in
Gander, W. and W. Gautschi, "Adaptive Quadrature – Revisited,"
BIT, Vol. 40, 2000, pp. 84-101

*/

#include <iostream>
#include <dlib/matrix.h>
#include <dlib/numeric_constants.h>
#include <dlib/numerical_integration.h>

using namespace std;
using namespace dlib;

// Here we the set of functions that we wish to integrate and comment in the domain of
// integration.

// x in [0,1]
double gg1(double x)
{
	return pow(e, x);
}

// x in [0,1]
double gg2(double x)
{
	return x*x;
}

// x in [0, pi]
double gg3(double x)
{
	return 1 / (x*x + cos(x)*cos(x));
}

// x in [-pi, pi]
double gg4(double x)
{
	return sin(x);
}

// x in [0,2]
double gg5(double x)
{
	return 1 / (1 + x*x);
}

int main()
{
	// We first define a tolerance parameter.  Roughly speaking, a lower tolerance will
	// result in a more accurate approximation of the true integral.  However, there are 
	// instances where too small of a tolerance may yield a less accurate approximation
	// than a larger tolerance.  We recommend taking the tolerance to be in the
	// [1e-10, 1e-8] region.

	double tol = 1e-10;


	// Here we compute the integrals of the five functions defined above using the same 
	// tolerance level for each.

	double m1 = integrate_function_adapt_simp(&gg1, 0.0, 1.0, tol);
	double m2 = integrate_function_adapt_simp(&gg2, 0.0, 1.0, tol);
	double m3 = integrate_function_adapt_simp(&gg3, 0.0, pi, tol);
	double m4 = integrate_function_adapt_simp(&gg4, -pi, pi, tol);
	double m5 = integrate_function_adapt_simp(&gg5, 0.0, 2.0, tol);

	// We finally print out the values of each of the approximated integrals to ten
	// significant digits.

	cout << "\nThe integral of exp(x) for x in [0,1] is " << std::setprecision(10) << m1 << endl;
	cout << "The integral of x^2 for in [0,1] is " << std::setprecision(10) << m2 << endl;
	cout << "The integral of 1/(x^2 + cos(x)^2) for in [0,pi] is " << std::setprecision(10) << m3 << endl;
	cout << "The integral of sin(x) for in [-pi,pi] is " << std::setprecision(10) << m4 << endl;
	cout << "The integral of 1/(1+x^2) for in [0,2] is " << std::setprecision(10) << m5 << endl;
	cout << endl;
	system("pause");
	return 0;
}


2.读取XML并显示




// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
/*

读取XML显示的一个程序
*/




#include <dlib/xml_parser.h>
#include <iostream>
#include <fstream>


using namespace std;
using namespace dlib;

// ----------------------------------------------------------------------------------------

class doc_handler : public document_handler
{
	/*
	As the parser runs it generates events when it encounters tags and
	data in an XML file.  To be able to receive these events all you have to
	do is make a class that inherits from dlib::document_handler and
	implements its virtual methods.   Then you simply associate an
	instance of your class with the xml_parser.

	So this class is a simple example document handler that just prints
	all the events to the screen.
	*/
public:

	virtual void start_document(
		)
	{
		cout << "parsing begins" << endl;
	}

	virtual void end_document(
		)
	{
		cout << "Parsing done" << endl;
	}

	virtual void start_element(
		const unsigned long line_number,
		const std::string& name,
		const dlib::attribute_list& atts
		)
	{
		cout << "on line " << line_number << " we hit the <" << name << "> tag" << endl;

		// print all the tag's attributes
		atts.reset();
		while (atts.move_next())
		{
			cout << "\tattribute: " << atts.element().key() << " = " << atts.element().value() << endl;
		}
	}

	virtual void end_element(
		const unsigned long line_number,
		const std::string& name
		)
	{
		cout << "on line " << line_number << " we hit the closing tag </" << name << ">" << endl;
	}

	virtual void characters(
		const std::string& data
		)
	{
		cout << "Got some data between tags and it is:\n" << data << endl;
	}

	virtual void processing_instruction(
		const unsigned long line_number,
		const std::string& target,
		const std::string& data
		)
	{
		cout << "on line " << line_number << " we hit a processing instruction with a target of '"
			<< target << "' and data '" << data << "'" << endl;
	}
};

// ----------------------------------------------------------------------------------------

int main(int argc, char** argv)
{
	try
	{
		// Check if the user entered an argument to this application.  
		if (argc != 2)
		{
			cout << "Please enter an xml file to parse on the command line" << endl;
			return 1;
		}

		doc_handler dh;
		// Now run the parser and tell it to call our doc_handler for each of the parsing
		// events.
		parse_xml(argv[1], dh);
	}
	catch (std::exception& e)
	{
		cout << e.what() << endl;
	}
}



先这样了,本来只想看看Dlib的效果追踪咋样的,结果一下子看到这么多有趣的东西……


接着去啃手势识别了…




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

朱铭德

五毛也是爱٩(●´৺`●)૭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值