Skip to content

Commit 26ef5a6

Browse files
committed
test: verify log works in guesses without safe_ prefix
1 parent 65b7128 commit 26ef5a6

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

pysr/test/test_main.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,26 @@ def test_guesses_use_zero_based_indexing(self):
10051005
model_wrong.fit(X, y)
10061006
self.assertGreater(model_wrong.equations_.iloc[-1]["loss"], 1.0)
10071007

1008+
def test_unary_operators_in_guesses(self):
1009+
# Test that unary operators (like log) can be used in guesses
1010+
X = np.abs(self.rstate.randn(100, 2)) + 1 # Ensure positive for log
1011+
y = np.log(X[:, 0]) + 2.5 * X[:, 1]
1012+
1013+
# Test that log operator is parsed and used correctly
1014+
model = PySRRegressor(
1015+
binary_operators=["+", "*"],
1016+
unary_operators=["log"],
1017+
guesses=["log(x0) + 1.0 * x1"], # Uses log operator (wrong constant)
1018+
niterations=0, # MUST use 0 to test the guess itself
1019+
progress=False,
1020+
temp_equation_file=False,
1021+
)
1022+
model.fit(X, y)
1023+
# With niterations=0, constants still get optimized, so loss should be near-zero
1024+
self.assertLess(model.equations_.iloc[-1]["loss"], 1e-10)
1025+
# Verify log is in the equation
1026+
self.assertIn("log", str(model.equations_.iloc[-1]["sympy_format"]))
1027+
10081028

10091029
def manually_create_model(equations, feature_names=None):
10101030
if feature_names is None:

0 commit comments

Comments
 (0)