MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: calculating with vectors of variable length

  • To: mathgroup at
  • Subject: [mg124607] Re: calculating with vectors of variable length
  • From: Ulrich Arndt <ulrich.arndt at>
  • Date: Thu, 26 Jan 2012 03:28:53 -0500 (EST)
  • Delivered-to:
  • References: <>

this should work if a and x should be in variable length and giving as input parameters.

f::nonequallength =
  "The argument `1` and `2` do not have the same length.";
f[x_?VectorQ, a_?VectorQ] :=
 If[Length[a] == Length[x], a.x, Message[f::nonequallength, x, a]]

f[{1, 2, 3}, {a1, a2, a3}]
f[{b1, b2, b3, b4}, {a1, a2, a3, a4}]
f[{b1, b2, b3, b4}, {a1, a2, a3}]

a = {a1, a2, a3};
b = {b1, b2, b3};

f[a, b]
b = {b1, b2, b3, b4};
f[a, b]

or do you want to generate a based on the length of x?

f[x_?VectorQ] := Table[Subscript[a, i], {i, 1, Length[x]}].x
f[{x1, x2, x3}]
f[{x1, x2, x3,x4}]


Am 25.01.2012 um 13:08 schrieb Benjamin Hell:

> Hi,
> I'd like to work with vectors of variable length. A short example would
> be this:
> a = {a1,...,an}
> f[x_]:=a.x
> The vector x should be treated as a vector {x1,...,xn} and n should be
> variable and not be specified prior to any calculations.
> One possible calculation would be:
> D[f,x3]
> which should give a3 according to the definition of f above.
> How can I implement that in mathematica?
> Thanks in advance,
> Benjamin

  • Prev by Date: FindRoot exploration of parameter space
  • Next by Date: Re: NDSolve and DAEs
  • Previous by thread: calculating with vectors of variable length
  • Next by thread: UnitStep plotting