for a C++ newbie formulas calculations

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.
You have x(1,1) and y(1,1) and then you talk about x and y. How do x(1,1) and y(1,1) relate to x and y?

What is level? does that change at all?'
what is std(x)?

A few general comments on calculating:
- use double, not float
- beware of integer division. i/j will truncate the result to an integer.
- To square a number x, use x*x. ^ is the exclusive-or operation, so x^2 won't do what you want. Also pow(x,2) is much slower than x*x.
- Since you use cos(t) and sin(t) multiple times and you're doing 2 billion calculations, I'd compute sin(t) and cos(t) and store them in temporary variables. If performance is still bad, try computing sin(t) and then compute cos(t) = sqrt(1-sin(t)*sin(t)), It might be faster.

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.
Last edited on
std is matlab (this is why I asked) for standard deviation.

I am not aware of any way to get std in c++ apart from writing your own. Normal distro has it, but I don't know how to borrow it for your own data.

a 2 cent (untested, off the cuff) c++ code for that is: (I assume you can loop and get the average, which I called mean here).

1
2
3
4
for(dx = 0; dx < m.size(); dx++)
 stddev += (m[dx]- mean)*(m[dx]- mean);
stddev /= (m.size()-1);
stddev = sqrt(stddev);
Last edited on
Topic archived. No new replies allowed.