Re: Defining N for a new entity
- To: mathgroup at smc.vnet.net
- Subject: [mg66740] Re: [mg66719] Defining N for a new entity
- From: "Carl K. Woll" <carlw at wolfram.com>
- Date: Sun, 28 May 2006 06:08:23 -0400 (EDT)
- References: <200605280103.VAA23345@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
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 (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}}] Carl Woll Wolfram Research
- References:
- Defining N for a new entity
- From: Giuseppe Bilotta <bilotta78@hotpop.com>
- Defining N for a new entity