MathGroup Archive 2006

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

Search the Archive

Re: Defining N for a new entity

  • To: mathgroup at
  • Subject: [mg66740] Re: [mg66719] Defining N for a new entity
  • From: "Carl K. Woll" <carlw at>
  • Date: Sun, 28 May 2006 06:08:23 -0400 (EDT)
  • References: <>
  • Sender: owner-wri-mathgroup at

Giuseppe Bilotta wrote:
> Hello,
> I'm try to build a Mathematica toolbox to work with Stolfi's affine
> expression. Until now, I've had no problems: I use AffineExpression[c,
> {{i, xi}, ...}] (where c and xi are Reals and the i are Integers) to
> represent c + \sum xi e_i and I can define addition, multiplication,
> quotient and everything else without any problems.
> Recently, I've begun using these entities in more complex expression,
> and in particular as elements of matrices to be fed to LinearSolve. 
> In the specific context, LinearSolve seems to apply N to all of its
> arguments, but as a side-effect of this the indices (i in the
> expressions above) are turned into Reals, so that for example
> AffineExpression[ .5, {{1, .2},{2, .1}}]
> comes out as
> AffineExpression[ .5, {{1., .2}, {2., .1}}]
> This has aesthetical and functional disadvantages (my codes also
> exploits the fact that the indices are integer), so I have to apply
> Rationalize or some other such function to re-convert the indices into
> Integers.
> 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 ( 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}}]

Carl Woll
Wolfram Research

  • Prev by Date: RE: Different text style for labels and ticks
  • Next by Date: Behavior of ReplaceAll with Computed Results from a Conditional Test
  • Previous by thread: Defining N for a new entity
  • Next by thread: Re: Defining N for a new entity