Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Say I have an expression (matrix) that depends on several variables.

f= {{a, b+c},{2a-b^2,c^5+a b}}

I want to define new matrices with the single parameter dependencies removed. For example, I want:

ma = f - Block[{a=0}, f]
mb = f - Block[{b=0}, f]
mc = f - Block[{c=0}, f]

or perhaps I should be using

ma = f - (f/.a->0)
mb = f - (f/.b->0)
mc = f - (f/.c->0)

Thusfar, these implementations work as expected. But in my actual problem, there are many more parameters than three so I would like to design something such as:

m[var_]:= f - Block[{var=0}, f]


m[var_]:= f - (f/.var->0)

The goal here is to create one general function matrix that I can specify a parameter, and it removes the parameter-free part. These methods give errors and don't work.

Another method I've tried is to simply create a Table of individual matrices rather than a matrix function. e.g.:

Table[m[z]] := f - Block[{z=0}, f], {z, {a, b, c}}] 

which should make individual expressions for m[a], m[b], and m[c]. This also doesn't work correctly.

My last idea would be to make a rank 3 tensor m using Table which has subdivisions that are the matrices I want (a list of matrices, essentially). This would be implemented, it seems, by simply adding one extra set of square brackets around z in the code above, resulting in a rank 3 tensor for m, with m[[a]], m[[b]], and m[[c]] as the appropriate matrices. No luck here either.

Actually, this Table method of defining many functions has not worked for me in the past in simpler contexts either. What is the proper way of solving a problem like this?

share|improve this question
m[var_] := f - (f /. var :> 0); m[a] == f - (f /. a -> 0) – Dr. belisarius Jan 30 '14 at 18:28
Perhaps you need ClearAll[m] ? – Dr. belisarius Jan 30 '14 at 18:29
It does seem that it works, as you say, with or without the delayed var specification (-> or :>). Sad mistake on my part I guess... – Steve Jan 30 '14 at 18:51
It happens! no problem! – Dr. belisarius Jan 30 '14 at 18:55
While I've got this here though, I would ask, can anyone implement properly the second method, creating a Table of functions? I feel like I've encountered this problem before in other contexts as well. Is there a good way to define a set of functions like this? should the "index" follow as a single or double bracket, or is there a way to build it directly into the string name of the functions? e.g. function[a],function[b],function[c] or function[[a]],function[[b]],function[[c]], or functiona,functionb,functionc – Steve Jan 30 '14 at 18:55

Is this the table you are looking for?

f= {{a, b+c},{2a-b^2,c^5+a b}};
tab = Table[f - (f /. z -> 0), {z, {a, b, c}}]

Now you can access the various versions by tab[[1]], tab[[2]], etc. Alternatively, if you want to access the various elements by name, then a function definition might be more appropriate:

f = {{a, b + c}, {2 a - b^2, c^5 + a b}};
g[x_] := f - (f /. x -> 0)

Now you can access the desired quantities by g[a], g[b], etc. Of course, this is basically the answer suggested by belisarius.

share|improve this answer
I appreciate this and it does work. I guess the "problem" with this solution is you still have to mentally associate the indices with the ordinal indices it replaces them (you have to remember that a corresponds to 1, etc.). What would be even more appealing in a larger system, especially, would be if you could create the variables with the same indices, i.e. being able to call them with tab[[a]], tab[[b]], rather than tab[[1]], tab[[2]]. – Steve Jan 30 '14 at 20:17
Hmm, I see now. I appreciate the help from you and Belisarius. – Steve Jan 30 '14 at 20:48

Your two m functions appear to work as intended, though you had a syntax error in the Block one which I corrected in the question. On your example both yield:

m[var_] := f - (f /. var -> 0)

m /@ {a, b, c} // Column
{{a, 0}, {2 a, a b}}
{{0, b}, {-b^2, a b}}
{{0, c}, {0, c^5}}

In a comment to bill's question you write:

What would be even more appealing in a larger system, especially, would be if you could create the variables with the same indices, i.e. being able to call them with tab[[a]], tab[[b]], rather than tab[[1]], tab[[2]].

That seems to be exactly what this m function does. Perhaps your issue is that you do not want to recompute the values with each call; in that case you can add memoization like this:

m[var_] := m[var] = f - (f /. var -> 0)

If this doesn't do what you want please try again to explain the problem.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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