Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2006
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

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

Search the Archive

Re: Defining N for a new entity

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66794] Re: Defining N for a new entity
  • From: Peter Pein <petsie at dordos.net>
  • Date: Tue, 30 May 2006 05:49:10 -0400 (EDT)
  • References: <200605280103.VAA23345@smc.vnet.net> <e5bsuq$afo$1@smc.vnet.net> <e5ei6d$800$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Giuseppe Bilotta schrieb:
> On Sun, 28 May 2006 10:10:34 +0000 (UTC), Carl K. Woll wrote:
> 
>> Giuseppe Bilotta wrote:
>>> So I was looking for a way to tell Mathematica that applying N to an
>>> AffineExpression should only actually apply it to c and xi, something
>>> like
>>>
>>> N[AffineExpression[c_, dev_]] :=
>>>   AffineExpression[N[c], MapAt[N,#,{2}]&/@ dev]
>>>
>>> but if I actually do this and then call 
>>>
>>> AffineExpression[1, {{1, 1}}]
>>> N[%]
>>>
>>> the Mathematica kernel (5.2.0.0) dies without any message.
>>>
>>> Does anybody have an idea of what could the reason be? And what could
>>> I do as a workaround?
>> One idea is to use the attribute NHoldRest (or if you change your data 
>> structure, NHoldFirst). For example:
>>
>> SetAttributes[AffineExpression, NHoldRest]
>>
>> In[42]:= N[AffineExpression[.5, {{1, .2}, {2, .1}}]]
>>
>> Out[42]= AffineExpression[0.5, {{1, 0.2}, {2, 0.1}}]
> 
> Thank you very much, this does the trick. It has the rather small
> side-effect of preventi N from working on the coefficients
> (N at AffineExpression[.5,{{1,Pi/6}}] will not numerize Pi/6), but it's
> good enough for my application.
> 
> Do you have any ideas on why the Mathematica kernel dies without a
> message?
> 

Salute Giuseppe,

  just guessing...

N[AffineExpression[c_,dev_]]:=... might evaluate the left hand side before applying SetDelayed. This is an explanation, why Mathematica doesn't complain about N being protected.
But N[AffineExpression[c_,dev_]] evaluates to AffineExpression[c_,dev_], leaving you with a hidden recursion:

N[f[x_]]:=f[N[x]]
N[f[13]] (* choose your favourite unlucky number here *)

reproduces the crash.

If you don't want the NHoldRest-solution, you might try:

In[1]:=
a1=AffineExpression[1,{{1,1},{2,Pi}}];

AffineExpression /: HoldPattern[N][AffineExpression[c_, dev_]] :=
     AffineExpression[N[c], MapAt[N, #, 2]& /@ dev]

N[a1]
--> AffineExpression[1.,{{1,1.},{2,3.14159}}]

Ciao,
   Peter


  • Prev by Date: Re: Mixed tensors as arguments of compiled functions
  • Next by Date: Re: Problem with a limit.
  • Previous by thread: Re: Defining N for a new entity
  • Next by thread: Re: Defining N for a new entity