MathGroup Archive 2007

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

Search the Archive

Re: Sign Declaration

  • To: mathgroup at smc.vnet.net
  • Subject: [mg80105] Re: [mg80028] Sign Declaration
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sun, 12 Aug 2007 07:26:23 -0400 (EDT)
  • References: <200708110602.CAA02393@smc.vnet.net>

On 11 Aug 2007, at 08:02, Mr Ajit Sen wrote:

>  Dear MathGroup,
>
>  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[]?
>

For example:
In[1]:= A = {a, b, c, d, e, f, g, h};
In[2]:= Do[Sign[A[[i]]] ^= Sign[5.5 - i], {i, 8}]
In[3]:= Sign /@ A
Out[3]= {1, 1, 1, 1, 1, -1, -1, -1}

(you can use Table instead of Do if you prefer it).



>  2. How do I achieve the same thing with the construct
>
>               x/:Sign[x]=1
>     in Table ?

Why do  you keep insisting on using Table? Here is one way using  
Function and Map:

A = {a, b, c, d, e};

Block[{x}, x /: Sign[x] = 1; Function[v, UpValues[v] = UpValues[x] /.  
x -> v] /@ A;]
Sign /@ A
{1, 1, 1, 1, 1}

I think I would just use
>
>  3. Any more efficient ways of doing those sign
>     declarations?
>


It depends on what sort of efficiency you are talking about. If you  
want o be able to easily add positive and negative symbols I would  
suggest a different approach. Use a heads, e.g. Pos and Neg to  
distinguish positive and negative symbols and define:

Pos /: Sign[Pos[x_]] = 1;
Neg /: Sign[Neg[x_]] = -1;

Then


Sign /@ Join[Pos /@ {a, b, c, d, e}, Neg /@ {f, g, h}]
{1, 1, 1, 1, 1, -1, -1, -1}

and you can easily creat as many positive or negative symbols as you  
like by simply mapping Pos or Neg on arbitrary lists.

Andrzej Kozlowski


  • Prev by Date: Re: hardware for Mathematica 6.0
  • Next by Date: Re: Mathematica v5 versus v6
  • Previous by thread: Sign Declaration
  • Next by thread: Re: Sign Declaration