0
$\begingroup$

I'm having a little crysis over here. What I basically want to do is some sort of 3D-Plot, but I'm only interested in the resulting values. I have two vectors x and p:

x={x1,x2}; p={p1,p2};

and I want to calculate the value of the function f depending on 2 variables, for example

f[x_,p_]=x*p;

The input

f[x,p]

however does only calculate the diagonal elements and not all terms f[x1,p1], f[x1,p2], f[x2,p1] and f[x2,p2] what I would intuiveley expect from it.

I tried some "Map" commands but didn't get the desired result. Could someone please help me out? And if possible even share some insight of why Mathematica is doing only the diagonal elements by default? I would be very interested in it because I have the feeling that my way of thinking is often quite perpendicular to the way Mathematica does -.-

$\endgroup$
  • 1
    $\begingroup$ You want something like Outer[Times, x, p] in your function. Just multiplying lists in the fashion of your example will give the respective elements multiplied, e.g., {a,b,c}*{d,e,f} results in {ab,be,cf}. $\endgroup$ – ciao Jan 31 '14 at 0:44
  • $\begingroup$ Thanks alot, in the more general case of f not simply being a multiplication it will be Outer[f,x,p] as I checked just now. Edit: btw. How can I write in code-style in comments? $\endgroup$ – user104857 Jan 31 '14 at 0:51
  • 1
    $\begingroup$ @user104857 for code in comments use backticks: `a` to print a $\endgroup$ – Mr.Wizard Jan 31 '14 at 2:20
1
$\begingroup$

Here's a way similar to the one proposed by rasher in the comments:

Times@@@Tuples[{x, p}]
$\endgroup$

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.