# Basic replace with parameters questions

I have looked over MMA SE but seen nothing to help clarify this for me.

what I want to do is set some parameters for a curve, and then I want to automatically replace the x,y,z of, say, a vector field with those new parameters.

For example, I have this code:

r[t_] = {3 Cos [t], 3 Sin [t], 0}; (*Parametrized S*)

F[x_, y_] = {2 y Cos[z], Exp[x] Sin[z], x Exp[y]};


I want to replace the x, y, and z in F with the parameters for x,y, and z I set up in r[t]. I've looked at the Replace function, but couldn't quite get it to work.

• In[51]:= F[x, y] /. Thread[{x, y, z} -> r[t]] Out[51]= {6 Sin[t], 0, 3 E^(3 Sin[t]) Cos[t]} Jan 13 '21 at 14:38

## 2 Answers

r[t_] = {3 Cos[t], 3 Sin[t], 0};(*Parametrized S*)


F has three parameters so it should have three arguments.

F[x_, y_, z_] = {2 y Cos[z], Exp[x] Sin[z], x Exp[y]};

F @@ r[t]

(* {6 Sin[t], 0, 3 E^(3 Sin[t]) Cos[t]} *)

• Hmmm..... That doesn't give me your nice answer. I'm using MMA 12.2, and it gives me a strange vector <x^2 z^2, y^2 z^2 etc.> Jan 12 '21 at 16:13
• Did you clear all of the previous definitions? Use Clear["Global*"] then try again. Jan 12 '21 at 16:16
• I tried that, but no, alas. Strange. Jan 12 '21 at 16:18
• It gives me this: {x^2 z^2, y^2 z^2, x y z}[3 Cos[t], 3 Sin[t], 0]. Jan 12 '21 at 16:20
• @Abcderia Try restarting the kernel. It's working for me and I get {6 Sin[t], 0, 3 E^(3 Sin[t]) Cos[t]}. You almost certainly assigned something to F. Jan 12 '21 at 16:21

I ran into this problem many years ago when I was learning Mathematica. I came up with this solution, which I've since learned has some drawbacks. However, for many uses, it's perfectly fine and convenient, and the drawbacks have no impact on performance. It takes advantage of polymorphism.

ClearAll[F];
F[{x_, y_, z_}] := F[x, y, z]; (* define F for a point *)
F[x_, y_, z_] := {2 y Cos[z], Exp[x] Sin[z], x Exp[y]}; (* 3-variable function *)


Note that the value F[point] is defined in terms of F[x, y, z], so that if you need to change the formula for F, you just need to change it in one place.

Since I was teaching multivariable calculus back then, I used this all the time. There are two main drawbacks, one which applies only to versions since V4 with the introduction of packed arrays. Since V4, my use of this approach has diminished. The other drawback has to do with the Listability of mathematical expression such as 2 y Cos[z], etc. If you don't use F on lists of points or separate lists of coordinates, then this won't matter. My applications of Mathematica to teaching did not use Listability, so everything worked fine.

It is possible to deal with the Listability problem, at least for rectangular arrays, as follows:

ClearAll[F];
F[a_?ArrayQ] /; Last@Dimensions[a] == 3 :=
Transpose[
F @@ Transpose[a, RotateLeft@Range@ArrayDepth[a]],
RotateRight@Range@ArrayDepth[a]];
F[x_, y_, z_] := {2 y Cos[z], Exp[x] Sin[z], x Exp[y]}; (* 3-variable function *)


Examples:

F[1., 2., 3.]
(* {-3.95997, 0.383604, 7.38906} *)

F[{1., 2., 3.}]
(* {-3.95997, 0.383604, 7.38906} *)

F[ConstantArray[{1., 2., 3.}, {2, 2}]]
(* {{{-3.95997, 0.383604, 7.38906}, {-3.95997, 0.383604, 7.38906}},
{{-3.95997, 0.383604, 7.38906}, {-3.95997, 0.383604, 7.38906}}}
*)
`

To some extent, this takes advantage of packed arrays at the expense of two transposes and one unpacking to level 1.