Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: simple iteration question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg89824] Re: simple iteration question
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sat, 21 Jun 2008 05:32:59 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <g3g017$l06$1@smc.vnet.net>

Francisco Gutierrez wrote:

> I have a list of the form list={{3,2,1},{5,6,1},{10,5,1}}, of any length
> I have a vector, say vector={1,2,3}
> I want to make the following iteration.  Take the first component of the =
> list, make the dot product of it and the vector, and substract a number (sa=
> y 50). If the result is bigger or equal than zero, stop and return the vect=
> or. If it is less than zero, then change the vector in the followig way: ve=
> ctor+list[[2]], and repeat the test.
> Iterate until the result is equal or bigger than zero, or until the list en=
> ds.
> I haven't found a neat way of doing this. Any help is welcome

Assuming I have correctly understood what you are looking for and since 
you did not show what you have already tried, I have chosen an approach 
that blends procedural and functional programming paradigms. Definitely 
not the best peace of code I ever written, but it has the merit to 
exemplify the versatility of Mathematica. (Note that the code below has 
not been extensively tested and that your specifications are sometimes 
ambiguous.)

In[1]:= f[vec_, lst_, n_] :=
  Module[{v = vec, test},
   test[v1_, v2_] = v1.v2 - n >= 0;
   If[Select[lst, test[v, #] &, 1] =!= {}, Return[v]];
   For[i = 2, i <= Length[list], i++,
    v += lst[[i]];
    If[Select[lst, test[v, #] &, 1] =!= {}, Return[v]];
    ];
   "No vectors were found."
   ]
list = {{3, 2, 1}, {5, 6, 1}, {10, 5, 1}};
vector = {1, 2, 3};
f[vector, list, 50]
f[vector, list, 1000]

Out[4]= {6, 8, 4}

Out[5]= "No vectors were found."


Regards,
-- Jean-Marc


  • Prev by Date: Insight into Solve...
  • Next by Date: Re: solving for 2 angles
  • Previous by thread: Re: Insight into Solve...
  • Next by thread: Re: simple iteration question