4
$\begingroup$

I want to define an indexed function $f$ with index $k$ over some arguments $m$. However, I only want to define the index for some set ${1,2,...,x}$ my approach was the following

For[k=1,k<x+1,k++,    
f[k,m_]:= g[k,m]
]
Clear[k]

However this seems not to define the function as

In[1]=f[3,n]

yields

Out[1]=g[k,n]

Is there another way to define $f$ as a function of $m$ but with index $k$ on the respective interval?

(Clarification: the original code has $k$ as an index-subscript, but I couldn't find how to do this here)

$\endgroup$
3
  • $\begingroup$ I don't understand. If you simply define f[k_, m_] := g[k, m] (without the loop) then whenever you ask for f[3,n] you will get g[3,n]. If you only want it defined for integers, then use f[k_IntegerQ, m_] := g[k, m] $\endgroup$
    – bill s
    Commented Sep 9, 2016 at 14:33
  • 2
    $\begingroup$ Denoting by list the list of elements {1, 2, ..., x} (where x is given), you can do f[k_Integer, m_] /; MemberQ[list, k] := g[k, m], or MemberQ[Range[x], k] (where x is given) if you're only considering a set of consecutive integers up to x. $\endgroup$
    – user31159
    Commented Sep 9, 2016 at 14:54
  • $\begingroup$ Welcome! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – user9660
    Commented Sep 9, 2016 at 15:52

2 Answers 2

5
$\begingroup$

You issue is, that the right hand side of your definition - the g[k,m] part - is not evaluated and the value of k is not inserted. You can see this by looking at a simple Trace:

ClearAll[f];

k = 1;
Trace[f[k, m_] := g[k, m]]
(* {f[k,m_]:=g[k,m],{{k,1},f[1,m_]},Null} *)

If you really want to follow your approach, you need to ensure, that the value of k is inserted, even if the rhs is not evaluated. This can be done using a pure function or With. I'm using Array or Range or Table to get rid of the ugly For loop. All three version should work, where the last one does indeed look funny:

ClearAll[f];
(f[#, m_] := g[#, m]) & /@ Range[10];

ClearAll[f];
Array[(f[#, m_] := g[#, m]) &, 10];

ClearAll[f];
Table[With[{i = i}, f[i, m_] := g[i, m]], {i, 10}]
$\endgroup$
1
$\begingroup$

You can force a function to be defined for certain integer variable. For instance

f[x_,n_Integer]:= n Sin[x]

will be evaluated only if n is an integer

Try f[2.34,3] and f[2.34, 3.2] or even f[2.34,3.]

the two last function will not be evaluated

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.