Comments (6)
I think it's suboptimal if the underlying backend used by sympy "leaks" out into the public sympy API, no?
This is already the case for other domains like ZZ:
In [1]: type(ZZ(1))
Out[1]: mpz
In [1]: type(ZZ(1))
Out[1]: flint.types.fmpz.fmpz
In [1]: type(ZZ(1))
Out[1]: int
Ultimately if we use types from gmpy2 and python-flint directly as domain elements then this is inevitable.
What should matter is that they are functionally equivalent.
I think this return call should wrap the result in a sympy type:
If you mean that this should dynamically create a class then I disagree. In fact I think that it is already a problem that dynamically created classes are used i.e. this should be changed:
sympy/sympy/polys/domains/modularinteger.py
Lines 226 to 228 in 8c94201
The way that python-flint does it is better i.e. you have a type that has a modulus attribute and it has a two argument constructor.
from sympy.
Yes, I agree that the dynamic class design is... weird (I never understood it). But whatever is returned (flint.types.nmod.nmod or something else), it would be nice if there is a well defined set of methods/attributes that can be used on the return value (otherwise consuming code will have to handle API-differences of the returned object in if isinstance(...)
blocks).
from sympy.
Domain elements should only be used in a context where you have the domain object and it is that domain object that provides a consistent interface:
In [2]: K = GF(2)
In [3]: K.of_type(K(1))
Out[3]: True
In [4]: K.to_sympy(K(1))
Out[4]: 1
In [5]: ZZ.convert_from(K(1), K)
Out[5]: 1
If you have the domain element but not the domain then you are not using the domains in the intended way. Code that is intended to operate on generic domain elements should not presume any interface of them beyond ring operations:
sympy/sympy/polys/matrices/_typing.py
Lines 7 to 16 in 8c94201
from sympy.
Yes, I agree that the dynamic class design is... weird (I never understood it).
I think it is a micro-optimisation for arithmetic operations so that you can do:
def __add__(self, other):
if type(self) == type(other):
return self.new(self.val + other.val)
Otherwise it would be:
def __add__(self, other):
if isinstance(other, ModularInteger) and self.modulus == other.modulus:
return self.new(self.val + other.val, self.modulus)
I presume that at some point someone benchmarked this and found that the dynamically generated classes were slightly faster.
The same design ended up being used for GF, RR, CC, PolyElement, etc. It is also used in mpmath for mpfs with different contexts.
from sympy.
Alright, I see, thank you for explaining this. Then perhaps there isn't a simple fix here. We could close this issue from my point of view.
from sympy.
Okay, let's close this.
from sympy.
Related Issues (20)
- [1.13.0rc1] possible regression in comparison (`numbers.One()` VS `float(1.0)`) ? HOT 6
- Question: Can `parse_expr` parse Symbols with subtraction char in it? HOT 4
- ccode, using numpy 2 HOT 2
- Access `objects.inv` of old sympy versions for intersphinx HOT 12
- GPU Support for Boolean Expressions HOT 6
- lambdify is no longer able to deal with `sympy.vector.BaseScalar` HOT 17
- Dirac delta doesn't get simplified by `assuming` HOT 1
- [3.13] Python compile recursion error due to huge expression HOT 12
- Singularity Functions that never return infinity for mechanics application HOT 8
- `pde_separate_mul` returns `None`
- Intersection of PermutationGroups HOT 1
- Simplification of equations with Singularity Functions HOT 1
- Request For Security Contact HOT 4
- `repr(Function | int)` returns results inconsistent with type name. HOT 3
- Bug with O(f) if f is a Function HOT 2
- Feature Request: Add Import/Export Functions for Boolean Expressions in Z3 and BDD Formats HOT 15
- Wrong translation of lerchphi into Mathematica HOT 3
- Add a factors function to get factors without multiplicity HOT 7
- collected_expr.coeff() always return 0 if some symbol was used as a Divisor? HOT 2
- Solution from dsolve_system violates all equations HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sympy.