Ask Your Question
1

When Must I Use `lambda`?

asked 2016-12-19 22:21:48 -0500

this post is marked as community wiki

This post is a wiki. Anyone with karma >750 is welcome to improve it.

I tried ahat(atheta)=vector([cos(atheta),sin(atheta),0]) and got: TypeError: unable to convert (cos(atheta), sin(atheta), 0) to a symbolic expression.

Then I tried ahat=lambda atheta: vector([cos(atheta),sin(atheta),0]) and got what I obviously wanted.

Ordinarily, there is no problem with the ordinary kind of function declaration. How am I to tell when I can and when I cannot use the ordinary kind of function declaration?

Perhaps this is just a bug as indicated by the traceback line:

File "/tmp/tmpi4HZNt/___code___.py", line 3, in <module> exec compile(u'__tmp__=var("atheta"); ahat = symbolic_expression(vector([cos(atheta),sin(atheta),_sage_const_0 ])).function(atheta)

Shouldn't this be the same as that generated by the labmda declaration above?

/tmp/tmp4V6zSv/___code___.py:exec compile(u'ahat=lambda atheta:vector([cos(atheta),sin(atheta),_sage_const_0 ])' + '\n', '', 'single')

That is to say, why are function declarations not treated as syntactic sugars (algebraic identities) for labmda expressions? Is not sagemath a computer aided algebra system?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-12-20 02:38:19 -0500

mforets gravatar image

For vector and matrix valued functions, you can also do:

sage: var('atheta')
sage: ahat=vector([cos(atheta),sin(atheta),0])
sage: ahat(atheta=0)
(1,0,0)

See also this related question.

edit flag offensive delete link more

Comments

That said, I agree that it is natural to use the ordinary kind of function declaration! Look what I found: Create callable matrices in function notation.

mforets gravatar imagemforets ( 2016-12-20 02:55:50 -0500 )edit

This kind of thing is what is killing SageMath. People can't be expected to pick up a new language if its rules for such basic things as vectors aren't even documented. I've been driven to SageMath by the recent, long-persisting, bug in Ubuntu's distribution of Maxima rendering my prior work on that platform unusable. I had tried making the transition a year ago but ran into other problems with SageMath and abandoned that effort.

jabowery gravatar imagejabowery ( 2016-12-20 09:43:37 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2016-12-19 22:21:48 -0500

Seen: 86 times

Last updated: Dec 21 '16