This site is supported by donations to The OEIS Foundation.

User:Anatoly E. Voevudko/VoeSeqOper.gp

From OeisWiki
Jump to: navigation, search

Operations with sequences

Notes for those, who are new to MediaWiki pages:

  • Everyone is allowed to view and copy source.
  • To copy or save a page without HTML tags click "view source" tab (at the top of this Wiki page).
  • Then select all code inside Wiki page sub-window with source (depends on browser and OS), "Copy" it and save.
  • Clean-up rules: delete all rows having at position 1 not a whitespace.
  • You may add "\\" in front of such rows instead of deleting. Or you can use cleanwsf() & clean4gpf() from VoeLib.gp to do this hard job for you. Note: both cleanwsf() & clean4gpf() work fine only here (with my style of formatting).


Operations +, - and * with 2 or 3 sequences

  \\\\ *** VoeSeqOper.gp
  \\   OPERATIONS WITH SEQUENCES
  \\  seqoper(infile,lim,oper=2,comp=1) - Operations with 2 or 3 sequences, where:
  \\         infile is an input text file (having 2 or 3 sequences as vectors); 
  \\         lim is a highest number to stop operation; oper is the operation: 
  \\         1,2,3 (+,-,* allowed); comp is a compare flag (1/0 - to compare 
  \\         result with 3rd sequence or not).
  \\         NOTE: Operations with sequences A, B and C, also related formulas like
  \\         A-B=C, should be understood as following: "Terms of sequence A" minus
  \\         "Terms of sequence B" are equal  "Terms of sequence C".
  \\ Author: A.E. Voevudko. Last Updated 11/14/15
  \\\\
   seqoper(infile,lim,oper=2,comp=1)= 
  {my(Vf,V1,V2,V3,Vr,Lr=List(),Lo=List(["PLUS","MINUS","MULT"]),vfn,v1n,v2n,v3n,vrn,
   ip,cr=1,t1,t2);
  if(oper<1||oper>3, return(-1));   
  \\ PART #1 READ infile
  print(" *** PART #1 READ infile: ",infile,"; lim: ",lim,"; oper: ",oper,"; comp: ",comp);
  Vf=readvec(infile); vfn = #Vf;
  print(" *** FOUND: ", vfn, " sequences in file"); 
  if(vfn<2, print(" *** ERROR: at least 2 sequences required!"); return(-1););
  V1=Vf[1]; v1n = #V1;
  V2=Vf[2]; v2n = #V2;
  V3=Vf[3]; v3n = #V3;
  print(" *** END PART #1 READ infile; Seq. sizes: ",v1n,"/",v2n,"/",v3n);
  \\ PART #2 OPERATION
  print(" *** PART #2 OPERATION: ",Lo[oper]);
  \\*** MINUS
  if(oper==2, 
     for(j=1, v2n, t2=V2[j]; if(t2>lim, break);
     for(i=1, v1n, t1=V1[i]; if(t1>lim, break(2)); if(t1==t2, V1[i]=-1; break);
     ));
     \\print(V1);
     for(i=1, v1n, t1=V1[i]; if(t1!=-1, listput(Lr,t1)));
     Vr=Vec(Lr); vrn=#Vr;
     print(Vr);
    );
  \\*** PLUS
  if(oper==1,  
     for(i=1, v1n, t1=V1[i]; if(t1>lim, break); listput(Lr,t1)); 
     for(j=1, v2n, t2=V2[j]; if(t2>lim, break); listput(Lr,t2));
     listsort(Lr);
     Vr=Vec(Lr); vrn=#Vr;
     print(Vr);
    );
  \\*** MULT
  if(oper==3,
     for(i=1, v1n, t1=V1[i]; if(t1>lim, break); listput(Lr,t1)); 
     for(j=1, v1n, if(v1n>v2n, break); t2=V2[j]; if(t2>lim, break); Lr[j]=Lr[j]*t2);
     Vr=Vec(Lr); vrn=#Vr;
     print(Vr);
    );
 print(" *** END PART #2 OPERATION: ",Lo[oper]);
 if(vfn<3||comp==0, return); 
 \\ PART #3 COMPARE Vr and V3
 print(" *** PART #3 COMPARE Vr and V3");
 if(vrn>v3n, vrn=v3n; print(" *** CAN COMPARE ONLY ",vrn," terms of Vr"));
 for(i=1, vrn, if(Vr[i]!=V3[i], ip=i; cr=0; break));
 if(cr, print(" *** Vr and V3 are identical"));
 if(!cr, print(" *** Vr and V3 are NOT identical at Vr[",ip,"]!"));
 print(" *** THE END");
 }

Operation f(n) with 1 or 2 sequences

  \\\\
  \\  seqoper1(infile,lim,oper,comp=0) - Operation with 1 or 2 sequence, where: infile
  \\          is an input text file (having 1 or more sequences as vectors);
  \\          lim is a highest number to stop operation (if lim==-1, then whole vector);
  \\          oper is a function applied to each term of a sequence; comp is a compare
  \\          flag (1/0 - to compare result with 2nd sequence or not).
  \\          NOTE: set oper as f(x)=x if you need just to compare 2 sequences. 
  \\ Author: A.E. Voevudko. Last Updated 11/17/15
  \\\\
  seqoper1(infile,lim,oper,comp=0)={my(Vf,V1,V2,Vr,Lr=List(),vfn,v1n,v2n,vrn,t1,ip,cr=1);
  \\ PART #1 READ infile
  print(" *** PART #1 READ infile: ",infile,"; lim: ",lim,,"; oper: ",oper,"; comp: ",comp);
  Vf=readvec(infile); vfn = #Vf;
  if(vfn<1, print(" *** ERROR: at least 1 sequence required!"); return(-1););
  print(" *** FOUND: ", vfn, " sequences in file (only 1 or 2 are used)."); 
  V1=Vf[1]; v1n = #V1;
  V2=Vf[2]; v2n = #V2;
  print(" *** END PART #1 READ infile; Seq. sizes: ",v1n,"/",v2n);
  \\ PART #2 OPERATION
  print(" *** PART #2 OPERATION: ",oper);
  \\*** WITH LIMIT
  if(lim!=-1, 
     for(i=1, v1n, if(V1[i]>lim, break); t1=oper(V1[i]); listput(Lr,t1));
     Vr=Vec(Lr); vrn=#Vr;
     print(Vr);
    );
  \\*** WITH NO LIMIT
  if(lim==-1, 
     Vr=apply(oper, V1);
     print(Vr);
     vrn=v1n;
    );
 print(" *** END PART #2 OPERATION; Seq. size: ",vrn);
 if(vfn<2||comp==0, return); 
 \\ PART #3 COMPARE Vr and V2
 print(" *** PART #3 COMPARE Vr and V2");
 if(vrn>v2n, vrn=v2n; print(" *** CAN COMPARE ONLY ",vrn," terms of Vr"));
 for(i=1, vrn, if(Vr[i]!=V2[i], ip=i; cr=0; break));
 if(cr, print(" *** Vr and V2 are identical"));
 if(!cr, print(" *** Vr and V2 are NOT identical at Vr[",ip,"]!"));
 print(" *** THE END");
 }

Operations with vectors or lists

Vectors or Lists Compare

  \\\\
  \\  volcomp(v1,v2) - Vectors or Lists Compare, i.e.,compare 2 vectors v1 and v2 (or lists, or
   vector&list). 
  \\  Author: A.E. Voevudko. Last Updated 11/18/15
  \\\\
  volcomp(v1,v2)={my(v1n=#v1,v2n=#v2,vmn=min(v1n,v2n),ip,cr=1);
  if(v1n<1||v2n<1, return(-1)); \\ wrong size(s)
  print(" *** COMPARE v1&v2 w/types: ",type(v1),"/",type(v2),"; w/sizes: ",v1n,"/",v2n);
  if(v1n!=v2n, print(" *** can compare only first ",vmn," elements."));
  for(i=1, vmn, if(v1[i]!=v2[i], ip=i; cr=0; break));
  if(cr, print(" *** v1 and v2 are identical!"),
         print(" *** v1 and v2 are NOT identical at v1[",ip,"]?"));
  }

Testing Samples

  /* TESTING FUNCTIONS
  1. Copy next 3 rows and save in a text file ApBeC.txt
  [4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529,
  576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681,
  1764, 1849, 1936, 2025]
  [8, 16, 27, 32, 64, 64, 81, 125, 128, 216, 243, 256, 256, 343, 512, 512, 625, 729, 729, 1000, 1024,
  1024, 1296, 1331, 1728, 2048]
  [4, 8, 9, 16, 16, 25, 29, 32, 36, 49, 64, 64, 64, 81, 81, 100, 121, 125, 128, 144, 169, 196, 216, 225,
  243, 256, 256, 256, 289, 324, 343, 361, 400, 441, 484, 512, 512, 529, 576, 625, 625, 676, 729, 729, 
  729, 784, 841, 900, 961, 1000, 1024, 1024, 1024, 1089, 1156, 1225, 1296, 1296, 1331, 1369, 1444, 1521, 
  1600, 1681, 1728, 1764, 1849, 1936, 2025, 2048]
  2. Copy  next 3 rows and save in a text file AmBeC.txt
  [4, 8, 9, 16, 16, 25, 27, 32, 36, 49, 64, 64, 64, 81, 81, 100, 121, 125, 128, 144, 169, 196, 216, 225,
  243, 256, 256, 256, 289, 324, 343, 361, 400, 441, 484, 512, 512, 529, 576, 625, 625, 676, 729, 729, 
  729, 784, 841, 900, 961, 1000, 1024, 1024, 1024, 1089, 1156, 1225, 1296, 1296, 1331, 1369, 1444, 1521,
  1600, 1681, 1728, 1764, 1849, 1936, 2025, 2048]
  [4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484, 529,
  576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 1681,
  1764, 1849, 1936, 2025]
  [8, 16, 27, 32, 64, 64, 81, 125, 128, 216, 243, 256, 256, 343, 512, 512, 625, 729, 729, 1000, 1024,
  1024, 1296, 1331, 1728, 2048]
  
  3. Copy next 3 rows and save in a text file AoBeC.txt
  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484,
  529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600, 
  1681, 1764, 1849, 1936, 2025]
  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484,
  529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600,
  1681, 1764, 1849, 1936, 2025]
  [0, 1, 16, 81, 256, 625, 1296, 2401]
  4. Copy next 2 rows and save in a text file AfeB.txt
  [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400, 441, 484,
  529, 576, 625, 676, 729, 784, 841, 900, 961, 1024, 1089, 1156, 1225, 1296, 1369, 1444, 1521, 1600,
  1681, 1764, 1849, 1936, 2025]
  [0, 1, 16, 81, 258, 625, 1296, 2401]
  NOTES:
  ApBeC.txt has 3 seqs to test formula: A000290(n>2) + A245713(n>1) = A072103
  AmBeC.txt has 3 seqs to test formula: A072103 - A000290(n>2) = A245713(n>1)  
  AoBeC.txt has 3 seqs to test formula: A000290 o A000290 = A000583  
  AfeB.txt  has 2 seqs to test formula: A000290 ^ 2 = A000583  
  5. Copy next 9 rows and paste it in GP/PARI. NOTE: file path and name shown for 
     Windows.
  read("VoeSeqOper.gp"); op(x)={return(x^2)}
  seqoper("c:\\pariData\\ApBeC.txt",2050,1);
  seqoper("c:\\pariData\\AmBeC.txt",2050);  
  seqoper("c:\\pariData\\AoBeC.txt",2050,3);
  seqoper1("c:\\pariData\\AfeB.txt",1601,op,1);
  seqoper1("c:\\pariData\\AoBeC.txt",-1,op);
  vec1=Vec([5,4,3,9,8,7,3,1]);  vec1=List(vec1);
  vec2=Vec([5,4,3,9,8,7,2,1,10,11,11]);
  volcomp(vec1,vec2);
  6. RESULT should be like following:
  
  (18:40) gp > read("VoeSeqOper.gp"); op(x)={return(x^2)}
  %1 = (x)->return(x^2)
  (18:41) gp > seqoper("c:\\pariData\\ApBeC.txt",2050,1);
  *** PART #1 READ infile: c:\pariData\ApBeC.txt; lim: 2050; oper: 1; comp: 1
  *** FOUND: 3 sequences in file
  *** END PART #1 READ infile; Seq. sizes: 44/26/70
  *** PART #2 OPERATION: PLUS
  [4, 8, 9, 16, 16, 25, 27, 32, 36, 49, 64, 64, 64, 81, 81, 100, 121, 125, 128, 14
  4, 169, 196, 216, 225, 243, 256, 256, 256, 289, 324, 343, 361, 400, 441, 484, 51
  2, 512, 529, 576, 625, 625, 676, 729, 729, 729, 784, 841, 900, 961, 1000, 1024,
  1024, 1024, 1089, 1156, 1225, 1296, 1296, 1331, 1369, 1444, 1521, 1600, 1681, 17
  28, 1764, 1849, 1936, 2025, 2048]
   *** END PART #2 OPERATION: PLUS
   *** PART #3 COMPARE Vr and V3
   *** Vr and V3 are NOT identical at Vr[7]!
   *** THE END
  (18:41) gp > seqoper("c:\\pariData\\AmBeC.txt",2050);
   *** PART #1 READ infile: c:\pariData\AmBeC.txt; lim: 2050; oper: 2; comp: 1
   *** FOUND: 3 sequences in file
   *** END PART #1 READ infile; Seq. sizes: 70/44/26
   *** PART #2 OPERATION: MINUS
  [8, 16, 27, 32, 64, 64, 81, 125, 128, 216, 243, 256, 256, 343, 512, 512, 625, 72
  9, 729, 1000, 1024, 1024, 1296, 1331, 1728, 2048]
   *** END PART #2 OPERATION: MINUS
   *** PART #3 COMPARE Vr and V3
   *** Vr and V3 are identical
   *** THE END
  (18:41) gp > seqoper("c:\\pariData\\AoBeC.txt",2050,3);
   *** PART #1 READ infile: c:\pariData\AoBeC.txt; lim: 2050; oper: 3; comp: 1
   *** FOUND: 3 sequences in file
   *** END PART #1 READ infile; Seq. sizes: 46/46/8
   *** PART #2 OPERATION: MULT
  [0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 14641, 20736, 28561, 38416, 50625, 65536,
  83521, 104976, 130321, 160000, 194481, 234256, 279841, 331776, 390625, 456976, 531441, 614656, 707281,
  810000, 923521, 1048576, 1185921, 1336336, 1500625, 1679616, 1874161, 2085136, 2313441, 2560000,
  2825761, 3111696, 3418801, 3748096, 4100625]
   *** END PART #2 OPERATION: MULT
   *** PART #3 COMPARE Vr and V3
   *** CAN COMPARE ONLY 8 terms of Vr
   *** Vr and V3 are identical
   *** THE END
  (18:41) gp > seqoper1("c:\\pariData\\AfeB.txt",1601,op,1);
   *** PART #1 READ infile: c:\pariData\AfeB.txt; lim: 1601; oper: (x)->return(x^2); comp: 1
   *** FOUND: 2 sequences in file (only 1 or 2 are used).
   *** END PART #1 READ infile; Seq. sizes: 46/8
   *** PART #2 OPERATION: (x)->return(x^2)
  [0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 14641, 20736, 28561, 384
  16, 50625, 65536, 83521, 104976, 130321, 160000, 194481, 234256, 279841, 331776, 390625, 456976,
  531441, 614656, 707281, 810000, 923521, 1048576, 1185921, 1336336, 1500625, 1679616, 1874161, 2085136,
  2313441, 2560000]
   *** END PART #2 OPERATION; Seq. size: 41
   *** PART #3 COMPARE Vr and V2
   *** CAN COMPARE ONLY 8 terms of Vr
   *** Vr and V2 are NOT identical at Vr[5]!
   *** THE END
  (18:41) gp > seqoper1("c:\\pariData\\AoBeC.txt",-1,op);
   *** PART #1 READ infile: c:\pariData\AoBeC.txt; lim: -1; oper: (x)->return(x^2); comp: 0
   *** FOUND: 3 sequences in file (only 1 or 2 are used).
   *** END PART #1 READ infile; Seq. sizes: 46/46
   *** PART #2 OPERATION: (x)->return(x^2)
  [0, 1, 16, 81, 256, 625, 1296, 2401, 4096, 6561, 10000, 14641, 20736, 28561, 38416, 50625, 65536,
  83521, 104976, 130321, 160000, 194481, 234256, 279841, 331776, 390625, 456976, 531441, 614656, 707281,
  810000, 923521, 1048576, 1185921, 1336336, 1500625, 1679616, 1874161, 2085136, 2313441, 2560000,
  2825761, 3111696, 3418801, 3748096, 4100625]
   *** END PART #2 OPERATION; Seq. size: 46
  (18:41) gp > vec1=Vec([5,4,3,9,8,7,3,1]); vec1=List(vec1);
  (18:41) gp > vec2=Vec([5,4,3,9,8,7,2,1,10,11,11]);
  (18:41) gp > volcomp(vec1,vec2);
   *** COMPARE v1&v2 w/types: t_LIST/t_VEC; w/sizes: 8/11
   *** can compare only first 8 elements.
   *** v1 and v2 are NOT identical at v1[7]?
   (18:41) gp >
   NOTE: 1. Clean-up rules: delete all rows starting at position 1.
         2. You may add "\\" in front of them instead of deleting.
         3. You can cut tale called "TESTING FUNCTIONS" and save it in a different file.
   */