公式计算

本文档展示了复杂数的加减乘除运算,以及二维矢量的打印、相乘和求逆操作。此外,还涵盖了产生正态分布随机数、获取随机变量、获取系统当前时间和使用Windows线程进行周期计算的方法。代码中包含了一些基本的线性代数运算,如矩阵乘法和行列式的计算。
摘要由CSDN通过智能技术生成

二维矢量的计算

打印

void PrintVector(vector<tuple<double,double,double>> vec)
{
	for(vector<tuple<double,double,double>>::iterator it = vec.begin(); it != vec.end(); it++)
	{
		cout<<(get<0>(*it)) + (get<1>(*it)) + (get<2>(*it));
		cout<<endl;
	}
}

相乘

vector<tuple<double,double,double>> VectorMultiply(vector<tuple<double,double,double>> left, vector<tuple<double,double,double>> right)
{
	//rows of left
	int row_left = left.size();
	//columns of left
	int col_left = tuple_size<decltype((tuple<double,double,double>)left.at(0))>::value;
	//rows of right
	int row_right = right.size();
	//columns of right
	int col_right = tuple_size<decltype((tuple<double,double,double>)right.at(0))>::value;
	//result of multiplication
	vector<tuple<double,double,double>>result;
	
	for(vector<tuple<double,double,double>>::iterator it = left.begin(); it != left.end(); it++)
	{
		double a,b,c;
		tuple<double,double,double>tp;
	
		a = get<0>(*it) * get<0>(right[0]);
		b = get<1>(*it) * get<0>(right[1]);
		c = get<2>(*it) * get<0>(right[2]);
		
		tp = make_tuple(a,b,c);
		result.push_back(tp);
	}
	return result;
}

求逆

bool ReverseVector(const vector<tuple<double,double,double>> before, vector<tuple<double,double,double>> after )
{
	//需要返回去的vector
	vector<tuple<double,double,double>>vec_return;
	//原vector
	double A[3][3] = 
	{
		(get<0>(before[0]), get<1>(before[0]), get<2>(before[0])),
		(get<0>(before[1]), get<1>(before[1]), get<2>(before[1])),
		(get<0>(before[2]), get<1>(before[2]), get<2>(before[2]))
	};
	//计算行列式 |A|
	double vec_determinant = 
		A[0][0] * (A[1][1] * A[2][2] - A[1][2] * A[2][1])-
		A[0][1] * (A[1][0] * A[2][2] - A[1][2] * A[2][0])+
		A[0][2] * (A[1][0] * A[2][1] - A[1][1] * A[2][0]);
	//行列式 |A| 为0,则不可逆
	if(vec_determinant == 0)
	{
		return false;
	}
	//计算A*
	double A_Star[3][3] = 
	{
		{A[1][1] * A[2][2] - A[1][2] * A[2][1], A[0][2] * A[2][1] - A[0][1] * A[2][2], A[0][1] * A[1][2] - A[0][2] * A[1][1]},
		{A[1][2] * A[2][0] - A[1][0] * A[2][2], A[0][0] * A[2][2] - A[0][2] * A[2][0], A[1][0] * A[0][2] - A[0][0] * A[1][2]},
		{A[1][0] * A[2][1] - A[1][1] * A[2][0], A[0][1] * A[2][0] - A[0][0] * A[2][1], A[0][0] * A[1][1] - A[1][0] * A[0][1]}
	}
	//计算A的逆
	for(int i = 0; i < 3; i++)
	{
		vec_return.emplace_back(make_tuple(
		(1 / vec_determinant * A_Star[i][0]),
		(1 / vec_determinant * A_Star[i][1]),
		(1 / vec_determinant * A_Star[i][2]) ));
	}
	after = move(vec_return);
	return true;
}

复数运算

Complex.h

#ifndef __COMPLEX__
#define __COMPLEX__

class Complex
{
public:
	Complex (double r = 0, double i = 0) : re(r), im(i){};
	Complex& operator +=(const Complex &);
	Complex& operator -=(const Complex &);
	Complex& operator *=(const Complex &);
	Complex& operator /=(const Complex &);
	double real() const {return re;};
	double imag() const {return im;};

	friend Complex& __doapl(Complex *, const Complex& );
	friend Complex& __doami(Complex *, const Complex& );
	friend Complex& __doaml(Complex *, const Complex& );
	
private:
	double re, im;
};

//重载+=
inline Complex& __doapl(Complex *ths, const Complex& r)
{
	ths->re += r.re;
	ths->im += r.im;
	return *ths;
}
inline Complex& Complex::operator +=(const Complex & r)
{
	return __doapl(this, r);
}

//重载-=
inline Complex& __doami(Complex *ths, const Complex& r)
{
	ths->re -= r.re;
	ths->im -= r.im;
	return *ths;
}
inline Complex& Complex::operator -=(const Complex & r)
{
	return __doami(this, r);
}
//重载*=
inline Complex& __doaml(Complex * ths, const Complex& r)
{
	double f = ths->re * r.re - ths->im * r.im;
	ths->im = ths->re * r.im + ths->im * r.re;
	return *ths;
}
inline Complex& Complex::operator *=(const Complex & r)
{
	return __doaml(this, r);
}

//求复数的虚部
inline double imag(const Complex& x)
{
	return x.imag();
}
//求复数的实部
inline double real(const Complex& x)
{
	return x.real();
}

/*	重载+	*/
//复数+复数
inline Complex operator + (const Complex& x, const Complex& y)
{
	return Complex(real(x) + real(y), imag(x) + imag(y));
}
//复数+实数
inline Complex operator + (const Complex& x, double y)
{
	return Complex(real(x) + y, imag(x));
}
//实数+复数
inline Complex operator + (double x, const Complex& y)
{
	return Complex(x + real(y), imag(y));
}

/*	重载-	*/
//复数-复数
inline Complex operator - (const Complex& x, const Complex& y)
{
	return Complex(real(x) - real(y), imag(x) - imag(y));
}
//复数-实数
inline Complex operator - (const Complex& x, double y)
{
	return Complex(real(x) - y, imag(x));
}
//实数-复数
inline Complex operator - (double x, const Complex& y)
{
	return Complex(x - real(y), - imag(y));
}

/*	重载*	*/
//复数*复数
inline Complex operator * (const Complex& x, const Complex& y)
{
	return Complex ( real(x) * real(y) - imag(x) * imag(y), real(x) * imag(y) + imag(x) * real(y) );
}
//复数*实数
inline Complex operator * (const Complex& x, double y)
{
	return Complex(real(x) * y, imag(x) * y);
}
//实数*复数
inline Complex operator * (double x, const Complex& y)
{
	return Complex(x * real(y), x * imag(y));
}

/*	重载/	*/
复数/复数
//Complex & operator / (const Complex&x)
//{
//	double f = this->re / x.re - this->im / x.im;
//	this->im = this->re / x.im + this->im / x.re;
//	this->re = f;
//	return *this;
//}
//复数/实数
inline Complex operator / (const Complex& x, double y)
{
	return Complex (real(x) / y, imag(x) / y);
}

//+复数
inline Complex operator + (const Complex& x)
{
	return x;
}
//-复数
inline Complex operator - (const Complex& x)
{
	return Complex (-real(x), -imag(x));
}

/*	重载==	*/
//2个复数是否相等
inline bool operator ==(const Complex& x, const Complex& y)
{
	return real(x) == real(y) && imag(x) == imag(y);
}
//复数与实数是否相等
inline bool operator ==(const Complex& x, double y)
{
	return real(x) == y && imag(x) == 0;
}
//实数与复数是否相等
inline bool operator == (double x, const Complex& y)
{
	return x == real(y) && imag(y) == 0;
}

/*	重载!=	*/
//2个复数是否不等
inline bool operator !=(const Complex& x, const Complex& y)
{
	return real(x) != real(y) || imag(x) != imag(y);
}
//复数与实数是否不等
inline bool operator !=(const Complex& x, double y)
{
	return real(x) != y || imag(x) != 0;
}
//实数与复数是否不等
inline bool operator != (double x, const Complex& y)
{
	return x != real(y) || imag(y) != 0;
}

//极坐标?
inline Complex polar(double r, double t)
{
	return Complex(r * cos(t), r * sin(t));
}
//求共轭复数(实部相同,虚部相反)
inline Complex conj(const Complex& x)
{
	return Complex(real(x), -imag(x));
}
//
inline double norm(const Complex& x)
{
	return real(x) * real(x) + imag(x) * imag(x);
}

//对复数取模
inline double GetModel(const Complex& x)
{
	double result = 0.0;
	result = sqrt(pow(real(x), 2.0) + pow(imag(x), 2.0));
	return result;
}

#endif

Complex.cpp

#include<iostream>
#include"Complex.h"
using namespace std;

//重载<<
ostream& operator << (ostream& os, const Complex& x)
{
	return os << '(' <<real(x)<<','<<imag(x)<<')';
}

int main()
{
	Complex c1(2,1);
	Complex c2(4,0);
	cout<<c1<<endl;
	cout<<c2<<endl;
	cout<<c1 + c2<<endl;
	cout<<c1 - c2<<endl;
	cout<<c1 * c2<<endl;

	cout<<conj(c1)<<endl;
	cout<<norm(c1)<<endl;
	cout<<polar(10,4)<<endl;
	cout<<(c1+=c2)<<endl;
	cout<<(c1==c2)<<endl;
	cout<<(c1!=c2)<<endl;
	cout<<+c1<<endl;
	cout<<-c1<<endl;
	cout<<(c2 -3)<<endl;
	cout<<(5 + c2)<<endl;
	return 0;
}

string转double

double String2Double(std::string input)
{
	double result = 0.0;
	stringstream oss;
	oss<<input;
	oss>>result;
	return result;
}

产生给定均值与方差的正态分布随机数

//均值:mean_value   方差:variance
double Normrand(const double mean_value, const double variance)
{
	int i, m;
	double s, w, v, t;
	double r = rand();

	s = 65536.0;
	w = 2053.0;
	v = 13849.0;
	t = 0.0;

	for(i = 0; i <= 12; i++)
	{
		r = r * w + v;
		m = (int)(r / s);
		r = r - m * s;
		t = t + r / s;
	}
	t = mean_value + variance * (t - 6.0);
	return (t);
}

获取[0, 2 * PI]区间上均匀分布的随机变量

#define PI 3.141592653
double GetRandomVariable(double input)
{
	srand((unsigned)time(NULL));
	double two = 2 * PI;
	double tmp = rand()%two;
	return tmp;
}

获取系统当前时间

#include<atltime.h>
using namespace std;
int main()
{
	CTime time = CTime::GetCurrentTime();
	int system_time_now;
	
	int h = time.GetHour();
	int m = time.GetMinute();
	int s = time.GetSecond();
	system_time_now = h * 3600 + m * 60 + s;
	cout<<"当前系统时间为:"<<system_time_now <<endl;
	system("pause");
	return 0;
}

欧拉公式

//任意实数都存在
e^ix^ = cos(x) + i * sin(x);
//e:自然对数的底数
//i:虚数单位
//x:则以弧度为单位

Windows线程实现周期计算

#include<Windows.h>
//thread_name, handle_name 根据实际情况来
DWORD WINAPI thread_name(LPVOID IpParater);//thread_name根据实际情况来
//创建线程
HANDLE handle_name = CreateThread(NULL, 0, thread_name, NULL, 0, NULL);
//释放线程
CloseHandle(handle_name);
//具体计算
DWORD WINAPI thread_name(LPVOID IpParater)
{
	for(;;)
	{
		//此处添加需要计算的代码
		//事件循环
		Sleep(calc_time);//每隔calc_time秒计算一次
	}
	return 0;
}

龙格库塔

#include<stdio.h>
#include<math.h>
#define f(x,y) (-1*(x)*(y)*(y))
int main()
{
	double a, b;			//a b:x在a到b的取值范围
	double  k1, k2, k3, k4;	//4个阶段的斜率
	double x0, y0;			//x0 y0:函数的初始值
	double h;				//h:每一小份,精度
	int n, i;				//n:计算的次数
	printf("input a, b, x0, y0, n: ");
	scanf("%lf%lf%lf%lf%d", &a, &b, &x0, &y0, &n);
	printf("x0\t\ty0\t\tk1\t\tk2\t\tk3\t\tk4\t\n");
	for(h = (b - a)/n, i = 0; i != n; i++)
	{
		k1 = f(x0, y0);						//时间段开始时的斜率
		k2 = f(x0 + h / 2, y0 + k1 * h / 2);//时间段中点的斜率
		k3 = f(x0 + h / 2, y0 + k2 * h / 2);//时间段中点的斜率,采用斜率k2决定y值
		k4 = f(x0 + h, y0 + h * k3);		//时间段终点的斜率,采用斜率k3决定y值

		printf("%lf\t%lf\t", x0, y0);
		printf("%lf\t%lf\t", k1, k2);
		printf("%lf\t%lf\n", k3, k4);

		y0 += h * (k1 + 2 * k2 + 2 * k3 + k4) / 6;
		x0 += h;
	}
	printf("xn = %lf\tyn = %lf\n", x0, y0);

	system("pause");
	return 0;
}

运行结果如下图:
龙格库塔

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值