diff --git a/Automatic-Differentiation/.gitignore b/Automatic-Differentiation/.gitignore deleted file mode 100755 index ff94bc4900dbabe8dd14234181c6b5c6ce72b59d..0000000000000000000000000000000000000000 --- a/Automatic-Differentiation/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -obj/ -bin/ -main -.gmake diff --git a/Automatic-Differentiation/Makefile b/Automatic-Differentiation/Makefile deleted file mode 100755 index 9c05710ee7ea0bd4f2ed22a39121321f256d331f..0000000000000000000000000000000000000000 --- a/Automatic-Differentiation/Makefile +++ /dev/null @@ -1,31 +0,0 @@ -CXX = g++ -BIN = bin -LIB = lib -OBJS_MAIN = root/obj/dual.o root/obj/gradient.o root/obj/main.o - -all : main - -main : $(BIN) - $(MAKE) -C root obj obj/dual.o obj/gradient.o obj/main.o - $(CXX) -o $(BIN)/main $(OBJS_MAIN) $(LIBS) - -$(BIN) : - if [ ! -d $(BIN) ]; then mkdir $(BIN); fi - -$(LIB) : - if [ ! -d $(LIB) ]; then mkdir $(LIB); fi - -clean : - $(MAKE) -C root clean - if [ -d $(BIN) ]; then rm $(BIN) -r; fi - -install : $(LIB) - ar rcs $(LIB)/libgradient.a root/obj/dual.o root/obj/gradient.o - sudo mv $(LIB)/libgradient.a /usr/local/lib/mylibs - sudo ln -sf /usr/local/lib/mylibs/libgradient.a /usr/local/lib/libgradient.a - sudo cp root/include/gradient.h /usr/local/include/mylibs - -.PHONY : all -.PHONY : install -.PHONY : main -.PHONY : clean diff --git a/Automatic-Differentiation/README.md b/Automatic-Differentiation/README.md deleted file mode 100755 index 505b8bc913c84db6784aaa433e7725b01107a454..0000000000000000000000000000000000000000 --- a/Automatic-Differentiation/README.md +++ /dev/null @@ -1,15 +0,0 @@ -# Automatic Differentiation - -Very simple automatic differentiation tool, implemented using dual numbers and operator overloading. - -This code is part of my article on **Medium** : **[Automatic Differentiation](https://medium.com/@omaraflak/automatic-differentiation-4d26d03b7508)**. - -# Example - -```c++ -// Dual(value, derivative=0) -Dual x(5, 1); // derivative=1 means we are going to derive with respect to this variable. -Dual y(6); -Dual f = pow(x,2)*y; // the derivative is calculated when the function is computed. -std::cout << f.getDerivative() << std::endl; // get the derivative of y*x^2 with respect to x, evaluated at (x=5,y=6). -``` diff --git a/Automatic-Differentiation/root/Makefile b/Automatic-Differentiation/root/Makefile deleted file mode 100755 index 6789bba6769c2639122ead4536178fbdff067a8d..0000000000000000000000000000000000000000 --- a/Automatic-Differentiation/root/Makefile +++ /dev/null @@ -1,25 +0,0 @@ -CXX = g++ -ODIR = obj -CXXFLAGS = -std=c++14 -OBJS = $(ODIR)/gradient.o $(ODIR)/dual.o $(ODIR)/main.o - -all : $(ODIR) $(OBJS) - -$(ODIR)/gradient.o : src/gradient.cpp include/gradient.h - $(CXX) -c $< -o $@ $(CXXFLAGS) - -$(ODIR)/dual.o : src/dual.cpp include/dual.h - $(CXX) -c $< -o $@ $(CXXFLAGS) - -$(ODIR)/main.o : src/main.cpp include/dual.h include/gradient.h - $(CXX) -c $< -o $@ $(CXXFLAGS) - -$(ODIR) : - if [ ! -d $(ODIR) ]; then mkdir $(ODIR); fi - -clean : - if [ -d $(ODIR) ]; then rm $(ODIR) -r; fi - -.PHONY : all -.PHONY : clean - diff --git a/Automatic-Differentiation/root/include/dual.h b/Automatic-Differentiation/root/include/dual.h deleted file mode 100755 index 75330398e02a8508263b560773441c02231a660b..0000000000000000000000000000000000000000 --- a/Automatic-Differentiation/root/include/dual.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef DUAL -#define DUAL - -#include -#include - -class Dual { - private: - double val; - double der; - - public: - Dual(); - Dual(double val); - Dual(double val, double der); - - void setValue(double value); - void setDerivative(double derivative); - - double getValue() const; - double getDerivative() const; - - // operators - friend Dual operator+(const Dual& u, const Dual& v); - friend Dual operator-(const Dual& u, const Dual& v); - friend Dual operator*(const Dual& u, const Dual& v); - friend Dual operator/(const Dual& u, const Dual& v); - - Dual operator+=(const Dual& u); - Dual operator-=(const Dual& u); - Dual operator*=(const Dual& u); - Dual operator/=(const Dual& u); - - friend std::ostream& operator<<(std::ostream& os, const Dual& a); - - // maths - friend Dual sin(Dual d); - friend Dual cos(Dual d); - friend Dual exp(Dual d); - friend Dual log(Dual d); - friend Dual abs(Dual d); - friend Dual pow(Dual d, double p); -}; - -#endif diff --git a/Automatic-Differentiation/root/include/gradient.h b/Automatic-Differentiation/root/include/gradient.h deleted file mode 100755 index 0d66a65d919ce1fe8f9022ee878e5139bb060464..0000000000000000000000000000000000000000 --- a/Automatic-Differentiation/root/include/gradient.h +++ /dev/null @@ -1,18 +0,0 @@ -#ifndef GRADIENT -#define GRADIENT - -#include "dual.h" -#include - -namespace dual{ - namespace{ - typedef std::vector Vector; - typedef std::vector Matrix; - } - - void resetDerivatives(Vector& input); - Vector gradient(Dual (*f)(Vector&), Vector& input); - Matrix jacobian(Vector (*f)(Vector&), Vector& input); -} - -#endif diff --git a/Automatic-Differentiation/root/src/dual.cpp b/Automatic-Differentiation/root/src/dual.cpp deleted file mode 100755 index 0e7b4da39a6c7f540c7c495997aa7739368ebd8f..0000000000000000000000000000000000000000 --- a/Automatic-Differentiation/root/src/dual.cpp +++ /dev/null @@ -1,100 +0,0 @@ -#include "../include/dual.h" - -Dual::Dual(){ - this->val = 0; - this->der = 0; -} - -Dual::Dual(double val){ - this->val = val; - this->der = 0; -} - -Dual::Dual(double val, double der){ - this->val = val; - this->der = der; -} - -void Dual::setValue(double value){ - this->val = value; -} - -void Dual::setDerivative(double derivative){ - this->der = derivative; -} - -double Dual::getValue() const{ - return val; -} - -double Dual::getDerivative() const{ - return der; -} - -// operators -Dual operator+(const Dual& u, const Dual& v){ - return Dual(u.val+v.val, u.der+v.der); -} - -Dual operator-(const Dual& u, const Dual& v){ - return Dual(u.val-v.val, u.der-v.der); -} - -Dual operator*(const Dual& u, const Dual& v){ - return Dual(u.val*v.val, u.der*v.val+u.val*v.der); -} - -Dual operator/(const Dual& u, const Dual& v){ - return Dual(u.val/v.val, (u.der*v.val-u.val*v.der)/(v.val*v.val)); -} - -Dual Dual::operator+=(const Dual& u){ - *this = *this+u; - return *this; -} - -Dual Dual::operator-=(const Dual& u){ - *this = *this-u; - return *this; -} - -Dual Dual::operator*=(const Dual& u){ - *this = *this*u; - return *this; -} - -Dual Dual::operator/=(const Dual& u){ - *this = *this/u; - return *this; -} - -std::ostream& operator<<(std::ostream& os, const Dual& a){ - os << a.val; - return os; -} - -// maths -Dual sin(Dual d){ - return Dual(::sin(d.val), d.der*::cos(d.val)); -} - -Dual cos(Dual d){ - return Dual(::cos(d.val), -d.der*::sin(d.val)); -} - -Dual exp(Dual d){ - return Dual(::exp(d.val), d.der*::exp(d.val)); -} - -Dual log(Dual d){ - return Dual(::log(d.val), d.der/d.val); -} - -Dual abs(Dual d){ - int sign = d.val==0 ? 0 : d.val/::abs(d.val); - return Dual(::abs(d.val), d.der*sign); -} - -Dual pow(Dual d, double p){ - return Dual(::pow(d.val, p), p*d.der*::pow(d.val, p-1)); -} diff --git a/Automatic-Differentiation/root/src/gradient.cpp b/Automatic-Differentiation/root/src/gradient.cpp deleted file mode 100755 index 75a0ce1796e2586b812953ada123ae599398b924..0000000000000000000000000000000000000000 --- a/Automatic-Differentiation/root/src/gradient.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "../include/gradient.h" - -void dual::resetDerivatives(dual::Vector& input){ - for(auto& e : input){ - e.setDerivative(0); - } -} - -dual::Vector dual::gradient(Dual (*f)(dual::Vector&), dual::Vector& input){ - resetDerivatives(input); - dual::Vector grad(input.size()); - for(size_t i=0 ; i -#include "../include/dual.h" -#include "../include/gradient.h" - -typedef std::vector Vector; - -std::ostream& operator<<(std::ostream& os, const Vector& v){ - os << "["; - for(size_t i=0 ; i