In [1]: FF(12)
Out[1]: ℤ₁₂
In [2]: FF(12)(4)
Out[2]: 4 mod 12
In [3]: FF(12)(4)/FF(12)(11)
Out[3]: 8 mod 12
Note that the name FF comes from finite field, so this may not work if
the modulus is not a power of a prime. For example:
In [4]: FF(12)(4)/FF(12)(2)
---------------------------------------------------------------------------
NotInvertible Traceback (most recent call last)
/Users/aaronmeurer/Documents/python/sympy/sympy/<ipython console> in <module>()
/Users/aaronmeurer/Documents/python/sympy/sympy/sympy/polys/domains/modularinteger.pyc
in __div__(self, other)
92
93 if val is not None:
---> 94 return self.__class__(self.val * self._invert(val))
95 else:
96 return NotImplemented
/Users/aaronmeurer/Documents/python/sympy/sympy/sympy/polys/domains/modularinteger.pyc
in _invert(cls, value)
160 @classmethod
161 def _invert(cls, value):
--> 162 return cls.dom.invert(value, cls.mod)
163
164 def invert(self):
/Users/aaronmeurer/Documents/python/sympy/sympy/sympy/polys/domains/ring.pyc
in invert(self, a, b)
39 return s % b
40 else:
---> 41 raise NotInvertible("zero divisor")
42
43 def revert(self, a):
NotInvertible: zero divisor
I'm not sure if you will get other errors if you use non-fields.
If you want to solve congruence relations, like x = 2 mod 3, then you
might look at this branch: https://github.com/sympy/sympy/pull/390.
It would be great if you could give that branch a complete review, as
I've only been able to comment on the code quality up to this point.
Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>
>
The CRT would solve the first problem where all moduli are prime, but
the 2nd problem has a composite modulus.
Hector is working on a congruence solver and that is nearly ready (so
that would solve the first problem) but I'm wondering if we should
have some ModulusNumber class to allow for such simple arithmetic as
presented in the 2nd problem. There is an old posting giving such a
class at
http://www.python.org/search/hypermail/python-1994q1/0523.html
There is also the online calculator at http://ptrow.com/perl/calculator.pl
Thanks for the suggestions,
/c
Maybe we should split out FiniteField into FiniteRing and FiniteField,
where FiniteField is a subclass of FiniteRing. Then, FiniteField
would be more strict about being a field (or else FF(n) would
automatically create the correct object).
Aaron Meurer
>
> http://www.python.org/search/hypermail/python-1994q1/0523.html
>
> There is also the online calculator at http://ptrow.com/perl/calculator.pl
>
> Thanks for the suggestions,
> /c
>
If you want to solve congruence relations, like x = 2 mod 3, then you
might look at this branch: https://github.com/sympy/sympy/pull/390.
It would be great if you could give that branch a complete review, as
I've only been able to comment on the code quality up to this point.
Aaron Meurer
On Fri, Sep 23, 2011 at 2:16 AM, smichr <smi...@gmail.com> wrote:
> I'm not sure where to look in sympy for support of things like solving
> congruence relationships (what number is 2 mod 3, 3 mod 5 and 2 mod 7;
> answer 23 mod 105) and operations modulo n, e.g., from http://tinyurl.com/3atamuj,
> the following question:
>
> In Z_12, divide 4 by 2, 3, 5, 7,8, and 11
>
> Do we have support for such things?
>
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
```python
>>> def div(a,b,m):
... m=FF(m)
... try: return m(a)/m(b)
... except:
... if m(a): return oo # perhaps this, rather than an error would be
more sympy like
... return S.NaN # ditto
...
>>> for i in range(5):
... for j in range(i,5):
... print '%i/%i =%s'%(i,j,div(i,j,5))
...
0/0 =nan
0/1 =0 mod 5
0/2 =0 mod 5
0/3 =0 mod 5
0/4 =0 mod 5
1/1 =1 mod 5
1/2 =3 mod 5
1/3 =2 mod 5
1/4 =4 mod 5
2/2 =1 mod 5
2/3 =4 mod 5
2/4 =3 mod 5
3/3 =1 mod 5
3/4 =2 mod 5
4/4 =1 mod 5
>>> for i in range(6):
... for j in range(i,6):
... print '%i/%i =%s'%(i,j,div(i,j,6))
...
0/0 =nan
0/1 =0 mod 6
0/2 =nan
0/3 =nan
0/4 =nan
0/5 =0 mod 6
1/1 =1 mod 6
1/2 =oo
1/3 =oo
1/4 =oo
1/5 =5 mod 6
2/2 =oo
2/3 =oo
2/4 =oo
2/5 =4 mod 6
3/3 =oo
3/4 =oo
3/5 =3 mod 6
4/4 =oo
4/5 =2 mod 6
5/5 =1 mod 6
>>> m7=FF(7)
>>> [(m7(i)) for i in range(7)]
[SymmetricModularIntegerMod7(0), SymmetricModularIntegerMod7(1), SymmetricModula
rIntegerMod7(2), SymmetricModularIntegerMod7(3), SymmetricModularIntegerMod7(4),
SymmetricModularIntegerMod7(5), SymmetricModularIntegerMod7(6)]
>>> [sqrt(m7(i)) for i in range(7)]
[0, 1, sqrt(2), sqrt(3), sqrt(3)*I, sqrt(2)*I, I]
>>> [str(m7(i)) for i in range(7)]
['0 mod 7', '1 mod 7', '2 mod 7', '3 mod 7', '4 mod 7', '5 mod 7', '6 mod 7']
>>> str(m7(4)+m7(3))
'0 mod 7'
>>> str(m7(4)-m7(3))
'1 mod 7'
>>> str(m7(4)*m7(3))
'5 mod 7'
>>> str(m7(4)/m7(3))
'6 mod 7'
>>> str(m7(4)**3)
'1 mod 7'
>>> str(sqrt(m7(4)))
'sqrt(3)*I'
>>> sqrt(m7(4))**2
-3
>>> m7(_)==m7(4)
True
```
I wonder if the positive value for the sqrt should be returned.
http://ptrow.com/perl/calculator.pl lists the square roots of 4 mod 7
as being 2 and 5 (half of the numbers between 1 and modulus - 1 -- in
this case half of 2, 3, 4, 5, or 2 and 5 -- are square roots mod n).
sympy returned sqrt(-3) rather than 2 or 5.
Also, I wonder if the str form should be the default printed in
interactive sessions rather than the repr form.
/c
```python
>>> m7(4)**Rational(1,3)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "sympy\polys\domains\modularinteger.py", line 129, in __pow__
return self.__class__(val**exp)
File "sympy\polys\domains\modularinteger.py", line 18, in __init__
self.val = self.dom.convert(val) % self.mod
File "sympy\polys\domains\domain.py", line 103, in convert
return K1.from_sympy(a)
File "sympy\polys\domains\pythonintegerring.py", line 34, in from_sympy
raise CoercionFailed("expected an integer, got %s" % a)
sympy.polys.polyerrors.CoercionFailed: expected an integer, got 2**(2/3)
```
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
>>> invert(S(3),5)
2
>>> (3*2)%5 == 1
True
>>> invert(S(4),6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "sympy\polys\polytools.py", line 4249, in invert
return f.invert(g)
File "sympy\core\numbers.py", line 1322, in invert
raise ZeroDivisionError("zero divisor")
ZeroDivisionError: zero divisor
To find an Integer given a description of it's modulus profile perhaps
a method "from_moduli" could be added so Integer.from_moduli((2, 3),
(3, 5)) would give 8. Hector, Mateusz, anyone else: does that look
like a good way to construct it? Is there a better method name?
I just noticed in Integer, too, the invert method which gives the
multiplicative inverse mode n of a number:
>>> invert(S(3),5)
2
>>> (3*2)%5 == 1
True
>>> invert(S(4),6)
Traceback (most recent call last):File "sympy\polys\polytools.py", line 4249, in invert
File "<stdin>", line 1, in <module>
return f.invert(g)
File "sympy\core\numbers.py", line 1322, in invert
raise ZeroDivisionError("zero divisor")
ZeroDivisionError: zero divisor
To find an Integer given a description of it's modulus profile perhaps
a method "from_moduli" could be added so Integer.from_moduli((2, 3),
(3, 5)) would give 8. Hector, Mateusz, anyone else: does that look
like a good way to construct it? Is there a better method name?
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
It's on the pull request page: search for 'solve_congruence' on page
https://github.com/sympy/sympy/pull/390
+1
>
> --Mike
But mathematically, FF(4) exists and is a field. However, FF(4) != ZZ_4.
> I second Aaron's suggestions of making class FiniteRing and put more
> constrains on FintiteField. So, the output **would** be something
> like
FiniteRing isn't a good name, because there can be many finite rings of
a given order. "CyclicRing" or "CongruenceRing" would be unambiguous.
>
> >>> FF(4)
> Error - Z4 is not a filed
>
> >>> FR(4)
> Z4
>
> >>> FR(4)(3).invert()
> 1 ## Additive inverse
>
> >>> FF(5)
> Z5
>
> >>> FF(5)(4).invert_mul()
> 4
>
> >>> FF(5)(4).invert_add()
> 1
>
>
>
> To find an Integer given a description of it's modulus profile
> perhaps
> a method "from_moduli" could be added so
> Integer.from_moduli((2, 3),
> (3, 5)) would give 8. Hector, Mateusz, anyone else: does that
> look
> like a good way to construct it? Is there a better method
> name?
That's the Chinese remainder problem[1]. The solution isn't unique so I
don't think this should be a classmethod of Integer.
[1]: http://en.wikipedia.org/wiki/Chinese_remainder_theorem
Nearly so, but the routine will construct the number even if the
moduli are not prime. Perhaps it should return a number like '15 mod
17' (FF(17)(15)).
--
You received this message because you are subscribed to the Google Groups "sympy" group.
To post to this group, send email to sy...@googlegroups.com.
To unsubscribe from this group, send email to sympy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sympy?hl=en.
I remember the changes were merged to enable sympy to solve congruence equations.
@ smichr or anyone : Can you please tell me how to do it with `solve' function?
Any keyword or input format that calls `csolve'?