Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #9031 > unrolled thread

How can I make a sequence that includes lists?

Started byJasonE <ebaughjason@gmail.com>
First post2012-03-14 05:46 +0000
Last post2012-03-15 05:31 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  How can I make a sequence that includes lists? JasonE <ebaughjason@gmail.com> - 2012-03-14 05:46 +0000
    Re: How can I make a sequence that includes lists? "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-03-15 05:27 +0000
    Re: How can I make a sequence that includes lists? "Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com> - 2012-03-15 05:28 +0000
    Re: How can I make a sequence that includes lists? Ray Koopman <koopman@sfu.ca> - 2012-03-15 05:31 +0000
    Re: How can I make a sequence that includes lists? Jason Ebaugh <ebaughjason@gmail.com> - 2012-03-15 05:31 +0000

#9031 — How can I make a sequence that includes lists?

FromJasonE <ebaughjason@gmail.com>
Date2012-03-14 05:46 +0000
SubjectHow can I make a sequence that includes lists?
Message-ID<jjpbbn$p9r$1@smc.vnet.net>
Hello folks,

How can I make a sequence that includes lists?
Ex:
Sequence[apple, {rock, tree), {ocean, lake}]

Here is my specific problem:

I use this function to make associative arrays:
IndexedVariable[var_,indicesValues__]:=With[{dict=List[indicesValues]},Scan[(var[#[[1]]]=#[[2]])&,dict]]

(I got it from: http://www.physic.ut.ee/~kkannike/english/prog/mathematica/index.html#true_dict)


Here is an example of how it takes arguments:
IndexedVariable[f, {1, "a"}, {2, "b"}, {3, "c"}]

In[2]:= f[1]
Out[2]= a

In[3]:= f[2]
Out[3]= b
ect...

You can imagine, some associations are too long to type out by hand.
Ex: {1, x1}, {2, x2}...{1000, x1000}

When I make these associations programtically, I get a list back:
{{1, x1}, {2, x2},...{1000, x1000}}

IndexedVariable[f,{{1, x1}, {2, x2}, {3, x3}...{1000, x1000}}]
does not work.

It wants a sequence.


How can I make a sequence that includes lists?

Thanks,
Jason

Jason Ebaugh
Contact: jasonebaugh@jasonebaugh.info
LinkedIn: www.linkedin.com/in/jasonebaugh

[toc] | [next] | [standalone]


#9042

From"Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com>
Date2012-03-15 05:27 +0000
Message-ID<jjrukd$95q$1@smc.vnet.net>
In reply to#9031
On Wed, 14 Mar 2012 05:46:31 -0000, JasonE <ebaughjason@gmail.com> wrote:

> Hello folks,
>
> How can I make a sequence that includes lists?
> Ex:
> Sequence[apple, {rock, tree), {ocean, lake}]
>
> Here is my specific problem:
>
> I use this function to make associative arrays:
> IndexedVariable[var_,indicesValues__]:=With[{dict=List[indicesValues]},Scan[(var[#[[1]]]=#[[2]])&,dict]]
>
> (I got it from:  
> http://www.physic.ut.ee/~kkannike/english/prog/mathematica/index.html#true_dict)
>
>
> Here is an example of how it takes arguments:
> IndexedVariable[f, {1, "a"}, {2, "b"}, {3, "c"}]
>
> In[2]:= f[1]
> Out[2]= a
>
> In[3]:= f[2]
> Out[3]= b
> ect...
>
> You can imagine, some associations are too long to type out by hand.
> Ex: {1, x1}, {2, x2}...{1000, x1000}
>
> When I make these associations programtically, I get a list back:
> {{1, x1}, {2, x2},...{1000, x1000}}
>
> IndexedVariable[f,{{1, x1}, {2, x2}, {3, x3}...{1000, x1000}}]
> does not work.
>
> It wants a sequence.
>
>
> How can I make a sequence that includes lists?
>
> Thanks,
> Jason
>

Just use

IndexedVariable[f, Sequence @@ lst]

or try

IndexedVariable2[var_, lst_List] := (
    MapThread[
     (var[#1] = #2) &,
     Transpose[lst]
    ];
   );
IndexedVariable2[var_, keys__] := IndexedVariable[var, {keys}];

which is both faster than IndexedVariable and accepts either lists or  
sequences of keys.

[toc] | [prev] | [next] | [standalone]


#9043

From"Oleksandr Rasputinov" <oleksandr_rasputinov@ymail.com>
Date2012-03-15 05:28 +0000
Message-ID<jjrulc$96j$1@smc.vnet.net>
In reply to#9031
On Wed, 14 Mar 2012 12:41:26 -0000, Oleksandr Rasputinov  
<oleksandr_rasputinov@ymail.com> wrote:

> On Wed, 14 Mar 2012 05:46:31 -0000, JasonE <ebaughjason@gmail.com> wrote:
>
>> Hello folks,
>>
>> How can I make a sequence that includes lists?
>> Ex:
>> Sequence[apple, {rock, tree), {ocean, lake}]
>>
>> Here is my specific problem:
>>
>> I use this function to make associative arrays:
>> IndexedVariable[var_,indicesValues__]:=With[{dict=List[indicesValues]},Scan[(var[#[[1]]]=#[[2]])&,dict]]
>>
>> (I got it from:  
>> http://www.physic.ut.ee/~kkannike/english/prog/mathematica/index.html#true_dict)
>>
>>
>> Here is an example of how it takes arguments:
>> IndexedVariable[f, {1, "a"}, {2, "b"}, {3, "c"}]
>>
>> In[2]:= f[1]
>> Out[2]= a
>>
>> In[3]:= f[2]
>> Out[3]= b
>> ect...
>>
>> You can imagine, some associations are too long to type out by hand.
>> Ex: {1, x1}, {2, x2}...{1000, x1000}
>>
>> When I make these associations programtically, I get a list back:
>> {{1, x1}, {2, x2},...{1000, x1000}}
>>
>> IndexedVariable[f,{{1, x1}, {2, x2}, {3, x3}...{1000, x1000}}]
>> does not work.
>>
>> It wants a sequence.
>>
>>
>> How can I make a sequence that includes lists?
>>
>> Thanks,
>> Jason
>>
>
> Just use
>
> IndexedVariable[f, Sequence @@ lst]
>
> or try
>
> IndexedVariable2[var_, lst_List] := (
>     MapThread[
>      (var[#1] = #2) &,
>      Transpose[lst]
>     ];
>    );
> IndexedVariable2[var_, keys__] := IndexedVariable[var, {keys}];
>
> which is both faster than IndexedVariable and accepts either lists or  
> sequences of keys.

Sorry:

IndexedVariable2[var_, keys__] := IndexedVariable[var, {keys}];

in the above should have been

IndexedVariable2[var_, keys__] := IndexedVariable2[var, {keys}];

(I renamed my version of the function before posting but forgot to update  
the definition.)

[toc] | [prev] | [next] | [standalone]


#9047

FromRay Koopman <koopman@sfu.ca>
Date2012-03-15 05:31 +0000
Message-ID<jjrur6$99r$1@smc.vnet.net>
In reply to#9031
On Mar 13, 10:46 pm, JasonE <ebaughja...@gmail.com> wrote:
> Hello folks,
>
> How can I make a sequence that includes lists?
> Ex:
> Sequence[apple, {rock, tree), {ocean, lake}]
>
> Here is my specific problem:
>
> I use this function to make associative arrays:
> IndexedVariable[var_,indicesValues__]:=With[{dict=List[indicesValues]},Scan[(var[#[[1]]]=#[[2]])&,dict]]
>
> (I got it from:http://www.physic.ut.ee/~kkannike/english/prog/mathematica/index.html...)
>
> Here is an example of how it takes arguments:
> IndexedVariable[f, {1, "a"}, {2, "b"}, {3, "c"}]
>
> In[2]:= f[1]
> Out[2]= a
>
> In[3]:= f[2]
> Out[3]= b
> ect...
>
> You can imagine, some associations are too long to type out by hand.
> Ex: {1, x1}, {2, x2}...{1000, x1000}
>
> When I make these associations programtically, I get a list back:
> {{1, x1}, {2, x2},...{1000, x1000}}
>
> IndexedVariable[f,{{1, x1}, {2, x2}, {3, x3}...{1000, x1000}}]
> does not work.
>
> It wants a sequence.
>
> How can I make a sequence that includes lists?
>
> Thanks,
> Jason
>
> Jason Ebaugh
> Contact: jasoneba...@jasonebaugh.info
> LinkedIn:www.linkedin.com/in/jasonebaugh

In[1]:= assoc = {{1, "a"}, {2, "b"}, {3, "c"}}

Out[1]= {{1,a},{2,b},{3,c}}

In[2]:= IndexedVariable[f,Sequence@@assoc]

Out[2]= IndexedVariable[f,{1,a},{2,b},{3,c}]

[toc] | [prev] | [next] | [standalone]


#9048

FromJason Ebaugh <ebaughjason@gmail.com>
Date2012-03-15 05:31 +0000
Message-ID<jjrus4$9ag$1@smc.vnet.net>
In reply to#9031
>From email:


One option is to use Apply:

IndexedVariable[f, ##] & @@ {{1, x1}, {2, x2}, {3, x3}}

Regards,
       Sseziwa

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web