MathGroup Archive 2001

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

Search the Archive

Re: Trouble modif. Matrix in function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg29072] Re: [mg29068] Trouble modif. Matrix in function
  • From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
  • Date: Sun, 27 May 2001 18:04:40 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

First of all, a minor point. Defining a double index list with
 In[1]:=
 TavGen[n_]:=Table[Table[1, {i, 1, p}], {p, 1, n}]

 is rather clumsy. You can get the same thing with

In[2]:=
TavGen1[n_]:=Table[1,{p,1,n},{i,1,p}]

Checking:

In[3]:=
TavGen1[5]==TavGen[5]

Out[3]=
True

As for your problem, it seems to be is one of the most common problems
people used to other programming languages encounter with Mathematica.

Your function ElimG
ElimG[P_, Rig_, Pos_, Offset_:0] :=
  (P[[Rig, #]] = 0) & /@ Table[i, {i, Pos, Pos + Offset}]

when called first evaluates it arguments, and then substitutes them into the
body of the function. When that happens P is no longer a symbol but an
evaluated matrix, so what you are doing is basically the same as if you
tried to evaluate:

In[5]:=
{{1,2},{3,4}}[[1,1]]=0
Set::setps: {{1,2},{3,4}} in assignment of part is not a symbol.
Out[5]=
0

There are several ways to deal with this but the simplest, I think,  is to
give your function the HoldFirst attribute, which will tell it not to
evaluate the first argument.

In[6]:=
TAB = TavGen[6]
Out[6]=
{{1},{1,1},{1,1,1},{1,1,1,1},{1,1,1,1,1},{1,1,1,1,1,1}}
In[7]:=
SetAttributes[ElimG,HoldFirst]
In[8]:=
 ElimG[P_, Rig_, Pos_, Offset_:0] :=
  (P[[Rig, #]] = 0) & /@
    Table[i, {i, Pos, Pos + Offset}]
In[9]:=
ElimG[TAB, 3, 2]
Out[9]=
{0}
In[10]:=
TAB
Out[10]=
{{1},{1,1},{1,0,1},{1,1,1,1},{1,1,1,1,1},{1,1,1,1,1,1}}


-- 
Andrzej Kozlowski
Toyama International University
JAPAN

http://platon.c.u-tokyo.ac.jp/andrzej/
http://sigma.tuins.ac.jp/~andrzej/



on 01.5.27 10:54 AM, Nicola at nicola-mingotti at nospam-libero.it wrote:

> Hello ,
> 
> I'm new to the comp.soft-sys.math.mathematica
> so I hope this question has not been asked many time yet :)
> 
> So :
> 
> Defining
> 
> TavGen[n_] := Table[
> Table[1, {i, 1, p}], {p, 1, n}]
> 
> TAB = TavGen[6]
> 
> Function 1) works , 2) does not work
> ***********************************************
> 1)  Elim[Rig_, Pos_, Offset_:0] :=
> (TAB[[Rig, #]] = 0) & /@ Table[i, {i, Pos, Pos + Offset}]
> *************************************************
> 
> *************************************************
> 2) ElimG[P_, Rig_, Pos_, Offset_:0] :=
> (P[[Rig, #]] = 0) & /@ Table[i, {i, Pos, Pos + Offset}]
> *************************************************
> 
> ElimG[TAB, 3, 2]
> this gives me :
> 
> Set::"setps": "\!\({\(\({1}\)\), \(\({1, 1}\)\), \(\({1, 0, 1}\)\), \(\({1,
> \
> 1, 1, 1}\)\), \(\({1, 1, 1, 1, 1}\)\), \(\({1, 1, 1, 1, 1, 1}\)\)}\) in \
> assignment of part is not a symbol."
> 
> **************************************************
> 
> I tried using a For instead of a Map but nothing changes  :(
> 
> Thanks in advance for your help .
> 
> Please excuse my poor explanations and English .
> 
> Bye.
> 
> 
> 
> 
> 
> 
> 
> 
> 




  • Prev by Date: Re: Newbie solving mod M linear equations
  • Next by Date: RE: Animations and Options Inspector
  • Previous by thread: Re: Trouble modif. Matrix in function
  • Next by thread: Re: OOP