ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 20 Dec 2016 16:43:37 +0100When Must I Use `lambda`?https://ask.sagemath.org/question/36051/when-must-i-use-lambda/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?Tue, 20 Dec 2016 05:21:48 +0100https://ask.sagemath.org/question/36051/when-must-i-use-lambda/Answer by mforets for <p>I tried
<code>ahat(atheta)=vector([cos(atheta),sin(atheta),0])</code> and got:
<code>TypeError: unable to convert (cos(atheta), sin(atheta), 0) to a symbolic
expression</code>.</p>
<p>Then I tried <code>ahat=lambda atheta: vector([cos(atheta),sin(atheta),0])</code> and got what I obviously wanted.</p>
<p>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?</p>
<p>Perhaps this is just a bug as indicated by the traceback line:</p>
<p><code>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)</code></p>
<p>Shouldn't this be the same as that generated by the labmda declaration above?</p>
<p><code>/tmp/tmp4V6zSv/___code___.py:exec compile(u'ahat=lambda atheta:vector([cos(atheta),sin(atheta),_sage_const_0 ])' + '\n', '', 'single')
</code></p>
<p>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?</p>
https://ask.sagemath.org/question/36051/when-must-i-use-lambda/?answer=36053#post-id-36053For 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](https://ask.sagemath.org/question/35101/functions-of-variables-with-matrices/#35111).Tue, 20 Dec 2016 09:38:19 +0100https://ask.sagemath.org/question/36051/when-must-i-use-lambda/?answer=36053#post-id-36053Comment by mforets for <p>For vector and matrix valued functions, you can also do:</p>
<pre><code>sage: var('atheta')
sage: ahat=vector([cos(atheta),sin(atheta),0])
sage: ahat(atheta=0)
(1,0,0)
</code></pre>
<p>See also this <a href="https://ask.sagemath.org/question/35101/functions-of-variables-with-matrices/#35111">related question</a>.</p>
https://ask.sagemath.org/question/36051/when-must-i-use-lambda/?comment=36055#post-id-36055That 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](https://trac.sagemath.org/ticket/12075).Tue, 20 Dec 2016 09:55:50 +0100https://ask.sagemath.org/question/36051/when-must-i-use-lambda/?comment=36055#post-id-36055Comment by jabowery for <p>For vector and matrix valued functions, you can also do:</p>
<pre><code>sage: var('atheta')
sage: ahat=vector([cos(atheta),sin(atheta),0])
sage: ahat(atheta=0)
(1,0,0)
</code></pre>
<p>See also this <a href="https://ask.sagemath.org/question/35101/functions-of-variables-with-matrices/#35111">related question</a>.</p>
https://ask.sagemath.org/question/36051/when-must-i-use-lambda/?comment=36059#post-id-36059This 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.Tue, 20 Dec 2016 16:43:37 +0100https://ask.sagemath.org/question/36051/when-must-i-use-lambda/?comment=36059#post-id-36059