Skip to content

Instantly share code, notes, and snippets.

View ahmadyan's full-sized avatar

Adel Ahmadyan ahmadyan

View GitHub Profile
@ahmadyan
ahmadyan / bfs_deque.cpp
Created December 13, 2016 00:04
BFS Implementation with std::dequeu
int bfs_deque(Graph* g){
int sig=0;
vector<bool> visited(g->getSize(), false);
deque<int> q;
auto init = rand()%g->getSize();
q.push_back(init);
visited[init]=true;
while(!q.empty()){
auto u = q.front();
@ahmadyan
ahmadyan / BFS_deque_vector.cpp
Last active December 13, 2016 00:06
BFS implementation with std::vector and std::deque
// This is the optimal implementation of BFS with deque
int bfs_deque(Graph* g){
int sig=0;
vector<bool> visited(g->getSize(), false);
deque<int> q;
auto init = rand()%g->getSize();
q.push_back(init);
visited[init]=true;
while(!q.empty()){
auto u = q.front();
@ahmadyan
ahmadyan / bfs_vector_no_ordering.cpp
Last active December 13, 2016 00:09
Fastest implementation of BFS with vector (no ordering)
// Fastest implementation of the BFS algorithm with std::vector, if we don't care about the ordering
int bfs_vector_no_order(Graph* g){
int sig=0;
vector<bool> visited(g->getSize(), false);
vector<int> q;
q.reserve(10);
auto init = rand()%g->getSize();
q.push_back(init);
visited[init]=true;
while(!q.empty()){
@ahmadyan
ahmadyan / bfs_implementations.cpp
Created December 13, 2016 00:10
Different implementations of the BFS algorithm
#include <iostream>
#include <vector>
#include <random>
#include <list>
#include <array>
#include <queue>
#include <deque>
using namespace std;
class Graph{
@ahmadyan
ahmadyan / bfs_benchmark.cpp
Created December 13, 2016 00:11
Benchmark of different BFS implementations
#include <benchmark/benchmark.h>
#include <algorithm>
#include <iostream>
#include <vector>
#include <random>
#include <list>
#include <array>
#include <queue>
#include <deque>
@ahmadyan
ahmadyan / simple_bundle_adjuster.cc
Created May 8, 2017 23:54
Test for computing the cost from residuals
// Ceres Solver - A fast non-linear least squares minimizer
// Copyright 2015 Google Inc. All rights reserved.
// http://ceres-solver.org/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice,
@ahmadyan
ahmadyan / jet.cpp
Created November 12, 2018 04:13
Jet Operator Overloading
Jet operator*(Jet& f, Jet& g) {
return Jet(f.a * g.a, // The scalar part
f.a * g.v + f.v * g.a // The infinitesimal vector part
);
}
@ahmadyan
ahmadyan / Rosenbrock.cpp
Created November 12, 2018 04:14
Rosenbrock function example for auto-differentiation
// Rosenbrock function, a = 1.0, b = 100
class Rosenbrock {
public:
template <typename T>
bool operator()(const T *const x, const T *const y, T *cost) const {
cost[0] = (T(1.0) - x[0]) * (T(1.0) - x[0]) +
T(100.0) * (y[0] - x[0] * x[0]) * (y[0] - x[0] * x[0]);
return true;
}
};
@ahmadyan
ahmadyan / Rosenbrock.py
Created November 12, 2018 04:15
Rosenbrock function in Python
def rosenbrock(x):
return 100*(x[1] - x[0]**2)**2 + (1 - x[0])**2
@ahmadyan
ahmadyan / RosenbrockJet.cpp
Created November 12, 2018 04:16
Auto diff for Rosenbrock function using Ceres' Jets
auto rosenbrockCostFunction = new ceres::AutoDiffCostFunction<Rosenbrock, 1, 1, 1>(new Rosenbrock());
constexpr double x = 1, y = 3;
parameters[0][0] = x;
parameters[1][0] = y;
rosenbrockCostFunction->Evaluate(parameters, &residuals, jacobians);
cout << residuals << " df/dx=" << jacobians[0][0] << " df/dy" << jacobians[1][0] << "\n";