1
$\begingroup$

I have the following situation. I will first post the code and then explain what I'm trying to do and what is going wrong

F[1,2] = Function[{x,y}, x^2 E[1] + y^2 E[2]];

coords = {x,y};
coeff[i_][a_, b_][x_,y_] := Coefficient[F[a, b][x,y], E[i]];
derF[a_, b_][x_,y_] := Table[D[coeff[i][a, b][x,y], coords[[j]]], {i, 1, 2}, {j, 1, 2}]

The idea is this: I'm defining some collection of functions of $(x,y)$ that I'm defining as F[a,b]. Each of the functions is a linear combination of the variables E[1] and E[2].

When I define coeff[i][a,b] I want to extract the function that appears as the coefficient of E[i] in F[a,b]. Then I want to define a matrix of functions derF[a,b] that contains the derivatives of the coefficients of E[1] and E[2] appearing in F[a,b].

My problem is that I want derF[a,b] to be a function in the sense that I can put whatever variable as argument. So for example, I would like to be able to use derF[a,b][t,s] and get the matrix with the functions as functions of $(t,s)$.

The problem is that if I use this code and compute derF[a,b][t,s] I get zero. And I do understand why: Mathematica is basically taking the derivative of a function of $(t,s)$ with respect to $(x,y)$. That's not what I want. I want it to first evaluate the derivative and then substitute the variable, but I have no idea how to achieve this.

So, how do I fix this code to achieve the right intent?

$\endgroup$
15
  • $\begingroup$ So, the E[i] are always either E[1] or E[2]? $\endgroup$
    – lericr
    Commented Oct 25 at 14:46
  • $\begingroup$ You have an expression like this F[a, b][x, y], but you've only defined F with F[1,2]. Does that mean a and b will always be 1 or 2? Or is that your actual question--how to define F for any a and b? $\endgroup$
    – lericr
    Commented Oct 25 at 14:51
  • $\begingroup$ I'm sorry, I'm having trouble understanding what you're saying. It seems to me that you either want f1 = Function[{x, y}, x^2 e[1] + y^2 e[2]] or f2[a_, b_] := Function[{x, y}, x^2 e[a] + y^2 e[b]]. Maybe you can give a concrete example of what you expect for f[a,b] for some specific a and b. And then what you expect for coeff[i][a,b][x,y] for some specific i, a, and b. $\endgroup$
    – lericr
    Commented Oct 25 at 15:00
  • $\begingroup$ Also, just a nit, but E is a pre-defined symbol, so you may want to avoid it. $\endgroup$
    – lericr
    Commented Oct 25 at 15:01
  • $\begingroup$ Hi @lericr, sorry, I should have used e[i] instead. It is always just either e[1] or e[2]. Now, I don't have a general formula for F[a,b] as a function of a and b. I have manually defined F[a, b] for a = 1,2,3 and b=1,2,3, but here I just put one of them as an example. The point is that I have a $3\times 3$ matrix of functions of {x,y} which are linear combinations of the symbols e[1] and e[2] $\endgroup$ Commented Oct 25 at 16:49

3 Answers 3

1
$\begingroup$

First of all E is the transcendental number 2.71828 ... Second problem is that you defined F[1,2] but not F[a,b]. Third problem is that even if your definition for F[__] would have been used, it was defined to return a Function, but you need it to return an algebraic expression because that is what Coefficient needs. This might be wanted to define:

Clear[F];
F[m_, n_] := x^2 e[m] + y^2 e[n];

See what you get from F[a,b] then. I could have used F[a_,b_] instead of F[m_,n_], but the version above better illustrates how this works.

Or you might have wanted to define this:

Clear[F];
F[x_,y_]:=x^2 e[1]+y^2 e[2];

See what you get from F[a,b] in that case.

Also instead of e[1], e[2] you could use (e1, e2) or (E1, E2). That will get you closer to code that does what you want.

Tip: Develop your code one step at a time and ensure each step does what you intend before taking on the next step.

$\endgroup$
1
  • $\begingroup$ Thanks @TedErsek. I'm sorry but I was unclear in my question. The point is that for each {a,b} I have a function F[a,b][x,y]. I don't have a general formula that specifies these functions in terms of {a,b}, I only know how to list them individually. In the question I just posted one, namely, F[1,2], but in principle I'd like {a,b} to have values from 1 to 3 for example. $\endgroup$ Commented Oct 25 at 17:03
1
$\begingroup$

I don't really like this, but here's a hack to hopefully get your desired end result. Keep all your other definitions, but change derF like this:

derF[a_, b_][x_, y_] := 
  Table[D[coeff[i][a, b][x, y], {x, y}[[j]]], {i, 1, 2}, {j, 1, 2}]

Per my comment, I would personally prefer to come up with a better abstraction.

$\endgroup$
0
$\begingroup$

I think this is an approach to what you want. I might have made some wrong turns, but I hope it's enough for you to untangle if necessary.

First step: Let's take both derivatives for all of your Fs (we're keeping everything as pure functions).

matrixOfDerivativeFunctions = 
  Transpose[Outer[Construct, Derivative @@@ IdentityMatrix[2], F @@@ Tuples[Range[3], 2]]]
(* {{Derivative[1, 0][F[1, 1]], Derivative[0, 1][F[1, 1]]},
    {Derivative[1, 0][F[1, 2]], Derivative[0, 1][F[1, 2]]}, 
    {Derivative[1, 0][F[1, 3]], Derivative[0, 1][F[1, 3]]}, 
    {Derivative[1, 0][F[2, 1]], Derivative[0, 1][F[2, 1]]},
    {Derivative[1, 0][F[2, 2]], Derivative[0, 1][F[2, 2]]},
    {Derivative[1, 0][F[2, 3]], Derivative[0, 1][F[2, 3]]},
    {Derivative[1, 0][F[3, 1]], Derivative[0, 1][F[3, 1]]},
    {Derivative[1, 0][F[3, 2]], Derivative[0, 1][F[3, 2]]},
    {Derivative[1, 0][F[3, 3]], Derivative[0, 1][F[3, 3]]}} *)

If that looks confusing, just work your way inside out. You said that you had F[n,m] defined for n,m = 1, 2, 3, so that's where the F @@@ Tuples[Range[3], 2] comes from. The transpose is just because I just assumed that's how you want it organized.

Okay, we have a bunch of functions. Let's apply them to some variables, say s and t.

allLinearCombos = Map[Construct[#, s, t] &, matrixOfDerivativeFunctions, {2}]

I won't keep posting the output, because it's tedious and your Fs will be defined already so you should hopefully be getting simpler outputs. Also, I'm just working with abstract, undefined F, so if there are errors here, it might be because of something I didn't take into account with regard to your Fs.

At this point, it seems like you want the coefficients of the "bases", e[1] and e[2].

Outer[Coefficient, allLinearCombos, {e[1], e[2]}]

Note that I'm assuming lowercase e not E.

Of course, you can bundle all this up into a function that gives just the "mini-matrix" for a single F that you had originally.

newDerF[a_, b_][x_, y_] :=
  Outer[
    Coefficient, 
    Map[Construct[#, x, y] &, Through[(Derivative @@@ IdentityMatrix[2])[F[a, b]]]], 
    {e[1], e[2]}]
$\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.