# Vector as an argument in function definition

Posted 8 years ago
7722 Views
|
8 Replies
|
0 Total Likes
|
 Can I somehow specify that G is a vector in function definition so that it won't complain? c0[G_, \[Theta]0_, \[CapitalPhi]0_, Q_] := Normal@SeriesCoefficient[Sqrt[ G[]^2 + G[]^2 - 2 Q Sin[\[Theta]] (G[] Cos[\[Phi]] + G[] Sin[\[Phi]])], {\[Theta], \[Theta]0, 0}, {\[Phi], \[Phi]0, 0}] 
8 Replies
Sort By:
Posted 8 years ago
 What function of G do you have in mind? The following works with immediate assignment, but it won't speed up calculations, I am afraid: f[u_?VectorQ, v_?VectorQ] = u.v Using vector notation you may write your function a little more elegantly c02[g_?VectorQ, \[Theta]0_, \[CapitalPhi]0_, Q_] := Normal@SeriesCoefficient[ Sqrt[g.g - 2 Q Sin[\[Theta]] g.{Cos[\[Phi]], Sin[\[Phi]]}], {\[Theta], \[Theta]0, 0}, {\[Phi], \[Phi]0, 0}] but it only makes sense for a 2-dimensional g, and it only works with delayed assignment. Also, I think SeriesCoefficient does not need Normal.
Posted 8 years ago
 I was just thinking that if G is multidimensional, say 100, there should be a way to pass it as an argument to a function.
Posted 8 years ago
 There are ways to use Hold to defer evaluation, but I have not encountered many cases where that is preferable to just defering the entire expression with SetDelayed. In:= tuplesPlus[x_, n_, y_] = Tuples[x, n] + y During evaluation of In:= Tuples::normal: Nonatomic expression expected at position 1 in Tuples[x,n]. >> Out= y + Tuples[x, n] In:= tuplesHeld[x_, n_, y_] = Hold[Tuples[x, n]] + y Out= y + Hold[Tuples[x, n]] In:= tuplesHeld[{a, b, c}, 2, d] // ReleaseHold Out= {{a + d, a + d}, {a + d, b + d}, {a + d, c + d}, {b + d, a + d}, {b + d, b + d}, {b + d, c + d}, {c + d, a + d}, {c + d, b + d}, {c + d, c + d}} 
Posted 8 years ago
 In:= c02[{g1_, g2_}, \[Theta]0_, \[CapitalPhi]0_, Q_] = Normal@SeriesCoefficient[ Sqrt[g1^2 + g2^2 - 2 Q Sin[\[Theta]] (g1 Cos[\[Phi]] + g2 Sin[\[Phi]])], {\[Theta], \[Theta]0, 0}, {\[Phi], \[Phi]0, 0}] Out= Sqrt[g1^2 + g2^2 - 2 g1 Q Cos[\[Phi]0] Sin[\[Theta]0] - 2 g2 Q Sin[\[Theta]0] Sin[\[Phi]0]] In:= c02[{a, b}, c, d, e] Out= Sqrt[a^2 + b^2 - 2 a e Cos[\[Phi]0] Sin[c] - 2 b e Sin[c] Sin[\[Phi]0]] 
Posted 8 years ago
 You are right, it works with SetDelayed, can we make it work with Set, so that the function will be evaluated right away instead of waiting? Part::partd: Part specification G[] is longer than depth of object. >> 
Posted 8 years ago
 Could you post an example of a complaint? In:= c0[G_, \[Theta]0_, \[CapitalPhi]0_, Q_] := Normal@SeriesCoefficient[ Sqrt[G[]^2 + G[]^2 - 2 Q Sin[\[Theta]] (G[] Cos[\[Phi]] + G[] Sin[\[Phi]])], {\[Theta], \[Theta]0, 0}, {\[Phi], \[Phi]0, 0}] In:= c0[{a, b}, c, d, e] Out= Sqrt[a^2 + b^2 - 2 a e Cos[\[Phi]0] Sin[c] - 2 b e Sin[c] Sin[\[Phi]0]] 
Posted 8 years ago
 Doesn't work.
Posted 8 years ago
 You can try with c0[G_?VectorQ, \[Theta]0_, \[CapitalPhi]0_, Q_] Beware that using capital letters for variables is not recommended in Mathematica.