-
Notifications
You must be signed in to change notification settings - Fork 38
Feature/qqbar direct backend #413
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
Open
Madhu18S
wants to merge
4
commits into
flintlib:main
Choose a base branch
from
Madhu18S:feature/qqbar-direct-backend
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
2971ad0
backend: integrate initial calcium qqbar infrastructure and arithmeti…
0fe76b4
feat: implement qqbar string formatting via POSIX stdout capture and …
edb2242
style: fix trailing whitespaces and newlines for cython-lint compliance
4d3b2af
refactor: replace low-level os pipe capture with cross-platform io.St…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,3 +20,4 @@ MANIFEST | |
| *.DS_Store | ||
| .venv | ||
| .hypothesis | ||
| venv/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| from flint.flintlib.types.flint cimport slong | ||
| from flint.flintlib.functions.fmpz cimport fmpz_t | ||
| from flint.flintlib.functions.fmpq cimport fmpq_t | ||
|
|
||
| cdef extern from "flint/qqbar.h" nogil: | ||
| ctypedef struct qqbar_struct: | ||
| pass | ||
| ctypedef qqbar_struct qqbar_t[1] | ||
|
|
||
| # Lifecycle methods | ||
| void qqbar_init(qqbar_t res) | ||
| void qqbar_clear(qqbar_t res) | ||
| void qqbar_set(qqbar_t res, const qqbar_t x) | ||
| void qqbar_set_si(qqbar_t res, slong x) | ||
|
|
||
| # Printing methods | ||
| void qqbar_print(const qqbar_t x) | ||
| void qqbar_printn(const qqbar_t x, slong n) | ||
|
|
||
| # Interoperability setters | ||
| void qqbar_set_fmpz(qqbar_t res, const fmpz_t x) | ||
| void qqbar_set_fmpq(qqbar_t res, const fmpq_t x) | ||
|
|
||
| # Arithmetic operations | ||
| void qqbar_add(qqbar_t res, const qqbar_t x, const qqbar_t y) | ||
| void qqbar_sub(qqbar_t res, const qqbar_t x, const qqbar_t y) | ||
| void qqbar_mul(qqbar_t res, const qqbar_t x, const qqbar_t y) | ||
| void qqbar_div(qqbar_t res, const qqbar_t x, const qqbar_t y) | ||
|
|
||
| from flint.flint_base.flint_base cimport flint_scalar | ||
|
|
||
| cdef class qqbar(flint_scalar): | ||
| cdef qqbar_t val |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| from flint.flintlib.functions.qqbar cimport ( | ||
| qqbar_init, | ||
| qqbar_clear, | ||
| qqbar_set, | ||
| qqbar_set_si, | ||
| qqbar_printn, | ||
| qqbar_set_fmpz, | ||
| qqbar_set_fmpq, | ||
| qqbar_add, | ||
| qqbar_sub, | ||
| qqbar_mul, | ||
| qqbar_div | ||
| ) | ||
| from flint.types.fmpz cimport fmpz | ||
| from flint.types.fmpq cimport fmpq | ||
|
|
||
| cdef class qqbar(flint_scalar): | ||
| """ | ||
| The qqbar class represents algebraic numbers. | ||
| """ | ||
|
|
||
| def __cinit__(self): | ||
| qqbar_init(self.val) | ||
|
|
||
| def __dealloc__(self): | ||
| qqbar_clear(self.val) | ||
|
|
||
| def __init__(self, val=None): | ||
| if val is not None: | ||
| if isinstance(val, qqbar): | ||
| qqbar_set(self.val, (<qqbar>val).val) | ||
| elif isinstance(val, int): | ||
| qqbar_set_si(self.val, val) | ||
| elif isinstance(val, fmpz): | ||
| qqbar_set_fmpz(self.val, (<fmpz>val).val) | ||
| elif isinstance(val, fmpq): | ||
| qqbar_set_fmpq(self.val, (<fmpq>val).val) | ||
| else: | ||
| raise TypeError("Cannot initialize qqbar type from input") | ||
|
|
||
| def str(self, Py_ssize_t digits=15): | ||
| """ | ||
| Converts the algebraic number to a decimal string representation by capturing C-level stdout. | ||
| """ | ||
| import os | ||
| import sys | ||
|
|
||
| cdef int stdout_fd = 1 | ||
| cdef int saved_stdout = os.dup(stdout_fd) | ||
|
|
||
| pipe_read, pipe_write = os.pipe() | ||
| os.dup2(pipe_write, stdout_fd) | ||
|
|
||
| try: | ||
| qqbar_printn(self.val, digits) | ||
| sys.stdout.flush() | ||
| finally: | ||
| os.close(pipe_write) | ||
| os.dup2(saved_stdout, stdout_fd) | ||
| os.close(saved_stdout) | ||
|
|
||
| cdef bytes captured = os.read(pipe_read, 4096) | ||
| os.close(pipe_read) | ||
|
|
||
| return captured.decode('utf-8').strip() | ||
|
|
||
| def __str__(self): | ||
| return self.str() | ||
|
|
||
| def __repr__(self): | ||
| return f"qqbar({self.str()})" | ||
|
|
||
| def __add__(x, y): | ||
| cdef qqbar res = qqbar() | ||
| cdef qqbar c_x = x if isinstance(x, qqbar) else qqbar(x) | ||
| cdef qqbar c_y = y if isinstance(y, qqbar) else qqbar(y) | ||
| qqbar_add(res.val, c_x.val, c_y.val) | ||
| return res | ||
|
|
||
| def __sub__(x, y): | ||
| cdef qqbar res = qqbar() | ||
| cdef qqbar c_x = x if isinstance(x, qqbar) else qqbar(x) | ||
| cdef qqbar c_y = y if isinstance(y, qqbar) else qqbar(y) | ||
| qqbar_sub(res.val, c_x.val, c_y.val) | ||
| return res | ||
|
|
||
| def __mul__(x, y): | ||
| cdef qqbar res = qqbar() | ||
| cdef qqbar c_x = x if isinstance(x, qqbar) else qqbar(x) | ||
| cdef qqbar c_y = y if isinstance(y, qqbar) else qqbar(y) | ||
| qqbar_mul(res.val, c_x.val, c_y.val) | ||
| return res | ||
|
|
||
| def __truediv__(x, y): | ||
| cdef qqbar res = qqbar() | ||
| cdef qqbar c_x = x if isinstance(x, qqbar) else qqbar(x) | ||
| cdef qqbar c_y = y if isinstance(y, qqbar) else qqbar(y) | ||
| qqbar_div(res.val, c_x.val, c_y.val) | ||
| return res | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is some weird AI stuff. There is no way that the os module should be involved here.