From: Leonid Shifrin on
Hi Stuart,

for completeness, let me mention that my function will fail in case when
some of the <aelement[i]> has some value at the moment when we create our
delayed assignment. Observe:

In[1]:=
Table[aelement[i] = i, {i, 3}]

Out[1]= {1, 2, 3}

In[2]:=
Scan[assignElements[#, "element", 3] &, {"a", "b"}]

In[3]:= ?a

Global`a

a:={1,2,3}

This is probably not what you would like. Here is a somewhat more
sophisticated version that is free from this flaw:

Clear[assignElementsFull];
assignElementsFull[name_String, elemName_String, length_Integer] :=
With[{heldName = ToExpression[name, InputForm, Hold]},
Clear @@ heldName;
With[{heldElementBaseName =
ToExpression[name <> elemName, InputForm, Hold]},
Block @@ Append[Map[List, heldElementBaseName],
Unevaluated[
Evaluate[First(a)heldName] :=
Evaluate[
Table[First[heldElementBaseName][i], {i, length}]]]]]];

For example:

In[6]:=
Clear[a, aelement];
Table[aelement[i] = i, {i, 3}];
assignElementsFull["a", "element", 3]

In[9]:= ?a

Global`a

a:={aelement[1],aelement[2],aelement[3]}

It will work even in this case:

In[10]:=
aelement = 1;

In[11]:= assignElementsFull["a", "element", 3]

In[11]:= ?a

Global`a

a:={aelement[1],aelement[2],aelement[3]}


Regards,
Leonid





On Wed, Jun 9, 2010 at 4:20 AM, Stuart Nettleton <
Stuart.Nettleton(a)uts.edu.au> wrote:

> Hi Leonid,
> What a wonderful little function!
> Just the thing.
> Many thanks indeed,
> Stuart
>
> --
> UTS CRICOS Provider Code: 00099F
> DISCLAIMER: This email message and any accompanying attachments may contain
> confidential information. If you are not the intended recipient, do not
> read, use, disseminate, distribute or copy this message or attachments. If
> you have received this message in error, please notify the sender
> immediately and delete this message. Any views expressed in this message
> are those of the individual sender, except where the sender expressly, and
> with authority, states them to be the views the University of Technology,
> Sydney. Before opening any attachments, please check them for viruses and
> defects.
>
>

From: Stuart Nettleton on
Hi Lenoid,
Your extended function is much appreciated. It will be even more useful
now you have generalised it. I have avoided using Hold until now because
it seemed to be quite an advanced topic. As you will see from my next
question, I am still a bit vague when it comes to this function.
My intended use for your assignElementsFull function is in a modelling
application that I simplify to the following for illustration:
Clear[Prev, eqns, a, b];
Prev[vec_] := Flatten[{First[vec], Most[vec]}];
eqns = {a == Prev[b], b == Table[1, {3}]};
lhs = Map[ToString[#] &, Cases[eqns, Equal[m_, n_] -> m]]
Scan[assignElementsFull[#, "element", 3] &, lhs]
eqns2 = Flatten[Map[Thread[#] &, eqns]]
Solve[eqns2, Flatten[lhs]]

I would like to isolate the left hand side of the equations without
evaluation, and then use these variables to create element vectors. Then I
would like to use the variables in the vector form. My experiments using
Hold to eliminate the errors have not yet been satisfactory. Please excuse
my laborious syntax!
Would you be able to suggest a way forward?
Many thanks,
Stuart

--
UTS CRICOS Provider Code: 00099F
DISCLAIMER: This email message and any accompanying attachments may contain
confidential information. If you are not the intended recipient, do not
read, use, disseminate, distribute or copy this message or attachments. If
you have received this message in error, please notify the sender
immediately and delete this message. Any views expressed in this message
are those of the individual sender, except where the sender expressly, and
with authority, states them to be the views the University of Technology,
Sydney. Before opening any attachments, please check them for viruses and
defects.

From: Stuart Nettleton on
Hi Leonid,
I have found a possible solution:

ClearAll[Prev, eqns, a, b];
Prev[vec_] := Flatten[{First[vec], Most[vec]}];
eqns = Hold[
a == Prev[b],
b == Table[1, {3}]
];
lhs = Map[ToString, Cases[eqns, Equal[m_, n_] -> m]]
Scan[assignElementsFull[#, "element", 3] &, lhs]
eqns2 = Apply[List, Map[ReleaseHold, eqns]]
lhs2 = Cases[eqns2, Equal[m_, n_] -> m]
Solve[eqns2, Flatten[lhs2]]

Stuart

On Thu, 10 Jun 2010 10:28:21 +1000, Stuart Nettleton
<Stuart.Nettleton(a)uts.edu.au> wrote:

> Hi Lenoid,
> Your extended function is much appreciated. It will be even more useful
> now you have generalised it. I have avoided using Hold until now because
> it seemed to be quite an advanced topic. As you will see from my next
> question, I am still a bit vague when it comes to this function.
> My intended use for your assignElementsFull function is in a modelling
> application that I simplify to the following for illustration:
> Clear[Prev, eqns, a, b];
> Prev[vec_] := Flatten[{First[vec], Most[vec]}];
> eqns = {a == Prev[b], b == Table[1, {3}]};
> lhs = Map[ToString[#] &, Cases[eqns, Equal[m_, n_] -> m]]
> Scan[assignElementsFull[#, "element", 3] &, lhs]
> eqns2 = Flatten[Map[Thread[#] &, eqns]]
> Solve[eqns2, Flatten[lhs]]
>
> I would like to isolate the left hand side of the equations without
> evaluation, and then use these variables to create element vectors. Then
> I would like to use the variables in the vector form. My experiments
> using Hold to eliminate the errors have not yet been satisfactory.
> Please excuse my laborious syntax!
> Would you be able to suggest a way forward?
> Many thanks,
> Stuart


--
Dr Stuart Nettleton
PhD, FCPA, MBA, MEngSci, BEng(Hons), GradDipAICD
Senior Lecturer
School of Management & Systems
Faculty of Engineering & IT
Energy Policy Research Centre

This e-mail may be confidential and /or legally privileged. It is
intended solely for the addressee. Access to this e-mail by anyone else
is unauthorised. If you are not the intended recipient, any disclosure,
copying, distribution or any action taken or omitted to be taken in
reliance on it, is prohibited and may be unlawful.

Virus Information: It is the recipient/client's duty to virus scan and
otherwise test the information provided before loading onto any computer
system. No warranty is made that this material is free from computer virus
or any other defect or error. Any loss/damage incurred by using this
material is not the sender's responsibility.

--
UTS CRICOS Provider Code: 00099F
DISCLAIMER: This email message and any accompanying attachments may contain
confidential information. If you are not the intended recipient, do not
read, use, disseminate, distribute or copy this message or attachments. If
you have received this message in error, please notify the sender
immediately and delete this message. Any views expressed in this message
are those of the individual sender, except where the sender expressly, and
with authority, states them to be the views the University of Technology,
Sydney. Before opening any attachments, please check them for viruses and
defects.