Ask Your Question

oldbrad's profile - activity

2021-02-02 13:07:35 +0100 received badge  Famous Question (source)
2020-06-10 14:55:10 +0100 received badge  Notable Question (source)
2018-06-13 22:05:38 +0100 received badge  Popular Question (source)
2018-06-06 11:41:31 +0100 received badge  Student (source)
2017-01-07 09:17:28 +0100 received badge  Notable Question (source)
2017-01-07 09:17:28 +0100 received badge  Popular Question (source)
2012-08-30 18:14:55 +0100 answered a question Way to make default output numerical not symbolic?

Wrap with (..).n() at the appropriate stage eg:

(vector([cos(rd(40)),sin(rd(40))])*60).n()
(45.9626665871387, 38.5672565811924)

or:

v=(vector([cos(rd(40)),sin(rd(40))])*60).n();v
(45.9626665871387, 38.5672565811924)

then:

v.norm()
60.0000000000000
2012-08-30 17:33:16 +0100 commented answer preparser state for regexs

No no no .. Something infinitly more diabolical! Experement with two cells as follows Cell one: preparser(False); print type(2) preparser(True); print type(2) Cell two: preparser(True); print type(2) preparser(False); print type(2) preparser(True) Also comment the last and see what happens. I'm totaly confused. I anycase, surely one should leave the preparser in the on state?

2012-08-30 11:58:18 +0100 asked a question preparser state for regexs

I would have expected the following to work:

reset()
import re

preparser(False)                         # preparser off else we have \\ instead of \
rx=re.compile(r".*(\sHun\s).*")          # in the regex

m = rx.match(r"The Attila the Hun Show") # Find "Hun" in "The Attila the Hun Show"
if m: print "found =>", m.group(1)       # 1 needs to be 1 not '1' here!

preparser(True)

In fact I get 'IndexError: no such group'. Using int(1) fixes the problem, but why? The preparser is off!

Even more curious the block works, as is, on every other evaluate! I tried moving the preparser(False) statement to before the import, but that makes no difference.

2012-08-27 10:44:12 +0100 commented answer How do I exec a function of N variables.

Can't quite see how to "accept". Is it a thumbs up or a tick or something else?

2012-08-27 10:41:49 +0100 received badge  Scholar (source)
2012-08-27 10:41:49 +0100 marked best answer How do I exec a function of N variables.

Usually needing to use exec is a sign something's wrong with a design, but I don't use @interacts much so maybe it's the right way here. It's definitely a little dangerous for remote use, though: if you give me access to this interact over the net I can probably delete all your files.

The problem is that you're using Sage-specific syntax handles by the Sage prepareser and exec doesn't know what to do with it. You can work around that in a few ways, say by calling the preparser specifically:

sage: reset()
sage: s = "f(t1, t2) = cos(t1) + sin(t2)"
sage: exec(s)
------------------------------------------------------------
   File "<string>", line 1
SyntaxError: can't assign to function call (<string>, line 1)

sage: preparse(s)
'__tmp__=var("t1,t2"); f = symbolic_expression(cos(t1) + sin(t2)).function(t1,t2)'
sage: exec(preparse(s))
sage: f
(t1, t2) |--> sin(t2) + cos(t1)

or by using the alternate function syntax:

sage: exec("g = (cos(t1)+sin(t2)).function(t1, t2)")
sage: g
(t1, t2) |--> sin(t2) + cos(t1)

where you'd have to make sure the variables were defined somewhere.

2012-08-27 10:41:47 +0100 received badge  Supporter (source)
2012-08-23 18:57:54 +0100 received badge  Teacher (source)
2012-08-23 18:30:59 +0100 answered a question Multiple loops in an animation?

Does this give you a clue?

def move(a):
    t=var('t')
    p1=Graphics()
    p2=Graphics()
    v = []
    p1 =parametric_plot((t,t*a),(t,-5,5),color="red")
    p2 =parametric_plot((t*a,t),(t,-5,5),color="blue")
    v.append(p1+p2)
    return v

animate([move(a) for a in srange(-5,5,1)], xmin=-2,ymin=-2,xmax=2,ymax=2,figsize=[2,2]).show()

Is this what you want to achieve? I'm not sure.

def move(i):
    p1=Graphics()
    p2=Graphics()
    p1 += parametric_plot((t,i),(t,-5,5),color="red")
    p2 += parametric_plot((i,t),(t,-5,5),color="blue")
    return p1+p2

animate([move(i) for i in srange(-5,5,1)]).show()
2012-08-22 15:56:11 +0100 answered a question How do I exec a function of N variables.

Is an embedded exec any more insecure than a live session in which anyone could just write one?

2012-08-22 15:52:00 +0100 commented answer How do I exec a function of N variables.

Nice answer thanks.

2012-08-21 14:49:37 +0100 asked a question How do I exec a function of N variables.

The python code: f(x,y)=sin(x) + cos(y) Can be both differentiated and evaluated.

like this: dfx=diff(f,x); dfy=diff(f,y) then evaluated like this: f(pi/6,pi/9) -> cos(1/9pi) + 1/2 dfx(pi/6,pi/9) -> 1/2sqrt(3)

I'm wanting to do something like: exec("f(t1,t2)=cos(t1)+sin(t2)") so that I can write an @interact function to define the equations of N variables interactively. When I try the above exec I receive the error: "SyntaxError: can't assign to function call"

I can almost do it, but I'm getting into a horrible inelegant tangle of of eval statements in the process!

Is there a clean way to do it with sage?