# Define a function of a vector without fully specifying the components of the vector [duplicate]

Can a function be made to accept a variable amount of inputs?

I have a vector A={1,2,3,4,5,6}A=\{1,2,3,4,5,6\}, and I want to define a function of AA as

f[A_]:=A.X

where XX is another vector. I want to define this function without fully specifying the components of AA, but Mathematica does not recognize this, so I have to write

f[A1_,A2_,A3_,A4_,_A5_,A6_]:= {A1,A2,A3,A4,A5,A6}.X

where Ai,i=1,…,6Ai, i=1,…,6 are equal to 1 to 6, respectively. The second method is not very convenient. So, I was wondering how I can use my first code so that Mathematica recognizes that. Many thanks!

It should be noted that this is not a duplicate of Can a function be made to accept a variable amount of inputs?, because the question you referred to asked about “how to accommodate varying number of inputs in a function”, however, my question is about how to change the representation of vector (based on its fixed components) in a function.

=================

=================

3

=================

f[a_List] := a.{3, 4, 5, 6, 7, 12};

f[{1, 2, 3, 4, 5, 6}]

(*
157
*)

Incidentally, do not use upper-case variables, as it is likely to conflict with internal functions (such as N).

I presume you know the number of components of a (i.e., you’re not asking about inputting a vector of arbitrary length), since you apparently have a fixed x (of known length).

If you want to use x as well, try:

f[a_List] := a.(x = {3, 4, 5, 6, 7, 12})

Thanks! That helped.
– Alex
Apr 9 ’15 at 0:18

f[a__] := {a}.x

x = Range[6];
f[a1, a2, a3, a4, a5, a6]
(* a1 + 2 a2 + 3 a3 + 4 a4 + 5 a5 + 6 a6 *)

Or

g = {##}.x &;
g[a1, a2, a3, a4, a5, a6]
(* a1 + 2 a2 + 3 a3 + 4 a4 + 5 a5 + 6 a6 *)

Thanks you for this!
– Alex
Apr 9 ’15 at 0:34

– Cheshire Cat
Apr 28 at 14:11

If you know that x will always have 6 items then you can restrict f by

f[a_List /; a \[Element] Vectors[6, Reals]] := a.x

This will not evaluate the function when a has the wrong dimensions.

Hope this helps.

Thanks you for this!
– Alex
Apr 9 ’15 at 0:34