Skip to content

Instantly share code, notes, and snippets.

@jshahbazi
jshahbazi / fortran_convolution.f90
Last active September 24, 2023 13:44
A simple convolution of two 1D vectors (signals) done in Fortran
function convolve(x, h)
implicit none
!x is the signal array
!h is the noise/impulse array
real, dimension(:), allocatable :: convolve, y
real, dimension(:) :: x, h
integer :: kernelsize, datasize
integer :: i,j,k
@jshahbazi
jshahbazi / djb_hash.f90
Last active April 3, 2020 19:27
Daniel J. Bernstein's hash algorithm written in Fortran
integer function djb_hash(str) result(hash)
implicit none
character(len=*),intent(in) :: str
integer :: hash
integer :: i
hash = 5381
do i=1,len(str)
@jshahbazi
jshahbazi / fmincg.f90
Created May 31, 2014 22:17
fmincg minimization function ported to Fortran
real function fmincg(length, nn_params, input_layer_size, hidden_layer_size, num_labels, inputdata, y, lambda)
implicit none
! Copyright (C) 2001 and 2002 by Carl Edward Rasmussen. Date 2002-02-13
! (C) Copyright 1999, 2000 & 2001, Carl Edward Rasmussen
!
! Permission is granted for anyone to copy, use, or modify these
! programs and accompanying documents for purposes of research or
! education, provided this copyright notice is retained, and note is
! made of any changes that have been made.
@jshahbazi
jshahbazi / mkl_matrix_multiply.f90
Last active January 17, 2017 03:20
Wrapper routine for using the Intel MKL xGEMM matrix multiplication routines
subroutine matrix_multiply(first_matrix, first_transposed, second_matrix, second_transposed, result_matrix)
!This subroutine multiplies two matrices using the Intel MKL xGEMM multiplication routines
!The routines can be a little tricky, so I've made it a little easier to use them consistently
!first_matrix and second_matrix are the input matrices
!first_transposed and second_transposed are integers indicating (with a 1 or 0) whether or not
!the respective matrix is transposed
!The resulting matrix is stored in result_matrix, but you'll have to allocate that outside the routine
!The sGEMM below can be changed to dGEMM for double precision without any other changes to the
!subroutine call. You'll then have to change the 'real' variables to 'double precision'.
@jshahbazi
jshahbazi / async_token_bucket.py
Last active January 16, 2021 15:29
Async Token Bucket implementation
class AsyncTokenBucket(object):
"""Implementation of an awaitable token bucket that defaults to a single token at a time.
Based on https://code.activestate.com/recipes/511490-implementation-of-the-token-bucket-algorithm/"""
def __init__(self, max_tokens, fill_rate):
self.max_tokens = float(max_tokens)
self.current_tokens = float(max_tokens)
self.fill_rate = float(fill_rate)
self.wait_time = (1.0 / self.fill_rate)
self.timestamp = time.time()