Skip to content

Instantly share code, notes, and snippets.

@dcoeurjo
Created January 30, 2017 11:57
Show Gist options
  • Save dcoeurjo/c5b9b1cc62b094807ff7ab8aa42f12a4 to your computer and use it in GitHub Desktop.
Save dcoeurjo/c5b9b1cc62b094807ff7ab8aa42f12a4 to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#include <dCpp.h>
var energy(const std::vector<double> &pointsx,
const std::vector<double> &pointsy,
const var &x,
const var &y)
{
var sum=0.0;
//initialize sum as a differentiable variable
for (auto i=0; i<pointsx.size(); ++i)
sum += (pointsx[i] - x)* (pointsx[i] - x) + (pointsy[i] - y) * (pointsy[i] - y);
return sum;
}
int main()
{
std::vector<double> px = {0.0, 1.0, 1.0, 0.0};
std::vector<double> py = {0.0, 0.0, 1.0, 1.0};
dCpp::initSpace(1);
var x(50.1);
var y(50.1);
double lambda=0.1;
var e;
double norm = 1.0;
while (norm > 0.000001)
{
dCpp::init(x);
dCpp::init(y);
e = energy(px,py,x,y);
std::cout<< "x= "<<x.id<<" y= "<<y.id<<std::endl;
std::cout<<"e = "<<e.id<<std::endl;
std::cout<<"de/dx = "<<e.d(&x).id<<std::endl;
std::cout<<"de/dy = "<<e.d(&y).id<<std::endl<<std::endl;
x += -lambda*e.d(&x);
y += -lambda*e.d(&y);
norm = e.d(&x).id*e.d(&x).id+e.d(&y).id*e.d(&y).id;
}
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <vector>
#include <dCpp.h>
var energy(const std::vector<var> &pointsx,
const std::vector<var> &pointsy,
const var &x,
const var &y)
{
var sum=0.0;
//initialize sum as a differentiable variable
for (auto i=0; i<pointsx.size(); ++i)
sum += (pointsx[i] - x)* (pointsx[i] - x) + (pointsy[i] - y) * (pointsy[i] - y);
return sum;
}
int main()
{
std::vector<var> px = {0.0, 1.0, 1.0, 0.0};
std::vector<var> py = {0.0, 0.0, 1.0, 1.0};
dCpp::initSpace(1);
var x(50.1);
var y(50.1);
for(auto i=0; i < 4; ++i)
{
dCpp::init(px[i]);
dCpp::init(py[i]);
}
double lambda=0.1;
var e;
double norm = 1.0;
while (norm > 0.000001)
{
dCpp::init(x);
dCpp::init(y);
e = energy(px,py,x,y);
std::cout<< "x= "<<x.id<<" y= "<<y.id<<std::endl;
std::cout<<"e = "<<e.id<<std::endl;
std::cout<<"de/dx = "<<e.d(&x).id<<std::endl;
std::cout<<"de/dy = "<<e.d(&y).id<<std::endl<<std::endl;
x += -lambda*e.d(&x);
y += -lambda*e.d(&y);
norm = e.d(&x).id*e.d(&x).id+e.d(&y).id*e.d(&y).id;
}
return 0;
}
@akdorafb
Copy link

Bu nedir

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment