1
$\begingroup$

I have a matrix given as a function of is indices and another free parameter as follows

A[x_, i_, j_] = x*i*j

Now I would like to evaluate this for a given vector of indices $i, j$, e.g.

iVec = {1,2}
jVec = {1,2}

such that the matrix

AMat[x_] = {{x,2*x},{2*x,4*x}}

is returned. We may assume that the matrix is square, i.e. iVec and jVec have the same length and the first argument of the function A is supposed to correspond to the row index of the resulting matrix.

Is there any way to do this? Ideally I would like to write something like

AMat[x_] = A[x, iVec, jVec]

but that will not work because Mathematica doesn't know which vector to treat as a transpose.

EDIT: The input should be A[x_, i_, j_] without modifying it's functional form. The reason is that I compute that automatically.

$\endgroup$
  • 2
    $\begingroup$ Perhaps A[x_,i_,j_]:=x*Outer[Times,i,j]; A[x,{1,2},{1,2}] ? $\endgroup$ – Bill Jul 4 '18 at 15:05
  • $\begingroup$ @Bill great minds... see MarcoB's answer and my comment to it. $\endgroup$ – Wolpertinger Jul 4 '18 at 15:26
1
$\begingroup$
ClearAll[AMat]
AMat[x_, iVec_, jVec_] := x Outer[Times, iVec, jVec]

AMat[x, {1, 2}, {1, 2}]
(* Out: {{x, 2 x}, {2 x, 4 x}} *)

In response to the edit in the OP, in order to use the existing definition of A:

(* Existing definition of A *)
ClearAll[A]
A[x_, i_, j_] = x*i*j

(* New definition of AMat *)
ClearAll[AMat]
AMat[x_, iVec_, jVec_] := Table[A[x, i, j], {i, iVec}, {j, jVec}]

So now:

AMat[x, {1, 2}, {1, 2}]
(* Out: {{x, 2 x}, {2 x, 4 x}} *) 
$\endgroup$
  • $\begingroup$ thanks for your answer! This works of course, but I originally wanted a way that does not modify A[x_, i_, j_]. The background is that A in terms of the indices is computed automatically. So I guess what is really missing is a way to automatically get from the form of A[x_, i_, j_] in the question to the second line in your answer. My question was probably unclear in that respect, I'll clarify it. +1 for the solution anyway! $\endgroup$ – Wolpertinger Jul 4 '18 at 15:25
  • $\begingroup$ @Wolpertinger See if the edit works for you $\endgroup$ – MarcoB Jul 4 '18 at 15:32
  • $\begingroup$ brilliant! Exactly what I wanted! $\endgroup$ – Wolpertinger Jul 4 '18 at 15:36
  • $\begingroup$ @Wolpertinger Great! Glad it worked. $\endgroup$ – MarcoB Jul 4 '18 at 15:37

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.