Skip to content

Instantly share code, notes, and snippets.

View jmbr's full-sized avatar

Juan M. Bello-Rivas jmbr

View GitHub Profile
/*
* Example: spy("foobar.ppm", A);
*/
int spy(std::string const& ppmfile, arma::mat const& M) {
ofstream ofs(ppmfile);
if (!ofs.is_open())
return -1;
ofs << "P3\n" << M.n_rows << " " << M.n_cols << "\n1\n";
@jmbr
jmbr / gist:667599
Created November 8, 2010 11:18
Plotting a coarse-grained model of (part of) a protein with PLPlot. This library apparently doesn't feature interactive rotation capabilities (version 5.9.7).
/* gcc -Wall -std=c99 plplotline3.c -o plplotline3 -lplplotd */
#include <stddef.h>
#include <plplot/plplot.h>
int main(void)
{
const size_t num_atoms = 10;
const double positions[][3] = {{ 13.935, 18.529, 29.843 }, { 13.088, 19.661, 26.283 }, { 12.726, 17.033, 23.612 }, { 12.179, 17.659, 19.887 }, { 10.253, 15.79, 17.221 }, { 11.082, 16.103, 13.475 }, { 8.009, 15.163, 11.389 }, { 8.628, 13.975, 7.913 }, { 5.213, 12.642, 6.966 }, { 3.589, 12.601, 3.497 }};
@jmbr
jmbr / bitset.c
Created November 8, 2010 11:23
Bit set data structure (C99)
/**
* @file bitset.c
* @brief Implementation of a bit set data structure.
*/
#include <stdlib.h>
#include <assert.h>
#include "bitset.h"
@jmbr
jmbr / cross_product_and_triple_scalar_product.c
Created November 8, 2010 18:53
Cross product and triple scalar product using GSL.
#include <gsl/gsl_vector.h>
#include <gsl/gsl_blas.h>
void cross_product(const gsl_vector *u, const gsl_vector *v, gsl_vector *product)
{
double p1 = gsl_vector_get(u, 1)*gsl_vector_get(v, 2)
- gsl_vector_get(u, 2)*gsl_vector_get(v, 1);
double p2 = gsl_vector_get(u, 2)*gsl_vector_get(v, 0)
- gsl_vector_get(u, 0)*gsl_vector_get(v, 2);
@jmbr
jmbr / test_stack_allocation_of_vectors.c
Created November 17, 2010 07:34
Use of stack allocated vectors with the GNU Scientific Library.
/*
Compile with:
gcc -Wall -std=c99 test_stack_allocation_of_vectors.c -o test_stack_allocation_of_vectors `gsl-config --libs`
*/
#include <stdio.h>
#include <stddef.h>
#include <string.h>
#include <assert.h>
@jmbr
jmbr / pdb2xyz.cpp
Created March 13, 2012 06:44
PDB to XYZ translator using ESBTL
#include <cstdlib>
#include <vector>
#include <iostream>
#include <ESBTL/default.h>
#include <ESBTL/selected_atom_iterator.h>
#include <ESBTL/atom_selectors.h>
@jmbr
jmbr / gist:3090021
Created July 11, 2012 12:19
Next power of two
#include <cmath>
inline unsigned next_power_of_two(unsigned x) {
return unsigned(exp2f(ceilf(log2f(float(x)))));
}
@jmbr
jmbr / abort_unless.h
Created August 30, 2013 17:56
Unlike the assert() macro, abort_unless() is not disabled by defining the preprocessor macro NDEBUG.
#define abort_unless(expr) do { \
if (!(expr)) { \
fprintf(stderr, "%s:%u (%s): Assertion `%s' failed.\n", \
__FILE__, __LINE__, __func__, __STRING(expr)); \
fflush(stderr); \
abort(); \
} \
} while (0)
@jmbr
jmbr / linspace.cpp
Created April 13, 2012 09:00
MATLAB's linspace in C++
template <typename T = double>
vector<T> linspace(T a, T b, size_t N) {
T h = (b - a) / static_cast<T>(N-1);
vector<T> xs(N);
typename vector<T>::iterator x;
T val;
for (x = xs.begin(), val = a; x != xs.end(); ++x, val += h)
*x = val;
return xs;
}
.text
.align 4,0x90
.globl __Z21matrix_vector_productPKdPd
__Z21matrix_vector_productPKdPd:
LFB18:
movsd (%rdi), %xmm0
movsd 8(%rdi), %xmm4
movapd %xmm0, %xmm5
movsd 16(%rdi), %xmm3
mulsd %xmm0, %xmm5