Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #1284

Re: alternatives to MapIndexed?

From DrMajorBob <btreat1@austin.rr.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: alternatives to MapIndexed?
Date 2011-03-30 09:16 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <imuscs$i8f$1@smc.vnet.net> (permalink)

Show all headers | View raw


The output asked for is garbled with unmatched brackets, and the verbal  
description is vague.

In {{1}, a}, I see that 1 is the depth of a, but in {{2}, b}, 2 is NOT the  
depth of b.

Try again?

In the meantime, take a look at

MapIndexed[({#2, #1}) &, F[a, b, F[x, y, G[v, F[q, r], w], z], c],
   Infinity] //. {{a___}, b_} :> {Length@{a}, b}

F[{1, a}, {1, b}, {1,
   F[{2, x}, {2, y}, {2,
     G[{3, v}, {3, F[{4, q}, {4, r}]}, {3, w}]}, {2, z}]}, {1, c}]

Bobby

On Tue, 29 Mar 2011 07:00:20 -0500, Michael <michael2718@gmail.com> wrote:

> Are there any alternatives to MapIndexed for determining where in an
> expression a sub-expression is?
>
> Specifically, I am trying to figure out a way to attach the depth of
> certain sub-expressions to those sub-expressions.  However, I want to
> ignore (or include only) certain heads in the calculation of depth.
>
> As an example:
>
> In[137]:= MapIndexed[({#2, #1}) &,
>   F[a, b, F[x, y, G[v, F[q, r], w], z], c], Infinity]
>
> Out[137]= F[{{1}, a}, {{2}, b}, {{3},
>    F[{{3, 1}, x}, {{3, 2}, y}, {{3, 3},
>      G[{{3, 3, 1}, v}, {{3, 3, 2},
>        F[{{3, 3, 2, 1}, q}, {{3, 3, 2, 2}, r}]}, {{3, 3, 3}, w}]}, {{3,
>        4}, z}]}, {{4}, c}]
>
> What I would like is something that produces the output:
>
> F[{{1}, a}, {{2}, b}, {{3},
>    F[{{3, 1}, x}, {{3, 2}, y}, {{3, 3},
>      G[v, F[{{3, 3, 1}, q}, {{3, 3, 2}, r}]}, w]}, {{3,
>        4}, z}]}, {{4}, c}]
>
> i.e. in the above example only expressions with a head of F are counted
> (or those with G are excluded).
>
> An option to MapIndexed to include or exclude based on a pattern would
> be great, if it existed.
>
> I have found that I can kind of hack the solution using Length[Stack[]]
> but it appears to be computationally prohibitive to use this approach
> for large expressions.  All the other things I have thought up wind up
> causing uncontrolled recursion - Mathematica's approach to re-evaluating
> an expression until nothing changes seems to be a big stumbling block I
> have.  Perhaps there is an easy solution I am missing.
>
> Any suggestions?
>
> Thanks,
>
> Michael
>


-- 
DrMajorBob@yahoo.com

Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: alternatives to MapIndexed? DrMajorBob <btreat1@austin.rr.com> - 2011-03-30 09:16 +0000

csiph-web