Skip to content

Instantly share code, notes, and snippets.

#!/usr/bin/env python
"""Observe the stdout redirected from sub thread to main thread through shared StringIO instance"""
import time
import contextlib
from concurrent.futures import ThreadPoolExecutor
from io import StringIO
import streamlit as st
@wrist
wrist / goertzel.py
Created October 22, 2019 12:33
goertzel.py
"""goertzel algorithm python implementation
https://en.wikipedia.org/wiki/Goertzel_algorithm
"""
import numpy as np
import matplotlib.pyplot as plt
def goertzel(xs, f, fs, N):
df = fs / N
k = int(f/df)
@wrist
wrist / power_method.py
Created October 20, 2019 10:38
power_method.py
"""べき乗則を用いて複素行列の最大固有値を求める"""
import numpy as np
import matplotlib.pyplot as plt
seed=1
ndim = 10
np.random.seed=seed
A = np.random.randn(ndim,ndim)+1.j*np.random.randn(ndim,ndim)
x = np.random.randn(ndim) + 1.j*np.random.randn(ndim)
niter = 300
main = do
contents <- getContents
print (sum $ map (length . (filter ('@' ==))) $ lines $ contents)
@wrist
wrist / golf.hs
Created August 5, 2015 11:30
improved (wc -c 112 -> 64)
main = do
c <- getContents
print (length $ filter ('@' ==) c)
@wrist
wrist / golf.hs
Created August 5, 2015 11:54
wc -cで46文字
main=getContents>>=print.length.filter('@'==)
@wrist
wrist / monadic.md
Created June 17, 2015 12:05
sugoih chapter14

14章 便利なモナディック関数特集

モナディック関数

  • モナド値を操作したりモナド値を返したりする関数
  • ここではliftM, join, filterM, foldMを扱う

liftMと愉快な仲間たち

ファンクター、アプリカティブファンクター、モナドの関係

@wrist
wrist / foldm_writer.hs
Created June 17, 2015 12:16
chapter14_problem1
-- Writerを返す2引数関数を用いたfoldMによる整数の畳込みを行うために、9より大きい数が表れた場合は9より小さい数が表れた場合とは異なるログを残すような関数を実装せよ
import Control.Monad.Writer
binSmallsWriter :: Int -> Int -> Writer [String] Int
binSmallsWriter acc x
| x > 9 = do
tell [show x ++ " too big, not added"]
return acc
| otherwise = do
@wrist
wrist / either_bird.hs
Created May 13, 2015 11:55
sugoi haskell chapter14 problem
import Control.Monad()
type Birds = Int
type Pole = (Birds, Birds)
landLeft :: Birds -> Pole -> Either String Pole
landLeft n (left, right)
| abs ((left + n) - right) < 4 = Right (left + n, right)
| otherwise = Left ("Last state: " ++ show (left + n, right))
@wrist
wrist / audioread_test.py
Created February 11, 2015 05:42
audioreadのテスト
#!/usr/bin/env python
# vim:fileencoding=utf8
import numpy as np
import matplotlib.pyplot as plt
import audioread as ar
def pcm2float(short_ndary):
float_ndary = np.array(short_ndary, dtype=np.float64)
return np.where(float_ndary > 0.0, float_ndary / 32767.0, float_ndary / 32768.0)