MathGroup Archive 2011

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

Search the Archive

Re: Preventing unwanted threading by Inner

  • To: mathgroup at smc.vnet.net
  • Subject: [mg123480] Re: Preventing unwanted threading by Inner
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
  • Date: Thu, 8 Dec 2011 05:27:28 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <jbni23$47f$1@smc.vnet.net>

On Wed, 07 Dec 2011 11:18:27 -0000, Chris Young <cy56 at comcast.net> wrote:

> I'm trying to prevent Inner from expanding my points into coordinates:
>
> In[283]:= Inner[Times, {a, b, c}, {{A, \[Alpha]}, {B, \[Beta]}, {C,  
> \[Chi]}}, Plus]
> Out[283]= {a A + b B + c C, a \[Alpha] + b \[Beta] + c \[Chi]}
>
> As I understand it, this has to do with Times being Listable:
>
> In[304]:= Attributes[Times]
> Out[304]= {Flat, Listable, NumericFunction, OneIdentity, Orderless,  
> Protected}
>
> But clearing the listable attribute doesn't do anything:
>
> Unprotect[Times];
> ClearAttributes[Times, Listable];
> myInnnerProd = Inner[Times, {a, b, c}, {{A, \[Alpha]}, {B, \[Beta]}, {C,  
> \[Chi]}}, Plus];
> SetAttributes[Times, Listable];
> Protect[Times];
>
> In[315]:= myInnnerProd
>
> Out[315]= {a A + b B + c C, a \[Alpha] + b \[Beta] + c \[Chi]}
>
> In addition, I have to resort to using Hold on the points. Not sure
> why; I would have thought that making Times non-listable would have
> been enough.
>
> In[343]:=
> Unprotect[Times];
> ClearAttributes[Times, Listable];
> myInnerProd =
>   Inner[Times, {a, b, c}, Hold /@ {{A, \[Alpha]}, {B, \[Beta]}, {C,  
> \[Chi]}}, Plus];
> myInnnerProd = ReleaseHold[myInnerProd]
> SetAttributes[Times, Listable];
> Protect[Times];
>
> Out[346]= a {A, \[Alpha]} + b {B, \[Beta]} + c {C, \[Chi]}
>

Redefining built-in functions in such a drastic way is seldom a good idea  
because Mathematica makes assumptions about the way these functions  
operate and does not handle alterations gracefully. In this case it seems  
that Inner does not check to see if the Attributes set on Times have been  
changed and instead uses a direct internal (kernel) function call as if it  
had the default Attributes.

I assume you want this form for display purposes or because you want to  
keep the coordinates as isolated entities. In that case, rather than  
redefining Times, you could use:

coordinate /: Format[coordinate[pts_List]] := pts

{a, b, c} . coordinate /@ {{A, \[Alpha]}, {B, \[Beta]}, {C, \[Chi]}}



  • Prev by Date: Re: Ploting a transformation of a set
  • Next by Date: Re: 100 rows and 100 columns random matrix
  • Previous by thread: Re: Preventing unwanted threading by Inner
  • Next by thread: Re: Preventing unwanted threading by Inner