eckity.base.untyped_functions

This module implements functions used in the function (internal) nodes of a GP tree. Note: all functions work on numpy arrays.

  1"""
  2This module implements functions used in the function (internal) nodes of a GP tree.
  3Note: all functions work on numpy arrays.
  4"""
  5
  6import numpy as np
  7
  8
  9def f_add(x, y):
 10    """x+y"""
 11    return np.add(x, y)
 12
 13
 14def f_sub(x, y):
 15    """x-y"""
 16    return np.subtract(x, y)
 17
 18
 19def f_mul(x, y):
 20    """x*y"""
 21    return np.multiply(x, y)
 22
 23
 24def f_div(x, y):
 25    """protected division: if abs(y) > 0.001 return x/y else return 0"""
 26    with np.errstate(divide="ignore", invalid="ignore"):
 27        return np.where(np.abs(y) > 0.001, np.divide(x, y), 0.0)
 28
 29
 30def f_sqrt(x):
 31    """protected square root: sqrt(abs(x))"""
 32    return np.sqrt(np.absolute(x))
 33
 34
 35def f_log(x):
 36    """protected log: if abs(x) > 0.001 return log(abs(x)) else return 0"""
 37    with np.errstate(divide="ignore", invalid="ignore"):
 38        return np.where(np.abs(x) > 0.001, np.log(np.abs(x)), 0.0)
 39
 40
 41def f_abs(x):
 42    """absolute value of x"""
 43    return np.absolute(x)
 44
 45
 46def f_neg(x):
 47    """negative of x"""
 48    return np.negative(x)
 49
 50
 51def f_inv(x):
 52    """protected inverse: if abs(x) > 0.001 return 1/x else return 0"""
 53    return f_div(1, x)
 54
 55
 56def f_max(x, y):
 57    """maximum(x,y)"""
 58    return np.maximum(x, y)
 59
 60
 61def f_min(x, y):
 62    """minimum(x,y)"""
 63    return np.minimum(x, y)
 64
 65
 66def f_sin(x):
 67    """sin(x)"""
 68    return np.sin(x)
 69
 70
 71def f_cos(x):
 72    """cos(x)"""
 73    return np.cos(x)
 74
 75
 76def f_tan(x):
 77    """tan(x)"""
 78    return np.tan(x)
 79
 80
 81def f_iflte0(x, y, z):
 82    """if x <= 0 return y else return z"""
 83    return np.where(x <= 0, y, z)
 84
 85
 86def f_ifgt0(x, y, z):
 87    """if x > 0 return y else return z"""
 88    return np.where(x > 0, y, z)
 89
 90
 91def f_iflte(x, y, z, w):
 92    """if x <= y return z else return w"""
 93    return np.where(x <= y, z, w)
 94
 95
 96def f_ifgt(x, y, z, w):
 97    """if x > y return z else return w"""
 98    return np.where(x > y, z, w)
 99
100
101def f_and(x, y):
102    """x and y"""
103    return np.bitwise_and(x, y)
104
105
106def f_or(x, y):
107    """x or y"""
108    return np.bitwise_or(x, y)
109
110
111def f_not(x):
112    """not x"""
113    return np.logical_not(x).astype(int)
114
115
116def f_if_then_else(test, dit, dif):
117    """if test return dit else return dif"""
118    return np.where(test, dit, dif)
119
120
121__all__ = [
122    "f_add",
123    "f_sub",
124    "f_mul",
125    "f_div",
126    "f_sqrt",
127    "f_log",
128    "f_abs",
129    "f_neg",
130    "f_inv",
131    "f_max",
132    "f_min",
133    "f_sin",
134    "f_cos",
135    "f_tan",
136    "f_iflte0",
137    "f_ifgt0",
138    "f_iflte",
139    "f_ifgt",
140    "f_and",
141    "f_or",
142    "f_not",
143    "f_if_then_else",
144]
def f_add(x, y):
10def f_add(x, y):
11    """x+y"""
12    return np.add(x, y)

x+y

def f_sub(x, y):
15def f_sub(x, y):
16    """x-y"""
17    return np.subtract(x, y)

x-y

def f_mul(x, y):
20def f_mul(x, y):
21    """x*y"""
22    return np.multiply(x, y)

x*y

def f_div(x, y):
25def f_div(x, y):
26    """protected division: if abs(y) > 0.001 return x/y else return 0"""
27    with np.errstate(divide="ignore", invalid="ignore"):
28        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 f_sqrt(x):
31def f_sqrt(x):
32    """protected square root: sqrt(abs(x))"""
33    return np.sqrt(np.absolute(x))

protected square root: sqrt(abs(x))

def f_log(x):
36def f_log(x):
37    """protected log: if abs(x) > 0.001 return log(abs(x)) else return 0"""
38    with np.errstate(divide="ignore", invalid="ignore"):
39        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 f_abs(x):
42def f_abs(x):
43    """absolute value of x"""
44    return np.absolute(x)

absolute value of x

def f_neg(x):
47def f_neg(x):
48    """negative of x"""
49    return np.negative(x)

negative of x

def f_inv(x):
52def f_inv(x):
53    """protected inverse: if abs(x) > 0.001 return 1/x else return 0"""
54    return f_div(1, x)

protected inverse: if abs(x) > 0.001 return 1/x else return 0

def f_max(x, y):
57def f_max(x, y):
58    """maximum(x,y)"""
59    return np.maximum(x, y)

maximum(x,y)

def f_min(x, y):
62def f_min(x, y):
63    """minimum(x,y)"""
64    return np.minimum(x, y)

minimum(x,y)

def f_sin(x):
67def f_sin(x):
68    """sin(x)"""
69    return np.sin(x)

sin(x)

def f_cos(x):
72def f_cos(x):
73    """cos(x)"""
74    return np.cos(x)

cos(x)

def f_tan(x):
77def f_tan(x):
78    """tan(x)"""
79    return np.tan(x)

tan(x)

def f_iflte0(x, y, z):
82def f_iflte0(x, y, z):
83    """if x <= 0 return y else return z"""
84    return np.where(x <= 0, y, z)

if x <= 0 return y else return z

def f_ifgt0(x, y, z):
87def f_ifgt0(x, y, z):
88    """if x > 0 return y else return z"""
89    return np.where(x > 0, y, z)

if x > 0 return y else return z

def f_iflte(x, y, z, w):
92def f_iflte(x, y, z, w):
93    """if x <= y return z else return w"""
94    return np.where(x <= y, z, w)

if x <= y return z else return w

def f_ifgt(x, y, z, w):
97def f_ifgt(x, y, z, w):
98    """if x > y return z else return w"""
99    return np.where(x > y, z, w)

if x > y return z else return w

def f_and(x, y):
102def f_and(x, y):
103    """x and y"""
104    return np.bitwise_and(x, y)

x and y

def f_or(x, y):
107def f_or(x, y):
108    """x or y"""
109    return np.bitwise_or(x, y)

x or y

def f_not(x):
112def f_not(x):
113    """not x"""
114    return np.logical_not(x).astype(int)

not x

def f_if_then_else(test, dit, dif):
117def f_if_then_else(test, dit, dif):
118    """if test return dit else return dif"""
119    return np.where(test, dit, dif)

if test return dit else return dif