eckity.base.typed_functions
This module implements functions used in the function nodes of a GP tree. Note: all functions work on numpy arrays.
1""" 2This module implements functions used in the function nodes of a GP tree. 3Note: all functions work on numpy arrays. 4""" 5 6from typing import Any 7import numpy as np 8from .types import t_argmax 9 10 11def add2floats(x: float, y: float) -> float: 12 """x+y""" 13 return np.add(x, y) 14 15 16def sub2floats(x: float, y: float) -> float: 17 """x-y""" 18 return np.subtract(x, y) 19 20 21def mul2floats(x: float, y: float) -> float: 22 """x*y""" 23 return np.multiply(x, y) 24 25 26def div2floats(x: float, y: float) -> float: 27 """protected division: if abs(y) > 0.001 return x/y else return 0""" 28 with np.errstate(divide="ignore", invalid="ignore"): 29 return np.where(np.abs(y) > 0.001, np.divide(x, y), 0.0) 30 31 32def sqrt_float(x: float) -> float: 33 """protected square root: sqrt(abs(x))""" 34 return np.sqrt(np.absolute(x)) 35 36 37def log_float(x: float) -> float: 38 """protected log: if abs(x) > 0.001 return log(abs(x)) else return 0""" 39 with np.errstate(divide="ignore", invalid="ignore"): 40 return np.where(np.abs(x) > 0.001, np.log(np.abs(x)), 0.0) 41 42 43def abs_float(x: float) -> float: 44 """absolute value of x""" 45 return np.absolute(x) 46 47 48def neg_float(x: float) -> float: 49 """negative of x""" 50 return np.negative(x) 51 52 53def inv_float(x: float) -> float: 54 """protected inverse: if abs(x) > 0.001 return 1/x else return 0""" 55 return div2floats(1, x) 56 57 58def max2floats(x: float, y: float) -> float: 59 """maximum(x,y)""" 60 return np.maximum(x, y) 61 62 63def min2floats(x: float, y: float) -> float: 64 """minimum(x,y)""" 65 return np.minimum(x, y) 66 67 68def sin_float(x: float) -> float: 69 """sin(x)""" 70 return np.sin(x) 71 72 73def cos_float(x: float) -> float: 74 """cos(x)""" 75 return np.cos(x) 76 77 78def tan_float(x: float) -> float: 79 """tan(x)""" 80 return np.tan(x) 81 82 83def iflte0_floats(x: float, y: float, z: float) -> float: 84 """if x <= 0 return y else return z""" 85 return np.where(x <= 0, y, z) 86 87 88def ifgt0_floats(x: float, y: float, z: float) -> float: 89 """if x > 0 return y else return z""" 90 return np.where(x > 0, y, z) 91 92 93def iflte_floats(x: float, y: float, z: float, w: float) -> float: 94 """if x <= y return z else return w""" 95 return np.where(x <= y, z, w) 96 97 98def ifgt_floats(x: float, y: float, z: float, w: float) -> float: 99 """if x > y return z else return w""" 100 return np.where(x > y, z, w) 101 102 103def and2floats(x: float, y: float) -> float: 104 """x and y""" 105 return np.logical_and(x, y) 106 107 108def or2floats(x: float, y: float) -> float: 109 """x or y""" 110 return np.logical_or(x, y) 111 112 113def not2floats(x: float) -> float: 114 """not x""" 115 return np.logical_not(x).astype(float) 116 117 118def if_then_else(test: bool, dit: Any, dif: Any) -> float: 119 """if test return dit else return dif""" 120 return np.where(test, dit, dif) 121 122 123def argmax2floats(x0: float, x1: float) -> t_argmax: 124 return np.argmax([x0, x1], axis=0) 125 126 127def and2ints(x: int, y: int) -> int: 128 """x and y""" 129 return np.logical_and(x, y) 130 131 132def or2ints(x: int, y: int) -> int: 133 """x or y""" 134 return np.logical_or(x, y) 135 136 137def not2ints(x: int) -> int: 138 """not x""" 139 return np.logical_not(x).astype(int) 140 141 142def if_then_else3ints(test: int, dit: int, dif: int) -> int: 143 """if test return dit else return dif""" 144 return np.where(test, dit, dif) 145 146 147def and2bools(x: bool, y: bool) -> bool: 148 """x and y""" 149 return np.bitwise_and(x, y) 150 151 152def or2bools(x: bool, y: bool) -> bool: 153 """x or y""" 154 return np.bitwise_or(x, y) 155 156 157def not2bools(x: bool) -> bool: 158 """not x""" 159 return np.logical_not(x) 160 161 162def if_then_else3bools(test: bool, dit: bool, dif: bool) -> bool: 163 """if test return dit else return dif""" 164 return np.where(test, dit, dif) 165 166 167__all__ = [ 168 "add2floats", 169 "sub2floats", 170 "mul2floats", 171 "div2floats", 172 "sqrt_float", 173 "log_float", 174 "abs_float", 175 "neg_float", 176 "inv_float", 177 "max2floats", 178 "min2floats", 179 "sin_float", 180 "cos_float", 181 "tan_float", 182 "iflte0_floats", 183 "ifgt0_floats", 184 "iflte_floats", 185 "ifgt_floats", 186 "and2floats", 187 "or2floats", 188 "not2floats", 189 "if_then_else", 190 "argmax2floats", 191 "and2ints", 192 "or2ints", 193 "not2ints", 194 "if_then_else3ints", 195 "and2bools", 196 "or2bools", 197 "not2bools", 198 "if_then_else3bools", 199]
def
add2floats(x: float, y: float) -> float:
x+y
def
sub2floats(x: float, y: float) -> float:
x-y
def
mul2floats(x: float, y: float) -> float:
x*y
def
div2floats(x: float, y: float) -> float:
27def div2floats(x: float, y: float) -> float: 28 """protected division: if abs(y) > 0.001 return x/y else return 0""" 29 with np.errstate(divide="ignore", invalid="ignore"): 30 return np.where(np.abs(y) > 0.001, np.divide(x, y), 0.0)
protected division: if abs(y) > 0.001 return x/y else return 0
def
sqrt_float(x: float) -> float:
33def sqrt_float(x: float) -> float: 34 """protected square root: sqrt(abs(x))""" 35 return np.sqrt(np.absolute(x))
protected square root: sqrt(abs(x))
def
log_float(x: float) -> float:
38def log_float(x: float) -> float: 39 """protected log: if abs(x) > 0.001 return log(abs(x)) else return 0""" 40 with np.errstate(divide="ignore", invalid="ignore"): 41 return np.where(np.abs(x) > 0.001, np.log(np.abs(x)), 0.0)
protected log: if abs(x) > 0.001 return log(abs(x)) else return 0
def
abs_float(x: float) -> float:
absolute value of x
def
neg_float(x: float) -> float:
negative of x
def
inv_float(x: float) -> float:
54def inv_float(x: float) -> float: 55 """protected inverse: if abs(x) > 0.001 return 1/x else return 0""" 56 return div2floats(1, x)
protected inverse: if abs(x) > 0.001 return 1/x else return 0
def
max2floats(x: float, y: float) -> float:
maximum(x,y)
def
min2floats(x: float, y: float) -> float:
minimum(x,y)
def
sin_float(x: float) -> float:
sin(x)
def
cos_float(x: float) -> float:
cos(x)
def
tan_float(x: float) -> float:
tan(x)
def
iflte0_floats(x: float, y: float, z: float) -> float:
84def iflte0_floats(x: float, y: float, z: float) -> float: 85 """if x <= 0 return y else return z""" 86 return np.where(x <= 0, y, z)
if x <= 0 return y else return z
def
ifgt0_floats(x: float, y: float, z: float) -> float:
89def ifgt0_floats(x: float, y: float, z: float) -> float: 90 """if x > 0 return y else return z""" 91 return np.where(x > 0, y, z)
if x > 0 return y else return z
def
iflte_floats(x: float, y: float, z: float, w: float) -> float:
94def iflte_floats(x: float, y: float, z: float, w: float) -> float: 95 """if x <= y return z else return w""" 96 return np.where(x <= y, z, w)
if x <= y return z else return w
def
ifgt_floats(x: float, y: float, z: float, w: float) -> float:
99def ifgt_floats(x: float, y: float, z: float, w: float) -> float: 100 """if x > y return z else return w""" 101 return np.where(x > y, z, w)
if x > y return z else return w
def
and2floats(x: float, y: float) -> float:
x and y
def
or2floats(x: float, y: float) -> float:
x or y
def
not2floats(x: float) -> float:
not x
def
if_then_else(test: bool, dit: Any, dif: Any) -> float:
119def if_then_else(test: bool, dit: Any, dif: Any) -> float: 120 """if test return dit else return dif""" 121 return np.where(test, dit, dif)
if test return dit else return dif
def
argmax2floats(x0: float, x1: float) -> eckity.base.types.argmax:
def
and2ints(x: int, y: int) -> int:
x and y
def
or2ints(x: int, y: int) -> int:
x or y
def
not2ints(x: int) -> int:
not x
def
if_then_else3ints(test: int, dit: int, dif: int) -> int:
143def if_then_else3ints(test: int, dit: int, dif: int) -> int: 144 """if test return dit else return dif""" 145 return np.where(test, dit, dif)
if test return dit else return dif
def
and2bools(x: bool, y: bool) -> bool:
x and y
def
or2bools(x: bool, y: bool) -> bool:
x or y
def
not2bools(x: bool) -> bool:
not x
def
if_then_else3bools(test: bool, dit: bool, dif: bool) -> bool:
163def if_then_else3bools(test: bool, dit: bool, dif: bool) -> bool: 164 """if test return dit else return dif""" 165 return np.where(test, dit, dif)
if test return dit else return dif