MathGroup Archive 2002

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

Search the Archive

Re: Some list questions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg37150] Re: [mg37132] Some list questions
  • From: BobHanlon at aol.com
  • Date: Sun, 13 Oct 2002 05:56:38 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

In a message dated 10/12/2002 6:21:51 AM, flip_alpha at safebunch.com writes:

>I have two seperate list questions that I was hoping to get help with.
>
>Question 1.
>
> I have a variable length list similar to that generated by FactorInteger,
>that is {number, exponent} pairs.  An example follows.
>
>lista =  {{2,3},{3,1},{5,1}}  ... this is the number 2^3 * 3^1 * 5^1
>
>I want to generate a list of "all" the products of numbers from this list.
>
>I can tell that I get a total (3+1)*(1+1)*(1+1) = 4*2*2 = 16, products
>and I
>want a list showing all of those.
>
>These would be:
>
>2^3 can generate {2^0, 2^1, 2^2, 2^3} = {1, 2, 4 ,8}
>3^1 can generate {3^0,  3^1} = {3} ... we dont care about the duplicate
>"1"
>5^1 can generate {5^0, 5^1} = {5} ... we dont care about the duplicate
>"1"
>
>Hence the 4*2*2 = 16 (the product of one more of the exponents) above.
>
>Next we should get 16 products (from these lists), namely (I left them
>as
>products below to show what I am after):
>
>{1,  2,  4,  8,  1*3,  2*3,  4*3,  8*3, 1*5, 2 * 5, 4* 5, 8* 5, 1*3*5,
>2*3*5,  4*3*5,  8*3*5}
>
>If the list were lista = {{2,4}, {3,2}, {5, 3},{7^5}}, we would have
>(4+1)(2+1)(3+1)(5+1) = 360 products, for example and the return values
>should be a single list showing all of those.
>
>Question 2.
>
>I have two lists and want to generate two new lists from them.  These two
>lists are {number, exponent} pairs.
>
>In the first list, I want the "minimum intersection" of {number, exponent}
>pairs.
>
>In the second list, I want the "maximum union" of {number, exponent} pairs.
>
>Let me show an example:
>
>Input:
>
>list1 = {{2, 3}, {3, 4}, {5, 6}, {7, 2}, {17, 5}}
>
>list2 = {{2, 5}, {3, 2}, {5, 1}, {7, 3}}
>
>Output:
>
>minint = {{2, 3}, {3, 2}, {5, 1}, {7, 2}}
>
>Note: In this example we only kept those pairs where the intersection of
>the
>number exists and also keep the min power of those.
>
>maxint =  {{2, 5}, {3, 4}, {5, 6}, {7, 3}, {17, 5}}
>
>Note: In this example we kept the union of lists and also keep the max
>power
>of each.
>

allProducts[x_] := 
    Module[{sx = Sort[x, #2[[2]] < #1[[2]] &], f},
      Union[Flatten[
            Outer[f, Sequence @@ 
                (PadRight[#, sx[[1, -1]] + 1, 
                        1] & /@
                    (#[[1]]^
                            Range[0, #[[2]]] & /@ sx))]] /.
          f -> Times]];

lista = {{2, 3}, {3, 1}, {5, 1}};

allProducts[lista]

{1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120}

minInt[x_, y_] :=
    FactorInteger[GCD[
        Times @@ (#[[1]]^#[[2]] & /@ x), 
        Times @@ (#[[1]]^#[[2]] & /@ y)]];

maxInt[x_, y_] :=
  Union[x, y] //. 
    {s___, {b_, e1_}, {b_, e2_}, r___} :>
      {s, {b, Max[e1, e2]}, r}

list1 = {{2, 3}, {3, 4}, {5, 6}, {7, 2}, {17, 5}};
list2 = {{2, 5}, {3, 2}, {5, 1}, {7, 3}};

minInt[list1, list2]

{{2, 3}, {3, 2}, {5, 1}, {7, 2}}

maxInt[list1, list2]

{{2, 5}, {3, 4}, {5, 6}, {7, 3}, {17, 5}}


Bob Hanlon


  • Prev by Date: Re: The Essence of Mathematica
  • Next by Date: Re: XML and the DTDs and etc.
  • Previous by thread: Re: Some list questions
  • Next by thread: Re: Some list questions