5
$\begingroup$

I have a matrix with 4 elements that I want to turn into functions without explicitely defininig all the functions "by hand":

matrix={{(6. u2 - 3 v2 + 0.3 u2 x11 + 0.1 v2 x11)/(
  3 u2 + 1. v2), (-15. + 1.5 u2 x12 + 0.5 v2 x12)/(-3. u2 - v2)}, {(
  5. u2 v2 + 3.6 u2 x21 + 1.2 v2 x21)/(
  3 u2 + 1. v2), (-9. u2 + 2 v2 - 1.8 u2 x22 - 0.6 v2 x22)/(
  3 u2 + 1. v2)}}

in other words, in order to define a function for each element I have to manually specify the functions:

eq1[u1_,u2_, v1_,v2_, x11_,x12_,x21_,x22_] := matrix[[1,1]];
eq2[u1_,u2_, v1_,v2_, x11_,x12_,x21_,x22_] := matrix[[1,2]];
eq3[u1_,u2_, v1_,v2_, x11_,x12_,x21_,x22_] := matrix[[2,1]];
eq4[u1_,u2_, v1_,v2_, x11_,x12_,x21_,x22_] := matrix[[2,2]];

(Although the arguments do not all appear in the RHS, they might in different scenarios, so I chose to specify all arguments).

I tried:

Table[eq[i, j][u1_,u2_, v1_,v2_, x11_,x12_,x21_,x22_] := matrix[[i, j]], {i, 2}, {j, 2}]

but that does not seem to work. Or at least I don't get how to access eq now:

eq[1, 1]

does not work. Any help appreciated.

$\endgroup$
6
$\begingroup$
matrix = {{(6. u2 - 3 v2 + 0.3 u2 x11 + 0.1 v2 x11)/(3 u2 + 1. v2),
    (-15. + 1.5 u2 x12 + 0.5 v2 x12)/(-3. u2 - v2)},
   {(5. u2 v2 + 3.6 u2 x21 + 1.2 v2 x21)/(3 u2 + 1. v2),
    (-9. u2 + 2 v2 - 1.8 u2 x22 - 0.6 v2 x22)/(3 u2 + 1. v2)}};

Table[eq[i, j][u1_, u2_, v1_, v2_, x11_, x12_, x21_, x22_] := 
  Evaluate[matrix[[i, j]]], {i, 2}, {j, 2}];

?eq

(definitions displayed)

Evaluation examples

eq[1, 1][1, 2, 3, 4, 5, 6, 7, 8]

0.5

Table[eq[i, j][1, 2, 3, 4, 5, 6, 7, 8], {i, 2}, {j, 2}]

{{0.5, -1.5}, {12.4, -5.8}}

$\endgroup$
  • 1
    $\begingroup$ Ah I see..well I was close :). Thank you! $\endgroup$ – holistic Dec 15 '18 at 16:11

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

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