Skip to content

Instantly share code, notes, and snippets.

View gdetor's full-sized avatar

Georgios Is. Detorakis gdetor

View GitHub Profile
@gdetor
gdetor / som.py
Created January 4, 2024 02:59
Self-organizing map online algorithm
import numpy as np
import matplotlib.pylab as plt
import torch
from torch import nn
from sklearn.metrics.pairwise import euclidean_distances
from som_measures import topographic_error, quantization_error
@gdetor
gdetor / minimal_ev_algo.py
Last active July 25, 2023 21:16
A minimal evolutionary computing algorithm
import numpy as np
import matplotlib.pylab as plt
def f(x):
return 50 - x**2
if __name__ == '__main__':
np.random.seed(12345)
@gdetor
gdetor / generic_fun.c
Created February 7, 2023 00:42
Create generic type functions in C
#include <stdio.h>
#define GENERIC_MAX( type ) \
type type##_MAX(type a, type b) \
{ \
return (a > b) ? a : b; \
}
@gdetor
gdetor / sym2actual.c
Created February 7, 2023 00:40
Converts a symbolic path to an absolute one (C, bash)
#include <stdio.h>
#include <stdlib.h>
#define PATH_MAX 3000
/* Compile with -D_BSD_SOURCE */
/* sym2actual converts a path to an absolute one
*
* Args:
* symlinkpath (pointer to char): The input path string
*
@gdetor
gdetor / viewbin.c
Created February 7, 2023 00:38
View the content of a binary file
#include <stdio.h>
#include <stdlib.h>
void print_buffer(void *, int);
void catbin(char *);
int main(int argc, char **argv) {
if (argc == 2){ viewbin(argv[1]); }
else{
printf("Please type the name of the binary input file!\n");
printf("Example: viewbin foo.dat \n");
@gdetor
gdetor / close_raii.c
Created February 7, 2023 00:33
RAII in C (close a file)
#include <stdio.h>
/* #include <stdlib.h> */
static inline void fclosep(FILE **fp) { if (*fp) fclose(*fp); *fp=NULL; }
#define _cleanup_fclose_ __attribute__((cleanup(fclosep)))
void example_usage(void)
{
_cleanup_fclose_ FILE *logfile = fopen("logfile.txt", "w+");
fputs("hello logfile!", logfile);
@gdetor
gdetor / closure.c
Created February 7, 2023 00:32
Closure in C
#include <stdio.h>
typedef struct TClosure {
int (*code)(struct TClosure *env, int);
int state;
} Closure;
int call(Closure *c, int x) {
return c->code(c, x);
@gdetor
gdetor / c_script.sh
Created February 7, 2023 00:28
C might be a scripting language
#!/usr/bin/bash
tail -n+3 "$0" | gcc -xc - && ./a.out ; exit
#include <stdio.h>
int main()
{
printf("C is a scripting language!\n");
}
@gdetor
gdetor / array_alignment.c
Created February 7, 2023 00:26
Example of POSIX memalign in C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define SIZE 1000000
typedef struct {
float *x;
} test_s;
@gdetor
gdetor / wclatex.py
Created February 7, 2023 00:05
Words counting for LaTeX files (Python, bash)
#!/usr/bin/python
# A minimal script that counts the number of words in a LaTeX file.
# Example of use:
# localhost@name$ wclatex filename
import re
import sys
def word_counter(filename):