ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 13 Aug 2014 23:12:22 -0500Defining a function of vector variableshttps://ask.sagemath.org/question/23758/defining-a-function-of-vector-variables/I would like to define a Sage function that takes a vector variable and computes the result of some vector function on it, something like
f(v) = v.dot_product(vector((1,1)))
However, when I try to run this in Sage, I get
AttributeError: 'sage.symbolic.expression.Expression' object has no
attribute 'dot_product'
Is there a way to achieve this effect in Sage?Wed, 13 Aug 2014 12:06:57 -0500https://ask.sagemath.org/question/23758/defining-a-function-of-vector-variables/Comment by kcrisman for <p>I would like to define a Sage function that takes a vector variable and computes the result of some vector function on it, something like</p>
<pre><code>f(v) = v.dot_product(vector((1,1)))
</code></pre>
<p>However, when I try to run this in Sage, I get</p>
<pre><code>AttributeError: 'sage.symbolic.expression.Expression' object has no
attribute 'dot_product'
</code></pre>
<p>Is there a way to achieve this effect in Sage?</p>
https://ask.sagemath.org/question/23758/defining-a-function-of-vector-variables/?comment=23769#post-id-23769To elaborate, `f(v)` creates a symbolic variable `v`, which is not (necessarily) a vector. Sage doesn't currently have symbolic vector types per se.Wed, 13 Aug 2014 23:12:22 -0500https://ask.sagemath.org/question/23758/defining-a-function-of-vector-variables/?comment=23769#post-id-23769Answer by zmwangx for <p>I would like to define a Sage function that takes a vector variable and computes the result of some vector function on it, something like</p>
<pre><code>f(v) = v.dot_product(vector((1,1)))
</code></pre>
<p>However, when I try to run this in Sage, I get</p>
<pre><code>AttributeError: 'sage.symbolic.expression.Expression' object has no
attribute 'dot_product'
</code></pre>
<p>Is there a way to achieve this effect in Sage?</p>
https://ask.sagemath.org/question/23758/defining-a-function-of-vector-variables/?answer=23760#post-id-23760 You can always use a Python `def`:
sage: def f(v):
return v.dot_product(vector((1,1)))
sage: f(vector((2,1)))
3Wed, 13 Aug 2014 12:54:56 -0500https://ask.sagemath.org/question/23758/defining-a-function-of-vector-variables/?answer=23760#post-id-23760