Hi, I'm very new to C++ and need to calculate something.
Can someone help me do:
t=2.22-6/(1+x0^2+y0^2);
x(1,1)=6+mu*(x0*cos(t)-y0*sin(t));
y(1,1)=mu*(x0*sin(t)+y0*cos(t));
x=x+randn(n,1)*level*std(x);
y=y+randn(n,1)*level*std(y);
and after that turn x and y to MOD 2 (x%2 and y%2)
I need the MOD returned only.
Need to do this calculation 2 000 000 000 times. Thanks for the help in advance I just never worked with C++ before.
|
pow(base, exponent) in <cmath> header does exponents. It is a little slow for small integer exponents so X*X is better than pow(X,2) if you care about speed --which you should, if it is being done 2 billion whatever times.
so anyway...
double t = 2.22-6.0/(1.0+x0*x0+y0*y0);
x[1][1] =6.0+mu*(x0*cos(t)-y0*sin(t)); //is this x[0][0]? Is this from matlab? the first item in an array in c++ is from zero not 1.
use fmod function for floating points, % for integers.
read
https://www.cplusplus.com/reference/random/
and use the code samples there to build a random generator similar to whatever yours is doing. you can fix your random generator so that *level* is part of it, and save a multiply.
for performance, try to avoid doing the same work twice. sin(t) and cos(t) are related: you can change 4 trig calls into 1 and make that faster just using math.
x = x+3 is the same as x+=3 and is preferred.
Give it a shot: the only way to learn c++ is to dive in and code c++. If you get stuck, post what you did. The above tips should get you close.
***this looks like nonsense to me.
%2 on integers is 0 or 1 result.
its based off a random number.
what exactly do you expect here that is different from just setting x and y to a random value of zero or one?! I mean it won't be a perfect match to the statistics of the original, but I suspect it won't be far off either... and you can set the random curve in <random> to get close to the original and save a LOT of time on all those computations. If it has some deep scientific meaning that I don't see, that is fine -- I trust you if you say it is so. But I sure don't see it.