Skip to content

Commit 3ed3098

Browse files
authored
Merge pull request #2395 from anutosh491/sinQ
Added query method for sin class
2 parents 09cf294 + d8a26e2 commit 3ed3098

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

integration_tests/symbolics_06.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,18 @@ def test_elementary_functions():
2828
# test composite functions
2929
a: S = exp(x)
3030
b: S = sin(a)
31+
b1: bool = b.func == sin
3132
c: S = cos(b)
3233
d: S = log(c)
3334
d1: bool = d.func == log
3435
e: S = Abs(d)
3536
print(e)
37+
assert(b1 == True)
38+
if b.func == sin:
39+
assert True
40+
else:
41+
assert False
42+
assert(b.func == sin)
3643
assert(d1 == True)
3744
if d.func == log:
3845
assert True

src/libasr/pass/intrinsic_function_registry.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ enum class IntrinsicScalarFunctions : int64_t {
8484
SymbolicMulQ,
8585
SymbolicPowQ,
8686
SymbolicLogQ,
87+
SymbolicSinQ,
8788
// ...
8889
};
8990

@@ -150,6 +151,7 @@ inline std::string get_intrinsic_name(int x) {
150151
INTRINSIC_NAME_CASE(SymbolicMulQ)
151152
INTRINSIC_NAME_CASE(SymbolicPowQ)
152153
INTRINSIC_NAME_CASE(SymbolicLogQ)
154+
INTRINSIC_NAME_CASE(SymbolicSinQ)
153155
default : {
154156
throw LCompilersException("pickle: intrinsic_id not implemented");
155157
}
@@ -3155,6 +3157,7 @@ create_symbolic_query_macro(SymbolicAddQ)
31553157
create_symbolic_query_macro(SymbolicMulQ)
31563158
create_symbolic_query_macro(SymbolicPowQ)
31573159
create_symbolic_query_macro(SymbolicLogQ)
3160+
create_symbolic_query_macro(SymbolicSinQ)
31583161

31593162

31603163
#define create_symbolic_unary_macro(X) \
@@ -3320,6 +3323,8 @@ namespace IntrinsicScalarFunctionRegistry {
33203323
{nullptr, &SymbolicPowQ::verify_args}},
33213324
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicLogQ),
33223325
{nullptr, &SymbolicLogQ::verify_args}},
3326+
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicSinQ),
3327+
{nullptr, &SymbolicSinQ::verify_args}},
33233328
};
33243329

33253330
static const std::map<int64_t, std::string>& intrinsic_function_id_to_name = {
@@ -3434,6 +3439,8 @@ namespace IntrinsicScalarFunctionRegistry {
34343439
"SymbolicPowQ"},
34353440
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicLogQ),
34363441
"SymbolicLogQ"},
3442+
{static_cast<int64_t>(IntrinsicScalarFunctions::SymbolicSinQ),
3443+
"SymbolicSinQ"},
34373444
};
34383445

34393446

@@ -3494,6 +3501,7 @@ namespace IntrinsicScalarFunctionRegistry {
34943501
{"MulQ", {&SymbolicMulQ::create_SymbolicMulQ, &SymbolicMulQ::eval_SymbolicMulQ}},
34953502
{"PowQ", {&SymbolicPowQ::create_SymbolicPowQ, &SymbolicPowQ::eval_SymbolicPowQ}},
34963503
{"LogQ", {&SymbolicLogQ::create_SymbolicLogQ, &SymbolicLogQ::eval_SymbolicLogQ}},
3504+
{"SinQ", {&SymbolicSinQ::create_SymbolicSinQ, &SymbolicSinQ::eval_SymbolicSinQ}},
34973505
};
34983506

34993507
static inline bool is_intrinsic_function(const std::string& name) {

src/libasr/pass/replace_symbolic.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,24 @@ class ReplaceSymbolicVisitor : public PassUtils::PassVisitor<ReplaceSymbolicVisi
951951
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)), nullptr));
952952
break;
953953
}
954+
case LCompilers::ASRUtils::IntrinsicScalarFunctions::SymbolicSinQ: {
955+
ASR::symbol_t* basic_get_type_sym = declare_basic_get_type_function(al, loc, module_scope);
956+
ASR::expr_t* value1 = handle_argument(al, loc, intrinsic_func->m_args[0]);
957+
Vec<ASR::call_arg_t> call_args;
958+
call_args.reserve(al, 1);
959+
ASR::call_arg_t call_arg;
960+
call_arg.loc = loc;
961+
call_arg.m_value = value1;
962+
call_args.push_back(al, call_arg);
963+
ASR::expr_t* function_call = ASRUtils::EXPR(ASRUtils::make_FunctionCall_t_util(al, loc,
964+
basic_get_type_sym, basic_get_type_sym, call_args.p, call_args.n,
965+
ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4)), nullptr, nullptr));
966+
// Using 35 as the right value of the IntegerCompare node as it represents SYMENGINE_SIN through SYMENGINE_ENUM
967+
return ASRUtils::EXPR(ASR::make_IntegerCompare_t(al, loc, function_call, ASR::cmpopType::Eq,
968+
ASRUtils::EXPR(ASR::make_IntegerConstant_t(al, loc, 35, ASRUtils::TYPE(ASR::make_Integer_t(al, loc, 4)))),
969+
ASRUtils::TYPE(ASR::make_Logical_t(al, loc, 4)), nullptr));
970+
break;
971+
}
954972
default: {
955973
throw LCompilersException("IntrinsicFunction: `"
956974
+ ASRUtils::get_intrinsic_name(intrinsic_id)

src/lpython/semantics/python_ast_to_asr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6055,6 +6055,9 @@ class BodyVisitor : public CommonVisitor<BodyVisitor> {
60556055
} else if (symbolic_type == "log") {
60566056
tmp = attr_handler.eval_symbolic_is_log(se, al, x.base.base.loc, args, diag);
60576057
return;
6058+
} else if (symbolic_type == "sin") {
6059+
tmp = attr_handler.eval_symbolic_is_sin(se, al, x.base.base.loc, args, diag);
6060+
return;
60586061
} else {
60596062
throw SemanticError(symbolic_type + " symbolic type not supported yet", x.base.base.loc);
60606063
}

src/lpython/semantics/python_attribute_eval.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,20 @@ struct AttributeHandler {
513513
{ throw SemanticError(msg, loc); });
514514
}
515515

516+
static ASR::asr_t* eval_symbolic_is_sin(ASR::expr_t *s, Allocator &al, const Location &loc,
517+
Vec<ASR::expr_t*> &args, diag::Diagnostics &/*diag*/) {
518+
Vec<ASR::expr_t*> args_with_list;
519+
args_with_list.reserve(al, args.size() + 1);
520+
args_with_list.push_back(al, s);
521+
for(size_t i = 0; i < args.size(); i++) {
522+
args_with_list.push_back(al, args[i]);
523+
}
524+
ASRUtils::create_intrinsic_function create_function =
525+
ASRUtils::IntrinsicScalarFunctionRegistry::get_create_function("SinQ");
526+
return create_function(al, loc, args_with_list, [&](const std::string &msg, const Location &loc)
527+
{ throw SemanticError(msg, loc); });
528+
}
529+
516530
}; // AttributeHandler
517531

518532
} // namespace LCompilers::LPython

0 commit comments

Comments
 (0)