[sage-support] Re: A little question about mod in function definition

2009-07-03 Thread Alasdair

So it seems there are (at least!) two classes of functions; those
which accept symbolic input, like sin:

m(x)=sin(x/2)

and those which don't, such as mod. I had not realized there was such
a distinction.  Thank you all.

-Alasdair

On Jul 2, 5:15 am, William Stein  wrote:
> On Wed, Jul 1, 2009 at 8:52 PM, Simon King wrote:
>
> > Hi Alasdair,
>
> > On 1 Jul., 13:00, David Joyner  wrote:
> >> I think the first tries to use Sage's symbolic expression machinery
> >> but the second does not.
>
> > Yes, it seems so.
>
> > Using Sage, one should always be aware that  some very handy/fancy
> > syntax is only available due to the Sage preparser.
>
> > E.g., some definitions such as f(x) = sin(x) or R.=QQ[] are not
> > valid Python. But when you do this in Sage, it internally becomes
> > sage: preparse('m(x)=sin(x)')
> > '__tmp__=var("x"); m = symbolic_expression(sin(x)).function(x)'
>
> > In your first approach, you get '__tmp__=var("x"); m =
> > symbolic_expression(mod(x,Integer(10))).function(x)'
>
> > But mod(x,Integer(10)) gives an error, since x is a symbolic variable
> > and not an integer, and since "mod" is not symbolic, in contrast to
> > "sin":
> >  sage: type(sin)
> >  
> >  sage: type(mod)
> >  
>
> For the record, at some point we may want to make "mod" work with
> symbolic input.
> I.e., I don't see any reason why at some point in the future we could
> make the following make sense:
>
>  sage: x = var('x')
>  sage: f = mod(x, 3)
>  sage: f
>  Mod(x, 3)
>  sage: f.subs(x=5)
>  2
>
> This is already how Mathematica works:
>
> f := Mod[x,3]; f
> Mod[x, 3]
>
> f  /.   x -> 5
> 2
>
> William
--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~--~~~~--~~--~--~---



[sage-support] Re: A little question about mod in function definition

2009-07-01 Thread William Stein

On Wed, Jul 1, 2009 at 8:52 PM, Simon King wrote:
>
> Hi Alasdair,
>
> On 1 Jul., 13:00, David Joyner  wrote:
>> I think the first tries to use Sage's symbolic expression machinery
>> but the second does not.
>
> Yes, it seems so.
>
> Using Sage, one should always be aware that  some very handy/fancy
> syntax is only available due to the Sage preparser.
>
> E.g., some definitions such as f(x) = sin(x) or R.=QQ[] are not
> valid Python. But when you do this in Sage, it internally becomes
> sage: preparse('m(x)=sin(x)')
> '__tmp__=var("x"); m = symbolic_expression(sin(x)).function(x)'
>
> In your first approach, you get '__tmp__=var("x"); m =
> symbolic_expression(mod(x,Integer(10))).function(x)'
>
> But mod(x,Integer(10)) gives an error, since x is a symbolic variable
> and not an integer, and since "mod" is not symbolic, in contrast to
> "sin":
>  sage: type(sin)
>  
>  sage: type(mod)
>  
>

For the record, at some point we may want to make "mod" work with
symbolic input.
I.e., I don't see any reason why at some point in the future we could
make the following make sense:

 sage: x = var('x')
 sage: f = mod(x, 3)
 sage: f
 Mod(x, 3)
 sage: f.subs(x=5)
 2

This is already how Mathematica works:

f := Mod[x,3]; f
Mod[x, 3]

f  /.   x -> 5
2

William

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~--~~~~--~~--~--~---



[sage-support] Re: A little question about mod in function definition

2009-07-01 Thread Simon King

Hi Alasdair,

On 1 Jul., 13:00, David Joyner  wrote:
> I think the first tries to use Sage's symbolic expression machinery
> but the second does not.

Yes, it seems so.

Using Sage, one should always be aware that  some very handy/fancy
syntax is only available due to the Sage preparser.

E.g., some definitions such as f(x) = sin(x) or R.=QQ[] are not
valid Python. But when you do this in Sage, it internally becomes
sage: preparse('m(x)=sin(x)')
'__tmp__=var("x"); m = symbolic_expression(sin(x)).function(x)'

In your first approach, you get '__tmp__=var("x"); m =
symbolic_expression(mod(x,Integer(10))).function(x)'

But mod(x,Integer(10)) gives an error, since x is a symbolic variable
and not an integer, and since "mod" is not symbolic, in contrast to
"sin":
  sage: type(sin)
  
  sage: type(mod)
  

Cheers,
   Simon

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~--~~~~--~~--~--~---



[sage-support] Re: A little question about mod in function definition

2009-07-01 Thread David Joyner

On Wed, Jul 1, 2009 at 4:43 AM, Alasdair wrote:
>
> Of these two examples:
>
> m(x)=mod(x,10)
>
> m=lambda x:mod(x,10)
>
> The first returns an error "unable to convert x (=x) to an integer".
> Can anyone explain what's going on here?

I think the first tries to use Sage's symbolic expression machinery
but the second
does not.

>
> Thanks,
> Alasdair
> >
>

--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~--~~~~--~~--~--~---



[sage-support] Re: A little question about mod in function definition

2009-07-01 Thread Kwankyu

You should define a Python function

def m(x):
return mod(x,10)


Kwankyu
--~--~-~--~~~---~--~~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~--~~~~--~~--~--~---