-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithmeticFunction.py
More file actions
117 lines (91 loc) · 4 KB
/
Copy patharithmeticFunction.py
File metadata and controls
117 lines (91 loc) · 4 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
# Arithmetic function primitives used by the GP framework.
# This module defines simple function wrappers for addition, subtraction, and multiplication.
from __future__ import annotations
import numpy as np
from typing import Callable
class Function:
def __init__(self, formula: Callable, name: str):
self.formula = formula
self.name = name
def __call__(self, x, y):
return self.formula(x, y)
# --- BINÁRNÍ FUNKCE ---
@staticmethod
def addFunction(): return Function(lambda x, y: x + y, "+")
@staticmethod
def subFunction(): return Function(lambda x, y: x - y, "-")
@staticmethod
def multFunction(): return Function(lambda x, y: x * y, "*")
@staticmethod
def divFunction():
return Function(lambda x, y: np.where(np.abs(y) < 1e-9, 1.0, x / y), "/")
@staticmethod
def powFunction():
return Function(lambda x, y: np.where(x < 0, np.nan, np.clip(x**y, -1e10, 1e10)), "^")
# --- UNÁRNÍ FUNKCE ---
@staticmethod
def sinFunction(): return Function(lambda x, y: np.sin(x), "sin")
@staticmethod
def cosFunction(): return Function(lambda x, y: np.cos(x), "cos")
@staticmethod
def tanFunction(): return Function(lambda x, y: np.tan(x), "tan")
@staticmethod
def expFunction():
return Function(lambda x, y: np.exp(np.clip(x, -100, 100)), "exp")
@staticmethod
def logFunction():
return Function(lambda x, y: np.where(np.abs(x) < 1e-9, 0.0, np.log(np.abs(x))), "log")
@staticmethod
def sqrtFunction(): return Function(lambda x, y: np.sqrt(np.abs(x)), "sqrt")
@staticmethod
def absFunction(): return Function(lambda x, y: np.abs(x), "abs")
@staticmethod
def asinFunction():
return Function(lambda x, y: np.arcsin(np.clip(x, -1, 1)), "asin")
@staticmethod
def acosFunction():
return Function(lambda x, y: np.arccos(np.clip(x, -1, 1)), "acos")
@staticmethod
def atanFunction(): return Function(lambda x, y: np.arctan(x), "atan")
@staticmethod
def sinhFunction():
return Function(lambda x, y: np.sinh(np.clip(x, -50, 50)), "sinh")
@staticmethod
def coshFunction():
return Function(lambda x, y: np.cosh(np.clip(x, -50, 50)), "cosh")
@staticmethod
def tanhFunction(): return Function(lambda x, y: np.tanh(x), "tanh")
# --- STATICKÁ MAPA (Inicializuje se pouze jednou při importu třídy) ---
_registry = {}
@classmethod
def decoder(cls, name: str) -> "Function":
"""
Maximálně efektivní vyhledávání v předpřipravené mapě bez alokace nové paměti.
"""
clean_name = name.strip().lower()
if clean_name in cls._registry:
return cls._registry[clean_name]
raise ValueError(f"Funkce s názvem '{name}' není v setu SRBench podporována.")
# Naplnění registru jednorázově hned pod třídou
Function._registry = {
# Binární
"+": Function.addFunction(), "add": Function.addFunction(),
"-": Function.subFunction(), "sub": Function.subFunction(), "subb": Function.subFunction(),
"*": Function.multFunction(), "mul": Function.multFunction(), "mult": Function.multFunction(),
"/": Function.divFunction(), "div": Function.divFunction(),
"^": Function.powFunction(), "**": Function.powFunction(), "pow": Function.powFunction(),
# Unární
"sin": Function.sinFunction(),
"cos": Function.cosFunction(),
"tan": Function.tanFunction(),
"exp": Function.expFunction(),
"log": Function.logFunction(), "ln": Function.logFunction(),
"sqrt": Function.sqrtFunction(),
"abs": Function.absFunction(),
"asin": Function.asinFunction(), "arcsin": Function.asinFunction(),
"acos": Function.acosFunction(), "arccos": Function.acosFunction(),
"atan": Function.atanFunction(), "arctan": Function.atanFunction(),
"sinh": Function.sinhFunction(),
"cosh": Function.coshFunction(),
"tanh": Function.tanhFunction(),
}