Skip to content

Instantly share code, notes, and snippets.

@ryotako
Created April 14, 2019 13:12
Show Gist options
  • Save ryotako/c9c0b771eab4094c344cce0dded916c8 to your computer and use it in GitHub Desktop.
Save ryotako/c9c0b771eab4094c344cce0dded916c8 to your computer and use it in GitHub Desktop.
行列積&非線形変換による何か
def setup():
size(*[360]*2)
global A, B, C, h
h = 180
A = [[random(-1, 1) for _ in range(3)] for _ in range(3)]
B = [[random(-1,1) for _ in range(3)] for _ in range(3)]
C = [[random(-1,1) for _ in range(3)] for _ in range(3)]
def draw():
# pixelsの各値をgetValue()関数で決める
loadPixels()
for i in range(width):
for j in range(height):
a, b, c = getValue([[i*0.005, j*0.005, frameCount*0.002]], A, B, C)[0]
pixels[i+j*width] = color((h+120*a)%360, 50+50*b, 50+50*c)
updatePixels()
saveFrame("frames/####.png")
print frameCount
if frameCount == 100:
noLoop()
# 行列積
def prod(A, B):
r = len(A)
c = len(B[0])
d = len(A[0])
m = [[0 for j in range(c)] for i in range(r)]
for i in range(r):
for j in range(c):
for k in range(d):
m[i][j] += A[i][k]*B[k][j]
return m
# 行列の各要素に関数を適用
def mmap(f, A):
return [[f(A[i][j]) for j in range(len(A[0]))] for i in range(len(A))]
# 非線形関数(Activation関数って呼ぶらしい)
def act(x):
return (sin(TWO_PI*sin(x*2))+1)/2.
# 入力vと、パラメータとなる行列A, B, Cから出力を得る
def getValue(v ,A, B, C):
return mmap(act, prod(mmap(act, prod(mmap(act, prod(v, A)), B)), C))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment