Message Boards Message Boards

0
|
8943 Views
|
8 Replies
|
0 Total Likes
View groups...
Share
Share this post:
GROUPS:

Vector as an argument in function definition

Posted 10 years ago

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[[1]]^2  + G[[2]]^2 - 
    2 Q Sin[\[Theta]] (G[[1]] Cos[\[Phi]] + 
       G[[2]] Sin[\[Phi]])], {\[Theta], \[Theta]0, 
    0}, {\[Phi], \[Phi]0, 0}]
POSTED BY: Al Guy
8 Replies

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 BY: Gianluca Gorni
Posted 10 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 BY: Al Guy
Posted 10 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[1]:= tuplesPlus[x_, n_, y_] = Tuples[x, n] + y

During evaluation of In[1]:= Tuples::normal: Nonatomic expression expected at position 1 in Tuples[x,n]. >>

Out[1]= y + Tuples[x, n]

In[2]:= tuplesHeld[x_, n_, y_] = Hold[Tuples[x, n]] + y

Out[2]= y + Hold[Tuples[x, n]]

In[3]:= tuplesHeld[{a, b, c}, 2, d] // ReleaseHold

Out[3]= {{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 BY: David Keith
Posted 10 years ago
In[17]:= 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[17]= Sqrt[g1^2 + g2^2 - 2 g1 Q Cos[\[Phi]0] Sin[\[Theta]0] - 
 2 g2 Q Sin[\[Theta]0] Sin[\[Phi]0]]

In[18]:= c02[{a, b}, c, d, e]

Out[18]= Sqrt[a^2 + b^2 - 2 a e Cos[\[Phi]0] Sin[c] - 
 2 b e Sin[c] Sin[\[Phi]0]]
POSTED BY: David Keith
Posted 10 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[[1]] is longer than depth of object. >>
POSTED BY: Al Guy
Posted 10 years ago

Could you post an example of a complaint?

In[12]:= c0[G_, \[Theta]0_, \[CapitalPhi]0_, Q_] := 
 Normal@SeriesCoefficient[
   Sqrt[G[[1]]^2 + G[[2]]^2 - 
     2 Q Sin[\[Theta]] (G[[1]] Cos[\[Phi]] + 
        G[[2]] Sin[\[Phi]])], {\[Theta], \[Theta]0, 
    0}, {\[Phi], \[Phi]0, 0}]

In[13]:= c0[{a, b}, c, d, e]

Out[13]= Sqrt[a^2 + b^2 - 2 a e Cos[\[Phi]0] Sin[c] - 
 2 b e Sin[c] Sin[\[Phi]0]]
POSTED BY: David Keith
Posted 10 years ago

Doesn't work.

POSTED BY: Al Guy

You can try with

c0[G_?VectorQ, \[Theta]0_, \[CapitalPhi]0_, Q_]

Beware that using capital letters for variables is not recommended in Mathematica.

POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract