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

Are methods from calcium available in python-flint 0.5.x? #126

Open
kiryph opened this issue Feb 28, 2024 · 7 comments
Open

Are methods from calcium available in python-flint 0.5.x? #126

kiryph opened this issue Feb 28, 2024 · 7 comments

Comments

@kiryph
Copy link

kiryph commented Feb 28, 2024

I have used Calcium 0.4 and its python interface pycalcium/pyca.

Now it does not work anymore since macOS homebrew updated my Flint installation from 2.x to 3.x to which it was linked.

Since the documentation of Flint 3 says Arb and Calcium are merged into Flint, maybe the Python wrapper for Calcium has been merged into python-flint.

I have installed the current release of python-flint to pypi and try to see if I can access methods from Calcium

pipenv install python-flint
Creating a virtualenv for this project...
...
❯ pipenv shell
Launching subshell in virtual environment...

❯ python
Python 3.9.4 (default, May  1 2021, 16:18:37)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import flint
>>> flint.__version__
'0.6.0'
>>> flint.
flint.Context(             flint.coerce_fmpz_mpolys(  flint.fmpz_mod(            flint.nmod(
flint.DomainError(         flint.ctx                  flint.fmpz_mod_ctx(        flint.nmod_mat(
flint.acb(                 flint.dirichlet_char(      flint.fmpz_mod_mat(        flint.nmod_poly(
flint.acb_mat(             flint.dirichlet_group(     flint.fmpz_mod_poly(       flint.nmod_series(
flint.acb_poly(            flint.flint_base           flint.fmpz_mod_poly_ctx(   flint.pyflint
flint.acb_series(          flint.fmpq(                flint.fmpz_mpoly(          flint.showgood(
flint.arb(                 flint.fmpq_mat(            flint.fmpz_mpoly_ctx(      flint.types
flint.arb_mat(             flint.fmpq_poly(           flint.fmpz_poly(           flint.utils
flint.arb_poly(            flint.fmpq_series(         flint.fmpz_series(
flint.arb_series(          flint.fmpz(                flint.functions
flint.arf(                 flint.fmpz_mat(            flint.good(

But I am struggling to check this.

The documentation of python-flint does not seem to be updated since the merge of Arb and Calcium into Flint:

https://fredrikj.net/python-flint/ (says version 0.3.0 from 2018)

@fredrik-johansson
Copy link
Collaborator

Instead of pycalcium it is currently possible to use flint_ctypes: https://github.com/flintlib/flint/blob/main/src/python/flint_ctypes.py

@oscarbenjamin
Copy link
Collaborator

It would definitely be good to add calcium wrappers in pytho-flint if anyone is interested in working on that.

@kiryph
Copy link
Author

kiryph commented Feb 28, 2024

Instead of pycalcium it is currently possible to use flint_ctypes: https://github.com/flintlib/flint/blob/main/src/python/flint_ctypes.py

I am not sure if this helps me.

I had a simple method written for the GAP system which turns Cyclotomic numbers into LaTeX expressions with the help of Calcium:

PYTHONPATH := Filename(DirectoriesSystemPrograms(), "python3");
PYCALCIUM_DIRECTORY := Directory("~/Repositories/calcium/pycalcium"); # 0.4.0

InstallGlobalFunction(LaTeXStringCyclotomic, function (q)
  local input, str, output, pycode;
  input := InputTextString(String(q));
  str := "";
  output := OutputTextString(str, true);
  pycode := "import sys; from pyca import qqbar,Exp,Pi,I;";
  pycode := Concatenation(pycode, "E = lambda n: Exp(2*Pi*I/n);");
  pycode := Concatenation(pycode, "print(qqbar(eval(sys.stdin.read().replace('^', '**'))).fexpr().latex(), end='')");
  Process( PYCALCIUM_DIRECTORY, PYTHONPATH, input, output, [ "-c", pycode ] );
  return str;
end);

which can be used as following in GAP:

gap> mynumber := Sqrt(2);
E(8)-E(8)^3
gap> LaTeXStringCyclotomic(mynumber);
"\\sqrt{2}"

@fredrik-johansson
Copy link
Collaborator

This works:

>>> from flint_ctypes import *
>>> Exp = fexpr("Exp")
>>> Pi = fexpr("Pi")
>>> I = fexpr("NumberI")
>>> E = lambda n: Exp(2*Pi*I/n)
>>> qqbar(E(8) - E(8)**3).fexpr().latex()
'\\sqrt{2}'

More directly, you could do:

>>> E = lambda n: (qqbar(2) / n).exp_pi_i()
>>> qqbar(E(8) - E(8)**3).fexpr().latex()
'\\sqrt{2}'

@kiryph
Copy link
Author

kiryph commented Feb 28, 2024

@fredrik-johansson Thanks for your help. Yes, this works as expected with Flint 3.1.0 without python-flint:

I have updated my GAP code accordingly:

PYTHONPATH := Filename(DirectoriesSystemPrograms(), "python3");
PYTHON_FLINT3_DIRECTORY := Directory("~/Repositories/flint-3.1.0/src/python");

InstallGlobalFunction(LaTeXStringCyclotomic, function (q)
  local input, str, output, pycode;
  input := InputTextString(String(q));
  str := "";
  output := OutputTextString(str, true);
  pycode := "import sys; from flint_ctypes import *;";
  pycode := Concatenation(pycode, "E = lambda n: (qqbar(2) / n).exp_pi_i();");
  pycode := Concatenation(pycode, "print(qqbar(eval(sys.stdin.read().replace('^', '**'))).fexpr().latex(), end='')");
  Process( PYTHON_FLINT3_DIRECTORY, PYTHONPATH, input, output, [ "-c", pycode ] );
  return str;
end);

Should I close this issue?

@fredrik-johansson
Copy link
Collaborator

Should I close this issue?

Looks like there is no other open issue about wrapping calcium in python-flint, so may as well leave it open if you don't mind!

@oscarbenjamin
Copy link
Collaborator

On the current main branch and also the 0.7.0a5 prerelease there is some very limited access to calcium via the newly added generic rings module:

In [10]: from flint.types import _gr

In [11]: CA = _gr.gr_complex_ca_ctx.new()

In [12]: CA(2).sqrt() + 1
Out[12]: 2.41421 {a+1 where a = 1.41421 [a^2-2=0]}

In [13]: pi = CA('pi')

In [14]: pi**2 - 1
Out[14]: 8.86960 {a^2-1 where a = 3.14159 [Pi]}

This is very incomplete and not well tested though. Also I would expect the interface to change in future.

Also the documentation for python-flint has been updated here:
https://python-flint.readthedocs.io/en/latest/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants