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

feat: Enable casting to native types and obtaining backend-native functions #135

Merged
merged 4 commits into from
Oct 24, 2024

Conversation

dexter2206
Copy link
Contributor

Description

While working on several features, we noticed that there are some limitations to the backend interface, namely:

  1. It is not possible to obtain native Python objects from expressions. There is a serialize method that always returns string, and there is value_of method that either returns a number OR None if it is not possible to convert an expression to number. There is no single function that would convert expressions representing numbers to numbers, and other expressions to strings.
  2. While manipulating symbolic expressions is easy through arithmetic operators, there's not really a way for applying function to symbolic expressions. The current solution involves going through backend.as_expression, e.g.: backend.as_expression(f"sin({backend.serialize(some_expr)})") which a) is not very readable , b) incurs a performance penalty if some_expr is long and complicated.

This PR solves both of this issues by defining backend.as_native(expr) and backend.func(func_name) methods.

The backend.func can be used as follows:

x = .... # Assume we have some expression for x
sin = backend.func("sin")
result = sin(x). # This is of type TExpr[T] (for backend specialized to T

The name func_name was chosen instead of e.g. backend.function so that we have more concise notation while still maintining a reasonable level of readability.

Please verify that you have completed the following steps

  • I have self-reviewed my code.
  • I have included test cases validating introduced feature/fix.
  • I have updated documentation.

Copy link
Contributor

@mstechly mstechly left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!

@dexter2206 dexter2206 merged commit 11628ff into main Oct 24, 2024
7 checks passed
@dexter2206 dexter2206 deleted the Implement-to_native-method branch October 24, 2024 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

2 participants