C++ 求 f(x) = sin x / x 的函数极限

Python: 

import sympy
from sympy import oo #注意无究符号表示形式为两个小定字母o
import numpy as np
x = sympy.Symbol('x') #注意Symbol首字母大写

f = sympy.sin(x) / x   # 定义极限表达式

lim = sympy.limit(f,x,oo)
print(lim)

 

输出:

0
 
C++

#include "X:\Work\Share\CCode\CPlatform\MathExt\_MathExt_out.h"
 
using namespace lf;
using namespace std;
  
int main()
{
       
    _Variable x;
     
    _function  f;
    

    //设置 x 趋向于正无穷大
    x.SetLimitsTo(_LimitsTo::PositiveInfinity);

    //这句其实没什么意义,只是为更像于数学表达式
    //相当于 f = x;
    f(x) = _sin(x) / x;

    _cout << f.limit() << "\n";        
     
}

输出:

0

主要源码:

_Variable _sin(const _Variable& x)
{
    _Variable vResult;
          
    if (x._limit == _LimitsTo::PositiveInfinity || x._limit == _LimitsTo::NegativeInfinity)
    {
        vResult._fixedMax = 1;
        vResult._fixedMin = -1;
    }

    return vResult;
}

_Variable _Variable::operator/(const _Variable& vRight) const
{
    //_cout << _t("_Variable _Variable::operator/(const _Variable& vRight) const");


    //有最大值边界
    if (this->_fixedMax._t != _ValueType::Null || this->_fixedLimitMax._t != _ValueType::Null)
    {
        //也有最小值边界
        if (this->_fixedMin._t != _ValueType::Null || this->_fixedLimitMin._t != _ValueType::Null)
        {
            //有最大值边界的数 /  正无穷或负无穷 = 0
            if (vRight._limit == _LimitsTo::PositiveInfinity || vRight._limit == _LimitsTo::NegativeInfinity)
            {
                return (_Variable)0;
            }
        }

    }
 
    
    return _Variable();
}

有兴趣的朋友自己去完善。 

头文件:

/// <summary>
/// 取值范围,趋向于。
/// </summary>
/// 创建时间: 2024-05-03     最后一修改时间:2024-05-03  
enum class _LimitsTo
{
	/// <summary>
	/// 正无穷大
	/// </summary>
	PositiveInfinity,

	/// <summary>
	/// 负无穷小
	/// </summary>
	NegativeInfinity,
	 
	/// <summary>
	/// 区间值
	/// </summary>
	IntervalValue,

	/// <summary>
	/// 固定值
	/// </summary>
	FixedValue,
	 
	/// <summary>
	/// 任意值
	/// </summary>
	AnyValue,
};


/// <summary>
/// 值类型
/// </summary>
/// 创建时间: 2024-05-03     最后一修改时间:2024-05-03
enum class _ValueType
{
	Null,
	Int,
	Double,


};



/// <summary>
/// 
/// </summary>
/// 创建时间: 2024-05-03     最后一修改时间:2024-05-03
class _Value : public _Object
{
public:
	/// <summary>
	/// 保存固定值的内存区域
	/// </summary>
	_Array<_byte> _m;

	_ValueType _t;

public:
	_Value();
	_Value(const int& i);
	_Value(const double& d);
	~_Value();

public: //--------------------------------重写
	virtual _string ToSplitString(const _string& sSplitString) const;
};


/// <summary>
/// _Variable实际描述的是一个集合
/// </summary>
/// 创建时间: 2024-04-14     最后一修改时间:2024-05-03
class _Variable  : public _Object
{
public:
	/// <summary>
	/// 所属数集数型
	/// </summary>
	_ManifoldType _mt;

	/// <summary>
	/// 固定值
	/// </summary>
	_Value _fixed;
	 
	/// <summary>
	/// 最大极限的固定值
	/// </summary>
	_Value _fixedLimitMax;

	/// <summary>
	/// 最小极限的固定值
	/// </summary>
	_Value _fixedLimitMin;


	/// <summary>
	/// 最大固定值
	/// </summary>
	_Value _fixedMax;


	/// <summary>
	/// 最小固定值
	/// </summary>
	_Value _fixedMin;

	/// <summary>
	/// 取值范围,趋向于。
	/// </summary>
	_LimitsTo _limit;
	 
	/// <summary>
	/// 符号名
	/// </summary>
	_string _symbolName; 

public: //构造函数与析构函数
	_Variable();
	~_Variable();


	_Variable(const _Variable& v);


	/// <summary>
	/// _Variable x = 5;  //相当于 _Variable x(5);
	/// 此时调用是 _Variable(const int& iValue)
	/// </summary>
	/// <param name="iValue"></param>
	/// 创建时间: 2024-04-14     最后一修改时间:2024-04-14  
	explicit _Variable(const int& i);


	/// <summary>
	/// 
	/// </summary>
	/// <param name="d"></param>
	/// 创建时间: 2024-04-15     最后一修改时间:2024-04-15  
	explicit _Variable(const double& d);

public:  //-------------------------------------运算符重载	 

	/// <summary>
	/// _Variable x;
	/// x = 5;  此时调用是operator=
	/// </summary>
	/// <param name="n"></param>
	/// <returns></returns>
	/// 创建时间: 2024-04-14     最后一修改时间:2024-04-14  
	_Variable operator=(const int& iValue);

	explicit operator int() const;	 


	_Variable operator-(const int& n) const;
	_Variable operator+(const int& n) const;

	_Variable operator/(const int& n) const;
	_Variable operator/(const _Variable& vRight) const;

	friend _Variable operator/(const int& iLeft, const _Variable& vRight);

	// 重载加法运算符
	_Variable operator+(const _Variable& other) const;

	_Variable operator*(const _Variable& other) const;

	_Variable operator^(const _Variable& other) const;

	_Variable operator^(const size_t& nPower) const;

public:  //--------------------------------功能函数
	/// <summary>
	/// 
	/// </summary>
	/// <param name="lim"></param>
	/// 创建时间: 2024-05-03     最后一修改时间:2024-05-03 
	void SetLimitsTo(const _LimitsTo& lim);



	/// <summary>
	/// 获取最大值,当取值范围在区间内存才有最大值,最小值。
	/// 例如闭区间 [0,1] 中的 1 , 没有返回NULL。
	/// </summary>
	/// <returns></returns>
	/// 创建时间: 2024-05-03     最后一修改时间:2024-05-03 
	_Variable GetMaxValue()const;


	/// <summary>
	/// 获取最小值,当取值范围在区间内才有最大值,最小值。
	/// 例如闭区间 [0,1) 中的 0 , 没有返回NULL。
	/// </summary>
	/// <returns></returns>
	/// 创建时间: 2024-05-03     最后一修改时间:2024-05-03 
	_Variable GetMinValue()const;


	/// <summary>
	/// 永远达不到又永远迫近的那个值。
	/// 例如开区间(0,1) 中的 0 和 1,没有返回NULL。
	/// </summary>
	/// <returns></returns>
	_Variable GetLimitValue()const;

public: //--------------------------------重写
	virtual _string ToSplitString(const _string& sSplitString) const;
private:

};

 





class _function : public _Object
{
private:
	_Variable _resultVar;

public:
	_function(const _Variable& v);
	_function();

	_Variable& operator()(const _Variable& other);

	_Variable& limit();
	 
};


/// <summary>
/// 
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
/// 创建时间: 2024-05-03     最后一修改时间:2024-05-03  
_Variable _sin(const _Variable& x);

源文件:


/************************************************************************

						_Variable


***********************************************************************/

_Variable::_Variable() : _mt(_ManifoldType::实数), _limit(_LimitsTo::AnyValue)
{
 
}
 

_Variable::_Variable(const int& i)
{

	_fixed = i;

	_mt = _ManifoldType::整数;
	_limit = _LimitsTo::FixedValue;
}

_Variable::_Variable(const double& d)
{
	_fixed = d;

	_mt = _ManifoldType::有理数;
}

_Variable::~_Variable()
{
	
}

_Variable::_Variable(const _Variable& v)
{
	_mt = v._mt;  // 所属数集数型

	 
	_fixed = v._fixed; // 固定值

 
	_fixedLimitMax = v._fixedLimitMax; // 最大极限的固定值

 
	_fixedLimitMin = v._fixedLimitMin;// 最小极限的固定值


	 
	_fixedMax = v._fixedMax; // 最大固定值


	 
	_fixedMin = v._fixedMin; // 最小固定值

	 
	_limit = v._limit;

	 
	_symbolName = v._symbolName;// 符号名
	  
}

_Variable _Variable::operator=(const int& iValue)
{
	_cout << _t("_Variable _Variable::operator=(const int& iValue)\n");

	_fixed = iValue;

	_mt = _ManifoldType::整数;

	return *this;	 
}
 
_Variable::operator int() const
{
	//const int* pint =
	return *((int*)(_fixed._m.DataConst));
}
 
 
_Variable _Variable::operator-(const int& n) const
{
	_cout << _t("_Variable _Variable::operator-\n");
	return _Variable();
}

_Variable _Variable::operator+(const int& n) const
{
	_cout << _t("_Variable _Variable::operator+\n");
	return _Variable();
}

_Variable _Variable::operator/(const int& n) const
{
	_Variable vResult = *this;

	_cout << _t("Variable _Variable::operator/\n");

	if (_limit == _LimitsTo::PositiveInfinity) //正无穷 /  整数
	{
		if (n > 0)
			return vResult;
		else //正无穷  /  负数 = 负无穷
		{			 
			vResult._limit = _LimitsTo::NegativeInfinity;
		}
	}
	else if(_limit == _LimitsTo::PositiveInfinity) //负无穷 /  整数
	{
		if (n > 0)
			return vResult;
		else //负无穷  /  负数 = 正无穷
		{
			vResult._limit = _LimitsTo::PositiveInfinity;
		}
	}
	return vResult;
}

_Variable _Variable::operator/(const _Variable& vRight) const
{
	//_cout << _t("_Variable _Variable::operator/(const _Variable& vRight) const");


	//有最大值边界
	if (this->_fixedMax._t != _ValueType::Null || this->_fixedLimitMax._t != _ValueType::Null)
	{
		//也有最小值边界
		if (this->_fixedMin._t != _ValueType::Null || this->_fixedLimitMin._t != _ValueType::Null)
		{
			//有最大值边界的数 /  正无穷或负无穷 = 0
			if (vRight._limit == _LimitsTo::PositiveInfinity || vRight._limit == _LimitsTo::NegativeInfinity)
			{
				return (_Variable)0;
			}
		}

	}
 
	
	return _Variable();
}

_Variable _Variable::operator+(const _Variable& other) const
{
	_cout << _t("_Variable _Variable::operator+\n");
	return _Variable();
}

_Variable _Variable::operator*(const _Variable& other) const
{
	_cout << _t("_Variable _Variable::operator*\n");
	return _Variable();
}

 
_Variable _Variable::operator^(const _Variable& other) const
{
	_cout << _t("_Variable::operator^\n");
	return _Variable();
}
 

_Variable _Variable::operator^(const size_t& nPower) const
{
	if (_mt == _ManifoldType::整数)
	{
		int i = *((int*)_fixed._m.DataConst);

		return  _Variable(_Math::pow(i, nPower));
	}
	else if (_mt == _ManifoldType::有理数)
	{
		int d = *((double*)_fixed._m.DataConst);
		return _Variable(_Math::pow(d, nPower));
 
	}
	else
	{
		_cout << "_Variable _Variable::operator^(const size_t& nPower) const";

		throw "未完成。";
	}




}

void _Variable::SetLimitsTo(const _LimitsTo& lim)
{
	_limit = lim;
}

_string _Variable::ToSplitString(const _string& sSplitString) const
{

	//_cout << "_string _Variable::ToSplitString(const _string& sSplitString) const";



	if (_fixed._t != _ValueType::Null)
	{
		return _fixed.ToString();
	}
 

	


	return _t("未完成!");
}




_function::_function(const _Variable& v)
{
}

_function::_function()
{
}

_Variable& _function::operator()(const _Variable& other)
{
	//_cout << _t("_function::operator()\n");
	return  _resultVar;
}

_Variable& _function::limit() 
{
	//_cout << _t("这里求函数极限!\n");
	
	return _resultVar;
}

 


_Variable operator/(const int& iLeft, const _Variable& vRight)
{
	if (vRight._mt == _ManifoldType::整数)
	{
		double d1 = iLeft;
		double d2 = *((int*)vRight._fixed._m.DataConst);
		
		return _Variable(d1 / d2);
	}
	_cout << _t("_Variable operator/(const int& iLeft, const _Variable& vRight)");
}



/// <summary>
/// 参考:https://baike.baidu.com/item/%E4%B8%89%E8%A7%92%E5%87%BD%E6%95%B0/1652457?fr=aladdin
/// 
/// 一般地,在直角坐标系中,给定单位圆,对任意角α,使角α的顶点与原点重合,
/// 始边与x轴非负半轴重合,终边与单位圆交于点P(u,v),那么点P的纵坐标v叫
/// 作角α的正弦函数,记作v=sinα。通常,用x表示自变量,即x表示角的大小,用y
/// 表示函数值,这样就定义了任意角的三角函数y=sin x,它的定义域为全体实数,
/// 值域为[-1,1]。表达式:f(x) = Asin(ωx + φ)
/// 
/// 定义域和值域
/// sin(x),cos(x)的定义域为R,值域为[-1, 1]。
/// tan(x)的定义域为x不等于π / 2 + kπ(k∈Z),值域为R。
/// cot(x)的定义域为x不等于kπ(k∈Z), 值域为R。
/// y = a·sin(x) + b·cos(x) + c的值域为[c - √(a² + b²), c + √(a² + b²)]
/// 周期T = 2π / ω
/// </summary>
/// <param name="x"></param>
/// <returns></returns>
_Variable _sin(const _Variable& x)
{
	_Variable vResult;
	 	 
	if (x._limit == _LimitsTo::PositiveInfinity || x._limit == _LimitsTo::NegativeInfinity)
	{
		vResult._fixedMax = 1;
		vResult._fixedMin = -1;
	}

	return vResult;
}

_LF_END_

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值