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

symbolic modular integers still broken #18787

Closed
sagetrac-tmonteil mannequin opened this issue Jun 25, 2015 · 51 comments
Closed

symbolic modular integers still broken #18787

sagetrac-tmonteil mannequin opened this issue Jun 25, 2015 · 51 comments

Comments

@sagetrac-tmonteil
Copy link
Mannequin

sagetrac-tmonteil mannequin commented Jun 25, 2015

sage: f(x) = Zmod(7)(1) * x**2 + Zmod(9)(1) * x**3
sage: f(1)
2
sage: Zmod(7)(1) + Zmod(9)(1)

...
TypeError: unsupported operand parent(s) for '+': 'Ring of integers modulo 7' and 'Ring of integers modulo 9'

}}}

Depends on #21391

CC: @kcrisman @slel

Component: symbolics

Branch/Commit: u/rws/symbolic_modular_integers_still_broken @ be9ec4e

Reviewer: Ralf Stephan

Issue created by migration from https://trac.sagemath.org/ticket/18787

@sagetrac-tmonteil sagetrac-tmonteil mannequin added this to the sage-6.8 milestone Jun 25, 2015
@jdemeyer

This comment has been minimized.

@jdemeyer

This comment has been minimized.

@jdemeyer jdemeyer changed the title Bug with matrice products over Symbolic Ring with modular integers Bug with products of symbolic variables with modular integers Jun 25, 2015
@sagetrac-tmonteil
Copy link
Mannequin Author

sagetrac-tmonteil mannequin commented Jun 25, 2015

comment:4

Thanks for isolating the problem.

@sagetrac-tmonteil

This comment has been minimized.

@kcrisman
Copy link
Member

comment:5

Thanks very much to both for reporting and simplifying this.

@nbruin
Copy link
Contributor

nbruin commented Jun 25, 2015

comment:6

A different issue, but it might be related:

sage: F=sum((i+1)*x^i for i in [0..20])
sage: G=sum(Zmod(7)(i+1)*x^i for i in [0..20])
sage: F*Zmod(7)(1) - G
x^19 + 2*x^18 + 0*x^13 + 0*x^6
sage: G*Zmod(7)(1) - G
0

so there seems to be something fishy with symbolic multiplication involving integers and elements of Z/n in general. It's not just a zero-divisor problem.

It's not just powers either:

sage: V=[SR.var("x%s"%i) for i in [0..20]]
sage: F=sum((i+1)*V[i] for i in [0..20])
sage: G=sum(Zmod(7)(i+1)*V[i] for i in [0..20])
sage: sage: F*Zmod(7)(1)-G
-x0 + 0*x13 + 6*x14 + 0*x20 + 3*x3 + 0*x6
sage: F-G 
14*x13 + 21*x20 + 7*x6

The last one is correct, since the terms with x6, x20 and x13 are really missing from G. This illustrates why mixing characteristics in SR is always going to be a mess (even if non-zero characteristic works properly otherwise).

@sagetrac-tmonteil
Copy link
Mannequin Author

sagetrac-tmonteil mannequin commented Jun 25, 2015

comment:7

It seems to be a pynac/ginac issue, right ? So what should we do on our side ? Add a stopgap during conversion/coercion Zmod(n) -> SR ?

@kcrisman
Copy link
Member

comment:8

(Or even just disallow it?)

@rwst
Copy link

rwst commented Jun 26, 2015

comment:9

This is working in pynac-0.4.1 (but not 0.3.9.1).

EDIT: I meant the original case.

@rwst
Copy link

rwst commented Jun 26, 2015

comment:10

Replying to @nbruin:

It's not just powers either:

sage: V=[SR.var("x%s"%i) for i in [0..20]]
sage: F=sum((i+1)*V[i] for i in [0..20])
sage: G=sum(Zmod(7)(i+1)*V[i] for i in [0..20])
sage: sage: F*Zmod(7)(1)-G
-x0 + 0*x13 + 6*x14 + 0*x20 + 3*x3 + 0*x6
sage: F-G 
14*x13 + 21*x20 + 7*x6

This is different in 0.4.1:

sage: sage: sage: F*Zmod(7)(1)-G
-x0 + 0*x13 + 0*x20 + 4*x9
sage: F-G
14*x13 + 21*x20 + 7*x6
sage: F
x0 + 2*x1 + 11*x10 + 12*x11 + 13*x12 + 14*x13 + 15*x14 + 16*x15 + 17*x16 + 18*x17 + 19*x18 + 20*x19 + 3*x2 + 21*x20 + 4*x3 + 5*x4 + 6*x5 + 7*x6 + 8*x7 + 9*x8 + 10*x9
sage: G
x0 + 2*x1 + 4*x10 + 5*x11 + 6*x12 + x14 + 2*x15 + 3*x16 + 4*x17 + 5*x18 + 6*x19 + 3*x2 + 4*x3 + 5*x4 + 6*x5 + x7 + 2*x8 + 3*x9

EDIT: Added F,G.

@rwst
Copy link

rwst commented Jun 26, 2015

comment:11

Correction: this is not pynac-0.4.1 but pynac master. Narrowing down the reponsible change...

@rwst

This comment has been minimized.

@jdemeyer

This comment has been minimized.

@rwst
Copy link

rwst commented Jun 26, 2015

Dependencies: pynac-0.3.9.2

@rwst
Copy link

rwst commented Jun 26, 2015

comment:14

The following patch works for these two examples, just to point at the direction further completion is needed:

diff --git a/ginac/expairseq.cpp b/ginac/expairseq.cpp
index 2abb7b7..acef09b 100644
--- a/ginac/expairseq.cpp
+++ b/ginac/expairseq.cpp
@@ -1207,16 +1207,17 @@ void expairseq::combine_same_terms_sorted_seq()
 	// must_copy will be set to true the first time some combination is 
 	// possible from then on the sequence has changed and must be compacted
 	bool must_copy = false;
+        numeric itin1_c = ex_to<numeric>(itin1->coeff);
 	while (itin2!=last) {
 		if (itin1->rest.compare(itin2->rest)==0 &&
 				unlikely(!is_a<infinity>(itin1->rest))) {
-			itin1->coeff = ex_to<numeric>(itin1->coeff).
+			itin1->coeff = itin1_c.
 			               add_dyn(ex_to<numeric>(itin2->coeff));
 			if (expair_needs_further_processing(itin1))
 				needs_further_processing = true;
 			must_copy = true;
 		} else {
-			if (!ex_to<numeric>(itin1->coeff).is_zero()) {
+			if (not itin1_c.is_zero() or itin1_c.is_parent_pos_char()) {
 				if (must_copy)
 					*itout = *itin1;
 				++itout;
@@ -1225,7 +1226,7 @@ void expairseq::combine_same_terms_sorted_seq()
 		}
 		++itin2;
 	}
-	if (!ex_to<numeric>(itin1->coeff).is_zero()) {
+	if (not itin1_c.is_zero() or itin1_c.is_parent_pos_char()) {
 		if (must_copy)
 			*itout = *itin1;
 		++itout;

The relevant ticket would be pynac/pynac#77.

@rwst
Copy link

rwst commented Jun 26, 2015

Upstream: Reported upstream. Developers acknowledge bug.

@rwst
Copy link

rwst commented Jun 30, 2015

@rwst
Copy link

rwst commented Jul 16, 2015

Author: Ralf Stephan

@rwst
Copy link

rwst commented Jul 16, 2015

Changed upstream from Reported upstream. Developers acknowledge bug. to none

@rwst
Copy link

rwst commented Jul 16, 2015

New commits:

6f9e55118787: doctests

@rwst
Copy link

rwst commented Jul 16, 2015

Commit: 6f9e551

@rwst
Copy link

rwst commented Jul 16, 2015

Changed dependencies from pynac-0.3.9.2 to none

@rwst rwst changed the title Bug with products of symbolic variables with modular integers doctest symbolic modular integers fix Jul 16, 2015
@jdemeyer
Copy link

comment:17

With Sage-6.8.rc0:

**********************************************************************
File "src/sage/symbolic/expression.pyx", line 2821, in sage.symbolic.expression.Expression._add_
Failed example:
    (A + 3*B)*Zmod(9)(6)
Expected:
    6*A
Got:
    0* + 6*A
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 2823, in sage.symbolic.expression.Expression._add_
Failed example:
    (3*A + 3*B)*Zmod(9)(6)
Expected:
    0
Got:
    0*
**********************************************************************
File "src/sage/symbolic/expression.pyx", line 2825, in sage.symbolic.expression.Expression._add_
Failed example:
    (3*A + 3*B)*Zmod(9)(6)*A
Exception raised:
    Traceback (most recent call last):
      File "/usr/local/src/sage-git/local/lib/python2.7/site-packages/sage/doctest/forker.py", line 496, in _run
        self.compile_and_execute(example, compiler, test.globs)
      File "/usr/local/src/sage-git/local/lib/python2.7/site-packages/sage/doctest/forker.py", line 858, in compile_and_execute
        exec(compiled, globs)
      File "<doctest sage.symbolic.expression.Expression._add_[24]>", line 1, in <module>
        (Integer(3)*A + Integer(3)*B)*Zmod(Integer(9))(Integer(6))*A
      File "sage/structure/element.pyx", line 1849, in sage.structure.element.RingElement.__mul__ (build/cythonized/sage/structure/element.c:16581)
        return (<RingElement>left)._mul_(<RingElement>right)
      File "sage/symbolic/expression.pyx", line 3148, in sage.symbolic.expression.Expression._mul_ (build/cythonized/sage/symbolic/expression.cpp:19837)
        x = gmul(left._gobj, _right._gobj)
    OverflowError: numeric::div(): division by zero
**********************************************************************

@rwst
Copy link

rwst commented Jan 16, 2017

@rwst
Copy link

rwst commented Jan 16, 2017

Changed author from Ralf Stephan to none

@rwst rwst removed this from the sage-6.9 milestone Jan 16, 2017
@sagetrac-tmonteil
Copy link
Mannequin Author

sagetrac-tmonteil mannequin commented Mar 6, 2017

comment:32

Replying to @rwst:

I believe this is resolved in #21391.

It seems inteed to works better now. But why not adding the tests discussed on the current ticket as you did previously ?

@sagetrac-tmonteil sagetrac-tmonteil mannequin added this to the sage-7.6 milestone Mar 6, 2017
@rwst
Copy link

rwst commented Mar 7, 2017

@rwst
Copy link

rwst commented Mar 7, 2017

comment:34

Made dependent on #21391 to prevent merge conflicts.


New commits:

714769921391: Disallow mixing of pos.char.ring elements and symbolic variables
dd6ad96Merge branch 'develop' into t/21391/disallow_mixing_of_pos_char_ring_elements_and_symbolic_variables
03ccd07Merge branch 'develop' into t/21391/disallow_mixing_of_pos_char_ring_elements_and_symbolic_variables
f2933cc21391: introduce ex.has_finite_parent()
333db3cMerge branch 'develop' into t/21391/disallow_mixing_of_pos_char_ring_elements_and_symbolic_variables
8700e00Merge branch 'develop' into t/21391/disallow_mixing_of_pos_char_ring_elements_and_symbolic_variables
c7f8ad221391: to prevent fails give the coordinate functions an explicit characteristic
943060521391: doctest fixes
be9ec4e18787: doctest

@rwst
Copy link

rwst commented Mar 7, 2017

Commit: be9ec4e

@rwst
Copy link

rwst commented Mar 7, 2017

Author: Ralf Stephan

@rwst
Copy link

rwst commented Mar 7, 2017

Dependencies: #21391

@koffie
Copy link

koffie commented Oct 25, 2017

comment:35

Do the people on this ticket agree that #24072 solves this ticket as is claimed there?

@rwst
Copy link

rwst commented Oct 25, 2017

comment:36

Agree.

@rwst rwst removed this from the sage-7.6 milestone Oct 25, 2017
@tscrim
Copy link
Collaborator

tscrim commented Oct 25, 2017

Reviewer: Ralf Stephan

@tscrim
Copy link
Collaborator

tscrim commented Oct 25, 2017

Changed author from Ralf Stephan to none

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

8 participants