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

Make a symbolic mod function #9935

Open
jasongrout opened this issue Sep 17, 2010 · 8 comments
Open

Make a symbolic mod function #9935

jasongrout opened this issue Sep 17, 2010 · 8 comments

Comments

@jasongrout
Copy link
Member

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

CC: @burcin @jdemeyer @rwst

Component: symbolics

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

@jasongrout
Copy link
Member Author

comment:1

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.

@burcin
Copy link

burcin commented Sep 18, 2010

comment:2

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.

@jdemeyer jdemeyer modified the milestones: sage-5.11, sage-5.12 Aug 13, 2013
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.1, sage-6.2 Jan 30, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.2, sage-6.3 May 6, 2014
@sagetrac-vbraun-spam sagetrac-vbraun-spam mannequin modified the milestones: sage-6.3, sage-6.4 Aug 10, 2014
@jdemeyer

This comment has been minimized.

@jdemeyer

This comment has been minimized.

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 10, 2016

comment:9

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).

@mkoeppe mkoeppe modified the milestones: sage-6.4, sage-7.4 Aug 10, 2016
@mkoeppe

This comment has been minimized.

@rwst
Copy link

rwst commented Aug 11, 2016

comment:12

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.

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 12, 2016

comment:13

Replying to @rwst:

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!

@mkoeppe mkoeppe removed this from the sage-7.4 milestone Dec 29, 2022
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

5 participants