Message Boards Message Boards

0
|
5657 Views
|
6 Replies
|
8 Total Likes
View groups...
Share
Share this post:
GROUPS:

How do I extract the variables from a polynomial?

Posted 12 years ago
 In[1] := -3591176434993748384348496449605057311 -
 931317410939396433823492399456035672 c1 -
 9952543970861457213652467061541331498 c2 +
 4048090288340722001114952049169242965 c3 +
 397164398434703247826095886842412575 px +
 1066996793775684645797090668168460265 c2 px -
 460328625674890624429065259310152110 c3 px +
 15946921216526762184564517273794735 py -
 35440141634773840046659693982601120 c2 py +
22495307563938104656216195952976885 c3 py +
46746177228030459461263589208063870 pz -
36428604512265070024530336222388650 c3 pz -
336574847215337605742315849219352600 qx -
856298637445659071245670123987864572 c2 qx +
381082557957344248171016160868208816 c3 qx +
20336384809932402456728418013439130 qy +
79580434879454594169496332062731380 c2 qy +
46111843705049866684186439724551130 c3 qy -
754447730345013048895996548762560 qz -
54862758834533020637102302666976820 c2 qz +
109847021021547323599497109075146120 c3 qz
Out[1] = {1, c1, c2, c3, px, c2 px, c3 px, py, c2 py, c3 py, pz, c3 pz, qx, c2 qx, c3 qx, qy, c2 qy, c3 qy, qz, c2 qz, c3 qz}

My question: How to acquire the Out from the In with the help of Mathematica?
POSTED BY: feng wei
6 Replies
Posted 12 years ago
You can use a rule to replace all numeric parts in the expression with 1:
 expr = -3591176434993748384348496449605057311 -
 931317410939396433823492399456035672 c1 -
 9952543970861457213652467061541331498 c2 +
 4048090288340722001114952049169242965 c3 +
 397164398434703247826095886842412575 px +
 1066996793775684645797090668168460265 c2 px -
 460328625674890624429065259310152110 c3 px +
 15946921216526762184564517273794735 py -
 35440141634773840046659693982601120 c2 py +
22495307563938104656216195952976885 c3 py +
46746177228030459461263589208063870 pz -
36428604512265070024530336222388650 c3 pz -
336574847215337605742315849219352600 qx -
856298637445659071245670123987864572 c2 qx +
381082557957344248171016160868208816 c3 qx +
20336384809932402456728418013439130 qy +
79580434879454594169496332062731380 c2 qy +
46111843705049866684186439724551130 c3 qy -
754447730345013048895996548762560 qz -
54862758834533020637102302666976820 c2 qz +
109847021021547323599497109075146120 c3 qz;
expr /. _?NumericQ -> 1

(Skim through the Learning Resources listed in the Rules guide to get a hang of how Rules work)

That replacement will result in:
1 + c1 + c2 + c3 + px + c2 px + c3 px + py + c2 py + c3 py + pz +
c3 pz + qx + c2 qx + c3 qx + qy + c2 qy + c3 qy + qz + c2 qz + c3 qz
Which is pretty close, only difference is that it's "a + b + c" instead of "{a,b,c}"  Applying List to the expression will fix this
List @@ (expr /. _?NumericQ -> 1)
(* {1, c1, c2, c3, px, c2 px, c3 px, py, c2 py, c3 py, pz, c3 pz, qx,
c2 qx, c3 qx, qy, c2 qy, c3 qy, qz, c2 qz, c3 qz} *)

You can look at the FullForm of the last two to see what happened, "Plus[a,b,c]"  became "List[a,b,c]"
FullForm[expr /. _?NumericQ -> 1]
FullForm[List@@(expr/._?NumericQ->1)]
POSTED BY: Simon Schmidt
Similar to Simon Schmidt, but using MonomialList:
expr = (* long input expression from above *) ;

MonomialList[expr] /. _Integer -> 1 // Sort
This also returns:
{1, c1, c2, c3, px, c2 px, c3 px, py, c2 py, c3 py, pz, c3 pz, qx, c2 qx, c3 qx, qy, c2 qy, c3 qy, qz, c2 qz, c3 qz}
POSTED BY: Arnoud Buzing
Posted 12 years ago
Thanks to help from Mr. Simon Schmidt and Mr.  Arnoud Buzing.However,another problem arise about the quadratic component.
For example
  expr =-706195094000394411609737806905802758216754976753979869659028490072021\
  8426948359642040245441156057529091892655574765196743335253461498910119\
  1637464217681227157444178174920745739217186711976920776652189897362939\
  5226687200492487026481592886256327265758087279193999606847879128145367\
  7947122941562523585331021859124563736502630637152851867 -
   414452773836320077456685307580418418269900926108704086112518150973423\
  4358800467908684550831600478243566185118403644236952710238377199259855\
  9753960195271217546484531699572260801289374372434868101113176267807401\
  4016681734708728970336659873111993345757338403660417239274562350026411\
 1773406283652234482964431639579792781547485318388759088 c1 +
  642397193462473886082092816305478178259035950111665320966407570852878\
 1941125067487595673842314175673845967156704035437884699182620954246079\
 7313575740367200953127292283421385677605678672506037110472819559083703\
 0414997771203614302157347355385890734070963640112937345905842006466546\
 888709533468725742033517008613047153218301011201334396 c1^2 -
  331297502896188912618974447982368381249097942032320767648205008525270\
 5998117688534814470567736853009020881555674855896962598803573274831918\
 5204428713135015976136982957414209343179447937225852118736839432065407\
 7018427143174886680005080708972203730337611305357081661039096721118462\
 1332631170140148262045981002944835762282608998681818669 c2 -
  106687544839597397900932643788153660052288090558910838709935669271829\
 4150562379438989017349386708682678874268768571432149075868204168408876\
 4590389741733986380023665680876710667605371081965468616482812681161424\
 8297238930853292548689635026063952392477097174603276921097723049254597\
 7912082879538737655579668942774101031488450710431513584 c1 c2 -
  183853826253445734915081844765048986625508296656132321361588290062536\
 6764505961760172206429157501890545706501460595671655894282454348927990\
 9583030281531204450740735531953486798969447188386671168656658370397120\
 7832877379927868189780898021931652876915744446164181678604000972385669\
 5573806825456566757970201201371901054723494041091936296 c2^2 +
  221737839485020949982972298194455044102553983245347505927434236890509\
 1227044802677562764521014388445492574668765549072424351202979362077047\
 0759208435307940850829013136200776593093272844656072633496077404526802\
 2272580300188501627982811671089099764352470706506078333357150953013994\
 29376993890397395145005883374431964291302413205335457368 c3 +
  272008926154217831770479275077077575639372303047976751248891415596468\
 3404433339928462695021280268258139133506796150653223731270001910365640\
 0449690960928947892869473640162704518045477337401794938565943310309002\
 3923429485849143798421003207419284733305122198969551971743386804637476\
 010035501463332471928054906486676423455583094028972280 c1 c3 +
  189971651708303600825518085799391291466189141991573149373016374878237\
 8826532687271262721437384263708978922520920205953687450787586356357698\
 7500458082807409777780713693501472157643125027095703081976838766947008\
 1239785579717439931702976444738017341188079722487914996423147384440921\
 591194742556939676790728929176553630176555767401859970 c2 c3 -
  347590161632352546669613983409125025200959176870266441303278405295419\
 2135424466307269268822194787465279555122610309700519427185699824906215\
 7883800764984472210913024468868693275351849167333120876277362133075772\
 2539265591492010718420562211864269092662874554039876474521650035267845\
 0056881364442792068708552525620276984025863040353911796 c3^2 +
  824624866225813180308867794556187561926813422633245272942927524639126\
 9179718565287909772307836247961424887830262157679343195506509968645531\
 4043737610481921808016425973785000323038296065033535915280989768746909\
 5957973029312591762975676335430635342540222075189210665385956112761519\
 982472135699317238754654146594305630324366331316309885 px -
  240522733155577859298831660898917822780648080688007102347117364750899\
 7792425783439612165062238683975228439068911033779035310625476947646903\
 0776401291868885287476597173396402856795168947079371981556226339080801\
 5031084420288840389298896583598063421510749500605267419875390129151065\
 4227726681059264991558743723073857979786328460688701045 c3 px +
  164930988192613243272190598213046566196407928075193655495314541299023\
 1062932935844164974830759970359790318990323551867087749513456423030934\
 7122541968771157180955232282355509099526945985973079162353823163759367\
 9099554219865217467728567328072723037806911977616352484908281816961135\
 193335948987003701802993436237413028646587860303590525 py -
  207091153337609167229295368164252668360618479433660029690842682165582\
 2326944041914976777432904204648064984993822661797687090152686534049406\
 1197384122662644082386458485528021006999256482211516852695941413077071\
 8859810550377564268833856880715897207716314679314385752111699812214773\
 803636065449708973082206350152210159440904191416016665 c2 py -
  244915926226544475054961071147043687531523702644837224326837217468664\
 1117655827680686247185603746064778701563125431627251374109286559853824\
 3808819790034262359487797573349773034975572907001523931909215700640006\
 4819196295770368405693430975531058815437059305227609052361206669289734\
 68243879035432782375944113293677498739244104361177930 c3 py +
  174739390531356938493904766104162491442934056374254387332224571524397\
 0661423785669557075786005481945959141233723318556000579398618412556966\
 4698109825235233132696134216243623254800171491570040070562400677937582\
 6992134275057445535078282453685636174306290860232870991160831948271804\
 261609291068321601844511826979819444763860352548013800 pz -
  173085255382804076090583207501963946946367360151845724281131700205507\
 5222417176174508937343815184660669158779649883122609689639619840189154\
 4917429634294920205384253017628699271012948108218237267012081177945746\
 0096976122780414270635983006354320177609962355123055383504222503745360\
 408427831668798870922246774571929554511988478399550800 c3 pz -
  663746350073602124651057210793167909898495605869526929689722586761681\
 1606856615064263263118131548657972602187601555806368318542068556049716\
 4749535639415157167928338892400099001756369429231537308066682301934254\
 1314402229446616614603956182410282288268862436040543534520941901326431\
 545140071660583016367313739393378127930091682902287948 qx +
  477991902040178833830198777501676671343597315228560685692984813738056\
 2079540081260783288556031897337749901198089538816603761243109944768219\
 2751399131243203349829161731593257847770142702442578789580638646271239\
 6169398744569738514021065581467984995470459438162187943873572334831559\
 95073600812804223948652188166770121437224489417473526 c2 qx +
  216399120458987587414542137376150239670293294153065588663970597524291\
 7942201911233340197192378765932486859056768260349257768900266991406037\
 3209657796034146763319711503698224523959088544063048722916492656456599\
 8896079766616330775232548366637511507493598106540569040147767313988068\
1670402150578401153555024999229904481213443583280754980 c3 qx +
173304871892676914398869667835418886204651869545884967434256491582574\
7592464090233531666330506097798325618168499949206608655609237153578748\
6083206896616252610623697161112399232303921313888192642947513604361713\
7163593051612954902697867579671966625829571164102156823217547715347176\
1048219788514699964986937595455786885183810862918270 px qx -
289353393814958371177356391924604285200964394805434330276297042360650\
7343628279989127772803219050896768849832220908463474993568459115942277\
1545510457483535959373854142801849151823138078476142118507462450653384\
2582549111329346477097292784092437893749615394748243568232191912609149\
0858153495444722307125877816941356000365149298326820 py qx +
496078373806825033939674800376818893003166977921616088664383294694844\
4279628151283093299132795916660286874186916399414535160182469417544996\
7818807381490029849723425386886942974209130041470618045585780914250745\
0434666299217515008218963780719632691956845528855842798216503551656409\
217825333654837500447225418975135024609619808921760 pz qx -
803943478886372443319937953454066004559372379759420000045994143759539\
2372404044464835848330545702177656281649596552863106351415637738904930\
0581030293163698601118652564850976643268344284594573154433344411305719\
8634042872856126935166930229570694749832297967955711360728951280935894\
6263577302686746885805957275462617063099186358795630 qy +
263611473138683063799062262726564940064006337061467440490020457746887\
6113034255808109093503697253571528002387695334792104510304513146469599\
4930793760003454818766355079864954755112479016230742150228602550187337\
8565852920991680601507134786287002206773622517786164089499132173196318\
364339190188353477923858528168370319600956982320352280 c2 qy -
141728498697413855110356584739239338529797052214995300470295806299789\
1831950334442371645371526724996572803827698149263948857826859613117446\
3573274069876869683036657134411374195532857094267313753091747583358639\
0012580694744638569401596016990583943515006735696232264163165989609473\
284151840516156599809492172407958046800879029140126970 c3 qy -
121144529004999893267437515494994671078701809642729698664310313703681\
0954117230301001783662650702780459017702726668021795299908630107220462\
3351049732388582992907124350804025426611429198489492138935441461033148\
6338489937423917900450847998894616133567091336297740499539920295889586\
4231249716816219318513669367229192787585603704821525 px qy +
212601387611233338816157076127589766893757264235381387259684984558794\
7338943186758799956706459679097256311706248791525963441715958240688012\
4459352280464492734440506152527625718108642811368081805458690644929008\
7228875664272974062975701352513165289219459350548121144520890964286649\
4589659830455942485458808221570884130600953052405250 py qy -
109112842653699450694345014068182659496201427839189924116966359056136\
7567821167857408588004068505932185732897837162497156862118569702960158\
7113510270999550588178459578600564194922741046143654128973099240412556\
7423852525559389944922955659367675150140110949594673839468171900299075\
0340903783078647210859222370878192937040804962489750 pz qy +
166039827474097013502077701451731661794618012109332743395695475661343\
7484795654266060639557186286857409893947493626296674372604489028731638\
2340777240882002636724817170493467074142556238577262145291343497133163\
1913849861169317971400406082285849605459618018640836426357452846498376\
7713516569640422815811844906914795402949888959578780 qx qy +
763724144967099619885516247196005666076025919410630811131394908705555\
5531510292478060475849272078029587875723657003096157879509103365877380\
3969178115442079157422807011464711941734333164844167987226621545122525\
1951962780882474811137529679513049861500374055091797069484083545770471\
334151921794865858503319747423219060016825741962940 qy^2 -
205354990910606905082127759852668020320933993294710053918579711104354\
8287371620303699568298733995628879084564347649375351623147197150534856\
4013346300871481612365925407543368198103322479754213266889147588559863\
6645876652629710089505118647071119415108832162572499748268047658482533\
140118162807575295142009494463418796817276211338688420 qz +
112350837973130861145392352659973574123425684627029229023261153924156\
7069691138666107846493230600200656203266552893607039281347905821460057\
7961069463658030030857912392641327828936825599767690607517909777384843\
2892665875238065222057271851818351351649363673979668491024248055697184\
389164671938472797502937531978399801360390609004909940 c2 qz +
281543199751220349835300104796358217518519752829842031581801215982401\
0462582710509027056229443545701175090599394812548516056239207295079288\
2605356296270345335614532250485613911328453453816515338155566374732833\
8630238245028330996802094934178086666578085173982861097428425111927274\
561485234324274511717132851857236023466696137114163600 c3 qz +
220682096401064877359971599366519819432397380066821706284016388891528\
2357761403552244844783327714847836568306984777948073683776462391547484\
9513399678728809410392887180130323955076392086519693899802152680967310\
6801172576267550364008919468931032637585241303182142098458273625267471\
86302004858166542979430073077181910423346230451829450 px qz -
789885094463641617907699224264598575159138221194843836241058102302812\
2896194542833863139911922928025046785890156991363790749406777221761313\
9703873436203274150832439786290770741175054278584912560310863941115595\
3405884718719857532827276116264628968133353858109690699853146339472822\
45904787632619490269741329116264273594556035877350 py qz +
504160528342973190215635757233260925668116728789866861603652600375608\
0675626460529980630916802809146845540702195608324107220239003086789597\
1429576771792563306274290495006114009762328824789966586028937399955534\
4196835022019718443768709931038123902679676332533683398612575517914749\
327605772860817795314372962541750342868356097077300 pz qz -
175526483812283171233178036423556053374054086239377018921440653299963\
3191757973386972114652222429490214450552002563555044743887121535302392\
2864124682714495322759578165633421391609297722696178782065805102272979\
0471757521616178805294850768423898290193635176600276150984322616466736\
21262056104958269159031539952386366425328743319210680 qx qz +
810404710453684404449711145642531979844039181424682790052988605948928\
4686422846578071322066066720631974338178371081463783932655933964481466\
9811055583542474886853184796791286727893321546450269470111277027868266\
8922291528822664229437063866864574626411960357222442866546783767880330\
91167675047232878995040262731053202519064508242400 qy qz +
152830097494642265504510707250222331142777355310815085217970902751603\
0347034722765085422348829892611273758147588297336587513397169685986778\
0904949164516520452330880016462926479443433587855015411345385880175023\
6187275276468709349327131656991739575588792542630056334796338102103746\
0421261195208842266193695005452058189938179360184840 qz^2
List @@ (expr /. _?NumericQ -> 1)
MonomialList[expr] /. _Integer -> 1 // Sort
The result is:
{1, 2 c1, 2 c2, c1 c2, 2 c3, c1 c3, c2 c3, px, c3 px, py, c2 py,
c3 py, pz, c3 pz, qx, c2 qx, c3 qx, px qx, py qx, pz qx, 2 qy, c2 qy,
  c3 qy, px qy, py qy, pz qy, qx qy, 2 qz, c2 qz, c3 qz, px qz, py qz,
  pz qz, qx qz, qy qz}

But the true result I want is:
{1, c1, c1^2, c2, c1 c2, c2^2, c3, c1 c3, c2 c3, c3^2, px, c3 px, py,
c2 py, c3 py, pz, c3 pz, qx, c2 qx, c3 qx, px qx, py qx, pz qx, qy,
c2 qy, c3 qy, px qy, py qy, pz qy, qx qy, qy^2, qz, c2 qz, c3 qz,
px qz, py qz, pz qz, qx qz, qy qz, qz^2}

How to solve it?
POSTED BY: feng wei
You can actually state the problem without the lengthy input. Simply, your problem is like how to extract the symbolic terms of
expr = 3 a^2 + 2 a b + a
in which you have the square term, mixed symbol term and a single symbol term. You can use Apply function to extract each term in the summation
List@@expr
For each element, you can map this rule to them (y is ahead of three Blank[])
List @@ expr /. HoldPattern[Times[x_, y___]] -> {x, y}
Then you have
{{3, a^2}, {2, a, b}, c}
Then you can write a function to handle the lists: 
f[x_List] := Times @@ Rest[x]
f[x_Numeric] := x
f[x_Symbol] := x
As you map it to the list, you get
In[]:= f /@{{3, a^2}, {2, a, b}, c}
Out[]= {a^2, a b, c}
POSTED BY: Shenghui Yang
Posted 12 years ago
this code may help you.
Level[expr // Expand, 1][[2 ;;, 2 ;;]]
out form is :
{c1,c1^2,c2,c1 c2,c2^2,c3,c1 c3,c2 c3,c3^2,px,c3 px,py,c2 py,c3 py,pz,c3 pz,qx,c2 qx,c3 qx,px qx,py qx,pz qx,qy,c2 qy,c3 qy,px qy,py qy,pz qy,qx qy,qy^2,qz,c2 qz,c3 qz,px qz,py qz,pz qz,qx qz,qy qz,qz^2}
POSTED BY: wayne wang
Posted 12 years ago
Thanks to  Mr.wayne wang and Mr. Shenghui Yang very much. With your help the problem can be solved.Once again, thanks to Mr. Simon Schmidt and Mr. Arnoud Buzing.
POSTED BY: feng wei
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract