# Evaluate a function with vectors and return matrix

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.

• Perhaps A[x_,i_,j_]:=x*Outer[Times,i,j]; A[x,{1,2},{1,2}] ?
– Bill
Jul 4, 2018 at 15:05
• @Bill great minds... see MarcoB's answer and my comment to it. Jul 4, 2018 at 15:26

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}} *)

• 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! Jul 4, 2018 at 15:25
• @Wolpertinger See if the edit works for you Jul 4, 2018 at 15:32
• brilliant! Exactly what I wanted! Jul 4, 2018 at 15:36
• @Wolpertinger Great! Glad it worked. Jul 4, 2018 at 15:37