Skip to content

Instantly share code, notes, and snippets.

View antoinelejay's full-sized avatar

Antoine Lejay antoinelejay

View GitHub Profile
@antoinelejay
antoinelejay / truncate_sympy_polynomial.jl
Created January 6, 2023 11:29
Truncate a polynomial expression up to a given order using SymPy in Julia
using SymPy
# Keep only terms of a given orders in a polynomial expression
function truncate_upto(ex::Sym, x::Sym, order::Int)
if ex.func.__name__ != "Add"
error("The main expression should be an addition")
end
return ex.func(ex.args[findall(y->degree(y,x) ≤ order,ex.args)]...)
end
@antoinelejay
antoinelejay / gist:b8d388de2430983ab28fa3427fb199aa
Created August 25, 2016 16:43
Reading the result of an external command as a table in R
# textConnection creates a connection from a string
a<-read.table(textConnection(system2("ls",stdout=T)))
# to_a transform each key=>value of the hash as [key,value].
# to_h reverts back [key,value] as key=>value.
hash=hash.to_a.map{|a,b| [a.to_sym,b]}.to_h
@antoinelejay
antoinelejay / gist:28f7ac883ca159f84f3c58aa164fb61c
Created April 12, 2016 10:46
Get the lastest commit hash in Git of the current C File being executed
char commit[256]; // to store the commit hash
char call_git[512]; // to store the command to be executed
sprintf(call_git,"git log -1 --pretty=format:'%%H' %s",__FILE__);
FILE *fp = popen(call_git, "r");
fscanf(fp, "%s", commit)
pclose(fp);
@antoinelejay
antoinelejay / gist:925840657275543a3c48
Created February 20, 2016 10:41
Custom assert in C
/* Assert is a nice way to avoid bugs
* but sometimes one needs to differentiate the reason for calling assert.
* This macro seems to do the job (using the stringification).
*/
#define assert_initialize(EX,MSG,CODE) if(!(EX)) {fprintf(stderr,"%s:%i Initialization error (" #EX "): " MSG "\nAborting :-(\n",__FILE__,__LINE__);exit(CODE);};
@antoinelejay
antoinelejay / gsl_sf_erf_Z_safe.c
Last active February 18, 2016 09:37
Avoid throwing errors when using special functions in the Gnu Scientific Library
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_sf_result.h> // for avoiding underflows and overflows
#include <gsl/gsl_sf_erf.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>