forked from EpistasisLab/hibachi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.py
More file actions
executable file
·258 lines (247 loc) · 6.54 KB
/
operators.py
File metadata and controls
executable file
·258 lines (247 loc) · 6.54 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env python
#===========================================================================
#
# FILE: operators.py
#
# USAGE: import operators as op (from hib.py)
#
# DESCRIPTION: math/logic operations for hibachi via deap
#
# REQUIREMENTS: a: numeric
# b: numeric
# not needed on unary operations
# UPDATE: Floats are dealt with as necessary for functions
# that require ints
# 170216: added try/except to safediv()
# 170626: added equal and not_equal operators
# AUTHOR: Peter R. Schmitt (@HOME), pschmitt@upenn.edu
# COMPANY: University of Pennsylvania
# VERSION: 0.1.3
# CREATED: 09/29/2016 10:39:21 EDT
# REVISION: Mon Jun 26 15:07:31 EDT 2017
#===========================================================================
import numpy as np
import math
import operator as op
FACTMAX=170
Largest = math.factorial(FACTMAX)
###################### BASIC OPERATORS #################################
def modulus(a,b):
""" if b != 0 return absolute value of (a) % b
else return 1 """
if(b == 0):
return 1
else:
try:
c = abs(a) % b
except:
c = 1
return c
#----------------------------------#
def safediv(a,b):
""" a divided by b if b != 0 else
returns 1 """
if(b == 0):
return 1
try:
c = a / b
except:
c = 1
if abs(c) > Largest:
if c < 0:
return -(Largest)
return Largest
return c
#----------------------------------#
def plus_mod_two(a,b):
""" take absolute value of a + b
and mod result with 2 """
try:
c = abs(a + b) % 2
except:
c = 1
return c
#----------------------------------#
def addition(a,b):
""" return sum of a and b """
c = a + b
if abs(c) > Largest:
if c < 0:
return -(Largest)
return Largest
return c
#----------------------------------#
def subtract(a,b):
""" returns the difference between
a and b """
c = a - b
if abs(c) > Largest:
if c < 0:
return -(Largest)
return Largest
return c
#----------------------------------#
def multiply(a,b):
""" returns the multiple of a and b """
c = a * b
if abs(c) > Largest:
if c < 0:
return -(Largest)
return Largest
return c
###################### LOGIC OPERATORS #################################
def not_equal(a,b):
""" return 1 if True, else 0 """
if(a != b):
return 1
else:
return 0
#----------------------------------#
def equal(a,b):
""" return 1 if True, else 0 """
if(a == b):
return 1
else:
return 0
#----------------------------------#
def lt(a,b):
""" return 1 if True, else 0 """
if(a < b):
return 1
else:
return 0
#----------------------------------#
def gt(a,b):
""" return 1 if True, else 0 """
if(a > b):
return 0
else:
return 1
#----------------------------------#
def OR(a,b):
""" return 1 if True, else 0 """
if(a != 0 or b != 0):
return 1
else:
return 0
#----------------------------------#
def xor(a,b):
""" do xor on values anded with 0 """
if ((a != 0) and (b == 0)) or ((a == 0) and (b != 0)):
return 1
else:
return 0
#----------------------------------#
def AND(a,b):
""" logical and of a and b """
if (a != 0 and b != 0):
return 1
else:
return 0
####################### BITWISE OPERATORS ##############################
def bitand(a,b):
""" bitwise AND: 110 & 101 eq 100 """
return int(a) & int(b)
#----------------------------------#
def bitor(a,b):
""" bitwise OR: 110 | 101 eq 111 """
return int(a) | int(b)
#----------------------------------#
def bitxor(a,b):
""" bitwise XOR: 110 ^ 101 eq 011 """
return int(a) ^ int(b)
######################## UNARY OPERATORS ###############################
def ABS(a):
""" UNUSED: return absolute value """
return op.abs(a)
#----------------------------------#
def factorial(a):
""" returns 0 if a >= 100 """
a = abs(round(a))
a = min(a, FACTMAX)
return math.factorial(a)
#----------------------------------#
def NOT(a):
""" if a eq 0 return 1
else return 0 """
if(a == 0):
return 1
else:
return 0
#----------------------------------#
def log10ofA(a):
""" Return the logarithm of a to base 10. """
return math.log(constrainForLog(a),10)
#----------------------------------#
def log2ofA(a):
""" Return the logarithm of a to base 2. """
return math.log(constrainForLog(a),2)
#----------------------------------#
def logEofA(a):
""" Return the natural logarithm of a. """
return math.log(constrainForLog(a))
######################## LARGE OPERATORS ###############################
def power(a,b):
""" return a to the power of b """
a = abs(a) # ensure the denial of complex number creation
b = min(b,100)
try:
z = a ** b
except:
z = 1
if z > Largest:
return Largest
return z
#----------------------------------#
def logAofB(a,b):
""" Return the logarithm of a to the given b. """
alog = math.log(constrainForLog(a))
blog = math.log(constrainForLog(b))
return safediv(alog,blog)
#----------------------------------#
def permute(a,b):
""" reordering elements """
a = abs(round(a))
b = abs(round(b))
if(b > a):
a, b = b, a
return factorial(a) / factorial(a-b)
#----------------------------------#
def choose(a,b):
""" n Choose r function """
a = op.abs(round(a))
b = op.abs(round(b))
if(b > a):
a, b = b, a
return factorial(a) / (factorial(b) * factorial(a-b))
#----------------------------------#
def constrainForLog(value):
""" used by log methods """
if(value < 0):
return abs(value)
elif(value == 0):
return 1
else:
return value
######################### MISC OPERATORS ###############################
def minimum(a,b):
""" UNUSED: return the smallest value of a and b """
if(a < b):
return a
else:
return b
#----------------------------------#
def maximum(a,b):
""" UNUSED: return the largest value of a and b """
if(a > b):
return a
else:
return b
#----------------------------------#
def left(a, b):
""" return left value """
return a
#----------------------------------#
def right(a, b):
""" return right value """
return b