Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed some query methods #1981

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions symengine/cwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ using SymEngine::ComplexDouble;
using SymEngine::CSRMatrix;
using SymEngine::DenseMatrix;
using SymEngine::down_cast;
using SymEngine::E;
using SymEngine::function_symbol;
using SymEngine::FunctionSymbol;
using SymEngine::has_symbol;
Expand Down Expand Up @@ -286,6 +287,33 @@ int basic_has_symbol(const basic e, const basic s)
return (int)(has_symbol(*(e->m), *(s->m)));
}

int basic_is_Add(const basic s)
{
return basic_get_type(s) == SYMENGINE_ADD;
}

int basic_is_Mul(const basic s)
{
return basic_get_type(s) == SYMENGINE_MUL;
}

int basic_is_Pow(const basic s)
{
return basic_get_type(s) == SYMENGINE_POW;
}

int basic_is_Log(const basic s)
{
return basic_get_type(s) == SYMENGINE_LOG;
}

int basic_is_Exp(const basic s)
{
SYMENGINE_ASSERT(basic_is_Pow(s) == 1);
auto args = s->m->get_args();
return args[1] == E;
}

CWRAPPER_OUTPUT_TYPE integer_set_si(basic s, long i)
{
CWRAPPER_BEGIN
Expand Down
10 changes: 10 additions & 0 deletions symengine/cwrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ int number_is_complex(const basic s);

//! Returns 1 if `e` contains the symbol `s`; 0 otherwise
int basic_has_symbol(const basic e, const basic s);
//! Returns 1 if `s` is of type Add; 0 otherwise
int basic_is_Add(const basic s);
//! Returns 1 if `s` is of type Mul; 0 otherwise
int basic_is_Mul(const basic s);
//! Returns 1 if `s` is of type Pow; 0 otherwise
int basic_is_Pow(const basic s);
//! Returns 1 if `s` is of type Log; 0 otherwise
int basic_is_Log(const basic s);
//! Returns 1 if `s` is of type Exp; 0 otherwise
int basic_is_Exp(const basic s);

//! Assign to s, a long.
CWRAPPER_OUTPUT_TYPE integer_set_si(basic s, long i);
Expand Down
5 changes: 5 additions & 0 deletions symengine/tests/cwrapper/test_cwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ void test_cwrapper()
SYMENGINE_C_ASSERT(basic_has_symbol(e, y) == 0);
SYMENGINE_C_ASSERT(basic_has_symbol(e, z) == 0);
basic_add(e, e, x);
SYMENGINE_C_ASSERT(basic_is_Add(e) == 1);
SYMENGINE_C_ASSERT(basic_has_symbol(e, x) == 1);
SYMENGINE_C_ASSERT(basic_has_symbol(e, y) == 0);
SYMENGINE_C_ASSERT(basic_has_symbol(e, z) == 0);
basic_mul(e, e, y);
SYMENGINE_C_ASSERT(basic_is_Mul(e) == 1);
SYMENGINE_C_ASSERT(basic_has_symbol(e, x) == 1);
SYMENGINE_C_ASSERT(basic_has_symbol(e, y) == 1);
SYMENGINE_C_ASSERT(basic_has_symbol(e, z) == 0);
Expand Down Expand Up @@ -102,7 +104,9 @@ void test_cwrapper()

integer_set_ui(e, 123);
basic_sqrt(e, e);
SYMENGINE_C_ASSERT(basic_is_Pow(e) == 1);
basic_exp(e, e);
SYMENGINE_C_ASSERT(basic_is_Exp(e) == 1);

s = basic_str(e);
SYMENGINE_C_ASSERT(strcmp(s, "exp(sqrt(123))") == 0);
Expand Down Expand Up @@ -1389,6 +1393,7 @@ void test_functions()

integer_set_ui(res, 2);
basic_log(res, res);
SYMENGINE_C_ASSERT(basic_is_Log(res) == 1);
SYMENGINE_C_ASSERT(basic_eq(res, ans));

real_double_set_d(res, 1.1);
Expand Down