Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tclements
tclements / mult.jl
Created October 7, 2020 02:33
Compare 2x2 matrix multiplication methods in Julia
using BenchmarkTools. LinearAlgebra
function naive_mul!(C,A,B)
C[1,1] = A[1,1] * B[1,1] + A[1,2] * B[2,1]
C[1,2] = A[1,1] * B[1,2] + A[1,2] * B[2,2]
C[2,1] = A[2,1] * B[1,1] + A[2,2] * B[2,1]
C[2,2] = A[2,1] * B[1,2] + A[2,2] * B[2,2]
return nothing
end
function strassen_mul!(C,A,B)
@tclements
tclements / hilbert.jl
Created May 31, 2019 21:52
Hilbert transform on the GPU with Julia
using GPUArrays, FFTW
"""
hilbert(x)
Computes the analytic representation of x, ``x_a = x + j
\\hat{x}``, where ``\\hat{x}`` is the Hilbert transform of x,
along the first dimension of x.
"""
function hilbert(x::GPUArray{T}) where T<:Real
N = size(x, 1)
@tclements
tclements / sliding_window.py
Created March 14, 2019 22:14
Create a Sliding Window function (with steps) using NumPy.
# Create a function to reshape a 1d array using a sliding window with a step.
# NOTE: The function uses numpy's internat as_strided function because looping in python is slow in comparison.
# Adopted from http://www.rigtorp.se/2011/01/01/rolling-statistics-numpy.html and
# https://gist.github.com/codehacken/708f19ae746784cef6e68b037af65788
import numpy as np
# Reshape a numpy array 'a' of shape (x) to form shape((n - window_size) // step + 1, window_size))
def rolling_window(a, window, step):
shape = a.shape[:-1] + ((a.shape[-1] - window + 1)//step, window)
@tclements
tclements / GPUArray-complex-transcendental.jl
Created February 14, 2019 19:34
sin, cos, exp with complex numbers on GPU with Julia
using GPUArrays
using CuArrays
import Base.sin, Base.cos, Base.exp
function sin(A::GPUArray{ComplexF64})
return sin.(real(A)) .* cosh.(imag(A)) .+ im .* cos.(real(A)) .* sinh.(imag(A))
end
function cos(A::GPUArray{ComplexF64})
return cos.(real(A)) .* cosh.(imag(A)) .- im .* sin.(real(A)) .* sinh.(imag(A))