I am trying to solve this question but in the output the last digit is rounded. Is there a way to convert this output to a string so that the output isn't rounded?
We are given a convex polygon with N sides. You have to answer Q queries. The ith query is described by two integers vi,ti. In this query, all sides of the polygon start moving parallel to their respective perpendicular vector away from the centroid with a constant velocity of viunitssec. Output the final area covered by N sides of the polygon after time ti seconds.
#include <bits/stdc++.h>
#define ll long long int
#define lld long double
using namespace std;
lld dist(int x1, int y1, int x2, int y2)
{
return sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
}
lld dist2(int x1, int y1, int x2, int y2)
{
return (pow(x2 - x1, 2) + pow(y2 - y1, 2) * 1.0);
}
lld cot_sum(int x1, int y1, int x2, int y2)
{
lld dot = x1 * x2 + y1 * y2;
lld cosi = dot / (sqrt((lld)(x1 * x1 + y1 * y1) * (x2 * x2 + y2 * y2)));
lld theta = acos(cosi);
theta = (M_PI - theta) / 2;
return cos(theta) / sin(theta);
}
lld polygonArea(vector<int> X, vector<int> Y)
{
int n = X.size();
lld area = 0.0;
int j = n - 1;
for (int i = 0; i < n; i++)
{
area += (X[j] + X[i]) * (Y[j] - Y[i]);
j = i; // j previous i
}
return abs(area / 2.0);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long test;
cin >> test;
while (test--)
{
ll n, q;
cin >> n >> q;
vector<int> x(n);
vector<int> y(n);
vector<int> vx(n);
vector<int> vy(n);
vector<lld> angle(n);
vector<int> v(q);
vector<int> t(q);
vector<lld> ans(q);
for (int i = 0; i < n; i++)
{
cin >> x[i] >> y[i];
}
for (int i = 0; i < q; i++)
{
cin >> v[i] >> t[i];
}
lld area = polygonArea(x, y);
lld perimeter = 0;
lld cotsum = 0;
// cout<<"Area: "<<area<<endl;
x.push_back(x[0]);
// x.push_back(x[1]);
y.push_back(y[0]);
// y.push_back(y[1]);
for (int i = 0; i < n; i++)
{
vx[i] = x[i + 1] - x[i];
vy[i] = y[i + 1] - y[i];
}
vx.push_back(vx[0]);
vy.push_back(vy[0]);
for (int i = 0; i < n; i++)
{
perimeter += dist(x[i], y[i], x[i + 1], y[i + 1]);
cotsum += cot_sum(vx[i], vy[i], vx[i + 1], vy[i + 1]);
// cotsum = cotsum + 1 / cot_sum(x[i], y[i], x[i + 1], y[i + 1], x[i + 2], y[i + 2]);
}
for (int i = 0; i < q; i++)
{
ans[i] = area + perimeter * v[i] * t[i] + cotsum * v[i] * t[i] * v[i] * t[i];
// for (int j = 0; j < n; j++)
// {
// // ans[i] += v[i]*t[i]*dist(x[j],y[j],x[j+1],y[j+1]);
// ans[i] += (lld)(v[i] * t[i] * v[i] * t[i]) / cot_sum(x[j], y[j], x[j + 1], y[j + 1],x[j+2],y[j+2]);
// }
// cout<<setprecision(7)<<ans[i]<<endl;
std::stringstream stream;
cout<< stream << std::fixed << std::setprecision(7) <<ans[i]<<endl;
//std::string s = stream.str();
}
}
return 0;
}
SAMPLE INPUT
2
4 1
1 1
2 1
2 2
1 2
1 1
3 2
1 1
2 1
1 2
1 1
2 3
SAMPLE OUTPUT
9.0000000
9.7426406
230.8086578
MY OUTPUT
9
9.742641
230.8087
Related
I'm studying MLP reversal while reading this, but there's nothing I'm curious about, so I'm posting questions
In the code above, It used W, V, c, and b as parameters for the train function, but I don't understand why we use random and what it means.
Also, I don't know why I resize and make_pair the value to be handed over to the training function in the training set regarding the vector.
To summarize the questions,
What the parameters of W,V,c,b mean and why rand() is written in the main function
Reasons for using pair, resize, and make_pair in Trainset vectors, which are data for training neural networks
#include <iostream>
#include <vector>
#include <math.h>
#include <time.h>
using namespace std;
#define Train_Set_Size 20
#define PI 3.141592653589793238463
#define N 5
#define epsilon 0.05
#define epoch 50000
double c[N] = {};
double W[N] = {};
double V[N] = {};
double b = 0;
double sigmoid(double x) {
return (1.0f / (1.0f + std::exp(-x)));
}
double f_theta(double x) {
double result = b;
for (int i = 0; i < N; i++) {
result += V[i] * sigmoid(c[i] + W[i] * x);
}
return result;
}
void train(double x, double y) {
for (int i = 0; i < N; i++) {
W[i] = W[i] - epsilon * 2 * (f_theta(x) - y) * V[i] * x *
(1 - sigmoid(c[i] + W[i] * x)) * sigmoid(c[i] + W[i] * x);
}
for (int i = 0; i < N; i++) {
V[i] = V[i] - epsilon * 2 * (f_theta(x) - y) * sigmoid(c[i] + W[i] * x);
}
b = b - epsilon * 2 * (f_theta(x) - y);
for (int i = 0; i < N; i++) {
c[i] = c[i] - epsilon * 2 * (f_theta(x) - y) * V[i] *
(1 - sigmoid(c[i] + W[i] * x)) * sigmoid(c[i] + W[i] * x);
}
}
int main() {
srand(time(NULL));
for (int i = 0; i < N; i++) {
W[i] = 2 * rand() / RAND_MAX -1;
V[i] = 2 * rand() / RAND_MAX -1;
c[i] = 2 * rand() / RAND_MAX -1;
}
vector<pair<double, double>> trainSet;
trainSet.resize(Train_Set_Size);
for (int i = 0; i < Train_Set_Size; i++) {
trainSet[i] = make_pair(i * 2 * PI / Train_Set_Size, sin(i * 2 * PI / Train_Set_Size));
}
for (int j = 0; j < epoch; j++) {
for (int i = 0; i < Train_Set_Size; i++) {
train(trainSet[i].first, trainSet[i].second);
}
std::cout << j << "\r";
}
//Plot the results
vector<float> x;
vector<float> y1, y2;
for (int i = 0; i < 1000; i++) {
x.push_back(i * 2 * PI / 1000);
y1.push_back(sin(i * 2 * PI / 1000));
y2.push_back(f_theta(i * 2 * PI / 1000));
}
FILE * gp = _popen("gnuplot", "w");
fprintf(gp, "set terminal wxt size 600,400 \n");
fprintf(gp, "set grid \n");
fprintf(gp, "set title '%s' \n", "f(x) = sin (x)");
fprintf(gp, "set style line 1 lt 3 pt 7 ps 0.1 lc rgb 'green' lw 1 \n");
fprintf(gp, "set style line 2 lt 3 pt 7 ps 0.1 lc rgb 'red' lw 1 \n");
fprintf(gp, "plot '-' w p ls 1, '-' w p ls 2 \n");
//Exact f(x) = sin(x) -> Green Graph
for (int k = 0; k < x.size(); k++) {
fprintf(gp, "%f %f \n", x[k], y1[k]);
}
fprintf(gp, "e\n");
//Neural Network Approximate f(x) = sin(x) -> Red Graph
for (int k = 0; k < x.size(); k++) {
fprintf(gp, "%f %f \n", x[k], y2[k]);
}
fprintf(gp, "e\n");
fflush(gp);
system("pause");
_pclose(gp);
return 0;
}
how do I replace the pow() function in two cases in my code ?
I think this can be done with a for loop
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double a, b, h, PI = 3.141592;
int n;
cin >> a >> b >> h >> n;
for (double x = a; x <= b; x += h) {
double ans = 1, y;
for (int k = 0; k <= n; k++) {
ans *= cos(k * PI / 4) * pow(x, k);
for (int i = 2; i <= k; i++) {
ans /= i;
}
}
y = pow(exp(cos(x * sin(PI / 4))), x * cos(PI / 4));
cout << ans << " " << y << " " << fabs(y-ans) << endl;
}
return 0;
}
Do not write everything in main.
Define double S(double x, int n) and double U(double x).
each element of sum can be calculated based on previous element.
cos(k * M_PI / 4) has repeating values so it can be stored in table.
double S(double x, int n)
{
double a = 1;
double s = a;
constexpr double q = std::cos(M_PI / 4);
constexpr double cos_val[]{ 1, q, 0, -q, -1, -q, 0, q };
for (int k = 1; k <= n; ++k) {
a *= x / k;
s += cos_val[k & 7] * a
}
return s;
}
For the inner loop, you need not calculate the power in each iteration if you consider that on the previous iteration you already calculated pow(x,k-1) and that pow(x,k) == pow(x,k-1)*x:
double pow_x = 1; // on first iteration pow(x,0) == 1
for (int k = 0; k <= n; k++) {
ans *= cos(k * PI / 4) * pow_x;
// ...
pow_x *= x; // pow(x,k) -> pow(x,k+1)
}
The second use of pow in your code cannot be easily replaced, because of the floating point exponent. You would have to rewrite pow to get the same result. However, your code does not match the formula in the image. The image says (pseudo maths notation):
e ^ ( x * C1 ) * C2
your code is calculating
y = pow(exp(cos(x * sin(PI / 4))), x * cos(PI / 4));
( e^(C2) ) ^ (x * C1)
change it to
y = exp(x * cos(PI / 4)) * cos(x * sin(PI / 4))
i'm trying to make my runge-kutta 4th order code modular. I don't want to have to write and declare the code everytime I use it, but declare it in a .hpp and a .cpp file to use it separetely. But i'm having some problems. Generally I want to solve a n-dimension system of equations. For that I use two functions: one for the system of equations and another for the runge-kutta method as follows:
double F(double t, double x[], int eq)
{
// System equations
if (eq == 0) { return (x[1]); }
else if (eq == 1) { return (gama * sin(OMEGA*t) - zeta * x[1] - alpha * x[0] - beta * pow(x[0], 3) - chi * x[2]); }
else if (eq == 2) { return (-kappa * x[1] - phi * x[2]); }
else { return 0; }
}
void rk4(double &t, double x[], double step)
{
double x_temp1[sistvar], x_temp2[sistvar], x_temp3[sistvar];
double k1[sistvar], k2[sistvar], k3[sistvar], k4[sistvar];
int j;
for (j = 0; j < sistvar; j++)
{
x_temp1[j] = x[j] + 0.5*(k1[j] = step * F(t, x, j));
}
for (j = 0; j < sistvar; j++)
{
x_temp2[j] = x[j] + 0.5*(k2[j] = step * F(t + 0.5 * step, x_temp1, j));
}
for (j = 0; j < sistvar; j++)
{
x_temp3[j] = x[j] + (k3[j] = step * F(t + 0.5 * step, x_temp2, j));
}
for (j = 0; j < sistvar; j++)
{
k4[j] = step * F(t + step, x_temp3, j);
}
for (j = 0; j < sistvar; j++)
{
x[j] += (k1[j] + 2 * k2[j] + 2 * k3[j] + k4[j]) / 6.0;
}
t += step;
}
The above code works and it is validated. However it has some dependencies as it uses some global variables to work:
gama, OMEGA, zeta, alpha, beta, chi, kappa and phi are global variables that I want to read from a .txt file. I already manage to do that, however only in a single .cpp file with all code included.
Also, sistvar is the system dimension and also a global variable. I'm trying to enter it as an argument in F. But the way it is written seems to give errors as sistvar is a const and can't be changed as a variable and I can't put variables inside an array's size.
In addition, the two functions has an interdependency as when a call F inside rk4, eq number is needeed.
Could you give me tips in how to do that? I already searched and read books about this and could not find an answer for it. It is probably an easy task but i'm relatively new in c/c++ programming languages.
Thanks in advance!
* EDITED (Tried to implement using std::vector)*
double F(double t, std::vector<double> x, int eq)
{
// System Equations
if (eq == 0) { return (x[1]); }
else if (eq == 1) { return (gama * sin(OMEGA*t) - zeta * x[1] - alpha * x[0] - beta * pow(x[0], 3) - chi * x[2]); }
else if (eq == 2) { return (-kappa * x[1] - phi * x[2]); }
else { return 0; }
}
double rk4(double &t, std::vector<double> &x, double step, const int dim)
{
std::vector<double> x_temp1(dim), x_temp2(dim), x_temp3(dim);
std::vector<double> k1(dim), k2(dim), k3(dim), k4(dim);
int j;
for (j = 0; j < dim; j++) {
x_temp1[j] = x[j] + 0.5*(k1[j] = step * F(t, x, j));
}
for (j = 0; j < dim; j++) {
x_temp2[j] = x[j] + 0.5*(k2[j] = step * F(t + 0.5 * step, x_temp1, j));
}
for (j = 0; j < dim; j++) {
x_temp3[j] = x[j] + (k3[j] = step * F(t + 0.5 * step, x_temp2, j));
}
for (j = 0; j < dim; j++) {
k4[j] = step * F(t + step, x_temp3, j);
}
for (j = 0; j < dim; j++) {
x[j] += (k1[j] + 2 * k2[j] + 2 * k3[j] + k4[j]) / 6.0;
}
t += step;
for (j = 0; j < dim; j++) {
return x[j];
}
}
vector array
2.434 s | | 0.859 s
2.443 s | | 0.845 s
2.314 s | | 0.883 s
2.418 s | | 0.884 s
2.505 s | | 0.852 s
2.428 s | | 0.923 s
2.097 s | | 0.814 s
2.266 s | | 0.922 s
2.133 s | | 0.954 s
2.266 s | | 0.868 s
_______ _______
average = 2.330 s average = 0.880 s
Using vector function where the vector arithmetic is taken from Eigen3
#include <eigen3/Eigen/Dense>
using namespace Eigen;
of the same parts as discussed in the question could look like (inspired by function pointer with Eigen)
VectorXd Func(const double t, const VectorXd& x)
{ // equations for solving simple harmonic oscillator
Vector3d dxdt;
dxdt[0] = x[1];
dxdt[1] = gama * sin(OMEGA*t) - zeta * x[1] - alpha * x[0] - beta * pow(x[0], 3) - chi * x[2];
dxdt[2] = -kappa * x[1] - phi * x[2];
return dxdt;
}
MatrixXd RK4(VectorXd Func(double t, const VectorXd& y), const Ref<const VectorXd>& y0, double t, double h, int step_num)
{
MatrixXd y(y0.rows(), step_num );
VectorXd k1, k2, k3, k4;
y.col(0) = y0;
for (int i=1; i<step_num; i++){
k1 = Func(t, y.col(i-1));
k2 = Func(t+0.5*h, y.col(i-1)+0.5*h*k1);
k3 = Func(t+0.5*h, y.col(i-1)+0.5*h*k2);
k4 = Func(t+h, y.col(i-1)+h*k3);
y.col(i) = y.col(i-1) + (k1 + 2*k2 + 2*k3 + k4)*h/6;
t = t+h;
}
return y.transpose();
}
Passing a vector to a function to be filled apparently requires some higher template contemplations in Eigen.
I am writing a program which is using bicubic interpolation, and I am using EasyBMP, but i have a problem. While input image are peppers.
My output looks like this.
#define BOUNDS(val, min, max) if (val < min) { val = min; } else if (val > max) { val = max; }
void resize(float value)
{
BMP* temp = new BMP();
int in_w = image->TellWidth();
int in_h = image->TellHeight();
temp->SetSize(in_w*value, in_h*value);
RGBApixel input;
for (int y = 0; y < temp->TellHeight(); ++y)
{
float v = float(y) / float(temp->TellHeight() - 1);
for (int x = 0; x < temp->TellWidth(); ++x)
{
float u = float(x) / float(temp->TellWidth() - 1);
input = this->bicubicInterpolation(u,v);
temp->SetPixel(x,y,input);
}
}
delete image;
image=temp;
}
RGBApixel bicubicInterpolation(float u, float v)
{
RGBApixel p[4][4];
int q,w;
float x = (u * image->TellWidth()) - 0.5;
int xint = int(x);
float dx = x - floor(x);
float y = (v * image->TellHeight()) - 0.5;
int yint = int(y);
float dy = y - floor(y);
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
{
q=xint - 1 + j;
BOUNDS(q,0,image->TellWidth()-1)
w=yint - 1 + i;
BOUNDS(w,0,image->TellHeight()-1)
p[j][i] = image->GetPixel(q,w);
}
}
RGBApixel toReturn;
RGBApixel el1 = this->interpolation(p[0][0],p[1][0], p[2][0], p[3][0], dx);
RGBApixel el2 = this->interpolation(p[0][1],p[1][1], p[2][1], p[3][1], dx);
RGBApixel el3 = this->interpolation(p[0][2],p[1][2], p[2][2], p[3][2], dx);
RGBApixel el4 = this->interpolation(p[0][3],p[1][3], p[2][3], p[3][3], dx);
RGBApixel value = this->interpolation(el1, el2, el3, el4, dy);
return value;
}
RGBApixel interpolation(RGBApixel A, RGBApixel B, RGBApixel C, RGBApixel D, float t)
{
float a[3],b[3],c[3],d[3];
RGBApixel toRet;
a[0]=A.Red;
b[0]=B.Red;
c[0]=C.Red;
d[0]=D.Red;
a[1]=A.Green;
b[1]=B.Green;
c[1]=C.Green;
d[1]=D.Green;
a[2]=A.Blue;
b[2]=B.Blue;
c[2]=C.Blue;
d[2]=D.Blue;
float w[3];
float x[3];
float y[3];
float z[3];
float color[3];
for(int i=0; i<3; i++)
{
w[i]= -a[i] / 2.0f + (3.0f*b[i]) / 2.0f - (3.0f*c[i]) / 2.0f + d[i] / 2.0f;
x[i]= a[i] - (5.0f*b[i]) / 2.0f + 2.0f*c[i] - d[i] / 2.0f;
y[i]= -a[i] / 2.0f + c[i] / 2.0f;
z[i]= b[i];
color[i]= w[i] * t*t*t + x[i] * t*t + y[i] * t +z[i];
}
toRet.Red=color[0];
toRet.Green=color[1];
toRet.Blue=color[2];
toRet.Alpha=255;
return toRet;
}
Have you noticed mistake that I made?
It was beign out of range. Just need to:
color[i]= w[i] * t*t*t + x[i] * t*t + y[i] * t +z[i];
BOUNDS(color[i],0,255);
I am trying to write a code to solve the n-body problem and i run into trouble when using an array with all the bodies instead of using the different bodies seperately. I currently have no idea what's going wrong in my code but when i plot x in function of y for any body i get a straight line which obviously isn't right.
This is my current code:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <fstream>
#define h 10000.0
#define N 3
#define G 6.67384*pow(10.0,-11)
using namespace std;
class particle{
public:
double kx1,kx2,kx3,kx4, kv1, kv2, kv3, kv4;
double ky1, ky2, ky3, ky4, kvy1, kvy2, kvy3, kvy4;
double x,y,vx,vy,m;
double dist(particle aap){
double dx = x - aap.x;
double dy = y - aap.y;
return sqrt(pow(dx,2.0)+pow(dy,2.0));
}
double g(double x1, double y1,particle aap){
return G*aap.m*(aap.x-x1)/pow(dist(aap),3.0);
}
double p(double x1, double y1, particle aap){
return G*aap.m*(aap.y-y1)/pow(dist(aap),3.0);
}
void update(){ //object advances 1 step
x = x + (1/6.0)*(kx1+2*kx2+2*kx3+kx4);
vx = vx + (1/6.0)*(kv1+2*kv2+2*kv3+kv4);
y = y + (1/6.0)*(ky1+2*ky2+2*ky3+ky4);
vy = vy + (1/6.0)*(kvy1+2*kvy2+2*kvy3+kvy4);
}
void create(double x1, double y1, double vx1, double vy1, double m1){
x = x1;
y = y1;
vx = vx1;
vy = vy1;
m =m1;
}
bool operator ==(particle &other){
if(x == other.x && y == other.y && vx == other.vx && vy == other.vy){
return true;
}
}
};
particle bodies[N];
void set(particle (&bodies)[N]){
bodies[0].create(1, 1, -2, 1, 2*pow(10.0,30));
bodies[1].create(2870671*pow(10.0,6), 0, 0, 6800, 8.6810*pow(10.0,25));
bodies[2].create(4498542*pow(10.0,6),0 ,0, 5430, 1.0243*pow(10.0,26));
}
double xforce(double x1, double y1, particle aap, particle bodies[N]){ //force in the x- direction
double fx = 0;
for (int i = 0; i <= N; i++){
if (bodies[i] == aap ){;}
else{
fx += aap.g(x1,y1,bodies[i]);
}
}
return fx;
}
double yforce(double x1, double y1, particle aap, particle bodies[N]){ //force in the y- direction
double fy = 0;
for (int i = 0; i <= N; i++){
if (bodies[i] == aap) {;}
else{
fy += aap.p(x1,y1,bodies[i]);
}
}
return fy;
}
void corr(double t, particle bodies[N]){ //runge kutta 4
for(int i =0; i <= N; i++){
bodies[i].kx1 = t*bodies[i].vx;
bodies[i].kv1 = t*xforce(bodies[i].x, bodies[i].y, bodies[i], bodies);
bodies[i].ky1 = t*bodies[i].vy;
bodies[i].kvy1 = t*yforce(bodies[i].x, bodies[i].y, bodies[i], bodies);
bodies[i].kx2 = t*(bodies[i].vx + 0.5*bodies[i].kv1);
bodies[i].kv2 = t*xforce(bodies[i].x + 0.5*bodies[i].kx1, bodies[i].y + 0.5*bodies[i].ky1, bodies[i], bodies);
bodies[i].ky2 = t*(bodies[i].vy + 0.5*bodies[i].kvy1);
bodies[i].kvy2 = t*yforce(bodies[i].x + 0.5*bodies[i].kx1, bodies[i].y + 0.5*bodies[i].ky1, bodies[i], bodies);
bodies[i].kx3 = t*(bodies[i].vx+ 0.5*bodies[i].kv2);
bodies[i].kv3 = t*xforce(bodies[i].x + 0.5*bodies[i].kx2, bodies[i].y + 0.5*bodies[i].ky2, bodies[i], bodies);
bodies[i].ky3 = t*(bodies[i].vy+ 0.5*bodies[i].kvy2);
bodies[i].kvy3 = t*yforce(bodies[i].x + 0.5*bodies[i].kx2, bodies[i].y + 0.5*bodies[i].ky2,bodies[i], bodies);
bodies[i].kx4 = t*(bodies[i].vx + bodies[i].kv3);
bodies[i].kv4 = t*xforce(bodies[i].x+ bodies[i].kx3, bodies[i].y + bodies[i].ky3, bodies[i], bodies);
bodies[i].ky4 = t*(bodies[i].vy + bodies[i].kvy3);
bodies[i].kvy4 = t*yforce(bodies[i].x + bodies[i].kx3, bodies[i].y + bodies[i].ky3, bodies[i], bodies);
}
}
void calculate(particle (&bodies)[N]){
set(bodies);
ofstream file;
file.open("tester.txt");
for(int i =0; i <=50000; i++){
corr(h, bodies);
for(int j = 0; j <= N; j++){
bodies[j].update();
}
if( i%1000 == 0){
file << i*h;
for(int j = 0; j <=N ; j++){
file <<" "<<bodies[j].x << " "<< bodies[j].y;
}
file <<" "<<"\n";
}
else{;}
}
file.close();
}
int main()
{
calculate(bodies);
system("pause");
return 0;
}
The problem probably lies outside of the class particle since the program worked before i started using the array bodies. Any suggestions for non essential improvements are ofcourse welcome. Another thing i'm trying to do is use std::vector instead of an array but i don't know how i could define a vector outside my functions like i defined the array bodies.
For a start, all of your i <= N are wrong, because your loop will execute 4 times (0, 1, 2, 3) instead of 3 for i < N.
You are likely experiencing energy drift, as RK4 is not symplectic, and the n-body problem is a Hamiltonian system. I had this same problem trying to use RK4 for a solar system n-body as well. So did this person. You should try another symplectic numerical method like Euler, Verlet, Ruth's 3rd, or Ruth's 4th order symplectic integrator.