Opened 9 years ago

Last modified 3 years ago

#9935 new defect

Make a symbolic mod function

Reported by: jason Owned by: burcin
Priority: major Milestone: sage-7.4
Component: symbolics Keywords:
Cc: burcin, jdemeyer, rws Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Description (last modified by mkoeppe)

A participant in the PREP program noticed that mod is not a symbolic function:

sage: mod(x,4)
ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (4562, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (4530, 0))

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (842, 0))

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/grout/sage-4.5.2/devel/sage-main/sage/functions/<ipython console> in <module>()

/home/grout/sage/local/lib/python2.6/site-packages/sage/rings/finite_rings/integer_mod.so in sage.rings.finite_rings.integer_mod.Mod (sage/rings/finite_rings/integer_mod.c:2633)()

/home/grout/sage/local/lib/python2.6/site-packages/sage/rings/finite_rings/integer_mod.so in sage.rings.finite_rings.integer_mod.IntegerMod (sage/rings/finite_rings/integer_mod.c:2952)()

/home/grout/sage/local/lib/python2.6/site-packages/sage/rings/finite_rings/integer_mod.so in sage.rings.finite_rings.integer_mod.IntegerMod_int.__init__ (sage/rings/finite_rings/integer_mod.c:14249)()

/home/grout/sage/local/lib/python2.6/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.__call__ (sage/structure/parent.c:6407)()

/home/grout/sage/local/lib/python2.6/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.NamedConvertMap._call_ (sage/structure/coerce_maps.c:4053)()

/home/grout/sage/local/lib/python2.6/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression._integer_ (sage/symbolic/expression.cpp:4026)()

TypeError: unable to convert x (=x) to an integer

Hopefully it should be easy to wrap mod in a symbolic function, something like:

sage: def eval_mod(self, x):
....:     if isinstance(x, (int, Integer)):
....:         return mod(x,5)
....:     return None
....:
sage: f=function('f', eval_func=eval_mod)
sage: f(x)
f(x)
sage: f(13)
3
sage: f(x^2+x+1)
f(x^2 + x + 1)
sage: f(x^2+x+1).subs(x=2)
2
sage: f(x^2+x+1).subs(x=3)
3
sage: f(x^2+x^3).subs(x=1)
2
sage: f(x^2+x^3).subs(x=2)
2
sage: f(x^2+x^3).subs(x=3)
1 

See also http://ask.sagemath.org/question/25037/declaring-variable-to-be-in-a-particular-fieldringgroup/

More discussions: https://groups.google.com/forum/#!searchin/sage-devel/mod|sort:relevance/sage-devel/goNosLk_t9M/xYDgEdIsLYsJ https://groups.google.com/forum/#!searchin/sage-devel/mod|sort:relevance/sage-devel/g7AdhAyQH-k/O1IGYbNRAQAJ

Change History (13)

comment:1 Changed 9 years ago by jason

Burcin, is there a nice example somewhere that we can just copy and modify to make a symbolic function? This sounds like it would be easy for a beginner to do if there was a good example almost like it somewhere.

comment:2 Changed 9 years ago by burcin

There are plenty of examples in the directory sage/functions/, just look for classes deriving from BuiltinFunction. You'll probably want to deprecate the parent keyword argument to mod(), so sage.functions.transcendental.Function_exp_integral might provide a good template.

comment:3 Changed 6 years ago by jdemeyer

  • Milestone changed from sage-5.11 to sage-5.12

comment:4 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:5 Changed 6 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:6 Changed 5 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:7 Changed 5 years ago by jdemeyer

  • Description modified (diff)

comment:8 Changed 5 years ago by jdemeyer

  • Description modified (diff)

comment:9 follow-up: Changed 3 years ago by mkoeppe

  • Cc jdemeyer rws added

We still don't have a good "mod". I would suspect that this has been discussed at length in the past years.

I'm bringing this up because I'm in need of periodic piecewise functions (#21215). Using a suitable symbolic mod would be one way. On Mathematica, using their 3-argument Mod function seems to be the standard way for defining periodic piecewise functions - see for example here: http://community.wolfram.com/groups/-/m/t/156025 We could to the same (one possible route).

Last edited 3 years ago by mkoeppe (previous) (diff)

comment:10 Changed 3 years ago by mkoeppe

  • Milestone changed from sage-6.4 to sage-7.4

comment:11 Changed 3 years ago by mkoeppe

  • Description modified (diff)

comment:12 in reply to: ↑ 9 ; follow-up: Changed 3 years ago by rws

Replying to mkoeppe:

I'm bringing this up because I'm in need of periodic piecewise functions (#21215). Using a suitable symbolic mod would be one way.

It is the more complicated way. You are asking for a feature of piecewise which can be had just by the addition of a period keyword. A symbolic mod is not necessary for it, and I'm arguing it's not necessary at all because there are no use cases that are handled better in the symbolic ring than in a dedicated finite ring, even if Sage does not have it at the moment. As the user of the ask.sagemath question above finally realized as well.

comment:13 in reply to: ↑ 12 Changed 3 years ago by mkoeppe

Replying to rws:

Replying to mkoeppe:

I'm bringing this up because I'm in need of periodic piecewise functions (#21215). Using a suitable symbolic mod would be one way.

It is the more complicated way. You are asking for a feature of piecewise which can be had just by the addition of a period keyword. A symbolic mod is not necessary for it, and I'm arguing it's not necessary at all because there are no use cases that are handled better in the symbolic ring than in a dedicated finite ring, even if Sage does not have it at the moment. As the user of the ask.sagemath question above finally realized as well.

Thanks!

Note: See TracTickets for help on using tickets.