33 views

Skip to first unread message

Jul 25, 2009, 3:31:17 PM7/25/09

to sage-support

Dear all,

I have been puzzled, and annoyed, by an unexpected side-effect. It

can be demonstrated in the notebook by the following example. It

models the speed as distance / time, then solve for the distance, then

evaluate the distance with different arguments:

var('v d t')

equation = v == d / t

solution = solve(equation, d, solution_dict = True)

d_ = solution[0][d]; print d_ #-> t*v

constants= {v: 2}

print d_(constants, t=3) #--> 6

# BEWARE: the previous call has the side effect that t=3 in later

evaluation !

print d_(constants) #--> 6 : the side-effect is apparent here !

print d_ #--> t*v

I would expect d_(constants) to result in t*2, but instead its using a

previous assignment of t to yield 6 !

I'm using Sage for an engineering pblm, and I find it useful to

separate constants from arguments. The side effect is thus annoying

to me: is there any other way to evaluate a function with constants

and arguments, without any side-effect with the arguments ? I'm new

to this forum, so not sure if it has been raised before. Sorry if

there is an easy answer.

Thanks in advance.

PC

I have been puzzled, and annoyed, by an unexpected side-effect. It

can be demonstrated in the notebook by the following example. It

models the speed as distance / time, then solve for the distance, then

evaluate the distance with different arguments:

var('v d t')

equation = v == d / t

solution = solve(equation, d, solution_dict = True)

d_ = solution[0][d]; print d_ #-> t*v

constants= {v: 2}

print d_(constants, t=3) #--> 6

# BEWARE: the previous call has the side effect that t=3 in later

evaluation !

print d_(constants) #--> 6 : the side-effect is apparent here !

print d_ #--> t*v

I would expect d_(constants) to result in t*2, but instead its using a

previous assignment of t to yield 6 !

I'm using Sage for an engineering pblm, and I find it useful to

separate constants from arguments. The side effect is thus annoying

to me: is there any other way to evaluate a function with constants

and arguments, without any side-effect with the arguments ? I'm new

to this forum, so not sure if it has been raised before. Sorry if

there is an easy answer.

Thanks in advance.

PC

Message has been deleted

Jul 25, 2009, 3:51:01 PM7/25/09

to sage-support

Thank you for your quick answer, Minh.

Although it would give the correct answer, it does not satisfy my wish

to distinguish constants from parameters. In my engineering pblm, I

have a set of several constants that I use in many statements, and I

wish to keep them together nicely, and use them in only one keyword.

In your solution, I would have to repeat them every time, and this is

not convenient.

Any idea why Sage behaves as it does, and how to circument this

strange behavior while keeping the "constant" idea ?

Thanks

PC

On 25 juil, 21:43, Minh Nguyen <nguyenmi...@gmail.com> wrote:

> Hi Pierre,

> Is the following what you're looking for?

>

> ----------------------------------------------------------------------

> | Sage Version 4.1, Release Date: 2009-07-09 |

> | Type notebook() for the GUI, and license() for information. |

> ----------------------------------------------------------------------

> sage: var("v, d, t");

> sage: equation = v == d/t

> sage: solution = solve(equation, d, solution_dict=True)

> sage: solution

> [{d: t*v}]

> sage: sol_d = solution[0][d]; sol_d

> t*v

> sage: sol_d.substitute(v=2)

> 2*t

> sage: sol_d.substitute(t=3)

> 3*v

> sage: sol_d.substitute(t=3, v=2)

> 6

> sage: sol_d

> t*v

>

> --

> Regards

> Minh Van Nguyen

Although it would give the correct answer, it does not satisfy my wish

to distinguish constants from parameters. In my engineering pblm, I

have a set of several constants that I use in many statements, and I

wish to keep them together nicely, and use them in only one keyword.

In your solution, I would have to repeat them every time, and this is

not convenient.

Any idea why Sage behaves as it does, and how to circument this

strange behavior while keeping the "constant" idea ?

Thanks

PC

On 25 juil, 21:43, Minh Nguyen <nguyenmi...@gmail.com> wrote:

> Hi Pierre,

>

> ----------------------------------------------------------------------

> | Sage Version 4.1, Release Date: 2009-07-09 |

> | Type notebook() for the GUI, and license() for information. |

> ----------------------------------------------------------------------

> sage: var("v, d, t");

> sage: equation = v == d/t

> sage: solution = solve(equation, d, solution_dict=True)

> sage: solution

> [{d: t*v}]

> sage: sol_d = solution[0][d]; sol_d

> t*v

> sage: sol_d.substitute(v=2)

> 2*t

> sage: sol_d.substitute(t=3)

> 3*v

> sage: sol_d.substitute(t=3, v=2)

> 6

> sage: sol_d

> t*v

>

> --

> Regards

> Minh Van Nguyen

Jul 25, 2009, 4:07:14 PM7/25/09

to sage-s...@googlegroups.com

On Sat, Jul 25, 2009 at 12:31 PM, pca<pierre.ca...@gmail.com> wrote:

>

> Dear all,

>

> I have been puzzled, and annoyed, by an unexpected side-effect. It

> can be demonstrated in the notebook by the following example. It

> models the speed as distance / time, then solve for the distance, then

> evaluate the distance with different arguments:

>

> var('v d t')

> equation = v == d / t

> solution = solve(equation, d, solution_dict = True)

> d_ = solution[0][d]; print d_ #-> t*v

> constants= {v: 2}

> print d_(constants, t=3) #--> 6

> # BEWARE: the previous call has the side effect that t=3 in later

> evaluation !

This is *definitely* a bug in Sage. I've made this trac #6622:
>

> Dear all,

>

> I have been puzzled, and annoyed, by an unexpected side-effect. It

> can be demonstrated in the notebook by the following example. It

> models the speed as distance / time, then solve for the distance, then

> evaluate the distance with different arguments:

>

> var('v d t')

> equation = v == d / t

> solution = solve(equation, d, solution_dict = True)

> d_ = solution[0][d]; print d_ #-> t*v

> constants= {v: 2}

> print d_(constants, t=3) #--> 6

> # BEWARE: the previous call has the side effect that t=3 in later

> evaluation !

http://trac.sagemath.org/sage_trac/ticket/6622

Here's how to workaround it (namely, put "dict(constants)" instead of

"constants"):

sage: var('v t')

sage: f = v*t

sage: s = {v: 2}

sage: f(dict(s), t=3)

6

sage: s

{v: 2}

William

> print d_(constants) #--> 6 : the side-effect is apparent here !

> print d_ #--> t*v

>

> I would expect d_(constants) to result in t*2, but instead its using a

> previous assignment of t to yield 6 !

>

> I'm using Sage for an engineering pblm, and I find it useful to

> separate constants from arguments. The side effect is thus annoying

> to me: is there any other way to evaluate a function with constants

> and arguments, without any side-effect with the arguments ? I'm new

> to this forum, so not sure if it has been raised before. Sorry if

> there is an easy answer.

>

> Thanks in advance.

> PC

>

> >

>

William Stein

Associate Professor of Mathematics

University of Washington

http://wstein.org

Jul 25, 2009, 4:19:13 PM7/25/09

to sage-support

GREAT ! Thanks for the amazing support !

I have just used Sage for one day, and I was disappointed by this side-

effect. But with such an outstanding support, I'm now convinced it's

the tool to use !

PC

On 25 juil, 22:07, William Stein <wst...@gmail.com> wrote:

I have just used Sage for one day, and I was disappointed by this side-

effect. But with such an outstanding support, I'm now convinced it's

the tool to use !

PC

On 25 juil, 22:07, William Stein <wst...@gmail.com> wrote:

Reply all

Reply to author

Forward

0 new messages

Search

Clear search

Close search

Google apps

Main menu