This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeightFuncs.jl
125 lines (97 loc) · 2.54 KB
/
WeightFuncs.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""
Define weighting functions here
Should be a function of the radius dr
"""
__precompile__()
module WeightFuncs
using Distributions
export WeightFunc, setAddDist!, setMultDist!, defaultIsingWF, altRWF, altCrossWF, randWF
# Assuming one method,
# Returns symbols of argnames
function func_argnames(f::Function)
ml = collect(methods(f))
return Base.method_argnames(last(ml))[2:end]
end
mutable struct WeightFunc
f::Function
NN::Integer
periodic::Bool
invoke::Function
addTrue::Bool
multTrue::Bool
addDist::Any
multDist::Any
function WeightFunc(func::Function, ; NN::Integer)
invoke(dr,i,j) = func(;dr,i,j)
return new(func,Int8(NN), true, invoke, false, false)
end
end
# Add an additive distribution
function setAddDist!(weightFunc, dist, func= (;dr,i,j,_...) -> 1)
weightFunc.addTrue = true
weightFunc.addDist = (;dr,i,j) -> func(;dr,i,j)*dist
if !weightFunc.multTrue
inv = (dr,i,j) -> rand(weightFunc.addDist(;dr,i,j)) + weightFunc.f(;dr,i,j)
else
inv = (dr,i,j) -> rand(weightFunc.multDist(;dr,i,j))*weightFunc.f(;dr,i,j)+rand(weightFunc.addDist(;dr,i,j))
end
weightFunc.invoke = inv
end
# Add an additive distribution
function setMultDist!(weightFunc, dist, func= (;dr,i,j,_...) -> 1)
weightFunc.multTrue = true
weightFunc.multDist = (;dr,i,j) -> func(;dr,i,j)*dist
if !weightFunc.addTrue
inv = (dr,i,j) -> rand(weightFunc.multDist(;dr,i,j))*weightFunc.f(;dr,i,j)
else
inv = (dr,i,j) -> rand(weightFunc.multDist(;dr,i,j))*weightFunc.f(;dr,i,j)+rand(weightFunc.addDist(;dr,i,j))
end
weightFunc.invoke = inv
end
# Default ising Function
defaultIsingWF = WeightFunc(
(;dr, _...) -> dr == 1 ? 1. : 0.,
NN = 1
)
altRWF = WeightFunc(
(;dr, _...) ->
if dr % 2 == 1
return -1.0*1/dr^2
elseif dr % 2 == 0
return 1*1/dr^2
else
return -1/dr^2
end,
NN = 2
)
altCrossWF = WeightFunc(
(;dr, _...) ->
if dr % 2 == 1
return -1
elseif dr % 2 == 0
return 1
else
return 0
end,
NN = 2
)
randWF = WeightFunc(
(;dr, _...) ->
if dist == nothing
return rand()
else
return rand(dist)
end,
NN = 1
)
""" Old """
# Use distribution centered around zero
function randomizeWeights(dr, func , dist)::Float32
weight = func(dr)
if weight == 0
return 0.
else
return weight + rand(dist)
end
end
end