Re: Matrices & Compile
- To: mathgroup at smc.vnet.net
- Subject: [mg22558] Re: Matrices & Compile
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Sat, 11 Mar 2000 17:52:35 -0500 (EST)
- Organization: Universitaet Leipzig
- References: <8a7ng3$k4e@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
my C compiler say L-value required in this case.
To assign a value you need a *memory* location to store
the result, a must be a symbol, with a value of a matrix
*and* the function must have the attribute HoldRest.
SetAttributes[f,HoldRest]
f[n_Integer,a_Symbol]:=a[[2,2]]=3
But since you need the symbol "a" to assing a value to some
memory cell in the value of a you can't compile it.
When you compile the arguments must be copied and the memory
location is lost.
Regards
Jens
If yo
Kurt Taretto wrote:
>
> Hi,
>
> I'm making some functions using Compile, but I found some features of
> Compile which I don't fully
> understand yet. The following trivial function should take an nxn
> matrix and asign the value 1.0
> to the element (2,2) of that matrix:
>
> In[1]:=
> f = Compile[{{n, _Integer}, {a, _Real, 2}}, a[[2, 2]] = 1.0]
>
> Out[1]=
> CompiledFunction[{n, a}, a[[2, 2]] = 1., "-CompiledCode-"]
>
> The compilation was succesfull, but the function does not work:
>
> In[2]:=
> a = {{3, 2}, {1, 4}};
>
> In[3]:=
> f[2, a]
>
> Set::"setps": {{3, 2}, {1, 4}}; in assignment of part is not a symbol."
>
> Out[3]=
> 1.
>
> In[4]:=
> a
>
> Out[4]=
> {{3, 2}, {1, 4}}
>
> If I assign matrix a to an object b, everything goes well
>
> In[5]:=
> f = Compile[{{n, _Integer}, {a, _Real, 2}},
> Module[{b}, b = a; b[[2, 2]] = 1; b]]
>
> Out[5]=
> CompiledFunction[{n, a},
> Module[{b}, b = a; b[[2, 2]]= 1;b], "-CompiledCode-"]
>
> In[6]:=
> a = f[2, a]
>
> Out[6]=
> {{3., 2.}, {1., 1.}}
>
> The problem with this alternative is the unecessary memory requirement
> for b, and also the
> need for extra notation. I'm dealing with functions that take several
> large matrices as arguments,
> and do operations on their elements. Making statements like the above
> "b=a" makes the resulting code
> unnecesary complicated and more difficult to read. Is there any way out
> of this or did I make some
> simple mistake?
>
> Any help will be appreciated! Thanks!
>
> Kurt Taretto