Skip to content

Instantly share code, notes, and snippets.

View khaotik's full-sized avatar
💫
gradient descending

khaotik

💫
gradient descending
View GitHub Profile
@khaotik
khaotik / minsecp256k1.py
Created June 11, 2021 14:49
minimal python implementation of secp256k1 curve
#!/usr/bin/env python
'''
minimal reference implementation of bitcoin's secp256k1 with general prime field
requires pari && cypari2 for certain operations.
'''
class EcPoint:
__slots__ = ('x', 'y')
def __init__(self, x, y):
@khaotik
khaotik / memfd_create.py
Created May 2, 2021 14:38
Simple memfd_create wrapper in python
class memfd_create:
'''create in memory file with actual fileno using memfd_create
usage:
with memfd_create('file_name') as f:
pass # treat f as file-like in binary mode
'''
import ctypes, os
_libc = ctypes.CDLL(None)
_syscall = _libc.syscall
def __init__(self, name:str):
@khaotik
khaotik / tarfile-min-example.py
Created May 2, 2021 11:53
Minimal python tarfile module usage
#!/usr/bin/env python
import io
import tarfile
a_content = '''
content:aaa
content:111
'''
b_content = '''
content:bbb
content:222
@khaotik
khaotik / accum.py
Created January 26, 2019 11:14
binary carry accumulator
# binary carry accumulator with O(log(N)) space requirement
# better numerical stability when adding lots of small numbers
class Accumulator:
__slots__ = ('fn', 'data', 'index', 'init')
def __init__(self, size=31, init=0., fn='add'):
if fn == 'mean':
fn=lambda x,wx,y,wy:(x*wx+y*wy)/(wx+wy)
elif fn == 'add':
fn=lambda x,wx,y,wy:x+y
self.fn = fn
@khaotik
khaotik / ezumount.py
Created January 8, 2019 05:01
ezumount - easily unmount and power off plugged USB disks from linux CLI
#!/usr/bin/env python3
import os
import sys
import subprocess as subp
if len(sys.argv) != 2:
appname = sys.argv[0]
print('%s - unmount USB directory and power it off' % appname, file=sys.stderr)
print('Usage: %s <directory>' % appname, file=sys.stderr)
sys.exit(-1)
@khaotik
khaotik / method_1.cpp
Last active September 5, 2018 07:19
Receive packed array via WSTP
#include <stdlib.h>
#include <vector>
#include <string>
#include "wstp.h"
using namespace std;
int main() {
int err;
auto env_p = WSInitialize((void*)0);
@khaotik
khaotik / test_roll.py
Created March 14, 2017 10:49
roll using generalized elemwise op
from random import randint
from time import time
import numpy as np
import theano as th
import theano.tensor as T
from theano.tensor.padding import idx, at_idx
N = 256
x = T.matrix()
@khaotik
khaotik / test_circconv.py
Created March 14, 2017 10:19
circular convolution with generalized elemwise op
from itertools import product
from random import randint
from time import time
import numpy as np
import theano as th
T = th.tensor
from theano.tensor.padding import idx, at_idx
from theano.tensor.signal import conv
N = 256
@khaotik
khaotik / th_scan_profiling_1.md
Last active November 30, 2016 12:01
Theano scan investigation 1

The snippet for scan test is:

x = T.matrix()
y, _ = th.scan(fn=lambda x : T.sum(x*x), sequences=x)
fn_sum_scan = theano.function([x], y)
xval = np.random.randn(5000,10000).astype(np.float32)
timeit(fn(xval))
@khaotik
khaotik / scan_test.py
Last active November 29, 2016 17:08
for investigating theano scan overhead
from __future__ import print_function
from time import time
import theano as th
import theano.tensor as T
import numpy as np
USE_PROFILER = True
if not USE_PROFILER: