Skip to content

Instantly share code, notes, and snippets.

@danilo-bc
danilo-bc / Compile python em WSL.md
Last active April 17, 2024 13:33
Series of commands to compile a Python version in WSL (2.0), Ubuntu.

Download source from Python.org

https://www.python.org/downloads/source/ e.g., wget https://www.python.org/ftp/python/3.12.3/Python-3.12.3.tgz

Extract

tar -xzf Python-3.12.3.tgz

Install deps

sudo apt install build-essential zlib1g-dev libbz2-dev liblzma-dev libncurses5-dev libreadline6-dev libsqlite3-dev libssl-dev libgdbm-dev liblzma-dev tk-dev lzma lzma-dev libgdbm-dev libgdbm-compat-dev

@danilo-bc
danilo-bc / scientific_time.py
Created April 16, 2024 23:52
Transform a double into scientific time string (ps, ns, ms, etc)
def scientific_time(time_in_seconds):
"""String of time converted to closest scientific representation
Args:
time_in_seconds (double): time in seconds
Returns:
str: Converted time. e.g., 0.005 => 5 ms
"""
powers = [(1e-12, 'ps'), (1e-9, 'ns'), (1e-6, 'μs'), (1e-3, 'ms'), (1, 's'), (60, 'min'), (3600, 'hr'), (86400, 'day')]
using Random
using Statistics: mean
using BenchmarkTools
function calculate_call_price(S, K, r, sigma, T, iterations)
"""
Calculates the price of a vanilla call option using Monte Carlo simulation
Parameters:
S (float): Initial stock price.
@danilo-bc
danilo-bc / fft-review.py
Last active May 29, 2024 13:54
Handy Numpy FFT crash course to revise the main concepts
import numpy as np
from numpy.fft import fft, fftfreq
import matplotlib.pyplot as plt
# Input: Sine wave with 30º offset
# Expected results: discrete impulse with A/2 amplitude at fc
# Phase equal to 90 - phase offset
#
# Phase may be noisy given lower oversampling rate
# Have to adjust the threshold parameter
A = 2
@danilo-bc
danilo-bc / schematic-draw-tools.md
Created June 21, 2022 17:26
List of free schematic/diagram drawing software
@danilo-bc
danilo-bc / rock-paper-scissors.jl
Created July 22, 2021 13:11
Julia multiple dispatch example using Rock, Paper, Scissors!
abstract type Janken end
struct Rock <: Janken end
struct Paper <: Janken end
struct Scissors <: Janken end
janken(j1::Janken, j2::Janken) = println("Same object means a tie :O!")
janken(r::Rock, p::Paper) = println("Rock is wrapped by paper :(")
janken(r::Rock, s::Scissors) = println("Rock crushes scissors!!!")
@danilo-bc
danilo-bc / terminal_color.h
Created July 12, 2021 14:17
Terminal color macros to be used in C/C++ projects. E.g.: cout<<MAGENTA<<"Hello, World"<<RESET<<endl; Not my code, can't remember the proper source.
#ifndef TERMINAL_COLOR_H
#define TERMINAL_COLOR_H
//the following are UBUNTU/LINUX, and MacOS ONLY terminal color codes.
#define RESET "\033[0m"
#define BLACK "\033[30m" /* Black */
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
#define YELLOW "\033[33m" /* Yellow */
#define BLUE "\033[34m" /* Blue */
#define MAGENTA "\033[35m" /* Magenta */
@danilo-bc
danilo-bc / WBG.py
Created July 11, 2021 15:46
Weighted Binary Stochastic Number Generator demo
# Author: Danilo Cavalcanti
# Importing System Modules
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
from bitarray import bitarray
len = 4
lfsrVal = bitarray('1001')
num = bitarray('1011')
@danilo-bc
danilo-bc / SCC.py
Created July 11, 2021 14:29
Calculate Stochastic Computing Correlation
from bitarray import bitarray
# Currently unused in the project
def SCC(X,Y):
'''Calculated Stochastic Circuit Correlation between X and Y'''
# overlapping 1's
a = (X&Y).count()
# overlapping 1's of X and 0's of Y
b = (X&~Y).count()
# overlapping 0's of X and 1's of Y
@danilo-bc
danilo-bc / matrixDiv.py
Created July 6, 2021 19:19
Demo of how to use ray to accelerate Python with multithreading for matrix processing
import numpy as np
'''Auxiliary library to subdivide a matrix in groupings of
3x3 elements to speedup Sobel algorithm simulation'''
def div2(mat):
rows = mat.shape[0]
cols = mat.shape[1]
# Half values truncated for use in range()
half_rows = int(rows/2)
half_cols = int(cols/2)