From 4f65128b3d153e089189ee8f53652f9b98b36425 Mon Sep 17 00:00:00 2001 From: metab0t Date: Fri, 22 Nov 2024 20:51:23 +0800 Subject: [PATCH] Add log10 --- include/pyoptinterface/nlexpr.hpp | 1 + lib/cppad_interface.cpp | 3 +++ lib/nlexpr_ext.cpp | 3 ++- src/pyoptinterface/_src/function_tracing.py | 1 + src/pyoptinterface/nlfunc.py | 1 + tests/test_nlp.py | 1 + 6 files changed, 9 insertions(+), 1 deletion(-) diff --git a/include/pyoptinterface/nlexpr.hpp b/include/pyoptinterface/nlexpr.hpp index f42cee3..6c42a2b 100644 --- a/include/pyoptinterface/nlexpr.hpp +++ b/include/pyoptinterface/nlexpr.hpp @@ -59,6 +59,7 @@ enum class UnaryOperator Sqrt, Exp, Log, + Log10 }; enum class BinaryOperator diff --git a/lib/cppad_interface.cpp b/lib/cppad_interface.cpp index a288058..003f34d 100644 --- a/lib/cppad_interface.cpp +++ b/lib/cppad_interface.cpp @@ -242,6 +242,9 @@ CppAD::AD cppad_build_unary_expression(UnaryOperator op, const CppAD::AD case UnaryOperator::Log: { return CppAD::log(operand); } + case UnaryOperator::Log10: { + return CppAD::log10(operand); + } default: { throw std::runtime_error("Invalid unary operator"); } diff --git a/lib/nlexpr_ext.cpp b/lib/nlexpr_ext.cpp index a7ae942..7022b1c 100644 --- a/lib/nlexpr_ext.cpp +++ b/lib/nlexpr_ext.cpp @@ -39,7 +39,8 @@ NB_MODULE(nlexpr_ext, m) .value("Abs", UnaryOperator::Abs) .value("Sqrt", UnaryOperator::Sqrt) .value("Exp", UnaryOperator::Exp) - .value("Log", UnaryOperator::Log); + .value("Log", UnaryOperator::Log) + .value("Log10", UnaryOperator::Log10); nb::enum_(m, "BinaryOperator") .value("Sub", BinaryOperator::Sub) diff --git a/src/pyoptinterface/_src/function_tracing.py b/src/pyoptinterface/_src/function_tracing.py index 39030d4..d45b7c4 100644 --- a/src/pyoptinterface/_src/function_tracing.py +++ b/src/pyoptinterface/_src/function_tracing.py @@ -166,6 +166,7 @@ def f(expr): sqrt = unary_mathematical_function(math.sqrt, UnaryOperator.Sqrt) exp = unary_mathematical_function(math.exp, UnaryOperator.Exp) log = unary_mathematical_function(math.log, UnaryOperator.Log) +log10 = unary_mathematical_function(math.log10, UnaryOperator.Log10) # Implement binary mathematical functions diff --git a/src/pyoptinterface/nlfunc.py b/src/pyoptinterface/nlfunc.py index 572f356..07fea50 100644 --- a/src/pyoptinterface/nlfunc.py +++ b/src/pyoptinterface/nlfunc.py @@ -11,6 +11,7 @@ sqrt, exp, log, + log10, pow, ifelse, ) diff --git a/tests/test_nlp.py b/tests/test_nlp.py index 1408d47..0319f00 100644 --- a/tests/test_nlp.py +++ b/tests/test_nlp.py @@ -44,6 +44,7 @@ def con(vars): nlfunc.cos, nlfunc.exp, nlfunc.log, + nlfunc.log10, nlfunc.pow, nlfunc.sin, nlfunc.sqrt,