Re: FactorInteger Print Formatting
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg758] Re: FactorInteger Print Formatting
- From: wagner at bullwinkle.cs.Colorado.EDU (Dave Wagner)
- Date: Tue, 18 Apr 1995 00:48:59 -0400
- Organization: University of Colorado, Boulder
> Alan Powell <POWELLA at delphi.com> wrote: >-> >-> >->Could anyone suggest a neat way to reformat the output of >->FactorInteger into a more usable print format? >-> Hey group, We're all spinning our wheels on this, because we (and that includes me) all made the assumption that the right way to proceed is to define some kind of special print form for the entire list returned by FactorInteger. That's the hard way to do it! All that's necessary is to define a head that prints a single pair of numbers in the form base^exponent; Mathematica does the rest. Then you can turn the result of FactorInteger into something that's truly useful: In[1]:= factors = FactorInteger[238500] Out[1]= {{2, 2}, {3, 2}, {5, 3}, {53, 1}} In[2]:= Format[IntegerPower[x_, y_]] := HoldForm[x^y] In[4]:= Apply[IntegerPower, factors, {1}] Out[4]= 2 2 3 1 {2 , 3 , 5 , 53 } Now if we apply Times to this list we'll get something that appears to be a product of powers: In[5]:= factorization = Times @@ % Out[5]= 2 2 3 1 2 3 5 53 But in reality it's a product of IntegerPowers, which is why it doesn't evaluate to an integer. In[6]:= FullForm[%] Out[6]//FullForm= Times[IntegerPower[2, 2], IntegerPower[3, 2], IntegerPower[5, 3], IntegerPower[53, 1]] So far, so good! Now we need to create a function that goes the other way: In[21]:= ExpandInteger[x:(a_IntegerPower * b_.)] := x /. IntegerPower->Power ExpandInteger[x_] := x In[23]:= ExpandInteger[factorization] Out[23]= 238500 In[24]:= ExpandInteger[%] Out[24]= 238500 This is just the tip of the iceberg. I'm working on a package that, I hope, will let one compute using these factorizations in much the same way as one uses integers. It's not in a finished form yet, but when it is I'll post it. Meanwhile, to whet your appetite, here are some examples of functionality that I've already implemented: In[44]:= x = FactorInteger[384] Out[44]= 7 1 2 3 In[75]:= N[x] Out[75]= 384. In[45]:= y = FactorInteger[36] Out[45]= 2 2 2 3 In[46]:= x y Out[46]= 9 3 2 3 In[55]:= y / x Out[55]= -5 1 2 3 In[61]:= FactorInteger[238500] / FactorInteger[40950] Out[61]= 1 1 -1 -1 1 2 5 7 13 53 In[62]:= 1/% Out[62]= -1 -1 1 1 -1 2 5 7 13 53 In[63]:= ExpandInteger[%] Out[63]= 91 --- 530 In[64]:= 40950/238500 Out[64]= 91 --- 530 In[65]:= FactorInteger[96] Out[65]= 5 1 2 3 In[66]:= %^3 Out[66]= 15 3 2 3 In[67]:= % ^ (1/4) Out[67]= 15 3 1/4 (2 3 ) In[68]:= PowerExpand[%] Out[68]= 15/4 3/4 2 3 In[69]:= ExpandInteger[%] Out[69]= 3/4 8 6 By now you probably all think I have *way* too much time on my hands, but the truth is that I'm writing a book and this was just the example I was looking for! This group is terrific for providing examples that are interesting and educational. Dave Wagner Principia Consulting (303) 786-8371 princon at csn.net http://www.csn.net/princon