When Must I Use lambda?

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 close merge delete

Sort by ยป oldest newest most voted

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)


more

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.

( 2016-12-20 09:55:50 +0200 )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.

( 2016-12-20 16:43:37 +0200 )edit