MathGroup Archive 2007

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

Search the Archive

Re: Sign Declaration

  • To: mathgroup at smc.vnet.net
  • Subject: [mg80074] Re: Sign Declaration
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sun, 12 Aug 2007 07:10:16 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <f9jjfn$2d5$1@smc.vnet.net>

Mr Ajit Sen wrote:
>  Given the list  A= {a,b,c,d,e,f,g,h}, I'd like to
> tell
>  Mathematica (5.2) that a,b,...,e are +ve, and f,g,h are -ve.
> 
>  I'm using the following
> 
>           Table[Sign[A[[i]]]^=1,{i,5}];
> 
>           Table[Sign[A[[i]]]^=-1,{i,6,8}];
> 
> 
>  My queries are :
> 
>  1. Is it possible to combine the above into 1
> Table[]?
<snip>

Yes. For instance,

Table[Sign[A[[i]]] ^= If[i > 5, -1, 1], {i, 8}];

Table[Sign[A[[i]]] ^= -1 + 2*Boole[i < 6], {i, 8}];

Table[Sign[A[[i]]] ^= 1 - 2*UnitStep[i - 6], {i, 8}];

will all yield the desired results.

Here is some tests:

In[1]:= Clear /@ A;
A = {a, b, c, d, e, f, g, h};
Table[Sign[A[[i]]] ^= 1, {i, 5}];
Table[Sign[A[[i]]] ^= -1, {i, 6, 8}];
UpValues /@ A

Out[5]= {{HoldPattern[Sign[a]] :> 1}, {HoldPattern[Sign[b]] :>
    1}, {HoldPattern[Sign[c]] :> 1}, {HoldPattern[Sign[d]] :>
    1}, {HoldPattern[Sign[e]] :>
    1}, {HoldPattern[Sign[f]] :> -1}, {HoldPattern[
     Sign[g]] :> -1}, {HoldPattern[Sign[h]] :> -1}}

In[6]:= Clear /@ A;
A = {a, b, c, d, e, f, g, h};
Table[Sign[A[[i]]] ^= If[i > 5, -1, 1], {i, 8}];
UpValues /@ A

Out[9]= {{HoldPattern[Sign[a]] :> 1}, {HoldPattern[Sign[b]] :>
    1}, {HoldPattern[Sign[c]] :> 1}, {HoldPattern[Sign[d]] :>
    1}, {HoldPattern[Sign[e]] :>
    1}, {HoldPattern[Sign[f]] :> -1}, {HoldPattern[
     Sign[g]] :> -1}, {HoldPattern[Sign[h]] :> -1}}

In[10]:= Clear /@ A;
A = {a, b, c, d, e, f, g, h};
Table[Sign[A[[i]]] ^= -1 + 2*Boole[i < 6], {i, 8}];
UpValues /@ A

Out[13]= {{HoldPattern[Sign[a]] :> 1}, {HoldPattern[Sign[b]] :>
    1}, {HoldPattern[Sign[c]] :> 1}, {HoldPattern[Sign[d]] :>
    1}, {HoldPattern[Sign[e]] :>
    1}, {HoldPattern[Sign[f]] :> -1}, {HoldPattern[
     Sign[g]] :> -1}, {HoldPattern[Sign[h]] :> -1}}

In[14]:= Clear /@ A;
A = {a, b, c, d, e, f, g, h};
Table[Sign[A[[i]]] ^= 1 - 2*UnitStep[i - 6], {i, 8}];
UpValues /@ A

Out[17]= {{HoldPattern[Sign[a]] :> 1}, {HoldPattern[Sign[b]] :>
    1}, {HoldPattern[Sign[c]] :> 1}, {HoldPattern[Sign[d]] :>
    1}, {HoldPattern[Sign[e]] :>
    1}, {HoldPattern[Sign[f]] :> -1}, {HoldPattern[
     Sign[g]] :> -1}, {HoldPattern[Sign[h]] :> -1}}

-- 
Jean-Marc


  • Prev by Date: Re: Indexed element treated as number?
  • Next by Date: Re: Re: Re: Locator Appearance mystery in LocatorPane
  • Previous by thread: Re: Sign Declaration
  • Next by thread: Re: Sign Declaration